longde123 / csharp-sqlite

Automatically exported from code.google.com/p/csharp-sqlite
Other
0 stars 0 forks source link

SqliteDataReader.GetSchemaTable returns wrong type for all columns #185

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to use version 3.7.7.1 and encountered problem that GetShemaTable 
reports wrong types:
var provider = SqliteClientFactory.Instance;
var loader = new DataTableLoader();
using (var connection = provider.CreateConnection())
{
    connection.ConnectionString = "data source=:memory:";
    connection.Open();
    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT CAST(1 AS INTEGER) AS INTCOL, CAST(1.0 AS REAL) AS DOUBLECOL, '1' AS STRINGCOL";
        using (var reader = cmd.ExecuteReader())
        {
            Assert.AreEqual(typeof(long), reader.GetFieldType(0));
            Assert.AreEqual(typeof(double), reader.GetFieldType(1));
            Assert.AreEqual(typeof(string), reader.GetFieldType(2));
            var schema = reader.GetSchemaTable();
            Assert.AreEqual(typeof(long), (Type)schema.Rows[0][SchemaTableColumn.DataType]);
            Assert.AreEqual(typeof(double), (Type)schema.Rows[1][SchemaTableColumn.DataType]);
            Assert.AreEqual(typeof(string), (Type)schema.Rows[2][SchemaTableColumn.DataType]);
        }
    }
}

I've checked the code and indeed 'DataType' columns value is 'hard-coded':
<from  Community.CsharpSqlite.SQLiteClient.SqliteDataReader.GetSchemaTable>
...
for (int i = 0; i < this.FieldCount; i++)
{
    DataRow dataRow = dataTable.NewRow();
    dataRow["ColumnName"] = this.columns[i];
    dataRow["ColumnOrdinal"] = i;
    dataRow["ColumnSize"] = 0;
    dataRow["NumericPrecision"] = 0;
    dataRow["NumericScale"] = 0;
    dataRow["IsUnique"] = false;
    dataRow["IsKey"] = false;
    dataRow["BaseCatalogName"] = "";
    dataRow["BaseColumnName"] = this.columns[i];
    dataRow["BaseSchemaName"] = "";
    dataRow["BaseTableName"] = "";
    dataRow["DataType"] = typeof(string);
    dataRow["AllowDBNull"] = true;
    dataRow["ProviderType"] = 0;
    dataRow["IsAliased"] = false;
    dataRow["IsExpression"] = false;
    dataRow["IsIdentity"] = false;
    dataRow["IsAutoIncrement"] = false;
    dataRow["IsRowVersion"] = false;
    dataRow["IsHidden"] = false;
    dataRow["IsLong"] = false;
    dataRow["IsReadOnly"] = false;
    dataTable.Rows.Add(dataRow);
    dataRow.AcceptChanges();
}
...

This makes new DataTable().Load(reader) to create table with string columns and 
makes it unusable for any purposes.

Original issue reported on code.google.com by i.sinis...@gmail.com on 14 May 2014 at 11:13