SqlDataReader.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // System.Data.SqlClient.SqlDataReader.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. //
  8. // (C) Ximian, Inc 2002
  9. // (C) Daniel Morgan 2002
  10. //
  11. // Credits:
  12. // SQL and concepts were used from libgda 0.8.190 (GNOME Data Access)
  13. // http://www.gnome-db.org/
  14. // with permission from the authors of the
  15. // PostgreSQL provider in libgda:
  16. // Michael Lausch <[email protected]>
  17. // Rodrigo Moya <[email protected]>
  18. // Vivien Malerba <[email protected]>
  19. // Gonzalo Paniagua Javier <[email protected]>
  20. //
  21. // *** uncomment #define to get debug messages, comment for production ***
  22. //#define DEBUG_SqlDataReader
  23. using System;
  24. using System.Collections;
  25. using System.ComponentModel;
  26. using System.Data;
  27. namespace System.Data.SqlClient {
  28. /// <summary>
  29. /// Provides a means of reading one or more forward-only streams
  30. /// of result sets obtained by executing a command
  31. /// at a SQL database.
  32. /// </summary>
  33. //public sealed class SqlDataReader : MarshalByRefObject,
  34. // IEnumerable, IDataReader, IDisposable, IDataRecord
  35. public sealed class SqlDataReader : IEnumerable,
  36. IDataReader, IDataRecord {
  37. #region Fields
  38. private SqlCommand cmd;
  39. private DataTable table = null;
  40. // columns in a row
  41. private object[] fields; // data value in a .NET type
  42. private string[] types; // PostgreSQL Type
  43. private bool[] isNull; // is NULL?
  44. private int[] actualLength; // ActualLength of data
  45. private DbType[] dbTypes; // DB data type
  46. // actucalLength = -1 is variable-length
  47. private bool open = false;
  48. IntPtr pgResult; // PGresult
  49. private int rows;
  50. private int cols;
  51. private int currentRow = -1; // no Read() has been done yet
  52. #endregion // Fields
  53. #region Constructors
  54. internal SqlDataReader (SqlCommand sqlCmd) {
  55. cmd = sqlCmd;
  56. open = true;
  57. }
  58. #endregion
  59. #region Public Methods
  60. [MonoTODO]
  61. public void Close() {
  62. open = false;
  63. // free SqlDataReader resources in SqlCommand
  64. // and allow SqlConnection to be used again
  65. cmd.CloseReader();
  66. // TODO: get parameters from result
  67. // clear unmanaged PostgreSQL result set
  68. PostgresLibrary.PQclear (pgResult);
  69. pgResult = IntPtr.Zero;
  70. }
  71. [MonoTODO]
  72. public DataTable GetSchemaTable() {
  73. return table;
  74. }
  75. [MonoTODO]
  76. public bool NextResult() {
  77. SqlResult res;
  78. currentRow = -1;
  79. res = cmd.NextResult();
  80. if(res.ResultReturned == true) {
  81. table = res.Table;
  82. pgResult = res.PgResult;
  83. rows = res.RowCount;
  84. cols = res.FieldCount;
  85. types = res.PgTypes;
  86. }
  87. return res.ResultReturned;
  88. }
  89. [MonoTODO]
  90. public bool Read() {
  91. string dataValue;
  92. int c = 0;
  93. if(currentRow < rows - 1) {
  94. currentRow++;
  95. // re-init row
  96. fields = new object[cols];
  97. //dbTypes = new DbType[cols];
  98. actualLength = new int[cols];
  99. isNull = new bool[cols];
  100. for(c = 0; c < cols; c++) {
  101. // get data value
  102. dataValue = PostgresLibrary.
  103. PQgetvalue(
  104. pgResult,
  105. currentRow, c);
  106. // is column NULL?
  107. //isNull[c] = PostgresLibrary.
  108. // PQgetisnull(pgResult,
  109. // currentRow, c);
  110. // get Actual Length
  111. actualLength[c] = PostgresLibrary.
  112. PQgetlength(pgResult,
  113. currentRow, c);
  114. DbType dbType;
  115. dbType = PostgresHelper.
  116. TypnameToSqlDbType(types[c]);
  117. if(dataValue == null) {
  118. fields[c] = null;
  119. isNull[c] = true;
  120. }
  121. else if(dataValue.Equals("")) {
  122. fields[c] = null;
  123. isNull[c] = true;
  124. }
  125. else {
  126. isNull[c] = false;
  127. fields[c] = PostgresHelper.
  128. ConvertDbTypeToSystem (
  129. dbType,
  130. dataValue);
  131. }
  132. }
  133. return true;
  134. }
  135. return false; // EOF
  136. }
  137. [MonoTODO]
  138. public byte GetByte(int i) {
  139. throw new NotImplementedException ();
  140. }
  141. [MonoTODO]
  142. public long GetBytes(int i, long fieldOffset,
  143. byte[] buffer, int bufferOffset,
  144. int length) {
  145. throw new NotImplementedException ();
  146. }
  147. [MonoTODO]
  148. public char GetChar(int i) {
  149. throw new NotImplementedException ();
  150. }
  151. [MonoTODO]
  152. public long GetChars(int i, long fieldOffset,
  153. char[] buffer, int bufferOffset,
  154. int length) {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. public IDataReader GetData(int i) {
  159. throw new NotImplementedException ();
  160. }
  161. [MonoTODO]
  162. public string GetDataTypeName(int i) {
  163. return types[i];
  164. }
  165. [MonoTODO]
  166. public DateTime GetDateTime(int i) {
  167. return (DateTime) fields[i];
  168. }
  169. [MonoTODO]
  170. public decimal GetDecimal(int i) {
  171. return (decimal) fields[i];
  172. }
  173. [MonoTODO]
  174. public double GetDouble(int i) {
  175. return (double) fields[i];
  176. }
  177. [MonoTODO]
  178. public Type GetFieldType(int i) {
  179. return table.Columns[i].DataType;
  180. }
  181. [MonoTODO]
  182. public float GetFloat(int i) {
  183. return (float) fields[i];
  184. }
  185. [MonoTODO]
  186. public Guid GetGuid(int i) {
  187. throw new NotImplementedException ();
  188. }
  189. [MonoTODO]
  190. public short GetInt16(int i) {
  191. return (short) fields[i];
  192. }
  193. [MonoTODO]
  194. public int GetInt32(int i) {
  195. return (int) fields[i];
  196. }
  197. [MonoTODO]
  198. public long GetInt64(int i) {
  199. return (long) fields[i];
  200. }
  201. [MonoTODO]
  202. public string GetName(int i) {
  203. return table.Columns[i].ColumnName;
  204. }
  205. [MonoTODO]
  206. public int GetOrdinal(string name) {
  207. int i;
  208. for(i = 0; i < cols; i ++) {
  209. if(table.Columns[i].ColumnName.Equals(name)) {
  210. return i;
  211. }
  212. }
  213. for(i = 0; i < cols; i++) {
  214. string ta;
  215. string n;
  216. ta = table.Columns[i].ColumnName.ToUpper();
  217. n = name.ToUpper();
  218. if(ta.Equals(n)) {
  219. return i;
  220. }
  221. }
  222. throw new MissingFieldException("Missing field: " + name);
  223. }
  224. [MonoTODO]
  225. public string GetString(int i) {
  226. return (string) fields[i];
  227. }
  228. [MonoTODO]
  229. public object GetValue(int i) {
  230. return fields[i];
  231. }
  232. [MonoTODO]
  233. public int GetValues(object[] values)
  234. {
  235. Array.Copy (fields, values, fields.Length);
  236. return fields.Length;
  237. }
  238. [MonoTODO]
  239. public bool IsDBNull(int i) {
  240. return isNull[i];
  241. }
  242. [MonoTODO]
  243. public bool GetBoolean(int i) {
  244. return (bool) fields[i];
  245. }
  246. [MonoTODO]
  247. public IEnumerator GetEnumerator() {
  248. throw new NotImplementedException ();
  249. }
  250. #endregion // Public Methods
  251. #region Destructors
  252. [MonoTODO]
  253. public void Dispose () {
  254. }
  255. [MonoTODO]
  256. ~SqlDataReader() {
  257. }
  258. #endregion // Destructors
  259. #region Properties
  260. public int Depth {
  261. [MonoTODO]
  262. get {
  263. throw new NotImplementedException ();
  264. }
  265. }
  266. public bool IsClosed {
  267. [MonoTODO]
  268. get {
  269. if(open == false)
  270. return true;
  271. else
  272. return false;
  273. }
  274. }
  275. public int RecordsAffected {
  276. [MonoTODO]
  277. get {
  278. throw new NotImplementedException ();
  279. }
  280. }
  281. public int FieldCount {
  282. [MonoTODO]
  283. get {
  284. return cols;
  285. }
  286. }
  287. public object this[string name] {
  288. [MonoTODO]
  289. get {
  290. int i;
  291. for(i = 0; i < cols; i ++) {
  292. if(table.Columns[i].ColumnName.Equals(name)) {
  293. return fields[i];
  294. }
  295. }
  296. for(i = 0; i < cols; i++) {
  297. string ta;
  298. string n;
  299. ta = table.Columns[i].ColumnName.ToUpper();
  300. n = name.ToUpper();
  301. if(ta.Equals(n)) {
  302. return fields[i];
  303. }
  304. }
  305. throw new MissingFieldException("Missing field: " + name);
  306. }
  307. }
  308. public object this[int i] {
  309. [MonoTODO]
  310. get {
  311. return fields[i];
  312. }
  313. }
  314. #endregion // Properties
  315. }
  316. }