예제 #1
0
        /// <summary>
        /// Constructs the foreign key with the details
        /// </summary>
        /// <param name="foreignKeyColumns">The foreign key columns</param>
        /// <param name="referencedTable">The referenced table</param>
        /// <param name="referencedColumns">The referenced columns</param>
        public DataForeignKey
        (
            DataColumnSchema[] foreignKeyColumns,
            DataTableSchema referencedTable,
            DataColumnSchema[] referencedColumns
        )
        {
            Validate.IsNotNull(foreignKeyColumns);
            Validate.IsNotNull(referencedTable);
            Validate.IsNotNull(referencedColumns);

            this.ForeignKeyColumns = foreignKeyColumns;
            this.ReferencedTable   = referencedTable;
            this.ReferencedColumns = referencedColumns;
        }
예제 #2
0
        /// <summary>
        /// Generates the table schema using a database connection
        /// </summary>
        /// <param name="connection">The database connection</param>
        /// <returns>The schema generated</returns>
        protected virtual DataTableSchema[] GenerateSchema
        (
            DbConnection connection
        )
        {
            var reader       = new DatabaseReader(connection);
            var schema       = reader.ReadAll();
            var tableSchemas = new List <DataTableSchema>();

            foreach (var dbTable in schema.Tables)
            {
                var columnSchemas = new List <DataColumnSchema>();

                foreach (var dbColumn in dbTable.Columns)
                {
                    columnSchemas.Add
                    (
                        new DataColumnSchema
                        (
                            dbColumn.Name,
                            dbColumn.DataType.GetNetType()
                        )
                    );
                }

                var tableSchema = new DataTableSchema
                                  (
                    dbTable.Name,
                    columnSchemas.ToArray()
                                  );

                // Add the primary keys if there are any in the table
                if (dbTable.PrimaryKey.Columns.Count > 0)
                {
                    var primaryKeyColumns = new List <DataColumnSchema>();

                    foreach (var columnName in dbTable.PrimaryKey.Columns)
                    {
                        var dbColumn = dbTable.FindColumn
                                       (
                            columnName
                                       );

                        primaryKeyColumns.Add
                        (
                            new DataColumnSchema
                            (
                                dbColumn.Name,
                                dbColumn.DataType.GetNetType()
                            )
                        );
                    }

                    tableSchema = tableSchema.WithPrimaryKey
                                  (
                        primaryKeyColumns.ToArray()
                                  );
                }

                // Add the foreign keys if there are any in the table
                if (dbTable.ForeignKeys.Any())
                {
                    var foreignKeySchemas = new List <DataForeignKey>();

                    foreach (var fk in dbTable.ForeignKeys)
                    {
                        if (false == String.IsNullOrEmpty(fk.RefersToTable))
                        {
                            var fkColumns         = new List <DataColumnSchema>();
                            var refPrimaryColumns = new List <DataColumnSchema>();
                            var refAllColumns     = new List <DataColumnSchema>();

                            // Build a list of the foreign key columns
                            foreach (var columnName in fk.Columns)
                            {
                                var dbColumn = dbTable.FindColumn
                                               (
                                    columnName
                                               );

                                fkColumns.Add
                                (
                                    new DataColumnSchema
                                    (
                                        dbColumn.Name,
                                        dbColumn.DataType.GetNetType()
                                    )
                                );
                            }

                            var referencedTable = fk.ReferencedTable(schema);

                            // Build a list of the referenced primary columns
                            foreach (var columnName in referencedTable.PrimaryKey.Columns)
                            {
                                var dbColumn = dbTable.FindColumn
                                               (
                                    columnName
                                               );

                                refPrimaryColumns.Add
                                (
                                    new DataColumnSchema
                                    (
                                        dbColumn.Name,
                                        dbColumn.DataType.GetNetType()
                                    )
                                );
                            }

                            // Build a list of the referenced table columns
                            foreach (var dbColumn in referencedTable.Columns)
                            {
                                refAllColumns.Add
                                (
                                    new DataColumnSchema
                                    (
                                        dbColumn.Name,
                                        dbColumn.DataType.GetNetType()
                                    )
                                );
                            }

                            var refTableSchema = new DataTableSchema
                                                 (
                                fk.RefersToTable,
                                refAllColumns.ToArray()
                                                 );

                            var fkSchema = new DataForeignKey
                                           (
                                fkColumns.ToArray(),
                                refTableSchema,
                                refPrimaryColumns.ToArray()
                                           );
                        }
                    }

                    tableSchema = tableSchema.WithForeignKeys
                                  (
                        foreignKeySchemas.ToArray()
                                  );
                }

                tableSchemas.Add(tableSchema);
            }

            this.DateSchemaResolved = DateTime.UtcNow;

            return(tableSchemas.ToArray());
        }