Ejemplo n.º 1
0
        /// <summary>
        /// Does a cascade delete of an object for this property
        /// </summary>
        /// <param name="Object">Object</param>
        /// <param name="Source">Source info</param>
        /// <param name="ObjectsSeen">Objects seen thus far</param>
        /// <returns>Batch object with the appropriate commands</returns>
        public override IBatch CascadeDelete(ClassType Object, ISourceInfo Source, IList <object> ObjectsSeen)
        {
            QueryProvider.Manager Provider        = IoC.Manager.Bootstrapper.Resolve <QueryProvider.Manager>();
            Mapper.Manager        MappingProvider = IoC.Manager.Bootstrapper.Resolve <Mapper.Manager>();
            IMapping PropertyMapping = MappingProvider[typeof(DataType), Source];
            IBatch   Batch           = Provider.Batch(Source);
            var      AspectObject    = Object as IORMObject;

            if (Object == null || (AspectObject != null && ObjectsSeen.Contains(Mapping.IDProperties.FirstOrDefault().GetValue(Object))))
            {
                return(Batch);
            }
            if (AspectObject != null)
            {
                ObjectsSeen.Add(Mapping.IDProperties.FirstOrDefault().GetValue(Object));
            }
            IEnumerable <DataType> IList = CompiledExpression(Object);

            if (IList == null)
            {
                return(Batch);
            }
            foreach (DataType Item in IList.Where(x => x != null))
            {
                foreach (IProperty <DataType> Property in PropertyMapping.Properties.Where(x => x.Cascade))
                {
                    Batch.AddCommand(Property.CascadeDelete(Item, Source, ObjectsSeen.ToList()));
                }
                Batch.AddCommand(Provider.Generate <DataType>(Source, PropertyMapping, Structure).Delete(Item));
            }
            Utilities.IoC.Manager.Bootstrapper.Resolve <DataTypes.Caching.Manager>().Cache().RemoveByTag(typeof(DataType).GetName());
            return(Batch);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns all objects based on the parameters provided
        /// </summary>
        /// <param name="Parameters">Parameters</param>
        /// <param name="Command">Command to run</param>
        /// <param name="ConnectionString">Connection string</param>
        /// <param name="Type">Command type</param>
        /// <returns>The list of objects requested</returns>
        public static IEnumerable <dynamic> All(string Command, CommandType Type, string ConnectionString, params object[] Parameters)
        {
            var    TempParameters = Parameters as IParameter[];
            IBatch TempBatch      = Batch(ConnectionString);

            if (TempParameters == null)
            {
                TempBatch.AddCommand(null, null, Command, Type, Parameters);
            }
            else
            {
                TempBatch.AddCommand(null, null, Command, Type, TempParameters);
            }
            return(TempBatch.Execute()[0]);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.tables.name as [Table] FROM sys.tables");
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES");
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, @"SELECT SPECIFIC_NAME as NAME,ROUTINE_DEFINITION as DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE INFORMATION_SCHEMA.ROUTINES.ROUTINE_TYPE='FUNCTION'");
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.procedures.name as NAME,OBJECT_DEFINITION(sys.procedures.object_id) as DEFINITION FROM sys.procedures");
 }
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.tables.name as [Table],sys.triggers.name as Name,sys.trigger_events.type as Type,
                                                         OBJECT_DEFINITION(sys.triggers.object_id) as Definition
                                                         FROM sys.triggers
                                                         INNER JOIN sys.trigger_events ON sys.triggers.object_id=sys.trigger_events.object_id
                                                         INNER JOIN sys.tables on sys.triggers.parent_id=sys.tables.object_id");
 }
        /// <summary>
        /// Sets up the specified database schema
        /// </summary>
        /// <param name="Mappings">The mappings.</param>
        /// <param name="Database">The database.</param>
        /// <param name="QueryProvider">The query provider.</param>
        public void Setup(ListMapping <IDatabase, IMapping> Mappings, IDatabase Database, QueryProvider.Manager QueryProvider)
        {
            ISourceInfo TempSource   = SourceProvider.GetSource(Database.Name);
            var         TempDatabase = new Schema.Default.Database.Database(Regex.Match(TempSource.Connection, "Initial Catalog=(.*?;)").Value.Replace("Initial Catalog=", "").Replace(";", ""));

            SetupTables(Mappings, Database, TempDatabase);
            SetupJoiningTables(Mappings, Database, TempDatabase);
            SetupAuditTables(Database, TempDatabase);

            foreach (ITable Table in TempDatabase.Tables)
            {
                Table.SetupForeignKeys();
            }
            List <string> Commands = GenerateSchema(TempDatabase, SourceProvider.GetSource(Database.Name)).ToList();
            IBatch        Batch    = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));

            for (int x = 0; x < Commands.Count; ++x)
            {
                if (Commands[x].ToUpperInvariant().Contains("CREATE DATABASE"))
                {
                    QueryProvider.Batch(SourceProvider.GetSource(Regex.Replace(SourceProvider.GetSource(Database.Name).Connection, "Initial Catalog=(.*?;)", ""))).AddCommand(null, null, CommandType.Text, Commands[x]).Execute();
                }
                else if (Commands[x].Contains("CREATE TRIGGER") || Commands[x].Contains("CREATE FUNCTION"))
                {
                    if (Batch.CommandCount > 0)
                    {
                        Batch.Execute();
                        Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
                    }
                    Batch.AddCommand(null, null, CommandType.Text, Commands[x]);
                    if (x < Commands.Count - 1)
                    {
                        Batch.Execute();
                        Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
                    }
                }
                else
                {
                    Batch.AddCommand(null, null, CommandType.Text, Commands[x]);
                }
            }
            Batch.Execute();
        }
 private static void CascadeDelete <ObjectType>(ObjectType Object, ISourceInfo Source, IMapping Mapping, IBatch TempBatch, List <object> ObjectsSeen)
     where ObjectType : class
 {
     Contract.Requires <ArgumentNullException>(Mapping != null, "Mapping");
     Contract.Requires <ArgumentNullException>(Mapping.Properties != null, "Mapping.Properties");
     foreach (IProperty <ObjectType> Property in Mapping.Properties.Where(x => x.Cascade))
     {
         TempBatch.AddCommand(Property.CascadeDelete(Object, Source, ObjectsSeen.ToList()));
     }
 }
        /// <summary>
        /// Generates a batch that will update the data from the objects
        /// </summary>
        /// <param name="Objects">Objects to update</param>
        /// <returns>Batch with the appropriate commands</returns>
        public IBatch Update(IEnumerable <T> Objects)
        {
            IBatch TempBatch = QueryProvider.Batch(Source);

            foreach (T Object in Objects)
            {
                TempBatch.AddCommand(Update(Object));
            }
            return(TempBatch);
        }
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.procedures.name as [Procedure],sys.systypes.name as TYPE,sys.parameters.name as NAME,
     sys.parameters.max_length as LENGTH,sys.parameters.default_value as [DEFAULT VALUE]
     FROM sys.procedures
     INNER JOIN sys.parameters on sys.procedures.object_id=sys.parameters.object_id
     INNER JOIN sys.systypes on sys.systypes.xusertype=sys.parameters.system_type_id
     WHERE sys.systypes.xusertype <> 256");
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Called to create a batch that saves items from the joining tables
        /// </summary>
        /// <param name="Object">Object</param>
        /// <param name="Source">Source info</param>
        /// <param name="ObjectsSeen">Objects seen thus far</param>
        /// <returns>Batch object with the appropriate commands</returns>
        public override IBatch CascadeJoinsSave(ClassType Object, ISourceInfo Source, IList <object> ObjectsSeen)
        {
            QueryProvider.Manager Provider        = IoC.Manager.Bootstrapper.Resolve <QueryProvider.Manager>();
            Mapper.Manager        MappingProvider = IoC.Manager.Bootstrapper.Resolve <Mapper.Manager>();
            IMapping PropertyMapping = MappingProvider[typeof(DataType), Source];
            IBatch   Batch           = Provider.Batch(Source);
            var      AspectObject    = Object as IORMObject;

            if (Object == null || (AspectObject != null && ObjectsSeen.Contains(Mapping.IDProperties.FirstOrDefault().GetValue(Object))))
            {
                return(Batch);
            }
            if (AspectObject != null)
            {
                ObjectsSeen.Add(Mapping.IDProperties.FirstOrDefault().GetValue(Object));
            }
            IEnumerable <DataType> IList = CompiledExpression(Object);

            if (IList == null)
            {
                return(Batch);
            }
            foreach (DataType Item in IList.Where(x => x != null))
            {
                foreach (IProperty <DataType> Property in PropertyMapping.Properties)
                {
                    if (!Property.Cascade &&
                        (Property is IMultiMapping ||
                         Property is ISingleMapping))
                    {
                        Batch.AddCommand(Property.JoinsSave(Item, Source, ObjectsSeen.ToList()));
                    }
                    else if (Property.Cascade)
                    {
                        Batch.AddCommand(Property.CascadeJoinsSave(Item, Source, ObjectsSeen.ToList()));
                    }
                }
            }
            Batch.AddCommand(Provider.Generate <ClassType>(Source, Mapping, Structure).JoinsSave <ICollection <DataType>, DataType>(this, Object));
            return(Batch);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.views.name as [View],OBJECT_DEFINITION(sys.views.object_id) as Definition,
                                                 sys.columns.name AS [Column], sys.systypes.name AS [COLUMN_TYPE],
                                                 sys.columns.max_length as [MAX_LENGTH], sys.columns.is_nullable as [IS_NULLABLE]
                                                 FROM sys.views
                                                 INNER JOIN sys.columns on sys.columns.object_id=sys.views.object_id
                                                 INNER JOIN sys.systypes ON sys.systypes.xtype = sys.columns.system_type_id
                                                 WHERE sys.systypes.xusertype <> 256");
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.tables.name as [Table],sys.triggers.name as Name,sys.trigger_events.type as Type,
                                                         OBJECT_DEFINITION(sys.triggers.object_id) as Definition
                                                         FROM sys.triggers
                                                         INNER JOIN sys.trigger_events ON sys.triggers.object_id=sys.trigger_events.object_id
                                                         INNER JOIN sys.tables on sys.triggers.parent_id=sys.tables.object_id");
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Gets the command.
        /// </summary>
        /// <param name="batch">The batch.</param>
        public void GetCommand(IBatch batch)
        {
            if (batch == null)
            {
                throw new ArgumentNullException(nameof(batch));
            }
            batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.procedures.name as [Procedure],sys.systypes.name as TYPE,sys.parameters.name as NAME,
sys.parameters.max_length as LENGTH,sys.parameters.default_value as [DEFAULT VALUE]
FROM sys.procedures
INNER JOIN sys.parameters on sys.procedures.object_id=sys.parameters.object_id
INNER JOIN sys.systypes on sys.systypes.xusertype=sys.parameters.system_type_id
WHERE sys.systypes.xusertype <> 256");
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.views.name as [View],OBJECT_DEFINITION(sys.views.object_id) as Definition,
                                                 sys.columns.name AS [Column], sys.systypes.name AS [COLUMN_TYPE],
                                                 sys.columns.max_length as [MAX_LENGTH], sys.columns.is_nullable as [IS_NULLABLE]
                                                 FROM sys.views
                                                 INNER JOIN sys.columns on sys.columns.object_id=sys.views.object_id
                                                 INNER JOIN sys.systypes ON sys.systypes.xtype = sys.columns.system_type_id
                                                 WHERE sys.systypes.xusertype <> 256");
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Called to create a batch that deletes items from the joining tables
        /// </summary>
        /// <param name="Object">Object</param>
        /// <param name="Source">Source info</param>
        /// <param name="ObjectsSeen">Objects seen thus far</param>
        /// <returns>Batch object with the appropriate commands</returns>
        public override IBatch CascadeJoinsDelete(ClassType Object, ISourceInfo Source, IList <object> ObjectsSeen)
        {
            QueryProvider.Manager Provider        = IoC.Manager.Bootstrapper.Resolve <QueryProvider.Manager>();
            Mapper.Manager        MappingProvider = IoC.Manager.Bootstrapper.Resolve <Mapper.Manager>();
            IMapping PropertyMapping = MappingProvider[typeof(DataType), Source];
            IBatch   Batch           = Provider.Batch(Source);
            var      AspectObject    = Object as IORMObject;

            if (Object == null || (AspectObject != null && ObjectsSeen.Contains(Mapping.IDProperties.FirstOrDefault().GetValue(Object))))
            {
                return(Provider.Batch(Source));
            }
            if (AspectObject != null)
            {
                ObjectsSeen.Add(Mapping.IDProperties.FirstOrDefault().GetValue(Object));
            }
            DataType Item = CompiledExpression(Object);

            if (Item == null)
            {
                return(Batch);
            }
            foreach (IProperty <DataType> Property in PropertyMapping.Properties)
            {
                if (!Property.Cascade &&
                    (Property is ISingleMapping ||
                     Property is IMultiMapping))
                {
                    Batch.AddCommand(Property.JoinsDelete(Item, Source, ObjectsSeen.ToList()));
                }
                else if (Property.Cascade)
                {
                    Batch.AddCommand(Property.CascadeJoinsDelete(Item, Source, ObjectsSeen.ToList()));
                }
            }
            return(Batch);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Deletes items from the joining table for the property
        /// </summary>
        /// <param name="Property">Property</param>
        /// <param name="Object">Object</param>
        /// <typeparam name="P">Property type</typeparam>
        /// <returns>The batch with the appropriate commands</returns>
        public IBatch JoinsDelete <P>(IProperty <T, P> Property, T Object)
        {
            IBatch ReturnValue = QueryProvider.Batch(Source);

            if (Object == null)
            {
                return(ReturnValue);
            }
            var List = (P)Property.GetValue(Object);

            if (List == null)
            {
                return(ReturnValue);
            }
            object CurrentID = Mapping.IDProperties.FirstOrDefault().GetValue(Object);

            IMapping ForeignMapping = Property.ForeignMapping;

            if (string.Compare(Mapping.TableName, ForeignMapping.TableName, StringComparison.Ordinal) == 0 &&
                Property as IManyToOne != null)
            {
                ReturnValue.AddCommand(null,
                                       Object,
                                       "DELETE FROM " + Property.TableName + " WHERE " + Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName + "2=@0",
                                       CommandType.Text,
                                       CurrentID);
            }
            else
            {
                ReturnValue.AddCommand(null,
                                       Object,
                                       "DELETE FROM " + Property.TableName + " WHERE " + Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName + "=@0",
                                       CommandType.Text,
                                       CurrentID);
            }
            return(ReturnValue);
        }
Ejemplo n.º 19
0
        private static void CascadeSave <ObjectType>(ObjectType Object, ISourceInfo Source, IMapping Mapping, IBatch TempBatch, List <object> ObjectsSeen)
            where ObjectType : class, new()
        {
            Contract.Requires <ArgumentNullException>(Mapping != null, "Mapping");
            Contract.Requires <ArgumentNullException>(Mapping.Properties != null, "Mapping.Properties");
            var ORMObject = Object as IORMObject;

            foreach (IProperty <ObjectType> Property in Mapping.Properties.Where(x => x.Cascade))
            {
                if (ORMObject == null || ORMObject.PropertiesChanged0.Contains(Property.Name))
                {
                    TempBatch.AddCommand(Property.CascadeSave(Object, Source, ObjectsSeen.ToList()));
                }
            }
        }
        private static void JoinsSave <ObjectType>(ObjectType Object, ISourceInfo Source, IMapping Mapping, IBatch TempBatch, List <object> ObjectsSeen)
            where ObjectType : class
        {
            Contract.Requires <ArgumentNullException>(Mapping != null, "Mapping");
            Contract.Requires <ArgumentNullException>(Mapping.Properties != null, "Mapping.Properties");
            var ORMObject = Object as IORMObject;

            foreach (IProperty <ObjectType> Property in Mapping.Properties)
            {
                if (ORMObject == null || ORMObject.PropertiesChanged0.Contains(Property.Name))
                {
                    if (!Property.Cascade &&
                        (Property is IMultiMapping ||
                         Property is ISingleMapping))
                    {
                        TempBatch.AddCommand(Property.JoinsSave(Object, Source, ObjectsSeen.ToList()));
                    }
                    else if (Property.Cascade)
                    {
                        TempBatch.AddCommand(Property.CascadeJoinsSave(Object, Source, ObjectsSeen.ToList()));
                    }
                }
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Saves the object to the source
        /// </summary>
        /// <typeparam name="PrimaryKeyType">Primary key type</typeparam>
        /// <param name="Object">Object to save</param>
        public IBatch Save <PrimaryKeyType>(T Object)
        {
            IBatch         TempBatch  = QueryProvider.Batch(Source);
            IProperty <T>  IDProperty = ((IProperty <T>)Mapping.IDProperties.FirstOrDefault());
            PrimaryKeyType IDValue    = IDProperty.GetValue(Object).To(default(PrimaryKeyType));

            var        Comparer = new GenericEqualityComparer <PrimaryKeyType>();
            IParameter Param1   = null;

            if (Comparer.Equals(IDValue, default(PrimaryKeyType)))
            {
                return(TempBatch.AddCommand(Insert(Object)));
            }
            if (IDProperty.AutoIncrement)
            {
                return(TempBatch.AddCommand(Update(Object)));
            }
            Param1 = typeof(PrimaryKeyType).Is(typeof(string)) ? (IParameter) new StringEqualParameter(IDValue.ToString(), IDProperty.FieldName, IDValue.ToString().Length, IDProperty.FieldName, Source.ParameterPrefix) : (IParameter) new EqualParameter <PrimaryKeyType>(IDValue, IDProperty.FieldName, IDProperty.FieldName, Source.ParameterPrefix);
            if (Any(Param1).Execute()[0].Count == 0)
            {
                return(TempBatch.AddCommand(Insert(Object)));
            }
            return(TempBatch.AddCommand(Update(Object)));
        }
Ejemplo n.º 22
0
 /// <summary>
 /// Deletes an object from the database
 /// </summary>
 /// <typeparam name="ObjectType">Object type</typeparam>
 /// <param name="Object">Object to delete</param>
 public void Delete <ObjectType>(ObjectType Object)
     where ObjectType : class
 {
     Cache.RemoveByTag(typeof(ObjectType).GetName());
     foreach (ISourceInfo Source in SourceProvider.Where(x => x.Writable).OrderBy(x => x.Order))
     {
         IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
         if (Mapping != null)
         {
             IGenerator <ObjectType> Generator = QueryProvider.Generate <ObjectType>(Source, MapperProvider[typeof(ObjectType), Source], MapperProvider.GetStructure(Mapping.DatabaseConfigType));
             IBatch TempBatch = QueryProvider.Batch(Source);
             CascadeDelete <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             TempBatch.AddCommand(Generator.Delete(Object));
             TempBatch.Execute();
         }
     }
 }
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.tables.name as [Table],sys.columns.name AS [Column], sys.systypes.name AS [COLUMN_TYPE],
                                                         sys.columns.max_length as [MAX_LENGTH], sys.columns.is_nullable as [IS_NULLABLE],
                                                         sys.columns.is_identity as [IS_IDENTITY], sys.index_columns.index_id as [IS_INDEX],
                                                         key_constraints.name as [PRIMARY_KEY], key_constraints_1.name as [UNIQUE],
                                                         tables_1.name as [FOREIGN_KEY_TABLE], columns_1.name as [FOREIGN_KEY_COLUMN],
                                                         sys.default_constraints.definition as [DEFAULT_VALUE]
                                                         FROM sys.tables
                                                         INNER JOIN sys.columns on sys.columns.object_id=sys.tables.object_id
                                                         INNER JOIN sys.systypes ON sys.systypes.xtype = sys.columns.system_type_id
                                                         LEFT OUTER JOIN sys.index_columns on sys.index_columns.object_id=sys.tables.object_id and sys.index_columns.column_id=sys.columns.column_id
                                                         LEFT OUTER JOIN sys.key_constraints on sys.key_constraints.parent_object_id=sys.tables.object_id and sys.key_constraints.parent_object_id=sys.index_columns.object_id and sys.index_columns.index_id=sys.key_constraints.unique_index_id and sys.key_constraints.type='PK'
                                                         LEFT OUTER JOIN sys.foreign_key_columns on sys.foreign_key_columns.parent_object_id=sys.tables.object_id and sys.foreign_key_columns.parent_column_id=sys.columns.column_id
                                                         LEFT OUTER JOIN sys.tables as tables_1 on tables_1.object_id=sys.foreign_key_columns.referenced_object_id
                                                         LEFT OUTER JOIN sys.columns as columns_1 on columns_1.column_id=sys.foreign_key_columns.referenced_column_id and columns_1.object_id=tables_1.object_id
                                                         LEFT OUTER JOIN sys.key_constraints as key_constraints_1 on key_constraints_1.parent_object_id=sys.tables.object_id and key_constraints_1.parent_object_id=sys.index_columns.object_id and sys.index_columns.index_id=key_constraints_1.unique_index_id and key_constraints_1.type='UQ'
                                                         LEFT OUTER JOIN sys.default_constraints on sys.default_constraints.object_id=sys.columns.default_object_id
                                                         WHERE sys.systypes.xusertype <> 256");
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Saves an object to the database
 /// </summary>
 /// <typeparam name="ObjectType">Object type</typeparam>
 /// <typeparam name="PrimaryKeyType">Primary key type</typeparam>
 /// <param name="Object">Object to save</param>
 public void Save <ObjectType, PrimaryKeyType>(ObjectType Object)
     where ObjectType : class, new()
 {
     Cache.RemoveByTag(typeof(ObjectType).GetName());
     foreach (ISourceInfo Source in SourceProvider.Where(x => x.Writable).OrderBy(x => x.Order))
     {
         IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
         if (Mapping != null)
         {
             IGenerator <ObjectType> Generator = QueryProvider.Generate <ObjectType>(Source, MapperProvider[typeof(ObjectType), Source]);
             IBatch TempBatch = QueryProvider.Batch(Source);
             CascadeSave <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             TempBatch.Execute();
             TempBatch = QueryProvider.Batch(Source);
             TempBatch.AddCommand(Generator.Save <PrimaryKeyType>(Object));
             TempBatch.Execute();
             TempBatch = QueryProvider.Batch(Source);
             JoinsDelete <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             JoinsSave <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             TempBatch.RemoveDuplicateCommands().Execute();
         }
     }
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
     {
         throw new ArgumentNullException(nameof(batch));
     }
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.tables.name as [Table],sys.columns.name AS [Column], sys.systypes.name AS [COLUMN_TYPE],
                                                         sys.columns.max_length as [MAX_LENGTH], sys.columns.is_nullable as [IS_NULLABLE],
                                                         sys.columns.is_identity as [IS_IDENTITY], sys.index_columns.index_id as [IS_INDEX],
                                                         key_constraints.name as [PRIMARY_KEY], key_constraints_1.name as [UNIQUE],
                                                         tables_1.name as [FOREIGN_KEY_TABLE], columns_1.name as [FOREIGN_KEY_COLUMN],
                                                         sys.default_constraints.definition as [DEFAULT_VALUE]
                                                         FROM sys.tables
                                                         INNER JOIN sys.columns on sys.columns.object_id=sys.tables.object_id
                                                         INNER JOIN sys.systypes ON sys.systypes.xtype = sys.columns.system_type_id
                                                         LEFT OUTER JOIN sys.index_columns on sys.index_columns.object_id=sys.tables.object_id and sys.index_columns.column_id=sys.columns.column_id
                                                         LEFT OUTER JOIN sys.key_constraints on sys.key_constraints.parent_object_id=sys.tables.object_id and sys.key_constraints.parent_object_id=sys.index_columns.object_id and sys.index_columns.index_id=sys.key_constraints.unique_index_id and sys.key_constraints.type='PK'
                                                         LEFT OUTER JOIN sys.foreign_key_columns on sys.foreign_key_columns.parent_object_id=sys.tables.object_id and sys.foreign_key_columns.parent_column_id=sys.columns.column_id
                                                         LEFT OUTER JOIN sys.tables as tables_1 on tables_1.object_id=sys.foreign_key_columns.referenced_object_id
                                                         LEFT OUTER JOIN sys.columns as columns_1 on columns_1.column_id=sys.foreign_key_columns.referenced_column_id and columns_1.object_id=tables_1.object_id
                                                         LEFT OUTER JOIN sys.key_constraints as key_constraints_1 on key_constraints_1.parent_object_id=sys.tables.object_id and key_constraints_1.parent_object_id=sys.index_columns.object_id and sys.index_columns.index_id=key_constraints_1.unique_index_id and key_constraints_1.type='UQ'
                                                         LEFT OUTER JOIN sys.default_constraints on sys.default_constraints.object_id=sys.columns.default_object_id
                                                         WHERE sys.systypes.xusertype <> 256");
 }
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT SPECIFIC_NAME as NAME,ROUTINE_DEFINITION as DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE INFORMATION_SCHEMA.ROUTINES.ROUTINE_TYPE='FUNCTION'");
 }
Ejemplo n.º 27
0
        /// <summary>
        /// Saves items to the joining table for the property
        /// </summary>
        /// <param name="Property">Property</param>
        /// <param name="Object">Object</param>
        /// <typeparam name="P">Property type</typeparam>
        /// <typeparam name="ItemType">Item type</typeparam>
        /// <returns>The batch with the appropriate commands</returns>
        public IBatch JoinsSave <P, ItemType>(IProperty <T, P> Property, T Object)
        {
            IBatch ReturnValue = QueryProvider.Batch(Source);

            if (Object == null)
            {
                return(ReturnValue);
            }
            if (Property as IManyToOne != null)
            {
                var Item = (P)Property.GetValue(Object);
                if (Item == null)
                {
                    return(ReturnValue);
                }

                object   CurrentID      = ((IProperty <T>)Mapping.IDProperties.FirstOrDefault()).GetValue(Object);
                IMapping ForeignMapping = Property.ForeignMapping;
                object   ForeignID      = ForeignMapping.IDProperties.FirstOrDefault().GetValue(Item);
                string   Parameters     = "";
                object[] Values         = new object[2];
                if (string.Compare(Mapping.TableName, ForeignMapping.TableName, StringComparison.InvariantCulture) == 0)
                {
                    Parameters = Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName + "," + ForeignMapping.TableName + ForeignMapping.IDProperties.FirstOrDefault().FieldName + "2";
                    Values[1]  = CurrentID;
                    Values[0]  = ForeignID;
                }
                else if (string.Compare(Mapping.TableName, ForeignMapping.TableName, StringComparison.InvariantCulture) <= 0)
                {
                    Parameters = Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName + "," + ForeignMapping.TableName + ForeignMapping.IDProperties.FirstOrDefault().FieldName;
                    Values[0]  = CurrentID;
                    Values[1]  = ForeignID;
                }
                else
                {
                    Parameters = ForeignMapping.TableName + ForeignMapping.IDProperties.FirstOrDefault().FieldName + "," + Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName;
                    Values[1]  = CurrentID;
                    Values[0]  = ForeignID;
                }
                ReturnValue.AddCommand(null, Object,
                                       "INSERT INTO " + Property.TableName + "(" + Parameters + ") VALUES (@0,@1)",
                                       CommandType.Text,
                                       Values);
                return(ReturnValue);
            }
            var List = (IEnumerable <ItemType>)Property.GetValue(Object);

            if (List == null)
            {
                return(ReturnValue);
            }
            foreach (ItemType Item in List)
            {
                if (Item != null)
                {
                    object   CurrentID      = Mapping.IDProperties.FirstOrDefault().GetValue(Object);
                    IMapping ForeignMapping = Property.ForeignMapping;
                    object   ForeignID      = ForeignMapping.IDProperties.FirstOrDefault().GetValue(Item);
                    string   Parameters     = "";
                    object[] Values         = new object[2];
                    if (string.Compare(Mapping.TableName, ForeignMapping.TableName, StringComparison.InvariantCulture) < 0)
                    {
                        Parameters = Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName + "," + ForeignMapping.TableName + ForeignMapping.IDProperties.FirstOrDefault().FieldName;
                        Values[0]  = CurrentID;
                        Values[1]  = ForeignID;
                    }
                    else if (string.Compare(Mapping.TableName, ForeignMapping.TableName, StringComparison.InvariantCulture) == 0)
                    {
                        Parameters = Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName + "," + ForeignMapping.TableName + ForeignMapping.IDProperties.FirstOrDefault().FieldName + "2";
                        Values[0]  = CurrentID;
                        Values[1]  = ForeignID;
                    }
                    else
                    {
                        Parameters = ForeignMapping.TableName + ForeignMapping.IDProperties.FirstOrDefault().FieldName + "," + Mapping.TableName + Mapping.IDProperties.FirstOrDefault().FieldName;
                        Values[1]  = CurrentID;
                        Values[0]  = ForeignID;
                    }
                    ReturnValue.AddCommand(null,
                                           Object,
                                           "INSERT INTO " + Property.TableName + "(" + Parameters + ") VALUES (@0,@1)",
                                           CommandType.Text,
                                           Values);
                }
            }
            return(ReturnValue);
        }
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.tables.name as [Table] FROM sys.tables");
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES");
 }
 /// <summary>
 /// Gets the command.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void GetCommand(IBatch batch)
 {
     if (batch == null)
         throw new ArgumentNullException(nameof(batch));
     batch.AddCommand(null, null, CommandType.Text, @"SELECT sys.procedures.name as NAME,OBJECT_DEFINITION(sys.procedures.object_id) as DEFINITION FROM sys.procedures");
 }