From 9f2f9dfac09ed16141d7036e8f0540aa37a7d7c1 Mon Sep 17 00:00:00 2001 From: Daniel Morgan Date: Sat, 11 May 2002 12:58:32 +0000 Subject: 2002-05-11 Daniel Morgan * System.Data.build: added copy of System.Data.dll to Test directory for easy testing. Also, added clean for it too. * System.Data.SqlClient/PostgresLibrary.cs: changed setting of boolean from PostgreSQL data type to .NET type. * System.Data.SqlClient/SqlDataReader.cs: beginnings handling of a NULL value from the database * Test/PostgresTest.cs: added tests for NULL values retrieved from the database * Test/ReadPostgresData.cs * Test/TestExecuteScalar.cs * Test/TestSqlDataReader.cs * Test/TestSqlException.cs * Test/TestSqlIsolationLevel.cs: updated tests to use databas user "postgres". These tests may eventually be removed since they are not flexible. svn path=/trunk/mcs/; revision=4529 --- .../Mono.Data.PostgreSqlClient/PgSqlCommand.cs | 2 +- .../Mono.Data.PostgreSqlClient/PgSqlDataReader.cs | 99 ++++++++++++++++------ .../Mono.Data.PostgreSqlClient/PostgresLibrary.cs | 35 +++++--- .../Mono.Data.PostgreSqlClient/PgSqlCommand.cs | 2 +- .../Mono.Data.PostgreSqlClient/PgSqlDataReader.cs | 99 ++++++++++++++++------ .../Mono.Data.PostgreSqlClient/PostgresLibrary.cs | 35 +++++--- 6 files changed, 200 insertions(+), 72 deletions(-) (limited to 'mcs/class/Mono.Data.PostgreSqlClient') diff --git a/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlCommand.cs b/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlCommand.cs index d287da27c5e..49183916f8d 100644 --- a/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlCommand.cs +++ b/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlCommand.cs @@ -240,7 +240,7 @@ namespace System.Data.SqlClient { int oid; types = new string[nFields]; - + for(nCol = 0; nCol < nFields; nCol++) { DbType dbType; diff --git a/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs b/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs index dc6ee48a677..ffaf076ca66 100644 --- a/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs +++ b/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs @@ -19,6 +19,10 @@ // Gonzalo Paniagua Javier // +// *** uncomment #define to get debug messages, comment for production *** +//#define DEBUG_SqlDataReader + + using System; using System.Collections; using System.ComponentModel; @@ -39,9 +43,13 @@ namespace System.Data.SqlClient { private SqlCommand cmd; private DataTable table; - private object[] fields; + // columns in a row + private object[] fields; // data value in a .NET type private string[] types; // PostgreSQL Type - private bool[] isNull; + private bool[] isNull; // is NULL? + private int[] actualLength; // ActualLength of data + private DbType[] dbTypes; // DB data type + // actucalLength = -1 is variable-length private bool open = false; IntPtr pgResult; // PGresult @@ -91,40 +99,60 @@ namespace System.Data.SqlClient { [MonoTODO] public bool Read() { - string value; - fields = new object[cols]; // re-init row - DbType dbType; - + string dataValue; + int c = 0; + + //Console.WriteLine("if current row: " + currentRow + " rows: " + rows); if(currentRow < rows - 1) { + + //Console.WriteLine("currentRow++: "); currentRow++; - int c; + + //Console.WriteLine("re-init row --- cols: " + cols); + + // re-init row + fields = new object[cols]; + //dbTypes = new DbType[cols]; + actualLength = new int[cols]; + isNull = new bool[cols]; + for(c = 0; c < cols; c++) { // get data value - value = PostgresLibrary. + dataValue = PostgresLibrary. PQgetvalue( pgResult, currentRow, c); - int columnIsNull; // is column NULL? - columnIsNull = PostgresLibrary. - PQgetisnull(pgResult, - currentRow, c); + //isNull[c] = PostgresLibrary. + // PQgetisnull(pgResult, + // currentRow, c); - int actualLength; // get Actual Length - actualLength = PostgresLibrary. + actualLength[c] = PostgresLibrary. PQgetlength(pgResult, currentRow, c); - + + DbType dbType; dbType = PostgresHelper. TypnameToSqlDbType(types[c]); - fields[c] = PostgresHelper. - ConvertDbTypeToSystem ( + if(dataValue == null) { + fields[c] = null; + isNull[c] = true; + } + else if(dataValue.Equals("")) { + fields[c] = null; + isNull[c] = true; + } + else { + isNull[c] = false; + fields[c] = PostgresHelper. + ConvertDbTypeToSystem ( dbType, - value); + dataValue); + } } return true; } @@ -162,12 +190,12 @@ namespace System.Data.SqlClient { [MonoTODO] public string GetDataTypeName(int i) { - throw new NotImplementedException (); + return types[i]; } [MonoTODO] public DateTime GetDateTime(int i) { - throw new NotImplementedException (); + return (DateTime) fields[i]; } [MonoTODO] @@ -182,7 +210,8 @@ namespace System.Data.SqlClient { [MonoTODO] public Type GetFieldType(int i) { - throw new NotImplementedException (); + + return table.Columns[i].DataType; } [MonoTODO] @@ -217,7 +246,27 @@ namespace System.Data.SqlClient { [MonoTODO] public int GetOrdinal(string name) { - throw new NotImplementedException (); + int i; + for(i = 0; i < cols; i ++) { + if(table.Columns[i].ColumnName.Equals(name)) { + return i; + } + + } + + for(i = 0; i < cols; i++) { + string ta; + string n; + + ta = table.Columns[i].ColumnName.ToUpper(); + n = name.ToUpper(); + + if(ta.Equals(n)) { + return i; + } + } + + throw new MissingFieldException("Missing field: " + name); } [MonoTODO] @@ -232,12 +281,14 @@ namespace System.Data.SqlClient { [MonoTODO] public int GetValues(object[] values) { - throw new NotImplementedException (); + + values = fields; + return fields.Length; } [MonoTODO] public bool IsDBNull(int i) { - throw new NotImplementedException (); + return isNull[i]; } [MonoTODO] diff --git a/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PostgresLibrary.cs b/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PostgresLibrary.cs index fb3284a9453..a45a3991ef7 100644 --- a/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PostgresLibrary.cs +++ b/mcs/class/Mono.Data.PostgreSqlClient/Mono.Data.PostgreSqlClient/PostgresLibrary.cs @@ -15,7 +15,7 @@ // // *** uncomment #define to get debug messages, comment for production *** -// #define DEBUG_PostgresLibrary +//#define DEBUG_PostgresLibrary using System; using System.Data; @@ -243,27 +243,40 @@ namespace System.Data.SqlClient { // from PostgreSQL oid type // to .NET System. - // TODO: need to handle a NULL for each type + // FIXME: need to handle a NULL for each type // maybe setting obj to System.DBNull.Value ? -#if DEBUG_PostgresLibrary - Console.WriteLine("ConvertDbTypeToSystem typ: " + - typ + " value: " + value); -#endif // DEBUG_PostgresLibrary +// +// if(value == null) { +//#if DEBUG_PostgresLibrary +// Console.WriteLine("column is NULL"); +//#endif // DEBUG_PostgresLibrary +// return null; +// } +// else if(value.Equals("")) { +//#if DEBUG_PostgresLibrary +// Console.WriteLine("column has a NULL"); +//#endif // DEBUG_PostgresLibrary +// +// return null; +// } +// +//#if DEBUG_PostgresLibrary +// Console.WriteLine("ConvertDbTypeToSystem typ: " + +// typ + " value: " + value); +//#endif // DEBUG_PostgresLibrary +// // Date, Time, and DateTime // are parsed based on ISO format - // "YYYY-MM-DD hh:mi:ss:ms" + // "YYYY-MM-DD hh:mi:ss.ms" switch(typ) { case DbType.String: obj = String.Copy(value); break; case DbType.Boolean: - if(value.Equals("t")) - obj = Boolean.Parse("true"); - else - obj = Boolean.Parse("false"); + obj = value.Equals("t"); break; case DbType.Int16: obj = Int16.Parse(value); diff --git a/mcs/class/Mono.Data.PostgreSqlClient/PgSqlCommand.cs b/mcs/class/Mono.Data.PostgreSqlClient/PgSqlCommand.cs index d287da27c5e..49183916f8d 100644 --- a/mcs/class/Mono.Data.PostgreSqlClient/PgSqlCommand.cs +++ b/mcs/class/Mono.Data.PostgreSqlClient/PgSqlCommand.cs @@ -240,7 +240,7 @@ namespace System.Data.SqlClient { int oid; types = new string[nFields]; - + for(nCol = 0; nCol < nFields; nCol++) { DbType dbType; diff --git a/mcs/class/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs b/mcs/class/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs index dc6ee48a677..ffaf076ca66 100644 --- a/mcs/class/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs +++ b/mcs/class/Mono.Data.PostgreSqlClient/PgSqlDataReader.cs @@ -19,6 +19,10 @@ // Gonzalo Paniagua Javier // +// *** uncomment #define to get debug messages, comment for production *** +//#define DEBUG_SqlDataReader + + using System; using System.Collections; using System.ComponentModel; @@ -39,9 +43,13 @@ namespace System.Data.SqlClient { private SqlCommand cmd; private DataTable table; - private object[] fields; + // columns in a row + private object[] fields; // data value in a .NET type private string[] types; // PostgreSQL Type - private bool[] isNull; + private bool[] isNull; // is NULL? + private int[] actualLength; // ActualLength of data + private DbType[] dbTypes; // DB data type + // actucalLength = -1 is variable-length private bool open = false; IntPtr pgResult; // PGresult @@ -91,40 +99,60 @@ namespace System.Data.SqlClient { [MonoTODO] public bool Read() { - string value; - fields = new object[cols]; // re-init row - DbType dbType; - + string dataValue; + int c = 0; + + //Console.WriteLine("if current row: " + currentRow + " rows: " + rows); if(currentRow < rows - 1) { + + //Console.WriteLine("currentRow++: "); currentRow++; - int c; + + //Console.WriteLine("re-init row --- cols: " + cols); + + // re-init row + fields = new object[cols]; + //dbTypes = new DbType[cols]; + actualLength = new int[cols]; + isNull = new bool[cols]; + for(c = 0; c < cols; c++) { // get data value - value = PostgresLibrary. + dataValue = PostgresLibrary. PQgetvalue( pgResult, currentRow, c); - int columnIsNull; // is column NULL? - columnIsNull = PostgresLibrary. - PQgetisnull(pgResult, - currentRow, c); + //isNull[c] = PostgresLibrary. + // PQgetisnull(pgResult, + // currentRow, c); - int actualLength; // get Actual Length - actualLength = PostgresLibrary. + actualLength[c] = PostgresLibrary. PQgetlength(pgResult, currentRow, c); - + + DbType dbType; dbType = PostgresHelper. TypnameToSqlDbType(types[c]); - fields[c] = PostgresHelper. - ConvertDbTypeToSystem ( + if(dataValue == null) { + fields[c] = null; + isNull[c] = true; + } + else if(dataValue.Equals("")) { + fields[c] = null; + isNull[c] = true; + } + else { + isNull[c] = false; + fields[c] = PostgresHelper. + ConvertDbTypeToSystem ( dbType, - value); + dataValue); + } } return true; } @@ -162,12 +190,12 @@ namespace System.Data.SqlClient { [MonoTODO] public string GetDataTypeName(int i) { - throw new NotImplementedException (); + return types[i]; } [MonoTODO] public DateTime GetDateTime(int i) { - throw new NotImplementedException (); + return (DateTime) fields[i]; } [MonoTODO] @@ -182,7 +210,8 @@ namespace System.Data.SqlClient { [MonoTODO] public Type GetFieldType(int i) { - throw new NotImplementedException (); + + return table.Columns[i].DataType; } [MonoTODO] @@ -217,7 +246,27 @@ namespace System.Data.SqlClient { [MonoTODO] public int GetOrdinal(string name) { - throw new NotImplementedException (); + int i; + for(i = 0; i < cols; i ++) { + if(table.Columns[i].ColumnName.Equals(name)) { + return i; + } + + } + + for(i = 0; i < cols; i++) { + string ta; + string n; + + ta = table.Columns[i].ColumnName.ToUpper(); + n = name.ToUpper(); + + if(ta.Equals(n)) { + return i; + } + } + + throw new MissingFieldException("Missing field: " + name); } [MonoTODO] @@ -232,12 +281,14 @@ namespace System.Data.SqlClient { [MonoTODO] public int GetValues(object[] values) { - throw new NotImplementedException (); + + values = fields; + return fields.Length; } [MonoTODO] public bool IsDBNull(int i) { - throw new NotImplementedException (); + return isNull[i]; } [MonoTODO] diff --git a/mcs/class/Mono.Data.PostgreSqlClient/PostgresLibrary.cs b/mcs/class/Mono.Data.PostgreSqlClient/PostgresLibrary.cs index fb3284a9453..a45a3991ef7 100644 --- a/mcs/class/Mono.Data.PostgreSqlClient/PostgresLibrary.cs +++ b/mcs/class/Mono.Data.PostgreSqlClient/PostgresLibrary.cs @@ -15,7 +15,7 @@ // // *** uncomment #define to get debug messages, comment for production *** -// #define DEBUG_PostgresLibrary +//#define DEBUG_PostgresLibrary using System; using System.Data; @@ -243,27 +243,40 @@ namespace System.Data.SqlClient { // from PostgreSQL oid type // to .NET System. - // TODO: need to handle a NULL for each type + // FIXME: need to handle a NULL for each type // maybe setting obj to System.DBNull.Value ? -#if DEBUG_PostgresLibrary - Console.WriteLine("ConvertDbTypeToSystem typ: " + - typ + " value: " + value); -#endif // DEBUG_PostgresLibrary +// +// if(value == null) { +//#if DEBUG_PostgresLibrary +// Console.WriteLine("column is NULL"); +//#endif // DEBUG_PostgresLibrary +// return null; +// } +// else if(value.Equals("")) { +//#if DEBUG_PostgresLibrary +// Console.WriteLine("column has a NULL"); +//#endif // DEBUG_PostgresLibrary +// +// return null; +// } +// +//#if DEBUG_PostgresLibrary +// Console.WriteLine("ConvertDbTypeToSystem typ: " + +// typ + " value: " + value); +//#endif // DEBUG_PostgresLibrary +// // Date, Time, and DateTime // are parsed based on ISO format - // "YYYY-MM-DD hh:mi:ss:ms" + // "YYYY-MM-DD hh:mi:ss.ms" switch(typ) { case DbType.String: obj = String.Copy(value); break; case DbType.Boolean: - if(value.Equals("t")) - obj = Boolean.Parse("true"); - else - obj = Boolean.Parse("false"); + obj = value.Equals("t"); break; case DbType.Int16: obj = Int16.Parse(value); -- cgit v1.2.3