SqlDataReader.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. using System;
  22. using System.Collections;
  23. using System.ComponentModel;
  24. using System.Data;
  25. namespace System.Data.SqlClient {
  26. /// <summary>
  27. /// Provides a means of reading one or more forward-only streams
  28. /// of result sets obtained by executing a command
  29. /// at a SQL database.
  30. /// </summary>
  31. //public sealed class SqlDataReader : MarshalByRefObject,
  32. // IEnumerable, IDataReader, IDisposable, IDataRecord
  33. public sealed class SqlDataReader : IEnumerable,
  34. IDataReader, IDataRecord {
  35. #region Fields
  36. private SqlCommand cmd;
  37. private DataTable table;
  38. private object[] fields;
  39. private string[] types; // PostgreSQL Type
  40. private bool[] isNull;
  41. private bool open = false;
  42. IntPtr pgResult; // PGresult
  43. private int rows;
  44. private int cols;
  45. private int currentRow = -1; // no Read() has been done yet
  46. #endregion // Fields
  47. #region Constructors
  48. internal SqlDataReader (SqlCommand sqlCmd,
  49. DataTable dataTableSchema, IntPtr pg_result,
  50. int rowCount, int fieldCount, string[] pgtypes) {
  51. cmd = sqlCmd;
  52. table = dataTableSchema;
  53. pgResult = pg_result;
  54. rows = rowCount;
  55. cols = fieldCount;
  56. types = pgtypes;
  57. open = true;
  58. }
  59. #endregion
  60. #region Public Methods
  61. [MonoTODO]
  62. public void Close() {
  63. // close result set
  64. PostgresLibrary.PQclear (pgResult);
  65. open = false;
  66. // TODO: change busy state on SqlConnection to not busy
  67. }
  68. [MonoTODO]
  69. public DataTable GetSchemaTable() {
  70. return table;
  71. }
  72. [MonoTODO]
  73. public bool NextResult() {
  74. throw new NotImplementedException ();
  75. }
  76. [MonoTODO]
  77. public bool Read() {
  78. string value;
  79. fields = new object[cols]; // re-init row
  80. DbType dbType;
  81. if(currentRow < rows - 1) {
  82. currentRow++;
  83. int c;
  84. for(c = 0; c < cols; c++) {
  85. // get data value
  86. value = PostgresLibrary.
  87. PQgetvalue(
  88. pgResult,
  89. currentRow, c);
  90. int columnIsNull;
  91. // is column NULL?
  92. columnIsNull = PostgresLibrary.
  93. PQgetisnull(pgResult,
  94. currentRow, c);
  95. int actualLength;
  96. // get Actual Length
  97. actualLength = PostgresLibrary.
  98. PQgetlength(pgResult,
  99. currentRow, c);
  100. dbType = PostgresHelper.
  101. TypnameToSqlDbType(types[c]);
  102. fields[c] = PostgresHelper.
  103. ConvertDbTypeToSystem (
  104. dbType,
  105. value);
  106. }
  107. return true;
  108. }
  109. return false; // EOF
  110. }
  111. [MonoTODO]
  112. public byte GetByte(int i) {
  113. throw new NotImplementedException ();
  114. }
  115. [MonoTODO]
  116. public long GetBytes(int i, long fieldOffset,
  117. byte[] buffer, int bufferOffset,
  118. int length) {
  119. throw new NotImplementedException ();
  120. }
  121. [MonoTODO]
  122. public char GetChar(int i) {
  123. throw new NotImplementedException ();
  124. }
  125. [MonoTODO]
  126. public long GetChars(int i, long fieldOffset,
  127. char[] buffer, int bufferOffset,
  128. int length) {
  129. throw new NotImplementedException ();
  130. }
  131. [MonoTODO]
  132. public IDataReader GetData(int i) {
  133. throw new NotImplementedException ();
  134. }
  135. [MonoTODO]
  136. public string GetDataTypeName(int i) {
  137. throw new NotImplementedException ();
  138. }
  139. [MonoTODO]
  140. public DateTime GetDateTime(int i) {
  141. throw new NotImplementedException ();
  142. }
  143. [MonoTODO]
  144. public decimal GetDecimal(int i) {
  145. return (decimal) fields[i];
  146. }
  147. [MonoTODO]
  148. public double GetDouble(int i) {
  149. return (double) fields[i];
  150. }
  151. [MonoTODO]
  152. public Type GetFieldType(int i) {
  153. throw new NotImplementedException ();
  154. }
  155. [MonoTODO]
  156. public float GetFloat(int i) {
  157. return (float) fields[i];
  158. }
  159. [MonoTODO]
  160. public Guid GetGuid(int i) {
  161. throw new NotImplementedException ();
  162. }
  163. [MonoTODO]
  164. public short GetInt16(int i) {
  165. return (short) fields[i];
  166. }
  167. [MonoTODO]
  168. public int GetInt32(int i) {
  169. return (int) fields[i];
  170. }
  171. [MonoTODO]
  172. public long GetInt64(int i) {
  173. return (long) fields[i];
  174. }
  175. [MonoTODO]
  176. public string GetName(int i) {
  177. return table.Columns[i].ColumnName;
  178. }
  179. [MonoTODO]
  180. public int GetOrdinal(string name) {
  181. throw new NotImplementedException ();
  182. }
  183. [MonoTODO]
  184. public string GetString(int i) {
  185. return (string) fields[i];
  186. }
  187. [MonoTODO]
  188. public object GetValue(int i) {
  189. return fields[i];
  190. }
  191. [MonoTODO]
  192. public int GetValues(object[] values) {
  193. throw new NotImplementedException ();
  194. }
  195. [MonoTODO]
  196. public bool IsDBNull(int i) {
  197. throw new NotImplementedException ();
  198. }
  199. [MonoTODO]
  200. public bool GetBoolean(int i) {
  201. return (bool) fields[i];
  202. }
  203. [MonoTODO]
  204. public IEnumerator GetEnumerator() {
  205. throw new NotImplementedException ();
  206. }
  207. #endregion // Public Methods
  208. #region Destructors
  209. [MonoTODO]
  210. public void Dispose () {
  211. }
  212. [MonoTODO]
  213. ~SqlDataReader() {
  214. }
  215. #endregion // Destructors
  216. #region Properties
  217. public int Depth {
  218. [MonoTODO]
  219. get {
  220. throw new NotImplementedException ();
  221. }
  222. }
  223. public bool IsClosed {
  224. [MonoTODO]
  225. get {
  226. if(open == false)
  227. return true;
  228. else
  229. return false;
  230. }
  231. }
  232. public int RecordsAffected {
  233. [MonoTODO]
  234. get {
  235. throw new NotImplementedException ();
  236. }
  237. }
  238. public int FieldCount {
  239. [MonoTODO]
  240. get {
  241. return cols;
  242. }
  243. }
  244. public object this[string name] {
  245. [MonoTODO]
  246. get {
  247. int i;
  248. for(i = 0; i < cols; i ++) {
  249. if(table.Columns[i].ColumnName.Equals(name)) {
  250. return fields[i];
  251. }
  252. }
  253. for(i = 0; i < cols; i++) {
  254. string ta;
  255. string n;
  256. ta = table.Columns[i].ColumnName.ToUpper();
  257. n = name.ToUpper();
  258. if(ta.Equals(n)) {
  259. return fields[i];
  260. }
  261. }
  262. throw new MissingFieldException("Missing field: " + name);
  263. }
  264. }
  265. public object this[int i] {
  266. [MonoTODO]
  267. get {
  268. return fields[i];
  269. }
  270. }
  271. #endregion // Properties
  272. }
  273. }