1 // -*- c-basic-offset: 8; inent-tabs-mode: nil -*-
5 // Author(s): Vladimir Vukicevic <vladimir@pobox.com>
7 // Copyright (C) 2002 Vladimir Vukicevic
11 using System.Runtime.InteropServices;
12 using System.Collections;
15 namespace Mono.Data.SqliteClient
17 public class SqliteDataReader : IDataReader
19 SqliteCommand command;
22 Hashtable column_names;
28 internal SqliteDataReader (SqliteCommand cmd)
31 rows = new ArrayList ();
32 columns = new ArrayList ();
33 column_names = new Hashtable ();
39 internal void ReadingDone ()
41 records_affected = command.NumChanges ();
50 public void Dispose ()
55 public DataTable GetSchemaTable ()
59 // We sort of cheat here since sqlite treats all types as strings
60 // we -could- parse the table definition (since that's the only info
61 // that we can get out of sqlite about the table), but it's probably
63 DataTable data_table = new DataTable ();
68 // first create the columns
69 for (int i = 0; i < column_names.Count; i++) {
70 col = new DataColumn ();
71 col.DataType = System.Type.GetType("System.String");
72 col.ColumnName = columns[i];
73 data_table.Columns.Add (col);
76 // then loop through the rows
77 for (int i = 0; i < rows.Length; i++) {
78 row = data_table.NewRow ();
79 for (int j = 0; j < column_names.Count; j++) {
80 row[columns[j]] = rows[i][j];
82 data_table.Rows.Add (row);
91 public bool NextResult ()
94 if (current_row < rows.Count)
101 return NextResult ();
110 public bool IsClosed {
116 public int RecordsAffected {
118 return records_affected;
123 internal unsafe int SqliteCallback (ref object o, int argc, sbyte **argv, sbyte **colnames)
125 // cache names of columns if we need to
126 if (column_names.Count == 0) {
127 for (int i = 0; i < argc; i++) {
128 string col = new String (colnames[i]);
130 column_names[col.ToLower ()] = i++;
134 ArrayList data_row = new ArrayList (argc);
135 for (int i = 0; i < argc; i++) {
136 if (argv[i] != ((sbyte *)0)) {
137 data_row.Add(new String (argv[i]));
147 // IDataRecord getters
150 public bool GetBoolean (int i)
152 return Convert.ToBoolean ((string) ((ArrayList) rows[current_row])[i]);
155 public byte GetByte (int i)
157 return Convert.ToByte ((string) ((ArrayList) rows[current_row])[i]);
160 public long GetBytes (int i, long fieldOffset, byte[] buffer,
161 int bufferOffset, int length)
163 throw new NotImplementedException ();
166 public char GetChar (int i)
168 return Convert.ToChar ((string) ((ArrayList) rows[current_row])[i]);
171 public long GetChars (int i, long fieldOffset, char[] buffer,
172 int bufferOffset, int length)
174 throw new NotImplementedException ();
177 public IDataReader GetData (int i)
179 // sigh.. in the MSDN docs, it says that "This member supports the
180 // .NET Framework infrastructure and is not nitended to be used
181 // directly from your code." -- so why the hell is it in the public
183 throw new NotImplementedException ();
186 public string GetDataTypeName (int i)
188 return "System.String";
191 public DateTime GetDateTime (int i)
193 return Convert.ToDateTime ((string) ((ArrayList) rows[current_row])[i]);
196 public decimal GetDecimal (int i)
198 return Convert.ToDecimal ((string) ((ArrayList) rows[current_row])[i]);
201 public double GetDouble (int i)
203 return Convert.ToDouble ((string) ((ArrayList) rows[current_row])[i]);
206 public Type GetFieldType (int i)
208 return System.Type.GetType ("System.String");
211 public float GetFloat (int i)
213 return Convert.ToSingle ((string) ((ArrayList) rows[current_row])[i]);
216 public Guid GetGuid (int i)
218 throw new NotImplementedException ();
221 public short GetInt16 (int i)
223 return Convert.ToInt16 ((string) ((ArrayList) rows[current_row])[i]);
226 public int GetInt32 (int i)
228 return Convert.ToInt32 ((string) ((ArrayList) rows[current_row])[i]);
231 public long GetInt64 (int i)
233 return Convert.ToInt64 ((string) ((ArrayList) rows[current_row])[i]);
236 public string GetName (int i)
238 return (string) columns[i];
241 public int GetOrdinal (string name)
243 return (int) column_names[name];
246 public string GetString (int i)
248 return ((string) ((ArrayList) rows[current_row])[i]);
251 public object GetValue (int i)
253 return ((ArrayList) rows[current_row])[i];
256 public int GetValues (object[] values)
258 int num_to_fill = Math.Min (values.Length, columns.Count);
259 for (int i = 0; i < num_to_fill; i++) {
260 if (((ArrayList) rows[current_row])[i] != null) {
261 values[i] = ((ArrayList) rows[current_row])[i];
263 values[i] = DBNull.Value;
269 public bool IsDBNull (int i)
271 if (((ArrayList) rows[current_row])[i] == null)
276 public int FieldCount {
278 if (current_row == -1 || current_row == rows.Count)
280 return columns.Count;
284 public object this[string name] {
286 return ((ArrayList) rows[current_row])[(int) column_names[name]];
290 public object this[int i] {
292 return ((ArrayList) rows[current_row])[i];