public override ITable GetTableFromDB(IDataProvider provider, string tableName) { ITable result = null; DataTable schema; using (var scope = new AutomaticConnectionScope(provider)) { var restrictions = new string[] { null, tableName, null }; // restrictions are {owner, table, column} schema = scope.Connection.GetSchema("COLUMNS", restrictions); } if (schema.Rows.Count > 0) { result = new DatabaseTable(tableName, provider); foreach (DataRow dr in schema.Rows) { IColumn col = new DatabaseColumn(dr["COLUMN_NAME"].ToString(), result); col.DataType = GetDbType(dr["DATATYPE"].ToString()); col.IsNullable = dr["NULLABLE"].ToString() == "Y"; var maxLength = dr["LENGTH"].ToString(); var precision = dr["PRECISION"].ToString(); var scale = dr["SCALE"].ToString(); int iMax = 0, iPrecision = 0, iScale = 0; int.TryParse(maxLength, out iMax); int.TryParse(precision, out iPrecision); int.TryParse(scale, out iScale); col.MaxLength = iMax; col.NumericPrecision = iPrecision; col.NumberScale = iScale; result.Columns.Add(col); } } return result; }
///<summary> ///Gets an ITable from the DB based on name ///</summary> public virtual ITable GetTableFromDB(IDataProvider provider, string tableName) { ITable result = null; DataTable schema; using(var scope = new AutomaticConnectionScope(provider)) { var restrictions = new string[4] {null, null, tableName, null}; schema = scope.Connection.GetSchema("COLUMNS", restrictions); } if(schema.Rows.Count > 0) { result = new DatabaseTable(tableName, provider); foreach(DataRow dr in schema.Rows) { IColumn col = new DatabaseColumn(dr["COLUMN_NAME"].ToString(), result); col.DataType = GetDbType(dr["DATA_TYPE"].ToString()); col.IsNullable = dr["IS_NULLABLE"].ToString() == "YES"; string maxLength = dr["CHARACTER_MAXIMUM_LENGTH"].ToString(); int iMax = 0; int.TryParse(maxLength, out iMax); col.MaxLength = iMax; result.Columns.Add(col); } } return result; }
public override ITable GetTableFromDB(IDataProvider provider, string tableName) { ITable result = null; DataTable schema; using (var scope = new AutomaticConnectionScope(provider)) { var restrictions = new string[4] { null, null, tableName, null }; schema = scope.Connection.GetSchema("Columns", restrictions); //case difference between default and the Npgsql provider } if (schema.Rows.Count > 0) { result = new DatabaseTable(tableName, provider); foreach (DataRow dr in schema.Rows) { IColumn col = new DatabaseColumn(dr["column_name"].ToString(), result); col.DataType = GetDbType(dr["DATA_TYPE"].ToString()); //TODO: might have to derive this from obvervation of attributes col.IsNullable = dr["is_nullable"].ToString() == "YES"; string maxLength = dr["character_maximum_length"].ToString(); int iMax = 0; int.TryParse(maxLength, out iMax); col.MaxLength = iMax; result.Columns.Add(col); } } return result; }