ReaderCache.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. //
  2. // System.Data.ProviderBase.ReaderCache.cs
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using java.sql;
  32. namespace System.Data.ProviderBase
  33. {
  34. public interface IReaderCacheContainer
  35. {
  36. void Fetch(ResultSet rs, int columnIndex, bool isSequential);
  37. bool IsNull();
  38. object GetValue();
  39. }
  40. internal abstract class ReaderCacheContainerBase : IReaderCacheContainer
  41. {
  42. #region Fields
  43. bool _isNull;
  44. #endregion // Fields
  45. #region Methods
  46. protected abstract void FetchInternal(ResultSet rs, int columnIndex);
  47. protected virtual void FetchInternal(ResultSet rs, int columnIndex, bool isSequential) {
  48. FetchInternal(rs, columnIndex);
  49. }
  50. public abstract object GetValue();
  51. public void Fetch(ResultSet rs, int columnIndex, bool isSequential)
  52. {
  53. FetchInternal(rs, columnIndex + 1, isSequential);
  54. _isNull = rs.wasNull();
  55. }
  56. public bool IsNull()
  57. {
  58. return _isNull;
  59. }
  60. #endregion // Methods
  61. }
  62. internal sealed class ArrayReaderCacheContainer : ReaderCacheContainerBase // Types.ARRAY
  63. {
  64. #region Fields
  65. object _a;
  66. #endregion // Fields
  67. #region Methods
  68. protected override void FetchInternal(ResultSet rs, int columnIndex)
  69. {
  70. _a = rs.getArray(columnIndex).getArray();
  71. }
  72. public override object GetValue()
  73. {
  74. return _a;
  75. }
  76. #endregion // Methods
  77. }
  78. internal sealed class Int64ReaderCacheContainer : ReaderCacheContainerBase // Types.BIGINT
  79. {
  80. #region Fields
  81. long _l;
  82. #endregion // Fields
  83. #region Methods
  84. protected override void FetchInternal(ResultSet rs, int columnIndex)
  85. {
  86. _l = rs.getLong(columnIndex);
  87. }
  88. public override object GetValue()
  89. {
  90. return _l;
  91. }
  92. internal long GetInt64()
  93. {
  94. return _l;
  95. }
  96. #endregion // Methods
  97. }
  98. internal class BytesReaderCacheContainer : ReaderCacheContainerBase // Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY
  99. {
  100. #region Fields
  101. protected byte[] _b;
  102. #endregion // Fields
  103. #region Methods
  104. protected override void FetchInternal(ResultSet rs, int columnIndex)
  105. {
  106. sbyte[] sbyteArray = rs.getBytes(columnIndex);
  107. if (sbyteArray != null) {
  108. _b = (byte[])vmw.common.TypeUtils.ToByteArray(sbyteArray);
  109. }
  110. }
  111. public override object GetValue()
  112. {
  113. return _b;
  114. }
  115. internal byte[] GetBytes()
  116. {
  117. return (byte[])GetValue();
  118. }
  119. internal virtual long GetBytes(
  120. long dataIndex,
  121. byte[] buffer,
  122. int bufferIndex,
  123. int length) {
  124. if (_b == null)
  125. return 0;
  126. if (buffer == null)
  127. return _b.LongLength;
  128. long actualLength = ((dataIndex + length) >= _b.LongLength) ? (_b.LongLength - dataIndex) : length;
  129. Array.Copy(_b,dataIndex,buffer,bufferIndex,actualLength);
  130. return actualLength;
  131. }
  132. #endregion // Methods
  133. }
  134. internal sealed class BooleanReaderCacheContainer : ReaderCacheContainerBase // Types.BIT
  135. {
  136. #region Fields
  137. bool _b;
  138. #endregion // Fields
  139. #region Methods
  140. protected override void FetchInternal(ResultSet rs, int columnIndex)
  141. {
  142. _b = rs.getBoolean(columnIndex);
  143. }
  144. public override object GetValue()
  145. {
  146. return _b;
  147. }
  148. internal bool GetBoolean()
  149. {
  150. return _b;
  151. }
  152. #endregion // Methods
  153. }
  154. internal sealed class BlobReaderCacheContainer : BytesReaderCacheContainer // Types.BLOB
  155. {
  156. #region Fields
  157. static readonly byte[] _emptyByteArr = new byte[0];
  158. java.sql.Blob _blob;
  159. #endregion // Fields
  160. #region Methods
  161. protected override void FetchInternal(ResultSet rs, int columnIndex) {
  162. throw new NotImplementedException("Should not be called");
  163. }
  164. protected override void FetchInternal(ResultSet rs, int columnIndex, bool isSequential)
  165. {
  166. _blob = rs.getBlob(columnIndex);
  167. if (!isSequential)
  168. ReadAll();
  169. }
  170. void ReadAll() {
  171. if (_blob != null) {
  172. long length = _blob.length();
  173. if (length == 0) {
  174. _b = _emptyByteArr;
  175. }
  176. else {
  177. java.io.InputStream input = _blob.getBinaryStream();
  178. byte[] byteValue = new byte[length];
  179. sbyte[] sbyteValue = vmw.common.TypeUtils.ToSByteArray(byteValue);
  180. input.read(sbyteValue);
  181. _b = byteValue;
  182. }
  183. }
  184. }
  185. public override object GetValue()
  186. {
  187. if (_b == null)
  188. ReadAll();
  189. return base.GetValue();
  190. }
  191. internal override long GetBytes(long dataIndex, byte[] buffer, int bufferIndex, int length) {
  192. if (_b != null)
  193. return base.GetBytes (dataIndex, buffer, bufferIndex, length);
  194. if (_blob == null)
  195. return 0;
  196. if (buffer == null)
  197. return _blob.length();
  198. java.io.InputStream input = _blob.getBinaryStream();
  199. input.skip(dataIndex);
  200. return input.read(vmw.common.TypeUtils.ToSByteArray(buffer), bufferIndex, length);
  201. }
  202. #endregion // Methods
  203. }
  204. internal abstract class CharsReaderCacheContainer : ReaderCacheContainerBase //
  205. {
  206. #region Fields
  207. #endregion // Fields
  208. #region Methods
  209. internal abstract long GetChars(
  210. long dataIndex,
  211. char[] buffer,
  212. int bufferIndex,
  213. int length);
  214. #endregion // Methods
  215. }
  216. internal sealed class GuidReaderCacheContainer : ReaderCacheContainerBase // Types.CHAR
  217. {
  218. #region Fields
  219. Guid _g;
  220. #endregion // Fields
  221. #region Methods
  222. protected override void FetchInternal(ResultSet rs, int columnIndex)
  223. {
  224. string s = rs.getString(columnIndex);
  225. if (s != null)
  226. _g = new Guid(s);
  227. }
  228. public override object GetValue()
  229. {
  230. return _g;
  231. }
  232. internal Guid GetGuid()
  233. {
  234. return _g;
  235. }
  236. #endregion // Methods
  237. }
  238. internal sealed class ClobReaderCacheContainer : StringReaderCacheContainer // Types.CLOB
  239. {
  240. #region Fields
  241. java.sql.Clob _clob;
  242. #endregion // Fields
  243. #region Methods
  244. protected override void FetchInternal(ResultSet rs, int columnIndex, bool isSequential)
  245. {
  246. _clob = rs.getClob(columnIndex);
  247. if (!isSequential)
  248. ReadAll();
  249. }
  250. void ReadAll() {
  251. if (_clob != null) {
  252. long length = _clob.length();
  253. if (length == 0) {
  254. _s = String.Empty;
  255. }
  256. else {
  257. java.io.Reader reader = _clob.getCharacterStream();
  258. char[] charValue = new char[length];
  259. reader.read(charValue);
  260. if (charValue != null)
  261. _s = new String(charValue);
  262. }
  263. }
  264. }
  265. public override object GetValue()
  266. {
  267. if (_s == null)
  268. ReadAll();
  269. return base.GetValue();
  270. }
  271. internal override long GetChars(long dataIndex, char[] buffer, int bufferIndex, int length) {
  272. if (_s != null)
  273. return base.GetChars (dataIndex, buffer, bufferIndex, length);
  274. if (_clob == null)
  275. return 0;
  276. if (buffer == null)
  277. return _clob.length();
  278. java.io.Reader reader = _clob.getCharacterStream();
  279. reader.skip(dataIndex);
  280. return reader.read(buffer, bufferIndex, length);
  281. }
  282. #endregion // Methods
  283. }
  284. internal sealed class TimeSpanReaderCacheContainer : ReaderCacheContainerBase // Types.TIME
  285. {
  286. #region Fields
  287. TimeSpan _t;
  288. #endregion // Fields
  289. #region Methods
  290. protected override void FetchInternal(ResultSet rs, int columnIndex)
  291. {
  292. Time t = rs.getTime(columnIndex);
  293. if (t != null) {
  294. _t = new TimeSpan(DbConvert.JavaTimeToClrTicks(t));
  295. }
  296. }
  297. public override object GetValue()
  298. {
  299. return _t;
  300. }
  301. internal TimeSpan GetTimeSpan()
  302. {
  303. return _t;
  304. }
  305. #endregion // Methods
  306. }
  307. internal class DateTimeReaderCacheContainer : ReaderCacheContainerBase // Types.TIMESTAMP
  308. {
  309. #region Fields
  310. protected DateTime _d;
  311. #endregion // Fields
  312. #region Methods
  313. protected override void FetchInternal(ResultSet rs, int columnIndex)
  314. {
  315. Date d = rs.getDate(columnIndex);
  316. if (d != null) {
  317. _d = new DateTime(DbConvert.JavaDateToClrTicks(d));
  318. }
  319. }
  320. public override object GetValue()
  321. {
  322. return _d;
  323. }
  324. internal DateTime GetDateTime()
  325. {
  326. return _d;
  327. }
  328. #endregion // Methods
  329. }
  330. internal sealed class TimestampReaderCacheContainer : DateTimeReaderCacheContainer // Types.DATE
  331. {
  332. protected override void FetchInternal(ResultSet rs, int columnIndex) {
  333. Timestamp ts = rs.getTimestamp(columnIndex);
  334. if (ts != null) {
  335. _d = new DateTime(DbConvert.JavaTimestampToClrTicks(ts));
  336. }
  337. }
  338. }
  339. internal sealed class DecimalReaderCacheContainer : ReaderCacheContainerBase // Types.DECIMAL, Types.NUMERIC
  340. {
  341. #region Fields
  342. decimal _d;
  343. #endregion // Fields
  344. #region Methods
  345. protected override void FetchInternal(ResultSet rs, int columnIndex)
  346. {
  347. java.math.BigDecimal bigDecimal = rs.getBigDecimal(columnIndex);
  348. if (bigDecimal != null) {
  349. _d = (decimal)vmw.common.PrimitiveTypeUtils.BigDecimalToDecimal(bigDecimal);
  350. }
  351. }
  352. public override object GetValue()
  353. {
  354. return _d;
  355. }
  356. internal decimal GetDecimal()
  357. {
  358. return _d;
  359. }
  360. #endregion // Methods
  361. }
  362. internal sealed class DoubleReaderCacheContainer : ReaderCacheContainerBase // Types.DOUBLE, Types.Float, Types.NUMERIC for Oracle with scale = -127
  363. {
  364. #region Fields
  365. double _d;
  366. #endregion // Fields
  367. #region Methods
  368. protected override void FetchInternal(ResultSet rs, int columnIndex)
  369. {
  370. _d = rs.getDouble(columnIndex);
  371. }
  372. public override object GetValue()
  373. {
  374. return _d;
  375. }
  376. internal double GetDouble()
  377. {
  378. return _d;
  379. }
  380. #endregion // Methods
  381. }
  382. internal sealed class Int32ReaderCacheContainer : ReaderCacheContainerBase // Types.INTEGER
  383. {
  384. #region Fields
  385. int _i;
  386. #endregion // Fields
  387. #region Methods
  388. protected override void FetchInternal(ResultSet rs, int columnIndex)
  389. {
  390. _i = rs.getInt(columnIndex);
  391. }
  392. public override object GetValue()
  393. {
  394. return _i;
  395. }
  396. internal int GetInt32()
  397. {
  398. return _i;
  399. }
  400. #endregion // Methods
  401. }
  402. internal class StringReaderCacheContainer : CharsReaderCacheContainer // Types.LONGVARCHAR, Types.VARCHAR, Types.CHAR
  403. {
  404. #region Fields
  405. protected string _s;
  406. #endregion // Fields
  407. #region Methods
  408. protected override void FetchInternal(ResultSet rs, int columnIndex)
  409. {
  410. _s = rs.getString(columnIndex);
  411. // Oracle Jdbc driver returns extra trailing 0 chars for NCHAR columns
  412. // if ((_s != null) && (_jdbcType == 1)) {
  413. // Console.WriteLine(_jdbcType);
  414. // int zeroIndex = ((string)_s).IndexOf((char)0);
  415. // if (zeroIndex > 0) {
  416. // Console.WriteLine("zero-padded");
  417. // _s = ((string)_s).Substring(0,zeroIndex);
  418. // }
  419. // else {
  420. // // Oracle sometimes pads with blanks (32)
  421. // int blankIndex = ((string)_s).IndexOf((char)32);
  422. // if (blankIndex > 0) {
  423. // Console.WriteLine("blank-padded");
  424. // _s = ((string)_s).Substring(0,blankIndex);
  425. // }
  426. // }
  427. // }
  428. }
  429. public override object GetValue()
  430. {
  431. return _s;
  432. }
  433. internal string GetString()
  434. {
  435. return _s;
  436. }
  437. internal override long GetChars(long dataIndex, char[] buffer, int bufferIndex, int length) {
  438. if (_s == null)
  439. return 0;
  440. if (buffer == null)
  441. return _s.Length;
  442. int actualLength = ((dataIndex + length) >= _s.Length) ? (_s.Length - (int)dataIndex) : length;
  443. for (int i = 0, stringIndex = (int)dataIndex; i < actualLength; i++)
  444. buffer[bufferIndex++] = _s[stringIndex++];
  445. return actualLength;
  446. }
  447. #endregion // Methods
  448. }
  449. internal sealed class NullReaderCacheContainer : ReaderCacheContainerBase // Types.NULL
  450. {
  451. #region Fields
  452. #endregion // Fields
  453. #region Methods
  454. protected override void FetchInternal(ResultSet rs, int columnIndex)
  455. {
  456. }
  457. public override object GetValue()
  458. {
  459. return DBNull.Value;
  460. }
  461. #endregion // Methods
  462. }
  463. internal sealed class FloatReaderCacheContainer : ReaderCacheContainerBase // Types.REAL
  464. {
  465. #region Fields
  466. float _f;
  467. #endregion // Fields
  468. #region Methods
  469. protected override void FetchInternal(ResultSet rs, int columnIndex)
  470. {
  471. _f = rs.getFloat(columnIndex);
  472. }
  473. public override object GetValue()
  474. {
  475. return _f;
  476. }
  477. internal float GetFloat()
  478. {
  479. return _f;
  480. }
  481. #endregion // Methods
  482. }
  483. internal sealed class RefReaderCacheContainer : ReaderCacheContainerBase // Types.REF
  484. {
  485. #region Fields
  486. java.sql.Ref _r;
  487. #endregion // Fields
  488. #region Methods
  489. protected override void FetchInternal(ResultSet rs, int columnIndex)
  490. {
  491. _r = rs.getRef(columnIndex);
  492. }
  493. public override object GetValue()
  494. {
  495. return _r;
  496. }
  497. #endregion // Methods
  498. }
  499. internal sealed class Int16ReaderCacheContainer : ReaderCacheContainerBase // Types.SMALLINT
  500. {
  501. #region Fields
  502. short _s;
  503. #endregion // Fields
  504. #region Methods
  505. protected override void FetchInternal(ResultSet rs, int columnIndex)
  506. {
  507. _s = rs.getShort(columnIndex);
  508. }
  509. public override object GetValue()
  510. {
  511. return _s;
  512. }
  513. internal short GetInt16()
  514. {
  515. return _s;
  516. }
  517. #endregion // Methods
  518. }
  519. internal sealed class ByteReaderCacheContainer : ReaderCacheContainerBase // Types.TINYINT
  520. {
  521. #region Fields
  522. byte _b;
  523. #endregion // Fields
  524. #region Methods
  525. protected override void FetchInternal(ResultSet rs, int columnIndex)
  526. {
  527. _b = (byte)rs.getByte(columnIndex);
  528. }
  529. public override object GetValue()
  530. {
  531. return _b;
  532. }
  533. internal byte GetByte()
  534. {
  535. return _b;
  536. }
  537. #endregion // Methods
  538. }
  539. internal sealed class ObjectReaderCacheContainer : ReaderCacheContainerBase // Types.Distinct, Types.JAVA_OBJECT, Types.OTHER, Types.STRUCT
  540. {
  541. #region Fields
  542. object o;
  543. #endregion // Fields
  544. #region Methods
  545. protected override void FetchInternal(ResultSet rs, int columnIndex)
  546. {
  547. o = rs.getObject(columnIndex);
  548. }
  549. public override object GetValue()
  550. {
  551. return o;
  552. }
  553. #endregion // Methods
  554. }
  555. }