Beispiel #1
0
        public static TableMetadata Deserialize(BinaryReader input)
        {
            var dtList        = new List <DerivativeTable>();
            var fkList        = new List <ForeignKey>();
            var schemaColList = new List <ColumnDefinition>();

            var t = new TableMetadata(input.ReadString())
            {
                IsStatic      = input.ReadBoolean(),
                SelectCommand = input.ReadString(),
                InsertCommand = input.ReadString()
            };

            var nbRows = input.ReadInt32();

            for (var i = 0; i < nbRows; i++)
            {
                dtList.Add(new DerivativeTable
                {
                    ServerId = input.ReadInt16(),
                    Database = input.ReadString(),
                    Schema   = input.ReadString(),
                    Table    = input.ReadString(),
                    Access   = (DerivativeTableAccess)input.ReadInt32(),
                    Cascade  = input.ReadBoolean()
                });
            }

            nbRows = input.ReadInt32();
            for (var i = 0; i < nbRows; i++)
            {
                var fkColList = new List <ForeignKeyColumn>();

                var fk = new ForeignKey
                {
                    ServerIdTo = input.ReadInt16(),
                    DatabaseTo = input.ReadString(),
                    SchemaTo   = input.ReadString(),
                    TableTo    = input.ReadString()
                };

                var nbRows2 = input.ReadInt32();
                for (var j = 0; j < nbRows2; j++)
                {
                    fkColList.Add(new ForeignKeyColumn
                    {
                        NameFrom = input.ReadString(),
                        NameTo   = input.ReadString()
                    });
                }

                fk.Columns = fkColList;
                fkList.Add(fk);
            }

            nbRows = input.ReadInt32();
            for (var i = 0; i < nbRows; i++)
            {
                schemaColList.Add(new ColumnDefinition
                {
                    Name            = input.ReadString(),
                    DbType          = (DbType)input.ReadInt32(),
                    IsPrimary       = input.ReadBoolean(),
                    IsForeignKey    = input.ReadBoolean(),
                    IsAutoIncrement = input.ReadBoolean(),
                    BuilderName     = input.ReadString(),
                    SqlType         = SqlType.Deserialize(input)
                });
            }

            t.DerivativeTables  = dtList;
            t.ForeignKeys       = fkList;
            t.ColumnsDefinition = schemaColList;

            return(t);
        }
Beispiel #2
0
        internal void LoadForeignKeys(IDataReader reader, Int16 serverId, String database)
        {
            var lstForeignKeys       = new List <ForeignKey>();
            var lstForeignKeyColumns = new List <ForeignKeyColumn>();

            if (!reader.Read())
            {
                return;
            }

            //Init first row
            var currentSchema          = reader.GetString(0);
            var previousTable          = this[serverId][database][currentSchema].First(t => t.Name.Equals(reader.GetString(1), StringComparison.OrdinalIgnoreCase));
            var previousConstraintName = reader.GetString(2);
            var previousConstraint     = new ForeignKey
            {
                ServerIdTo = serverId,
                DatabaseTo = database,
                SchemaTo   = currentSchema,
                TableTo    = reader.GetString(5)
            };

            //Pour chaque ligne
            do
            {
                currentSchema = reader.GetString(0);
                var currentTable      = reader.GetString(1);
                var currentConstraint = reader.GetString(2);

                //Si on change de constraint
                if (currentTable != previousTable.Name || currentConstraint != previousConstraintName)
                {
                    previousConstraint.Columns = lstForeignKeyColumns;
                    lstForeignKeys.Add(previousConstraint);

                    lstForeignKeyColumns = new List <ForeignKeyColumn>();
                    previousConstraint   = new ForeignKey
                    {
                        ServerIdTo = serverId,
                        DatabaseTo = database,
                        SchemaTo   = currentSchema,
                        TableTo    = reader.GetString(5)
                    };
                    previousConstraintName = currentConstraint;
                }

                //Si on change de table
                if (currentTable != previousTable.Name)
                {
                    previousTable.ForeignKeys = lstForeignKeys;

                    //Change de table
                    previousTable  = this[serverId][database][currentSchema].First(t => t.Name.Equals(reader.GetString(1), StringComparison.OrdinalIgnoreCase));
                    lstForeignKeys = new List <ForeignKey>();
                }

                //Ajoute la colonne
                var colName = reader.GetString(3);
                lstForeignKeyColumns.Add(new ForeignKeyColumn
                {
                    NameFrom = colName,
                    NameTo   = reader.GetString(6)
                });

                //Affecte l'indicateur dans le schema
                var col = previousTable.ColumnsDefinition.FirstOrDefault(c => c.Name.Equals(colName, StringComparison.OrdinalIgnoreCase));
                if (col == null)
                {
                    throw new Exception($"The column {colName} has not been found in the metadata for the table {previousTable.Name}.");
                }
                col.IsForeignKey = true;
            } while (reader.Read());

            //Ajoute la dernière table / schema
            if (lstForeignKeyColumns.Count > 0)
            {
                previousConstraint.Columns = lstForeignKeyColumns;
                lstForeignKeys.Add(previousConstraint);
                previousTable.ForeignKeys = lstForeignKeys;
            }
        }