DISQUS

Just Sayin' More Words: How I Use SubSonic, Part 2

  • Hamed · 6 months ago
    Thanks for your nice template

    I was generating code using your proposed template for Repository Pattern against Oracle database 10.

    The problem that I faced was that when you use tbl.ForeighnKeys to get the list of the foreign tables, it was not not working. This was due to a problem in GetTableSchema in OracleDataProvider. To be more specific, in the implementation of GetTableSchema for OracleDataProvider, when columns are retrieved, ForeignKeys property is not initialized and foreignkey tables are not added into it. Thus, when code generation is being done, it cannot find foreignkey tables to make the properties for them in the generated code. I suggest modify the GetTableSchema to the following one:

    public override TableSchema.Table GetTableSchema(string tableName, TableType tableType)
    {
    TableSchema.TableColumnCollection columns = new TableSchema.TableColumnCollection();
    TableSchema.Table tbl = new TableSchema.Table(tableName, tableType, this);
    //tbl.ClassName = Convention.ClassName(tableName);
    //string sql = TABLE_COLUMN_SQL;
    QueryCommand cmd = new QueryCommand(TABLE_COLUMN_SQL, Name);
    cmd.AddParameter(TABLE_NAME_PARAMETER, tableName, DbType.AnsiString);
    TableSchema.TableColumn column;

    using(IDataReader rdr = DataService.GetReader(cmd))
    {
    //get information about both the table and it's columns
    while(rdr.Read())
    {
    tbl.SchemaName = rdr["USER"].ToString();

    column = new TableSchema.TableColumn(tbl);
    column.ColumnName = rdr[OracleSchemaVariable.COLUMN_NAME].ToString();

    string scale = rdr[OracleSchemaVariable.NUMBER_SCALE].ToString();
    string precision = rdr[OracleSchemaVariable.NUMBER_PRECISION].ToString();

    column.NumberScale = 0;
    column.NumberPrecision = 0;

    if(!String.IsNullOrEmpty(scale) && scale != "0")
    column.NumberScale = int.Parse(scale);

    if(!String.IsNullOrEmpty(precision) && precision != "0")
    column.NumberPrecision = int.Parse(precision);

    // column.DataType = GetDbType(rdr[OracleSchemaVariable.DATA_TYPE].ToString().ToLower());
    column.DataType = GetDbTypeOracle(rdr[OracleSchemaVariable.DATA_TYPE].ToString().ToLower(), column.NumberScale, column.NumberPrecision);
    column.AutoIncrement = false;
    int maxLength;
    int.TryParse(rdr[OracleSchemaVariable.MAX_LENGTH].ToString(), out maxLength);
    column.MaxLength = maxLength;
    column.IsNullable = Utility.IsMatch(rdr[OracleSchemaVariable.IS_NULLABLE].ToString(), "Y");
    column.IsReadOnly = false;
    columns.Add(column);
    }
    }

    cmd.CommandSql = INDEX_SQL;
    //cmd.AddParameter(TABLE_NAME_PARAMETER, tableName);
    tbl.ForeignKeys = new TableSchema.ForeignKeyTableCollection();

    using(IDataReader rdr = DataService.GetReader(cmd))
    {
    while(rdr.Read())
    {
    string colName = rdr[OracleSchemaVariable.COLUMN_NAME].ToString();
    string constraintType = rdr[OracleSchemaVariable.CONSTRAINT_TYPE].ToString();
    column = columns.GetColumn(colName);

    if(constraintType == SqlSchemaVariable.PRIMARY_KEY)
    column.IsPrimaryKey = true;
    else if(constraintType == SqlSchemaVariable.FOREIGN_KEY)
    column.IsForeignKey = true;

    //HACK: Allow second pass naming adjust based on whether a column is keyed
    column.ColumnName = column.ColumnName;

    //Hamed

    if (column.IsForeignKey)
    {
    TableSchema.ForeignKeyTable fkTable = new TableSchema.ForeignKeyTable(this);
    TableSchema.Table ftbl = this.GetForeignKeyTable(column, tbl);
    column.ForeignKeyTableName = ftbl.TableName;
    fkTable.ColumnName = column.ColumnName;
    fkTable.TableName = ftbl.TableName;

    //not needed for now
    //fkTable.PrimaryColumnName = drFK[i]["PK_Column"].ToString();
    //fkTable.ForeignColumnName = drFK[i]["FK_Column"].ToString();

    tbl.ForeignKeys.Add(fkTable);
    }

    }
    rdr.Close();
    }
    if(columns.Count > 0)
    {
    tbl.Columns = columns;
    return tbl;
    }
    return null;
    }
  • John Sheehan · 6 months ago
    Hi Hamed. You should submit an issue at
    http://code.google.com/p/subsonicproject for that issue.