00001 using System;
00002 using System.IO;
00003 using System.Data;
00004
00005 using SQLiteCSLib.Inner;
00006
00007 namespace SQLiteCSLib
00008 {
00012 public class SQLiteDataReader : IDataReader, IDisposable
00013 {
00017 protected OSQLiteStmtWrap m_stmt = null;
00018
00023 public SQLiteDataReader( OSQLiteStmtWrap stmt )
00024 {
00025 m_stmt = stmt;
00026 }
00027
00031 protected int m_affected = 0;
00032
00036 public int RecordsAffected
00037 {
00038 get
00039 {
00040 return m_stmt.DbWrap().getChanges();
00041 }
00042 }
00043
00047 public bool IsClosed
00048 {
00049 get
00050 {
00051 if( m_stmt == null )
00052 return true;
00053
00054 return false;
00055 }
00056 }
00057
00062 public bool NextResult()
00063 {
00064 if( m_stmt.Step() == ResultEnum.ROW )
00065 return true;
00066
00067 return false;
00068 }
00069
00073 public void Close()
00074 {
00075 if( m_stmt != null )
00076 {
00077 m_stmt.Dispose();
00078 m_stmt = null;
00079 }
00080 }
00081
00086 public bool Read()
00087 {
00088 ResultEnum iRes = m_stmt.Step();
00089 if( iRes == ResultEnum.ROW )
00090 return true;
00091
00092 if( iRes != ResultEnum.DONE &&
00093 iRes != ResultEnum.OK &&
00094 iRes != ResultEnum.NOTFOUND )
00095 throw new SQLiteException( m_stmt );
00096
00097 return false;
00098 }
00099
00103 public int Depth
00104 {
00105 get
00106 {
00107 return 0;
00108 }
00109 }
00110
00115 public DataTable GetSchemaTable()
00116 {
00117 DataTable dt = new DataTable();
00118
00119 int iColCnt = -1;
00120
00121 while( Read() == true )
00122 {
00123 if( iColCnt == -1 )
00124 {
00125 iColCnt = m_stmt.column_count();
00126
00127 for( int iCol=0; iCol<iColCnt; iCol++ )
00128 {
00129
00130 DataColumn dc = new DataColumn();
00131 Type fieldtype = GetFieldType( iCol );
00132 if( fieldtype != null )
00133 {
00134 dc.DataType = fieldtype;
00135 }
00136 dc.Caption = m_stmt.getColumnName( iCol );
00137 dc.ColumnName = dc.Caption;
00138 dt.Columns.Add( dc );
00139 }
00140 }
00141
00142
00143 object[] rowdata = new object[iColCnt];
00144 int iResCnt = GetValues(rowdata);
00145 DataRow row = dt.NewRow();
00146 for( int iIdx=0; iIdx<iResCnt; iIdx++ )
00147 {
00148 row[ dt.Columns[iIdx] ] = rowdata[ iIdx ] ;
00149 }
00150 dt.Rows.Add( row );
00151 }
00152
00153 return dt;
00154 }
00155
00159 public void Dispose()
00160 {
00161 Close();
00162 }
00163
00167 public int FieldCount
00168 {
00169 get
00170 {
00171 return m_stmt.column_count();
00172 }
00173 }
00174
00180 public string GetName(int i)
00181 {
00182 return m_stmt.getColumnName( i );
00183 }
00184
00191 public string GetColumnTableName( int i )
00192 {
00193 return m_stmt.getColumnTableName( i );
00194 }
00195
00202 public string GetColumnOriginalName( int i )
00203 {
00204 return m_stmt.getColumnOriginalName( i );
00205 }
00206
00213 public string GetColumnDecltype( int i )
00214 {
00215 return m_stmt.getColumnDecltype( i );
00216 }
00217
00223 public int GetInt32(int i)
00224 {
00225 return m_stmt.getInt( i );
00226 }
00227
00233 public short GetInt16(int i)
00234 {
00235 return (short)m_stmt.getInt( i );
00236 }
00237
00244 public bool GetBoolean(int i)
00245 {
00246 short sVal = (short)m_stmt.getInt( i );
00247
00248 return ( sVal == 0 ) ? false:true;
00249 }
00250
00256 public float GetFloat(int i)
00257 {
00258 return (float)m_stmt.getDouble( i );
00259 }
00260
00266 public string GetString(int i)
00267 {
00268 return m_stmt.getText( i );
00269 }
00270
00276 public double GetDouble(int i)
00277 {
00278 return m_stmt.getDouble( i );
00279 }
00280
00286 public decimal GetDecimal(int i)
00287 {
00288 return (decimal)m_stmt.getDouble( i );
00289 }
00290
00296 public long GetInt64(int i)
00297 {
00298 return m_stmt.getInt64( i );
00299 }
00300
00310 public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
00311 {
00312 if( buffer == null )
00313 {
00314 return m_stmt.getSize( i );
00315 }
00316
00317 MemoryStream blobstream = m_stmt.getBlob( i );
00318 blobstream.Seek( fieldOffset, SeekOrigin.Begin );
00319 if( blobstream.Length <= fieldOffset )
00320 return 0;
00321
00322 long lLen = blobstream.Length - fieldOffset;
00323 lLen = ( lLen < (bufferoffset+length) ) ? lLen:(bufferoffset+length);
00324
00325 return (long)blobstream.Read( buffer, bufferoffset, (int)lLen );
00326 }
00327
00333 public object GetValue(int i)
00334 {
00335 switch( m_stmt.getType( i ) )
00336 {
00337 case DATATYPE.INTEGER:
00338 return GetInt64( i );
00339
00340 case DATATYPE.FLOAT:
00341 return GetDouble( i );
00342
00343 case DATATYPE.TEXT:
00344 return GetString( i );
00345
00346 case DATATYPE.BLOB:
00347 {
00348 long binsize = GetBytes( i, 0, null, 0, 0 );
00349 byte[] val = new byte[binsize];
00350 GetBytes( i, 0, val, 0, (int)binsize );
00351 return val;
00352 }
00353
00354 case DATATYPE.DBNULL:
00355 return null;
00356 }
00357
00358 return null;
00359 }
00360
00366 public int GetValues(object[] values)
00367 {
00368 int iResCount = 0;
00369
00370 int iResultCol = m_stmt.column_count();
00371 for( int iValIdx=0; iValIdx<values.Length; iValIdx++ )
00372 {
00373 if( iValIdx >= iResultCol )
00374 break;
00375
00376 values[ iValIdx ] = GetValue( iValIdx );
00377 iResCount++;
00378 }
00379
00380 return iResCount;
00381 }
00382
00388 public string GetDataTypeName(int i)
00389 {
00390 return GetValue( i ).GetType().Name;
00391 }
00392
00398 public Type GetFieldType(int i)
00399 {
00400 object fieldval = GetValue( i );
00401 if( fieldval == null )
00402 return null;
00403 return fieldval.GetType();
00404 }
00405
00409 public object this[string name]
00410 {
00411 get
00412 {
00413 for( int iCol=0; iCol<m_stmt.column_count(); iCol++ )
00414 {
00415 if( m_stmt.getColumnName( iCol ) == name )
00416 return this.GetValue( iCol );
00417 }
00418
00419 return null;
00420 }
00421 }
00422
00426 object System.Data.IDataRecord.this[int i]
00427 {
00428 get
00429 {
00430 return GetValue( i );
00431 }
00432 }
00433
00439 public bool IsDBNull(int i)
00440 {
00441 if( m_stmt.getType( i ) == DATATYPE.DBNULL )
00442 return true;
00443 return false;
00444 }
00445
00452 public byte GetByte(int i)
00453 {
00454 return 0;
00455 }
00456
00463 public Guid GetGuid(int i)
00464 {
00465 return new Guid ();
00466 }
00467
00474 public DateTime GetDateTime(int i)
00475 {
00476 return new DateTime ();
00477 }
00478
00485 public int GetOrdinal(string name)
00486 {
00487 for( int iIdx=0; iIdx<m_stmt.column_count(); iIdx++ )
00488 {
00489 if( name.ToUpper() == m_stmt.getColumnName( iIdx ).ToUpper() )
00490 return iIdx;
00491 }
00492
00493 return -1;
00494 }
00495
00502 public IDataReader GetData(int i)
00503 {
00504 return null;
00505 }
00506
00517 public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
00518 {
00519 return 0;
00520 }
00521
00528 public char GetChar(int i)
00529 {
00530 return '¥0';
00531 }
00532
00533 }
00534 }