Exemplo n.º 1
0
        public static CollectionType ExecuteDirectQuery <CollectionType, EntityType>(string query, bool disableChangeTracking = false, params object[] parameters)
            where CollectionType : ORMCollection <EntityType>, new()
            where EntityType : ORMEntity
        {
            if (query.Contains("INNER JOIN") ||
                query.Contains("LEFT JOIN") ||
                query.Contains("RIGHT JOIN") ||
                query.Contains("FULL OUTER JOIN"))
            {
                throw new ORMInvalidJoinException("Joins are not supported on POCO's when using direct queries.");
            }

            var collection = ORMUtilities.ConvertTo <CollectionType, EntityType>(ExecuteDirectQuery(query, parameters), disableChangeTracking);

            collection.ExecutedQuery = query;

            return(collection);
        }
Exemplo n.º 2
0
        internal static void ExecuteEntityQuery <EntityType>(EntityType entity, SQLBuilder sqlBuilder)
            where EntityType : ORMEntity
        {
            if (!entity.PrimaryKey.IsCombinedPrimaryKey)
            {
                var primaryKey = entity.PrimaryKey.Keys[0];
                var tableName  = ORMUtilities.GetTableNameFromEntity(entity);
                var id         = sqlBuilder.SqlParameters.Where(x => x.SourceColumn == primaryKey.PropertyName).FirstOrDefault().Value;

                var reader = ORMUtilities.MemoryEntityDatabase.FetchEntityById(tableName, primaryKey, id);

                if (reader == null)
                {
                    throw new ArgumentException($"No record found for { primaryKey.PropertyName }: {id}.");
                }

                reader = ApplyEntityJoinsToReader(entity, reader, sqlBuilder);

                SQLHelper.DataReader(entity, reader, sqlBuilder);
            }
            else
            {
                var tableName = ORMUtilities.GetTableNameFromEntity(entity);

                var ids = new List <object>(entity.PrimaryKey.Keys.Count);

                foreach (var key in entity.PrimaryKey.Keys)
                {
                    ids.Add(sqlBuilder.SqlParameters.Where(x => x.SourceColumn == key.PropertyName).FirstOrDefault().Value);
                }

                var reader = ORMUtilities.MemoryEntityDatabase.FetchEntityByCombinedId(tableName, entity.PrimaryKey, ids);

                if (reader == null)
                {
                    throw new ArgumentException($"No record found.");
                }

                reader = ApplyEntityJoinsToReader(entity, reader, sqlBuilder);

                SQLHelper.DataReader(entity, reader, sqlBuilder);
            }
        }