Commits

malweka committed 8a75d9e

lazy loading many to many hydration done.

  • Participants
  • Parent commits 10d334b

Comments (0)

Files changed (7)

File src/Goliath.Data.Console/Program.cs

                 var zooEntMap = map.EntityConfigs.Where(c => string.Equals(c.Name, "Zoo", StringComparison.Ordinal))
                     .FirstOrDefault();
 
+                var employeeMap = map.EntityConfigs.Where(c => string.Equals(c.Name, "Employee", StringComparison.Ordinal))
+                    .FirstOrDefault();
+
                 var wst = new WhereStatement("Id");
 
                 SelectSqlBuilder selectBuilder = new SelectSqlBuilder(mapper, animalEntMap)
                 //Providers.SqlServer.Mssq2008SqlMapper mapper = new Mssq2008SqlMapper();
 
 
+                var leftColumn1 = new Relation() { ColumnName = "EmployeeId", PropertyName = "EmployeeId" };
+                var leftcolumn2 = new Relation() { ColumnName = "AnimalId", PropertyName = "AnimalId"};
+
+                var mapTableMap = UnMappedTableMap.Create("animals_handlers", leftColumn1, leftcolumn2);
+                mapTableMap.TableAlias = "mX1";
+
+                var currEntMap = UnMappedTableMap.Create(animalEntMap.TableName);
+
+                SelectSqlBuilder manytomanySql = new SelectSqlBuilder(mapper, mapTableMap)
+                       .AddJoin(new SqlJoin(mapTableMap, JoinType.Inner).OnTable(employeeMap).OnLeftColumn(leftColumn1).OnRightColumn("Id"))
+                       .AddJoin(new SqlJoin(mapTableMap, JoinType.Inner).OnTable(currEntMap).OnLeftColumn(leftcolumn2).OnRightColumn("Id"));
+
+                string sql = manytomanySql.ToSqlString();
 
                 var animalQuery = new SelectSqlBuilder(mapper, animalEntMap).WithPaging(15, 0).ToSqlString();
                 var zooQuery = new SelectSqlBuilder(mapper, zooEntMap).ToSqlString();
                 serializer.SerializeAll<WebZoo.Data.Animal>(dataReader, animalEntMap);
                 dataReader.Dispose();
 
-                var m1 = animals[0];
+                var m1 = animals[1];
                 m1.Name = "Just_Updated";
                 m1.Location = "UP345";
 
+                Console.WriteLine(m1.EmployeesOnAnimalsHandler_AnimalId.Count);
                 var aniAdapter = sess.CreateDataAccessAdapter<Animal>();
                 aniAdapter.Update(m1);
 

File src/Goliath.Data/DataAccess/EntitySerialization/EntitySerializer.cs

                                 oneToManyHelper.Serialize(settings, this, rel, instanceEntity, keyVal.Value, entityMap, getSetInfo, columns, dbReader);
                                 break;
                             case RelationshipType.ManyToMany:
+                                SerializeManyToMany manyToMany = new SerializeManyToMany(SqlMapper, getSetStore);
+                                manyToMany.Serialize(settings, this, rel, instanceEntity, keyVal.Value, entityMap, getSetInfo, columns, dbReader);
                                 break;
                             default:
                                 break;
                     {
                         var val = dbReader[ordinal];
                         var fieldType = dbReader.GetFieldType(ordinal);
-                        if (fieldType.Equals(keyVal.Value.PropertType))
+                        if (fieldType.Equals(keyVal.Value.PropertType) && (val != DBNull.Value))
                         {
                             keyVal.Value.Setter(instanceEntity, val);
                         }

File src/Goliath.Data/DataAccess/EntitySerialization/SerializeManyToMany.cs

                 if (columns.TryGetValue(rel.ColumnName, out ordinal))
                 {
                     var val = dbReader[ordinal];
+
                     if (val != null)
                     {
-                        QueryParam qp = new QueryParam(ParameterNameBuilderHelper.ColumnQueryName(relEntMap.TableAlias, rel.ReferenceColumn)) { Value = val };
-                        SelectSqlBuilder sqlBuilder = new SelectSqlBuilder(sqlMapper, relEntMap)
-                       .Where(new WhereStatement(ParameterNameBuilderHelper.ColumnWithTableAlias(relEntMap.TableAlias, rel.ReferenceColumn))
-                                .Equals(sqlMapper.CreateParameterName(qp.Name)));
+                        var leftColumn1 = new Relation() { ColumnName = rel.MapColumn, PropertyName = rel.MapColumn };
+                        var leftcolumn2 = new Relation() { ColumnName = rel.MapReferenceColumn, PropertyName = rel.MapReferenceColumn };
+
+                        var mapTableMap = UnMappedTableMap.Create(rel.MapTableName, leftColumn1, leftcolumn2);
+                        mapTableMap.TableAlias = "mX1";
+
+                        var currEntMap = UnMappedTableMap.Create(entityMap.TableName);
+
+                        QueryParam qp = new QueryParam(ParameterNameBuilderHelper.ColumnQueryName(mapTableMap.TableAlias, rel.MapColumn)) { Value = val };
+
+                        SelectSqlBuilder sqlBuilder = new SelectSqlBuilder(sqlMapper, mapTableMap)
+                            .AddJoin(new SqlJoin(mapTableMap, JoinType.Inner).OnTable(relEntMap).OnLeftColumn(leftColumn1).OnRightColumn(rel.ReferenceColumn))
+                            .AddJoin(new SqlJoin(mapTableMap, JoinType.Inner).OnTable(currEntMap).OnLeftColumn(leftcolumn2).OnRightColumn(rel.ColumnName))
+                            .Where(new WhereStatement(ParameterNameBuilderHelper.ColumnWithTableAlias(mapTableMap.TableAlias, rel.MapColumn))
+                            .Equals(sqlMapper.CreateParameterName(qp.Name)));
 
                         SqlOperationInfo qInfo = new SqlOperationInfo() { CommandType = SqlStatementType.Select };
+
                         qInfo.SqlText = sqlBuilder.ToSqlString();
                         qInfo.Parameters = new QueryParam[] { qp };
 

File src/Goliath.Data/Goliath.Data.csproj

     <Compile Include="Mapping\DataModelGenerator.cs" />
     <Compile Include="Mapping\Enums.cs" />
     <Compile Include="Mapping\Models\ConfigProperty.cs" />
+    <Compile Include="Mapping\Models\UnMappedTableMap.cs" />
     <Compile Include="Mapping\PostGenProcessors\IPostGenerationProcessor.cs" />
     <Compile Include="Mapping\MappingExtensions.cs" />
     <Compile Include="Mapping\PostGenProcessors\NamePostProcessor.cs" />

File src/Goliath.Data/Mapping/Models/UnMappedTableMap.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Goliath.Data.Mapping
+{
+    class UnMappedTableMap : EntityMap
+    {
+        private UnMappedTableMap(string entityName) : base(entityName, entityName) { }
+
+        public static UnMappedTableMap Create(string entityName, params Property[] properties)
+        {
+            var table = new UnMappedTableMap(entityName);
+            table.TableAlias = entityName;
+            if (properties != null)
+            {
+                foreach (var prop in properties)
+                {
+                    table.Add(prop);
+                }
+            }
+            return table;
+        }
+    }
+
+}

File src/Goliath.Data/Sql/SelectSqlBuilder.cs

         public SelectSqlBuilder(SqlMapper sqlMapper, EntityMap entMap)
             : base(sqlMapper, entMap)
         {
-            foreach (var col in entMap)
+            if (entMap is UnMappedTableMap)
             {
-                if (col is Relation)
+                //we should do nothing
+            }
+            else
+            {
+                foreach (var col in entMap)
                 {
-                    Relation rel = (Relation)col;
-                    if (rel.RelationType != RelationshipType.ManyToOne)
-                        continue;
-                    if (!rel.LazyLoad)
+                    if (col is Relation)
                     {
-                        var rightTable = entMap.Parent.GetEntityMap(rel.ReferenceEntityName);
-                        //var rightColumn = rightTable[rel.ReferenceColumn];
-                        AddJoin(new SqlJoin(entMap, JoinType.Inner).OnTable(rightTable)
-                            .OnRightColumn(rel.ReferenceColumn)
-                            .OnLeftColumn(rel));
+                        Relation rel = (Relation)col;
+                        if (rel.RelationType != RelationshipType.ManyToOne)
+                            continue;
+                        if (!rel.LazyLoad)
+                        {
+                            var rightTable = entMap.Parent.GetEntityMap(rel.ReferenceEntityName);
+                            //var rightColumn = rightTable[rel.ReferenceColumn];
+                            AddJoin(new SqlJoin(entMap, JoinType.Inner).OnTable(rightTable)
+                                .OnRightColumn(rel.ReferenceColumn)
+                                .OnLeftColumn(rel));
+                        }
                     }
+
+                    string colKey = ParameterNameBuilderHelper.ColumnWithTableAlias(entMap.TableAlias, col.ColumnName);  //string.Format("{0}.{1}", entMap.TableAlias, col.ColumnName);
+                    //var tuple = Tuple.Create<string, string>(sqlMapper.CreateParameterName(col.Name), CreateColumnName(entMap, col));
+                    if (!Columns.ContainsKey(colKey))
+                        Columns.Add(colKey, BuildColumnSelectString(col.ColumnName, entMap.TableAlias));
                 }
-
-                string colKey = ParameterNameBuilderHelper.ColumnWithTableAlias(entMap.TableAlias, col.ColumnName);  //string.Format("{0}.{1}", entMap.TableAlias, col.ColumnName);
-                //var tuple = Tuple.Create<string, string>(sqlMapper.CreateParameterName(col.Name), CreateColumnName(entMap, col));
-                if (!Columns.ContainsKey(colKey))
-                    Columns.Add(colKey, BuildColumnSelectString(col.ColumnName, entMap.TableAlias));
             }
 
         }
 
         public SqlQueryBody Build()
         {
-            List<string> printColumns = new List<string>();
+            Dictionary<string, string> printColumns = new Dictionary<string, string>();
             List<SqlJoin> sJoins = new List<SqlJoin>();
             sJoins.AddRange(Joins);
-            printColumns.AddRange(Columns.Values);
+
+            foreach (var keyPair in Columns)
+                printColumns.Add(keyPair.Key, keyPair.Value);
 
             SqlQueryBody queryBody = new SqlQueryBody();
 
                 }
             }
 
-            queryBody.ColumnEnumeration = string.Join(", ", printColumns);
+            queryBody.ColumnEnumeration = string.Join(", ", printColumns.Values);
             queryBody.From = BuildTableFromString(entMap.TableName, entMap.TableAlias);
 
             if (sJoins.Count > 0)
             {
-                queryBody.JoinEnumeration = string.Join(", ", sJoins);
+                queryBody.JoinEnumeration = string.Join(" ", sJoins);
             }
 
             int wheresCount = wheres.Count;
             return queryBody.ToString();
         }
 
-        void BuildColumsAndJoins(EntityMap entMap, List<string> cols, List<SqlJoin> sjoins)
+        void BuildColumsAndJoins(EntityMap entMap, Dictionary<string, string> cols, List<SqlJoin> sjoins)
         {
+           
+            if (entMap is UnMappedTableMap)
+                return;
+
             SelectSqlBuilder selBuilder = new SelectSqlBuilder(sqlMapper, entMap);
-            cols.AddRange(selBuilder.Columns.Values);
+            foreach (var kp in selBuilder.Columns)
+            {
+                if (!cols.ContainsKey(kp.Key))
+                    cols.Add(kp.Key, kp.Value);
+            }
+            
             for (int i = 0; i < selBuilder.Joins.Count; i++)
             {
                 var jn = selBuilder.Joins[i];

File src/Goliath.Data/Sql/SqlJoin.cs

         public SqlJoin(EntityMap table, JoinType joinType)
         {
             JoinType = joinType;
+
             if(table == null)
                 throw new ArgumentNullException("table");
+
             Table = table;
         }