Exemple #1
0
        /// <summary>
        /// Load the metadata with the columns read from the IDataReader.
        /// The query must have this defenition in order :
        /// Schema, Table, Column, DataType, Precision, Scale, IsUnsigned, IsPrimaryKey, IsAutoIncrement
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="serverId"></param>
        /// <param name="database"></param>
        /// <param name="typeConverter"></param>
        internal void LoadColumns(IDataReader reader, Int16 serverId, String database, ISqlTypeConverter typeConverter)
        {
            var    schemaMetadata  = new SchemaMetadata();
            var    lstSchemaColumn = new List <ColumnDefinition>();
            string currentSchema;

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

            //Init first row
            var previousSchema = reader.GetString(0);
            var previousTable  = new TableMetadata(reader.GetString(1));

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

                //Si on change de table
                if (currentSchema != previousSchema || currentTable != previousTable.Name)
                {
                    previousTable.ColumnsDefinition = lstSchemaColumn;
                    schemaMetadata.Add(previousTable);

                    lstSchemaColumn = new List <ColumnDefinition>();
                    previousTable   = new TableMetadata(currentTable);
                }

                //Si on change de schema
                if (currentSchema != previousSchema)
                {
                    this[serverId, database, currentSchema] = schemaMetadata;
                    schemaMetadata = new SchemaMetadata();
                }

                //Ajoute la colonne
                var col = new ColumnDefinition
                {
                    Name    = reader.GetString(2),
                    SqlType = new SqlType
                    {
                        DataType   = reader.GetString(3),
                        Precision  = reader.GetInt32(4),
                        Scale      = reader.GetInt32(5),
                        IsUnsigned = reader.GetBoolean(6)
                    },
                    IsPrimary       = reader.GetBoolean(7),
                    IsAutoIncrement = reader.GetBoolean(8)
                };
                col.DbType = typeConverter.ConvertFromSql(col.SqlType);

                lstSchemaColumn.Add(col);
            } while (reader.Read());

            //Ajoute la dernière table / schema
            if (lstSchemaColumn.Count > 0)
            {
                previousTable.ColumnsDefinition = lstSchemaColumn;
                schemaMetadata.Add(previousTable);
                this[serverId, database, currentSchema] = schemaMetadata;
            }
        }