Esempio n. 1
0
        /// <summary>
        /// Creates a new DBSchemaTable for a returned GetSchema DbTable definition
        /// </summary>
        #region protected virtual DBSchemaTable CreateSchemaTable(TableMappingClass mapping, DataRow tblRow)

        /// <param name="tblRow"></param>
        /// <returns></returns>
        protected virtual DBSchemaTable CreateSchemaTable(TableMappingClass mapping, DataRow tblRow)
        {
            DBSchemaTable sTbl = new DBSchemaTable();

            DataColumn col = GetColumn(tblRow.Table, mapping.TableCatalogColumn, false);

            sTbl.Catalog = this.GetColumnValue(tblRow, col);

            col         = GetColumn(tblRow.Table, mapping.TableSchemaColumn, false);
            sTbl.Schema = this.GetColumnValue(tblRow, col);

            col       = GetColumn(tblRow.Table, mapping.TableNameColumn, false);
            sTbl.Name = this.GetColumnValue(tblRow, col);

            return(sTbl);
        }
Esempio n. 2
0
        //
        // protected implementation
        //

        /// <summary>
        /// Loads a complete schema for the specified Table schema reference including all columns, and indexes.
        /// </summary>
        #region protected virtual DBSchemaTable LoadATable(DbConnection con, DBSchemaItemRef forRef)

        /// <param name="con"></param>
        /// <param name="forRef"></param>
        /// <returns></returns>
        protected virtual DBSchemaTable LoadATable(DbConnection con, DBSchemaItemRef forRef)
        {
            DBSchemaTable sTbl    = null;
            string        catalog = string.IsNullOrEmpty(forRef.Catalog) ? null : forRef.Catalog;
            string        schema  = string.IsNullOrEmpty(forRef.Schema) ? null : forRef.Schema;

            DataTable tbl     = con.GetSchema(SchemaTablesName, new string[] { catalog, schema, forRef.Name, null });
            DataTable columns = con.GetSchema(SchemaColumnsName, new string[] { catalog, schema, forRef.Name, null });

            if (tbl.Rows.Count > 0)
            {
                DataRow                 tblRow        = tbl.Rows[0];
                TableMappingClass       tblmapping    = this.GetTableMapping();
                TableColumnMappingClass tblcolmapping = this.GetTableColumnMapping();

                sTbl = CreateSchemaTable(tblmapping, tblRow);

                FillTableColumns(sTbl, tblcolmapping, columns);
            }


            return(sTbl);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates and populates the DBSchemaTable.Columns property based upon a returned DataTable for the GetSchema("Columns") query
        /// </summary>
        #region private void FillTableColumns(DBSchemaTable sTbl,  DataTable columns)

        /// <param name="sTbl"></param>
        /// <param name="columns"></param>
        protected virtual void FillTableColumns(DBSchemaTable sTbl, TableColumnMappingClass mapping, DataTable columns)
        {
            DataColumn colCatalog = this.GetColumn(columns, mapping.TableCatalogColumn, false);
            DataColumn colSchema  = this.GetColumn(columns, mapping.TableSchemaColumn, false);
            DataColumn colTable   = this.GetColumn(columns, mapping.TableNameColumn, true);
            DataColumn colName    = this.GetColumn(columns, mapping.ColumnNameColumn, true);
            DataColumn colDefault = this.GetColumn(columns, mapping.DefaultValueColumn, false);
            DataColumn colType    = this.GetColumn(columns, mapping.DataTypeColumn, true);
            DataColumn colMaxLen  = this.GetColumn(columns, mapping.MaxCharacterLengthColumn, false);
            DataColumn colAuto    = this.GetColumn(columns, mapping.AutoNumberColumn, false);
            DataColumn colNull    = this.GetColumn(columns, mapping.IsNullableColumn, false);
            DataColumn colOrdinal = this.GetColumn(columns, mapping.OrdinalPositionColumn, false);
            DataColumn colPK      = this.GetColumn(columns, mapping.PrimaryKeyColumn, false);

            foreach (DataRow colRow in columns.Rows)
            {
                if (sTbl.Name == colRow[colTable].ToString())
                {
                    DBSchemaTableColumn column = new DBSchemaTableColumn();
                    column.Name = GetColumnValue(colRow, colName);

                    if (string.IsNullOrEmpty(column.Name))
                    {
                        throw new DBSchemaProviderException(string.Format(Errors.SchemaColumnCannotBeEmpty, mapping.ColumnNameColumn));
                    }

                    string type     = GetColumnValue(colRow, colType);
                    string len      = GetColumnValue(colRow, colMaxLen);
                    string auto     = GetColumnValue(colRow, colAuto);
                    string ord      = GetColumnValue(colRow, colOrdinal);
                    string nullable = GetColumnValue(colRow, colNull);
                    string def      = GetColumnValue(colRow, colDefault);
                    string pk       = GetColumnValue(colRow, colPK);
                    int    i;
                    bool   b;

                    //Set AutoAssign
                    if (auto.IndexOf("auto_increment") > -1)
                    {
                        b = true;
                    }
                    else if (auto.ToLower() == "yes")
                    {
                        b = true;
                    }
                    else if (bool.TryParse(auto, out b) == false)
                    {
                        b = false;
                    }
                    column.AutoAssign = b;

                    //Set DbType
                    column.DbType = GetDbTypeForSqlType(type);

                    //Set DefaultValue and HasDefault
                    column.HasDefault   = string.IsNullOrEmpty(def) == false;
                    column.DefaultValue = def;

                    //Set IsNullable
                    if (nullable.ToLower() == "yes")
                    {
                        b = true;
                    }
                    else if (!bool.TryParse(nullable, out b))
                    {
                        b = false;
                    }
                    column.Nullable = b;

                    //Set ordinal position
                    if (int.TryParse(ord, out i))
                    {
                        column.OrdinalPosition = i;
                    }
                    else
                    {
                        column.OrdinalPosition = -1;
                    }

                    //Set readonly (same as autoassign)
                    column.ReadOnly = column.AutoAssign;

                    //Set column.Type
                    column.Type = DBHelper.GetRuntimeTypeForDbType(column.DbType);

                    //Set Primary Key
                    if (pk.ToLower() == "yes")
                    {
                        b = true;
                    }
                    else if (!bool.TryParse(pk, out b))
                    {
                        b = false;
                    }
                    column.PrimaryKey = b;

                    //Set Size
                    if (string.IsNullOrEmpty(len) == false && int.TryParse(len, out i))
                    {
                        column.Size = i;
                    }
                    else
                    {
                        column.Size = -1;
                    }


                    sTbl.Columns.Add(column);
                }
            }
        }