예제 #1
0
        public ISchemaBuilder CreateMapIndexTable(string name, Action <ICreateTableCommand> table)
        {
            try
            {
                var createTable   = new CreateTableCommand(Prefix(name));
                var collection    = CollectionHelper.Current;
                var documentTable = collection.GetPrefixedName(Store.DocumentTable);

                createTable
                .Column <int>("Id", column => column.PrimaryKey().Identity().NotNull())
                .Column <int>("DocumentId");

                table(createTable);
                Execute(_builder.CreateSql(createTable));

                CreateForeignKey("FK_" + name, name, new[] { "DocumentId" }, documentTable, new[] { "Id" });
            }
            catch
            {
                if (ThrowOnError)
                {
                    throw;
                }
            }

            return(this);
        }
예제 #2
0
        public SchemaBuilder CreateReduceIndexTable(string name, Action <CreateTableCommand> table)
        {
            try
            {
                var createTable   = new CreateTableCommand(Prefix(name));
                var collection    = CollectionHelper.Current;
                var documentTable = collection.GetPrefixedName(Store.DocumentTable);

                createTable
                .Column <int>("Id", column => column.Identity().NotNull())
                ;

                table(createTable);
                Execute(_builder.CreateSql(createTable));

                var bridgeTableName = name + "_" + documentTable;

                CreateTable(bridgeTableName, bridge => bridge
                            .Column <int>(name + "Id", column => column.NotNull())
                            .Column <int>("DocumentId", column => column.NotNull())
                            );

                CreateForeignKey("FK_" + bridgeTableName + "_Id", bridgeTableName, new[] { name + "Id" }, name, new[] { "Id" });
                CreateForeignKey("FK_" + bridgeTableName + "_DocumentId", bridgeTableName, new[] { "DocumentId" }, documentTable, new[] { "Id" });
            }
            catch
            {
                if (ThrowOnError)
                {
                    throw;
                }
            }

            return(this);
        }
예제 #3
0
        public ISchemaBuilder CreateMapIndexTable(Type indexType, Action <ICreateTableCommand> table, string collection)
        {
            try
            {
                var indexName     = indexType.Name;
                var indexTable    = TableNameConvention.GetIndexTable(indexType, collection);
                var createTable   = new CreateTableCommand(Prefix(indexTable));
                var documentTable = TableNameConvention.GetDocumentTable(collection);

                // NB: Identity() implies PrimaryKey()

                createTable
                .Column <int>("Id", column => column.Identity().NotNull())
                .Column <int>("DocumentId")
                ;

                table(createTable);
                Execute(_commandInterpreter.CreateSql(createTable));

                CreateForeignKey("FK_" + (collection ?? "") + indexName, indexTable, new[] { "DocumentId" }, documentTable, new[] { "Id" });

                AlterTable(indexTable, table =>
                           table.CreateIndex($"IDX_FK_{indexTable}", "DocumentId")
                           );
            }
            catch
            {
                if (ThrowOnError)
                {
                    throw;
                }
            }

            return(this);
        }
예제 #4
0
        /// <summary>Creates a column with the name of TBaseRecordProperty_Id. This follows the standard orchard conventions.</summary>
        /// <typeparam name="TForeignRecord">The Record-Type of the virtual property.</typeparam>
        /// <typeparam name="TBaseRecord">The ContentPartRecord holding the virtual property pointing to the foreign record.</typeparam>
        /// <param name="self">A CreateTableCommand</param>
        /// <param name="expression">An expression to get the virtual property.</param>
        /// <param name="column">Actions to alter the column further.</param>
        /// <returns>The CreateTableCommand</returns>
        public static CreateTableCommand ColumnForForeignKey <TForeignRecord, TBaseRecord>(this CreateTableCommand self, Expression <Func <TBaseRecord, TForeignRecord> > expression, Action <CreateColumnCommand> column = null)
        {
            var dbType = SchemaUtils.ToDbType(typeof(int));

            var property = ReflectOn <TBaseRecord> .NameOf(expression);

            var columnName = string.Format("{0}_Id", property);

            return(self.Column(columnName, dbType, column));
        }
예제 #5
0
        public static CreateTableCommand Column <TModel, TProperty>(
            this CreateTableCommand command,
            Expression <Func <TModel, TProperty> > propertySelector,
            Action <CreateColumnCommand> column = null,
            bool isPrefixWithModelType          = false)
        {
            var columnName   = GetColumnName(propertySelector, isPrefixWithModelType);
            var propertyType = GetPropertyType <TProperty>();
            var dbType       = SchemaUtils.ToDbType(propertyType);

            return(command.Column(columnName, dbType, column));
        }
예제 #6
0
        public SchemaBuilder CreateMapIndexTable(string name, Action <CreateTableCommand> table)
        {
            var createTable = new CreateTableCommand(FormatTable(name));

            createTable
            .Column <int>("Id", column => column.PrimaryKey().Identity().NotNull())
            .Column <int>("DocumentId");

            table(createTable);
            Execute(_builder.CreateSql(createTable));

            CreateForeignKey("FK_" + name, name, new[] { "DocumentId" }, "Document", new[] { "Id" });
            return(this);
        }
예제 #7
0
        public SchemaBuilder CreateReduceIndexTable(string name, Action <CreateTableCommand> table)
        {
            var createTable = new CreateTableCommand(FormatTable(name));

            createTable
            .Column <int>("Id", column => column.Identity().NotNull())
            ;

            table(createTable);
            Execute(_builder.CreateSql(createTable));

            var bridgeTableName = name + "_Document";

            CreateTable(bridgeTableName, bridge => bridge
                        .Column <int>(name + "Id", column => column.NotNull())
                        .Column <int>("DocumentId", column => column.NotNull())
                        );

            CreateForeignKey("FK_" + bridgeTableName + "_Id", bridgeTableName, new[] { name + "Id" }, name, new[] { "Id" });
            CreateForeignKey("FK_" + bridgeTableName + "DocumentId", bridgeTableName, new[] { "DocumentId" }, "Document", new[] { "Id" });
            return(this);
        }
예제 #8
0
        public ISchemaBuilder CreateReduceIndexTable(Type indexType, Action <ICreateTableCommand> table, string collection = null)
        {
            try
            {
                var indexName     = indexType.Name;
                var indexTable    = TableNameConvention.GetIndexTable(indexType, collection);
                var createTable   = new CreateTableCommand(Prefix(indexTable));
                var documentTable = TableNameConvention.GetDocumentTable(collection);

                createTable
                .Column <int>("Id", column => column.Identity().NotNull())
                ;

                table(createTable);
                Execute(_commandInterpreter.CreateSql(createTable));

                var bridgeTableName = indexTable + "_" + documentTable;

                CreateTable(bridgeTableName, bridge => bridge
                            .Column <int>(indexName + "Id", column => column.NotNull())
                            .Column <int>("DocumentId", column => column.NotNull())
                            );

                CreateForeignKey("FK_" + bridgeTableName + "_Id", bridgeTableName, new[] { indexName + "Id" }, indexTable, new[] { "Id" });
                CreateForeignKey("FK_" + bridgeTableName + "_DocumentId", bridgeTableName, new[] { "DocumentId" }, documentTable, new[] { "Id" });
            }
            catch
            {
                if (ThrowOnError)
                {
                    throw;
                }
            }

            return(this);
        }
        /// <summary>
        /// Generates SchemaCommand instances in order to create the schema for a specific feature
        /// </summary>
        public IEnumerable<SchemaCommand> GetCreateFeatureCommands(string feature, bool drop)
        {
            var dependencies = _extensionManager.AvailableFeatures()
                .Where(f => String.Equals(f.Id, feature, StringComparison.OrdinalIgnoreCase))
                .Where(f => f.Dependencies != null)
                .SelectMany(f => f.Dependencies)
                .ToList();

            var shellDescriptor = new ShellDescriptor {
                Features = dependencies.Select(id => new ShellFeature { Name = id }).Union(new[] { new ShellFeature { Name = feature }, new ShellFeature { Name = "Coevery.Framework" } })
            };

            var shellBlueprint = _compositionStrategy.Compose(_shellSettings, shellDescriptor);

            if ( !shellBlueprint.Records.Any() ) {
                yield break;
            }

            //var features = dependencies.Select(name => new ShellFeature {Name = name}).Union(new[] {new ShellFeature {Name = feature}, new ShellFeature {Name = "Coevery.Framework"}});

            var parameters = _sessionFactoryHolder.GetSessionFactoryParameters();
            parameters.RecordDescriptors = shellBlueprint.Records.ToList();

            var configuration = _dataServicesProviderFactory
                .CreateProvider(parameters)
                .BuildConfiguration(parameters);

            Dialect.GetDialect(configuration.Properties);
            var mapping = configuration.BuildMapping();

            // get the table mappings using reflection
            var tablesField = typeof(Configuration).GetField("tables", BindingFlags.Instance | BindingFlags.NonPublic);
            var tables = ((IDictionary<string, Table>) tablesField.GetValue(configuration)).Values;

            string prefix = feature.Replace(".", "_") + "_";

            foreach(var table in tables.Where(t => parameters.RecordDescriptors.Any(rd => rd.Feature.Descriptor.Id == feature && rd.TableName == t.Name))) {
                string tableName = table.Name;
                var recordType = parameters.RecordDescriptors.First(rd => rd.Feature.Descriptor.Id == feature && rd.TableName == tableName).Type;
                var isContentPart = typeof(ContentPartRecord).IsAssignableFrom(recordType);

                if ( tableName.StartsWith(prefix) ) {
                    tableName = tableName.Substring(prefix.Length);
                }

                if(drop) {
                    yield return new DropTableCommand(tableName);
                }

                var command = new CreateTableCommand(tableName);

                foreach (var column in table.ColumnIterator) {
                    // create copies for local variables to be evaluated at the time the loop is called, and not lately when the la;bda is executed
                    var tableCopy = table;
                    var columnCopy = column;

                    var sqlType = columnCopy.GetSqlTypeCode(mapping);
                    command.Column(column.Name, sqlType.DbType,
                        action => {
                            if (tableCopy.PrimaryKey.Columns.Any(c => c.Name == columnCopy.Name)) {
                                action.PrimaryKey();

                                if ( !isContentPart ) {
                                    action.Identity();
                                }
                            }

                            if ( columnCopy.IsLengthDefined()
                                && new[] { DbType.StringFixedLength, DbType.String, DbType.AnsiString, DbType.AnsiStringFixedLength }.Contains(sqlType.DbType)
                                && columnCopy.Length != Column.DefaultLength) {
                                action.WithLength(columnCopy.Length);
                            }

                            if (columnCopy.IsPrecisionDefined()) {
                                action.WithPrecision((byte) columnCopy.Precision);
                                action.WithScale((byte) columnCopy.Scale);
                            }
                            if (columnCopy.IsNullable) {
                                action.Nullable();
                            }

                            if ( columnCopy.IsUnique ) {
                                action.Unique();
                            }

                            if(columnCopy.DefaultValue != null) {
                                action.WithDefault(columnCopy.DefaultValue);
                            }
                        });
                }

                yield return command;
            }
        }
예제 #10
0
        /// <summary>
        /// Generates SchemaCommand instances in order to create the schema for a specific feature
        /// </summary>
        public IEnumerable <SchemaCommand> GetCreateFeatureCommands(string feature, bool drop)
        {
            var dependencies = _extensionManager.AvailableFeatures()
                               .Where(f => String.Equals(f.Id, feature, StringComparison.OrdinalIgnoreCase))
                               .Where(f => f.Dependencies != null)
                               .SelectMany(f => f.Dependencies)
                               .ToList();

            var shellDescriptor = new ShellDescriptor {
                Features = dependencies.Select(id => new ShellFeature {
                    Name = id
                }).Union(new[] { new ShellFeature {
                                     Name = feature
                                 }, new ShellFeature {
                                     Name = "Orchard.Framework"
                                 } })
            };

            var shellBlueprint = _compositionStrategy.Compose(_shellSettings, shellDescriptor);

            if (!shellBlueprint.Records.Any())
            {
                yield break;
            }

            //var features = dependencies.Select(name => new ShellFeature {Name = name}).Union(new[] {new ShellFeature {Name = feature}, new ShellFeature {Name = "Orchard.Framework"}});

            var parameters = _sessionFactoryHolder.GetSessionFactoryParameters();

            parameters.RecordDescriptors = shellBlueprint.Records;

            var configuration = _dataServicesProviderFactory
                                .CreateProvider(parameters)
                                .BuildConfiguration(parameters);

            Dialect.GetDialect(configuration.Properties);
            var mapping = configuration.BuildMapping();

            // get the table mappings using reflection
            var tablesField = typeof(Configuration).GetField("tables", BindingFlags.Instance | BindingFlags.NonPublic);
            var tables      = ((IDictionary <string, Table>)tablesField.GetValue(configuration)).Values;

            string prefix = feature.Replace(".", "_") + "_";

            foreach (var table in tables.Where(t => parameters.RecordDescriptors.Any(rd => rd.Feature.Descriptor.Id == feature && rd.TableName == t.Name)))
            {
                string tableName     = table.Name;
                var    recordType    = parameters.RecordDescriptors.First(rd => rd.Feature.Descriptor.Id == feature && rd.TableName == tableName).Type;
                var    isContentPart = typeof(ContentPartRecord).IsAssignableFrom(recordType);

                if (tableName.StartsWith(prefix))
                {
                    tableName = tableName.Substring(prefix.Length);
                }

                if (drop)
                {
                    yield return(new DropTableCommand(tableName));
                }

                var command = new CreateTableCommand(tableName);

                foreach (var column in table.ColumnIterator)
                {
                    // create copies for local variables to be evaluated at the time the loop is called, and not lately when the la;bda is executed
                    var tableCopy  = table;
                    var columnCopy = column;

                    var sqlType = columnCopy.GetSqlTypeCode(mapping);
                    command.Column(column.Name, sqlType.DbType,
                                   action => {
                        if (tableCopy.PrimaryKey.Columns.Any(c => c.Name == columnCopy.Name))
                        {
                            action.PrimaryKey();

                            if (!isContentPart)
                            {
                                action.Identity();
                            }
                        }


                        if (columnCopy.IsLengthDefined() &&
                            new[] { DbType.StringFixedLength, DbType.String, DbType.AnsiString, DbType.AnsiStringFixedLength }.Contains(sqlType.DbType) &&
                            columnCopy.Length != Column.DefaultLength)
                        {
                            action.WithLength(columnCopy.Length);
                        }

                        if (columnCopy.IsPrecisionDefined())
                        {
                            action.WithPrecision((byte)columnCopy.Precision);
                            action.WithScale((byte)columnCopy.Scale);
                        }
                        if (columnCopy.IsNullable)
                        {
                            action.Nullable();
                        }

                        if (columnCopy.IsUnique)
                        {
                            action.Unique();
                        }

                        if (columnCopy.DefaultValue != null)
                        {
                            action.WithDefault(columnCopy.DefaultValue);
                        }
                    });
                }

                yield return(command);
            }
        }
예제 #11
0
        public static CreateTableCommand ColumnFor <TRecord>(this CreateTableCommand self, Expression <Func <TRecord, object> > expression, DbType dbType, Action <CreateColumnCommand> column = null)
        {
            var property = ObjectExtensions.PropertyName(expression);

            return(self.Column(property, dbType, column));
        }
예제 #12
0
        public SchemaBuilder CreateReduceIndexTable(string name, Action<CreateTableCommand> table)
        {
            var createTable = new CreateTableCommand(FormatTable(name));

            createTable
                .Column<int>("Id", column => column.Identity().NotNull())
                ;

            table(createTable);
            Execute(_builder.CreateSql(createTable));

            var bridgeTableName = name + "_Document";

            CreateTable(bridgeTableName, bridge => bridge
                .Column<int>(name + "Id", column => column.NotNull())
                .Column<int>("DocumentId", column => column.NotNull())
            );

            CreateForeignKey("FK_" + bridgeTableName + "_Id", bridgeTableName, new[] { name + "Id" }, name, new[] { "Id" });
            CreateForeignKey("FK_" + bridgeTableName + "DocumentId" , bridgeTableName, new[] { "DocumentId" }, "Document", new[] { "Id" });
            return this;
        }
예제 #13
0
        public SchemaBuilder CreateMapIndexTable(string name, Action<CreateTableCommand> table)
        {
            var createTable = new CreateTableCommand(FormatTable(name));

            createTable
                .Column<int>("Id", column => column.PrimaryKey().Identity().NotNull())
                .Column<int>("DocumentId");

            table(createTable);
            Execute(_builder.CreateSql(createTable));

            CreateForeignKey("FK_" + name, name, new[] { "DocumentId" }, "Document", new[] { "Id" });
            return this;
        }
예제 #14
0
        public SchemaBuilder CreateMapIndexTable(string name, Action<CreateTableCommand> table)
        {
            var createTable = new CreateTableCommand(FormatTable(name));
            var collection = CollectionHelper.Current;
            var documentTable = collection.GetPrefixedName(Store.DocumentTable);

            createTable
                .Column<int>("Id", column => column.PrimaryKey().Identity().NotNull())
                .Column<int>("DocumentId");

            table(createTable);
            Execute(_builder.CreateSql(createTable));

            CreateForeignKey("FK_" + name, name, new[] { "DocumentId" }, documentTable, new[] { "Id" });
            return this;
        }