AbstractDataReader.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. //
  2. // System.Data.Common.AbstractDataReader
  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 System.Data;
  32. using System.Collections;
  33. using System.Data.Common;
  34. using java.io;
  35. using java.sql;
  36. namespace System.Data.ProviderBase
  37. {
  38. public abstract class AbstractDataReader : DbDataReaderBase, ISafeDataRecord {
  39. #region Fields
  40. private ResultSetMetaData _resultsMetaData;
  41. protected AbstractDbCommand _command;
  42. private DataTable _schemaTable;
  43. private ReaderState _readerState = ReaderState.Uninitialized;
  44. private IReaderCacheContainer[] _readerCache;
  45. private int _currentCacheFilledPosition;
  46. private Stack _resultSetStack = new Stack();
  47. private bool _isClosed = false;
  48. [Flags]
  49. private enum ReaderState { Uninitialized = 0, Empty = 1, HasRows = 2, FirstRed = 4, Eof = 8, Fetching = 16 };
  50. protected internal enum SCHEMA_TABLE { ColumnName,
  51. ColumnOrdinal,
  52. ColumnSize,
  53. NumericPrecision,
  54. NumericScale,
  55. IsUnique,
  56. IsKey,
  57. BaseServerName,
  58. BaseCatalogName,
  59. BaseColumnName,
  60. BaseSchemaName,
  61. BaseTableName,
  62. DataType,
  63. AllowDBNull,
  64. ProviderType,
  65. IsAliased,
  66. IsExpression,
  67. IsIdentity,
  68. IsAutoIncrement,
  69. IsRowVersion,
  70. IsHidden,
  71. IsLong,
  72. IsReadOnly};
  73. #endregion // Fields
  74. #region Constructors
  75. protected AbstractDataReader() : base (CommandBehavior.Default) {
  76. }
  77. public AbstractDataReader(AbstractDbCommand command): base(command.Behavior) {
  78. _command = command;
  79. if (_command.Connection != null) {
  80. ((AbstractDBConnection)_command.Connection).AddReference(this);
  81. }
  82. }
  83. #endregion // Constructors
  84. #region Properties
  85. public override bool HasRows {
  86. get {
  87. if (IsClosed) {
  88. throw new InvalidOperationException("Invalid attempt to HasRows when reader is closed.");
  89. }
  90. try {
  91. if(null == Results)
  92. return false;
  93. }
  94. catch(SystemException) {
  95. //suppress
  96. return false;
  97. }
  98. return (_readerState & ReaderState.HasRows) != 0;
  99. }
  100. }
  101. public override int RecordsAffected
  102. {
  103. // MSDN : The RecordsAffected property is not set
  104. // until all rows are read and you close the reader.
  105. get {
  106. return _command.RecordsAffected;
  107. }
  108. }
  109. public override int FieldCount
  110. {
  111. get {
  112. if (ResultsMetaData == null)
  113. return 0;
  114. try {
  115. return ResultsMetaData.getColumnCount();
  116. }
  117. catch (SQLException exp) {
  118. throw CreateException(exp);
  119. }
  120. }
  121. }
  122. protected internal CommandBehavior Behavior
  123. {
  124. get {
  125. return _command.Behavior;
  126. }
  127. }
  128. public override Object this[String columnName]
  129. {
  130. get {
  131. try {
  132. int columnIndex = Results.findColumn(columnName) - 1;
  133. return this[columnIndex];
  134. }
  135. catch (SQLException exp) {
  136. throw new IndexOutOfRangeException(exp.Message, exp);
  137. }
  138. }
  139. }
  140. public override Object this[int columnIndex]
  141. {
  142. get { return GetValue(columnIndex); }
  143. }
  144. protected ResultSet Results
  145. {
  146. get {
  147. if (_readerState == ReaderState.Uninitialized) {
  148. if (_resultSetStack.Count == 0) {
  149. ResultSet resultSet = _command.CurrentResultSet;
  150. if (resultSet == null)
  151. return null;
  152. _resultSetStack.Push(resultSet);
  153. }
  154. _readerState = ReaderState.Fetching;
  155. for (;;) {
  156. try {
  157. Configuration.BooleanSetting prefetchSchema = Configuration.Switches.PrefetchSchema;
  158. if (prefetchSchema == Configuration.BooleanSetting.NotSet) {
  159. AbstractDBConnection conn = (AbstractDBConnection)((ICloneable)_command.Connection);
  160. string driverName = conn.JdbcConnection.getMetaData().getDriverName();
  161. if (driverName.IndexOf("DB2") >= 0)
  162. prefetchSchema = Configuration.BooleanSetting.True;
  163. }
  164. if (prefetchSchema == Configuration.BooleanSetting.True)
  165. GetSchemaTable();
  166. ResultSet resultSet = (ResultSet)_resultSetStack.Peek();
  167. if (resultSet.next()) {
  168. _readerState = (ReaderState.HasRows | ReaderState.FirstRed);
  169. ResultSetMetaData rsMetaData = ResultsMetaData;
  170. DbConvert.JavaSqlTypes javaSqlType = (DbConvert.JavaSqlTypes)rsMetaData.getColumnType(1);
  171. if (javaSqlType == DbConvert.JavaSqlTypes.OTHER) {
  172. object value = GetValue(0);
  173. if (value != null && value is ResultSet) {
  174. _resultsMetaData = null;
  175. _readerCache = null;
  176. SchemaTable = null;
  177. _readerState = ReaderState.Fetching;
  178. _resultSetStack.Push(value);
  179. continue;
  180. }
  181. }
  182. }
  183. else
  184. _readerState = ReaderState.Empty;
  185. break;
  186. }
  187. catch(SQLException e) {
  188. throw CreateException(e);
  189. }
  190. }
  191. }
  192. return (_resultSetStack.Count > 0) ? (ResultSet)_resultSetStack.Peek() : null;
  193. }
  194. }
  195. protected ResultSetMetaData ResultsMetaData
  196. {
  197. get {
  198. ResultSet results = Results;
  199. if (results == null) {
  200. return null;
  201. }
  202. if(_resultsMetaData == null) {
  203. _resultsMetaData = results.getMetaData();
  204. }
  205. return _resultsMetaData;
  206. }
  207. }
  208. protected DataTable SchemaTable
  209. {
  210. get {
  211. if (_schemaTable == null) {
  212. _schemaTable = ConstructSchemaTable();
  213. }
  214. return _schemaTable;
  215. }
  216. set {_schemaTable = value; }
  217. }
  218. internal protected IReaderCacheContainer[] ReaderCache
  219. {
  220. get {
  221. if (_readerCache == null) {
  222. _readerCache = CreateReaderCache();
  223. _currentCacheFilledPosition = -1;
  224. }
  225. return _readerCache;
  226. }
  227. }
  228. public override bool IsClosed {
  229. get { return _isClosed; }
  230. }
  231. #endregion // Properties
  232. #region Methods
  233. protected abstract int GetProviderType(int jdbcType);
  234. protected abstract SystemException CreateException(string message, SQLException e);
  235. protected abstract SystemException CreateException(IOException e);
  236. protected SystemException CreateException(SQLException e)
  237. {
  238. return CreateException(e.Message,e);
  239. }
  240. private bool CloseCurrentResultSet() {
  241. if (_resultSetStack.Count > 0) {
  242. try{
  243. _resultsMetaData = null;
  244. _readerCache = null;
  245. _readerState = ReaderState.Uninitialized;
  246. ResultSet rs = (ResultSet)_resultSetStack.Pop();
  247. rs.close();
  248. return true;
  249. }
  250. catch (SQLException exp) {
  251. throw CreateException(exp);
  252. }
  253. }
  254. return false;
  255. }
  256. // FIXME : add Close(bool readAllRecords) and pass this bool to skip looping over NextResult(), override AbstractDbCommand.ExecuteScalar
  257. public override void Close()
  258. {
  259. if (IsClosed)
  260. return;
  261. try {
  262. CloseCurrentResultSet();
  263. _command.OnReaderClosed(this);
  264. }
  265. finally {
  266. CloseInternal();
  267. }
  268. }
  269. internal void CloseInternal()
  270. {
  271. _resultsMetaData = null;
  272. _readerCache = null;
  273. _isClosed = true;
  274. }
  275. public override bool NextResult()
  276. {
  277. CloseCurrentResultSet();
  278. if ((_command.Behavior & CommandBehavior.SingleResult) != 0) {
  279. while (CloseCurrentResultSet());
  280. while (_command.NextResultSet());
  281. return false;
  282. }
  283. try {
  284. while (_resultSetStack.Count > 0) {
  285. ResultSet rs = (ResultSet)_resultSetStack.Peek();
  286. if(!rs.next()) {
  287. CloseCurrentResultSet();
  288. continue;
  289. }
  290. // must be a ResultSet
  291. object childRs = rs.getObject(1);
  292. if (childRs != null) {
  293. SchemaTable = null;
  294. _resultSetStack.Push(childRs);
  295. return true;
  296. }
  297. }
  298. }
  299. catch (SQLException exp) {
  300. throw CreateException(exp);
  301. }
  302. if (_command.NextResultSet()) {
  303. SchemaTable = null;
  304. return true;
  305. }
  306. return false;
  307. }
  308. public override bool Read()
  309. {
  310. if(null == Results ||
  311. (_readerState & (ReaderState.HasRows | ReaderState.Eof)) != ReaderState.HasRows)
  312. return false;
  313. bool firstRead = false;
  314. try {
  315. if ((_readerState & ReaderState.FirstRed) != 0) {
  316. firstRead = true;
  317. _readerState &= ~ReaderState.FirstRed;
  318. return true;
  319. }
  320. else {
  321. bool next = Results.next();
  322. if (!next)
  323. _readerState |= ReaderState.Eof;
  324. return next;
  325. }
  326. }
  327. catch (SQLException exp) {
  328. // suppress exception as .Net does
  329. return false;
  330. }
  331. finally {
  332. // in case of first read we could sampled the first value
  333. // to see whether there is a resultset, so _currentCacheFilledPosition
  334. // might be already inited
  335. if (!firstRead)
  336. _currentCacheFilledPosition = -1;
  337. }
  338. }
  339. public override bool GetBoolean(int columnIndex)
  340. {
  341. FillReaderCache(columnIndex);
  342. return ((BooleanReaderCacheContainer)ReaderCache[columnIndex]).GetBoolean();
  343. }
  344. public bool GetBooleanSafe(int columnIndex)
  345. {
  346. if (ReaderCache[columnIndex] is BooleanReaderCacheContainer) {
  347. return GetBoolean(columnIndex);
  348. }
  349. else {
  350. return Convert.ToBoolean(GetValue(columnIndex));
  351. }
  352. }
  353. public override byte GetByte(int columnIndex)
  354. {
  355. FillReaderCache(columnIndex);
  356. return ((ByteReaderCacheContainer)ReaderCache[columnIndex]).GetByte();
  357. }
  358. public byte GetByteSafe(int columnIndex)
  359. {
  360. if (ReaderCache[columnIndex] is ByteReaderCacheContainer) {
  361. return GetByte(columnIndex);
  362. }
  363. else {
  364. return Convert.ToByte(GetValue(columnIndex));
  365. }
  366. }
  367. public override long GetBytes(
  368. int columnIndex,
  369. long dataIndex,
  370. byte[] buffer,
  371. int bufferIndex,
  372. int length)
  373. {
  374. FillReaderCache(columnIndex);
  375. return ((BytesReaderCacheContainer)ReaderCache[columnIndex])
  376. .GetBytes(dataIndex, buffer, bufferIndex, length);
  377. }
  378. public virtual byte[] GetBytes(int columnIndex)
  379. {
  380. FillReaderCache(columnIndex);
  381. return ((BytesReaderCacheContainer)ReaderCache[columnIndex]).GetBytes();
  382. }
  383. public override char GetChar(int columnIndex)
  384. {
  385. FillReaderCache(columnIndex);
  386. string s = ((StringReaderCacheContainer)ReaderCache[columnIndex]).GetString();
  387. if(s == null) {
  388. return '\0';
  389. }
  390. return s[0];
  391. }
  392. public char GetCharSafe(int columnIndex)
  393. {
  394. if (ReaderCache[columnIndex] is StringReaderCacheContainer) {
  395. return GetChar(columnIndex);
  396. }
  397. else {
  398. return Convert.ToChar(GetValue(columnIndex));
  399. }
  400. }
  401. public override long GetChars(
  402. int columnIndex,
  403. long dataIndex,
  404. char[] buffer,
  405. int bufferIndex,
  406. int length)
  407. {
  408. FillReaderCache(columnIndex);
  409. return ((CharsReaderCacheContainer)ReaderCache[columnIndex])
  410. .GetChars(dataIndex, buffer, bufferIndex, length);
  411. }
  412. public override string GetDataTypeName(int columnIndex)
  413. {
  414. try {
  415. if (ResultsMetaData == null) {
  416. return String.Empty;
  417. }
  418. return ResultsMetaData.getColumnTypeName(columnIndex + 1);
  419. }
  420. catch (SQLException exp) {
  421. throw CreateException(exp);
  422. }
  423. }
  424. public override DateTime GetDateTime(int columnIndex)
  425. {
  426. return GetDateTimeUnsafe(columnIndex);
  427. }
  428. DateTime GetDateTimeUnsafe(int columnIndex)
  429. {
  430. FillReaderCache(columnIndex);
  431. return ((DateTimeReaderCacheContainer)ReaderCache[columnIndex]).GetDateTime();
  432. }
  433. public DateTime GetDateTimeSafe(int columnIndex)
  434. {
  435. if (ReaderCache[columnIndex] is DateTimeReaderCacheContainer) {
  436. return GetDateTimeUnsafe(columnIndex);
  437. }
  438. else {
  439. return Convert.ToDateTime(GetValue(columnIndex));
  440. }
  441. }
  442. public virtual TimeSpan GetTimeSpan(int columnIndex)
  443. {
  444. FillReaderCache(columnIndex);
  445. return ((TimeSpanReaderCacheContainer)ReaderCache[columnIndex]).GetTimeSpan();
  446. }
  447. public override Guid GetGuid(int columnIndex)
  448. {
  449. FillReaderCache(columnIndex);
  450. return ((GuidReaderCacheContainer)ReaderCache[columnIndex]).GetGuid();
  451. }
  452. public override decimal GetDecimal(int columnIndex)
  453. {
  454. return GetDecimalUnsafe(columnIndex);
  455. }
  456. decimal GetDecimalUnsafe(int columnIndex)
  457. {
  458. FillReaderCache(columnIndex);
  459. return ((DecimalReaderCacheContainer)ReaderCache[columnIndex]).GetDecimal();
  460. }
  461. public decimal GetDecimalSafe(int columnIndex)
  462. {
  463. if (ReaderCache[columnIndex] is DecimalReaderCacheContainer) {
  464. return GetDecimalUnsafe(columnIndex);
  465. }
  466. else {
  467. return Convert.ToDecimal(GetValue(columnIndex));
  468. }
  469. }
  470. public override double GetDouble(int columnIndex)
  471. {
  472. return GetDoubleUnsafe(columnIndex);
  473. }
  474. double GetDoubleUnsafe(int columnIndex)
  475. {
  476. FillReaderCache(columnIndex);
  477. return ((DoubleReaderCacheContainer)ReaderCache[columnIndex]).GetDouble();
  478. }
  479. public double GetDoubleSafe(int columnIndex)
  480. {
  481. if (ReaderCache[columnIndex] is DoubleReaderCacheContainer) {
  482. return GetDoubleUnsafe(columnIndex);
  483. }
  484. else {
  485. return Convert.ToDouble(GetValue(columnIndex));
  486. }
  487. }
  488. public override float GetFloat(int columnIndex)
  489. {
  490. return GetFloatUnsafe(columnIndex);
  491. }
  492. float GetFloatUnsafe(int columnIndex)
  493. {
  494. FillReaderCache(columnIndex);
  495. return ((FloatReaderCacheContainer)ReaderCache[columnIndex]).GetFloat();
  496. }
  497. public float GetFloatSafe(int columnIndex)
  498. {
  499. if (ReaderCache[columnIndex] is FloatReaderCacheContainer) {
  500. return GetFloatUnsafe(columnIndex);
  501. }
  502. else {
  503. return Convert.ToSingle(GetValue(columnIndex));
  504. }
  505. }
  506. public override short GetInt16(int columnIndex)
  507. {
  508. return GetInt16Unsafe(columnIndex);
  509. }
  510. short GetInt16Unsafe(int columnIndex)
  511. {
  512. FillReaderCache(columnIndex);
  513. return ((Int16ReaderCacheContainer)ReaderCache[columnIndex]).GetInt16();
  514. }
  515. public short GetInt16Safe(int columnIndex)
  516. {
  517. if (ReaderCache[columnIndex] is Int16ReaderCacheContainer) {
  518. return GetInt16Unsafe(columnIndex);
  519. }
  520. else {
  521. return Convert.ToInt16(GetValue(columnIndex));
  522. }
  523. }
  524. public override int GetInt32(int columnIndex)
  525. {
  526. return GetInt32Unsafe(columnIndex);
  527. }
  528. int GetInt32Unsafe(int columnIndex)
  529. {
  530. FillReaderCache(columnIndex);
  531. return ((Int32ReaderCacheContainer)ReaderCache[columnIndex]).GetInt32();
  532. }
  533. public int GetInt32Safe(int columnIndex)
  534. {
  535. if (ReaderCache[columnIndex] is Int32ReaderCacheContainer) {
  536. return GetInt32Unsafe(columnIndex);
  537. }
  538. else {
  539. return Convert.ToInt32(GetValue(columnIndex));
  540. }
  541. }
  542. public override long GetInt64(int columnIndex)
  543. {
  544. return GetInt64Unsafe(columnIndex);
  545. }
  546. long GetInt64Unsafe(int columnIndex)
  547. {
  548. FillReaderCache(columnIndex);
  549. return ((Int64ReaderCacheContainer)ReaderCache[columnIndex]).GetInt64();
  550. }
  551. public long GetInt64Safe(int columnIndex)
  552. {
  553. if (ReaderCache[columnIndex] is Int64ReaderCacheContainer) {
  554. return GetInt64Unsafe(columnIndex);
  555. }
  556. else {
  557. return Convert.ToInt64(GetValue(columnIndex));
  558. }
  559. }
  560. public override string GetName(int columnIndex)
  561. {
  562. try {
  563. if (ResultsMetaData == null) {
  564. return String.Empty;
  565. }
  566. return ResultsMetaData.getColumnName(columnIndex + 1);
  567. }
  568. catch (SQLException exp) {
  569. throw new IndexOutOfRangeException(exp.Message, exp);
  570. }
  571. }
  572. public override int GetOrdinal(String columnName)
  573. {
  574. try {
  575. int retVal = Results.findColumn(columnName);
  576. if(retVal != -1) {
  577. retVal -= 1;
  578. }
  579. return retVal;
  580. }
  581. catch (SQLException exp) {
  582. throw new IndexOutOfRangeException(exp.Message, exp);
  583. }
  584. }
  585. public override string GetString(int columnIndex)
  586. {
  587. return GetStringUnsafe(columnIndex);
  588. }
  589. string GetStringUnsafe(int columnIndex)
  590. {
  591. FillReaderCache(columnIndex);
  592. return ((StringReaderCacheContainer)ReaderCache[columnIndex]).GetString();
  593. }
  594. public string GetStringSafe(int columnIndex) {
  595. if (ReaderCache[columnIndex] is StringReaderCacheContainer) {
  596. return GetStringUnsafe(columnIndex);
  597. }
  598. else {
  599. return Convert.ToString(GetValue(columnIndex));
  600. }
  601. }
  602. public override object GetValue(int columnIndex)
  603. {
  604. FillReaderCache(columnIndex);
  605. if (ReaderCache[columnIndex].IsNull()) {
  606. return DBNull.Value;
  607. }
  608. return ReaderCache[columnIndex].GetValue();
  609. }
  610. public override int GetValues(Object[] values)
  611. {
  612. int columnCount = FieldCount;
  613. int i = 0;
  614. for (; i < values.Length && i < columnCount; i++) {
  615. values[i] = GetValue(i);
  616. }
  617. return i;
  618. }
  619. private void FillReaderCache(int columnIndex)
  620. {
  621. try {
  622. IReaderCacheContainer[] readerCache = ReaderCache;
  623. if ((Behavior & CommandBehavior.SequentialAccess) == 0) {
  624. while (_currentCacheFilledPosition < columnIndex) {
  625. _currentCacheFilledPosition++;
  626. readerCache[_currentCacheFilledPosition].Fetch(Results,_currentCacheFilledPosition, false);
  627. }
  628. }
  629. else {
  630. readerCache[columnIndex].Fetch(Results,columnIndex, true);
  631. }
  632. }
  633. catch(SQLException e) {
  634. _currentCacheFilledPosition = -1;
  635. throw CreateException(e);
  636. }
  637. catch (IOException e) {
  638. _currentCacheFilledPosition = -1;
  639. throw CreateException(e);
  640. }
  641. }
  642. protected virtual IReaderCacheContainer CreateReaderCacheContainer(int jdbcType, int columnIndex) {
  643. switch ((DbConvert.JavaSqlTypes)jdbcType) {
  644. case DbConvert.JavaSqlTypes.ARRAY :
  645. return new ArrayReaderCacheContainer();
  646. case DbConvert.JavaSqlTypes.BIGINT :
  647. return new Int64ReaderCacheContainer();
  648. case DbConvert.JavaSqlTypes.BINARY :
  649. case DbConvert.JavaSqlTypes.VARBINARY :
  650. case DbConvert.JavaSqlTypes.LONGVARBINARY :
  651. return new BytesReaderCacheContainer();
  652. case DbConvert.JavaSqlTypes.BIT :
  653. return new BooleanReaderCacheContainer();
  654. case DbConvert.JavaSqlTypes.BLOB :
  655. return new BlobReaderCacheContainer();
  656. case DbConvert.JavaSqlTypes.CHAR :
  657. if (String.CompareOrdinal("uniqueidentifier", ResultsMetaData.getColumnTypeName(columnIndex)) == 0) {
  658. return new GuidReaderCacheContainer();
  659. }
  660. else {
  661. return new StringReaderCacheContainer();
  662. }
  663. case DbConvert.JavaSqlTypes.CLOB :
  664. return new ClobReaderCacheContainer();
  665. case DbConvert.JavaSqlTypes.TIME :
  666. return new TimeSpanReaderCacheContainer();
  667. case DbConvert.JavaSqlTypes.DATE :
  668. AbstractDBConnection conn = (AbstractDBConnection)((ICloneable)_command.Connection);
  669. string driverName = conn.JdbcConnection.getMetaData().getDriverName();
  670. if (driverName.StartsWith("PostgreSQL")) {
  671. return new DateTimeReaderCacheContainer();
  672. }
  673. else
  674. goto case DbConvert.JavaSqlTypes.TIMESTAMP;
  675. case DbConvert.JavaSqlTypes.TIMESTAMP :
  676. return new TimestampReaderCacheContainer();
  677. case DbConvert.JavaSqlTypes.DECIMAL :
  678. case DbConvert.JavaSqlTypes.NUMERIC :
  679. // jdbc driver for oracle identitfies both FLOAT and NUMBEr columns as
  680. // java.sql.Types.NUMERIC (2), columnTypeName NUMBER, columnClassName java.math.BigDecimal
  681. // therefore we relay on scale
  682. int scale = ResultsMetaData.getScale(columnIndex);
  683. if (scale == -127) {
  684. // Oracle db type FLOAT
  685. return new DoubleReaderCacheContainer();
  686. }
  687. else {
  688. return new DecimalReaderCacheContainer();
  689. }
  690. case DbConvert.JavaSqlTypes.DOUBLE :
  691. case DbConvert.JavaSqlTypes.FLOAT :
  692. return new DoubleReaderCacheContainer();
  693. case DbConvert.JavaSqlTypes.INTEGER :
  694. return new Int32ReaderCacheContainer();
  695. case DbConvert.JavaSqlTypes.LONGVARCHAR :
  696. case DbConvert.JavaSqlTypes.VARCHAR :
  697. return new StringReaderCacheContainer();
  698. case DbConvert.JavaSqlTypes.NULL :
  699. return new NullReaderCacheContainer();
  700. case DbConvert.JavaSqlTypes.REAL :
  701. return new FloatReaderCacheContainer();
  702. case DbConvert.JavaSqlTypes.REF :
  703. return new RefReaderCacheContainer();
  704. case DbConvert.JavaSqlTypes.SMALLINT :
  705. return new Int16ReaderCacheContainer();
  706. case DbConvert.JavaSqlTypes.TINYINT :
  707. return new ByteReaderCacheContainer();
  708. case DbConvert.JavaSqlTypes.DISTINCT :
  709. case DbConvert.JavaSqlTypes.JAVA_OBJECT :
  710. case DbConvert.JavaSqlTypes.OTHER :
  711. case DbConvert.JavaSqlTypes.STRUCT :
  712. default :
  713. return new ObjectReaderCacheContainer();
  714. }
  715. }
  716. private IReaderCacheContainer[] CreateReaderCache()
  717. {
  718. try {
  719. IReaderCacheContainer[] readerCache = new IReaderCacheContainer[FieldCount];
  720. for(int i=1; i <= readerCache.Length; i++)
  721. readerCache[i-1] = CreateReaderCacheContainer(ResultsMetaData.getColumnType(i), i);
  722. return readerCache;
  723. }
  724. catch(SQLException e) {
  725. throw CreateException(e);
  726. }
  727. }
  728. protected bool IsNumeric(int columnIndex)
  729. {
  730. return ReaderCache[columnIndex].IsNumeric();
  731. }
  732. public override bool IsDBNull(int columnIndex)
  733. {
  734. FillReaderCache(columnIndex);
  735. return ReaderCache[columnIndex].IsNull();
  736. }
  737. public override Type GetFieldType(int i)
  738. {
  739. try {
  740. int javaSqlType = ResultsMetaData.getColumnType(i + 1);
  741. return DbConvert.JavaSqlTypeToClrType(javaSqlType);
  742. }
  743. catch (SQLException exp) {
  744. throw new IndexOutOfRangeException(exp.Message, exp);
  745. }
  746. }
  747. public IDataReader GetData(int i)
  748. {
  749. throw new NotSupportedException();
  750. }
  751. protected virtual void SetSchemaType(DataRow schemaRow, ResultSetMetaData metaData, int columnIndex) {
  752. DbConvert.JavaSqlTypes columnType = (DbConvert.JavaSqlTypes)metaData.getColumnType(columnIndex);
  753. switch (columnType) {
  754. case DbConvert.JavaSqlTypes.ARRAY: {
  755. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  756. schemaRow [(int)SCHEMA_TABLE.DataType] = typeof (java.sql.Array);
  757. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  758. break;
  759. }
  760. case DbConvert.JavaSqlTypes.BIGINT: {
  761. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  762. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfInt64;
  763. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  764. break;
  765. }
  766. case DbConvert.JavaSqlTypes.BINARY: {
  767. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  768. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  769. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  770. break;
  771. }
  772. case DbConvert.JavaSqlTypes.BIT: {
  773. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  774. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfBoolean;
  775. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  776. break;
  777. }
  778. case DbConvert.JavaSqlTypes.BLOB: {
  779. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  780. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  781. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  782. break;
  783. }
  784. case DbConvert.JavaSqlTypes.CHAR: {
  785. // FIXME : specific for Microsoft SQl Server driver
  786. if (metaData.getColumnTypeName(columnIndex).Equals("uniqueidentifier")) {
  787. schemaRow [(int)SCHEMA_TABLE.ProviderType] = DbType.Guid;
  788. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfGuid;
  789. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  790. }
  791. else {
  792. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  793. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;
  794. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  795. }
  796. break;
  797. }
  798. case DbConvert.JavaSqlTypes.CLOB: {
  799. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  800. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString; // instead og .java.sql.Clob
  801. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  802. break;
  803. }
  804. case DbConvert.JavaSqlTypes.DATE: {
  805. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  806. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDateTime;
  807. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  808. break;
  809. }
  810. // else if(DbConvert.JavaSqlTypes.DISTINCT)
  811. // {
  812. // schemaRow ["ProviderType = (int)GetProviderType((int)columnType);
  813. // schemaRow ["DataType = typeof (?);
  814. // schemaRow ["IsLong = false;
  815. // }
  816. case DbConvert.JavaSqlTypes.DOUBLE: {
  817. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  818. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDouble; // was typeof(float)
  819. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  820. break;
  821. }
  822. case DbConvert.JavaSqlTypes.FLOAT: {
  823. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  824. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDouble;
  825. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  826. break;
  827. }
  828. case DbConvert.JavaSqlTypes.REAL: {
  829. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  830. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfFloat;
  831. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  832. break;
  833. }
  834. case DbConvert.JavaSqlTypes.INTEGER: {
  835. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  836. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfInt32;
  837. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  838. break;
  839. }
  840. case DbConvert.JavaSqlTypes.JAVA_OBJECT: {
  841. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  842. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfObject;
  843. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  844. break;
  845. }
  846. case DbConvert.JavaSqlTypes.LONGVARBINARY: {
  847. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  848. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  849. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  850. break;
  851. }
  852. case DbConvert.JavaSqlTypes.LONGVARCHAR: {
  853. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  854. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;
  855. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  856. break;
  857. }
  858. case DbConvert.JavaSqlTypes.DECIMAL:
  859. case DbConvert.JavaSqlTypes.NUMERIC: {
  860. int scale = ResultsMetaData.getScale(columnIndex);
  861. if (scale == -127) {
  862. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  863. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDouble;
  864. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  865. }
  866. else {
  867. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  868. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDecimal;
  869. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  870. }
  871. break;
  872. }
  873. case DbConvert.JavaSqlTypes.REF: {
  874. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  875. schemaRow [(int)SCHEMA_TABLE.DataType] = typeof (java.sql.Ref);
  876. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  877. break;
  878. }
  879. case DbConvert.JavaSqlTypes.SMALLINT: {
  880. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  881. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfInt16;
  882. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  883. break;
  884. }
  885. case DbConvert.JavaSqlTypes.STRUCT: {
  886. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  887. schemaRow [(int)SCHEMA_TABLE.DataType] = typeof (java.sql.Struct);
  888. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  889. break;
  890. }
  891. case DbConvert.JavaSqlTypes.TIME: {
  892. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  893. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfTimespan;
  894. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  895. break;
  896. }
  897. case DbConvert.JavaSqlTypes.TIMESTAMP: {
  898. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  899. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDateTime;
  900. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  901. break;
  902. }
  903. case DbConvert.JavaSqlTypes.TINYINT: {
  904. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  905. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByte;
  906. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  907. break;
  908. }
  909. case DbConvert.JavaSqlTypes.VARBINARY: {
  910. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  911. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  912. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  913. break;
  914. }
  915. case DbConvert.JavaSqlTypes.VARCHAR: {
  916. // FIXME : specific for Microsoft SQl Server driver
  917. if (metaData.getColumnTypeName(columnIndex).Equals("sql_variant")) {
  918. schemaRow [(int)SCHEMA_TABLE.ProviderType] = DbType.Object;
  919. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfObject;
  920. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  921. }
  922. else {
  923. schemaRow [(int)SCHEMA_TABLE.ProviderType] = GetProviderType((int)columnType);
  924. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;// (char[]);
  925. schemaRow [(int)SCHEMA_TABLE.IsLong] = false;//true;
  926. }
  927. break;
  928. }
  929. // else if(columnType == -8 && metaData.getColumnTypeName(columnIndex).Equals("ROWID")) {
  930. // // FIXME : specific for Oracle JDBC driver : OracleTypes.ROWID
  931. // schemaRow [(int)SCHEMA_TABLE.ProviderType] = DbType.String;
  932. // schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;
  933. // schemaRow [(int)SCHEMA_TABLE.IsLong] = false;
  934. // }
  935. default: {
  936. schemaRow [(int)SCHEMA_TABLE.ProviderType] = DbType.Object;
  937. schemaRow [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfObject;
  938. schemaRow [(int)SCHEMA_TABLE.IsLong] = true;
  939. break;
  940. }
  941. }
  942. }
  943. public override DataTable GetSchemaTable()
  944. {
  945. if (SchemaTable.Rows != null && SchemaTable.Rows.Count > 0) {
  946. return SchemaTable;
  947. }
  948. ResultSetMetaData metaData;
  949. if (Behavior == CommandBehavior.SchemaOnly) {
  950. try {
  951. metaData = ((PreparedStatement)_command.Statement).getMetaData();
  952. }
  953. catch(SQLException e) {
  954. throw CreateException("CommandBehaviour.SchemaOnly is not supported by the JDBC driver.",e);
  955. }
  956. }
  957. else {
  958. metaData = ResultsMetaData;
  959. }
  960. if (metaData == null) {
  961. return SchemaTable;
  962. }
  963. DatabaseMetaData dbMetaData = null;
  964. AbstractDBConnection clonedConnection = null;
  965. if ((_command.Behavior & CommandBehavior.KeyInfo) != 0) {
  966. clonedConnection = (AbstractDBConnection)((ICloneable)_command.Connection).Clone();
  967. try {
  968. clonedConnection.Open();
  969. dbMetaData = clonedConnection.JdbcConnection.getMetaData();
  970. }
  971. catch {
  972. //suppress
  973. if (clonedConnection != null) {
  974. clonedConnection.Close();
  975. }
  976. }
  977. }
  978. try {
  979. int tmp;
  980. for(int i = 1; i <= metaData.getColumnCount(); i++) {
  981. DataRow row = SchemaTable.NewRow ();
  982. string columnName = metaData.getColumnLabel(i);
  983. string baseColumnName = metaData.getColumnName(i);
  984. row [(int)SCHEMA_TABLE.ColumnName] = columnName; // maybe we should use metaData.getColumnLabel(i);
  985. row [(int)SCHEMA_TABLE.ColumnSize] = metaData.getColumnDisplaySize(i);
  986. row [(int)SCHEMA_TABLE.ColumnOrdinal] = i - 1;
  987. try {
  988. // FIXME : workaround for Oracle JDBC driver bug
  989. // getPrecision on BLOB, CLOB, NCLOB throws NumberFormatException
  990. tmp = metaData.getPrecision(i);
  991. }
  992. catch(java.lang.NumberFormatException e) {
  993. // supress exception
  994. tmp = 255;
  995. }
  996. row [(int)SCHEMA_TABLE.NumericPrecision] = Convert.ToInt16(tmp > 255 ? 255 : tmp);
  997. tmp = metaData.getScale(i);
  998. row [(int)SCHEMA_TABLE.NumericScale] = Convert.ToInt16(tmp > 255 ? 255 : tmp);
  999. row [(int)SCHEMA_TABLE.BaseServerName] = DBNull.Value;
  1000. string catalog = null;
  1001. try {
  1002. catalog = metaData.getCatalogName(i);
  1003. }
  1004. catch (Exception e) {
  1005. // supress exception
  1006. }
  1007. if (catalog != null && catalog.Length == 0)
  1008. catalog = ((AbstractDBConnection)_command.Connection).JdbcConnection.getCatalog();
  1009. row [(int)SCHEMA_TABLE.BaseCatalogName] = catalog;
  1010. row [(int)SCHEMA_TABLE.BaseColumnName] = baseColumnName;
  1011. string schemaName;
  1012. string tableName;
  1013. try {
  1014. tableName = metaData.getTableName(i);
  1015. }
  1016. catch {
  1017. tableName = null;
  1018. }
  1019. try {
  1020. schemaName = metaData.getSchemaName(i);
  1021. }
  1022. catch {
  1023. schemaName = null;
  1024. }
  1025. if (tableName != null && tableName.Length == 0)
  1026. tableName = null;
  1027. if (schemaName != null && schemaName.Length == 0)
  1028. schemaName = null;
  1029. row [(int)SCHEMA_TABLE.BaseSchemaName] = schemaName;
  1030. row [(int)SCHEMA_TABLE.BaseTableName] = tableName;
  1031. row [(int)SCHEMA_TABLE.AllowDBNull] = Convert.ToBoolean(metaData.isNullable(i));
  1032. InitKeyInfo(row, dbMetaData, catalog, schemaName, tableName);
  1033. row [(int)SCHEMA_TABLE.IsAliased] = columnName != baseColumnName;
  1034. row [(int)SCHEMA_TABLE.IsExpression] = false;
  1035. row [(int)SCHEMA_TABLE.IsAutoIncrement] = metaData.isAutoIncrement(i);
  1036. row [(int)SCHEMA_TABLE.IsHidden] = false;
  1037. row [(int)SCHEMA_TABLE.IsReadOnly] = metaData.isReadOnly(i);
  1038. SetSchemaType(row, metaData, i);
  1039. SchemaTable.Rows.Add (row);
  1040. }
  1041. }
  1042. catch (SQLException e) {
  1043. throw CreateException(e);
  1044. }
  1045. finally {
  1046. if (clonedConnection != null) {
  1047. clonedConnection.Close();
  1048. }
  1049. }
  1050. return SchemaTable;
  1051. }
  1052. private void InitKeyInfo(DataRow row, DatabaseMetaData dbMetaData, String catalog, String schema, String table) {
  1053. string column = (string)row [(int)SCHEMA_TABLE.BaseColumnName];
  1054. row [(int)SCHEMA_TABLE.IsUnique] = false;
  1055. row [(int)SCHEMA_TABLE.IsKey] = false;
  1056. row [(int)SCHEMA_TABLE.IsIdentity] = false;
  1057. row [(int)SCHEMA_TABLE.IsRowVersion] = false;
  1058. if ((_command.Behavior & CommandBehavior.KeyInfo) == 0)
  1059. return;
  1060. if(table == null || column == null || dbMetaData == null)
  1061. return;
  1062. ResultSet versionCol = dbMetaData.getVersionColumns(catalog, schema, table);
  1063. try {
  1064. while(versionCol.next()) {
  1065. if(versionCol.getString("COLUMN_NAME") == column) {
  1066. if (DatabaseMetaData__Finals.versionColumnPseudo == versionCol.getShort("PSEUDO_COLUMN")) {
  1067. row [(int)SCHEMA_TABLE.IsIdentity] = true;
  1068. row [(int)SCHEMA_TABLE.IsRowVersion] = true;
  1069. }
  1070. }
  1071. }
  1072. }
  1073. finally {
  1074. versionCol.close();
  1075. }
  1076. ResultSet primaryKeys = dbMetaData.getPrimaryKeys(catalog,schema,table);
  1077. bool primaryKeyExists = false;
  1078. int columnCount = 0;
  1079. try {
  1080. while(primaryKeys.next()) {
  1081. columnCount++;
  1082. if(primaryKeys.getString("COLUMN_NAME") == column) {
  1083. row [(int)SCHEMA_TABLE.IsKey] = true;
  1084. primaryKeyExists = true;
  1085. }
  1086. }
  1087. // column constitutes a key by itself, so it should be marked as unique
  1088. if ((columnCount == 1) && (((bool)row [(int)SCHEMA_TABLE.IsKey]) == true)) {
  1089. row [(int)SCHEMA_TABLE.IsUnique] = true;
  1090. }
  1091. }
  1092. finally {
  1093. primaryKeys.close();
  1094. }
  1095. ResultSet indexInfoRes = dbMetaData.getIndexInfo(catalog,schema,table,true,false);
  1096. string currentIndexName = null;
  1097. columnCount = 0;
  1098. bool belongsToCurrentIndex = false;
  1099. bool atFirstIndex = true;
  1100. bool uniqueKeyExists = false;
  1101. try {
  1102. while(indexInfoRes.next()) {
  1103. if (indexInfoRes.getShort("TYPE") == DatabaseMetaData__Finals.tableIndexStatistic) {
  1104. // index of type tableIndexStatistic identifies table statistics - ignore it
  1105. continue;
  1106. }
  1107. uniqueKeyExists = true;
  1108. string iname = indexInfoRes.getString("INDEX_NAME");
  1109. if (currentIndexName == iname) {
  1110. // we're within the rows of the same index
  1111. columnCount++;
  1112. }
  1113. else {
  1114. // we jump to row of new index
  1115. if (belongsToCurrentIndex && columnCount == 1) {
  1116. // there is a constraint of type UNIQUE that applies only to this column
  1117. row [(int)SCHEMA_TABLE.IsUnique] = true;
  1118. }
  1119. if (currentIndexName != null) {
  1120. atFirstIndex = false;
  1121. }
  1122. currentIndexName = iname;
  1123. columnCount = 1;
  1124. belongsToCurrentIndex = false;
  1125. }
  1126. if(indexInfoRes.getString("COLUMN_NAME") == column) {
  1127. // FIXME : this will cause "spare" columns marked as IsKey. Needs future investigation.
  1128. // only the first index we met should be marked as a key
  1129. //if (atFirstIndex) {
  1130. row [(int)SCHEMA_TABLE.IsKey] = true;
  1131. //}
  1132. belongsToCurrentIndex = true;
  1133. }
  1134. }
  1135. // the column appears in the last index, which is single-column
  1136. if (belongsToCurrentIndex && columnCount == 1) {
  1137. // there is a constraint of type UNIQUE that applies only to this column
  1138. row [(int)SCHEMA_TABLE.IsUnique] = true;
  1139. }
  1140. }
  1141. finally {
  1142. indexInfoRes.close();
  1143. }
  1144. if(!primaryKeyExists && !uniqueKeyExists) {
  1145. ResultSet bestRowId = dbMetaData.getBestRowIdentifier(catalog, schema, table, DatabaseMetaData__Finals.bestRowTemporary, false);
  1146. try {
  1147. while(bestRowId.next()) {
  1148. if(bestRowId.getString("COLUMN_NAME") == column)
  1149. row [(int)SCHEMA_TABLE.IsKey] = true;
  1150. }
  1151. }
  1152. finally {
  1153. bestRowId.close();
  1154. }
  1155. }
  1156. }
  1157. protected static DataTable ConstructSchemaTable ()
  1158. {
  1159. Type booleanType = DbTypes.TypeOfBoolean;
  1160. Type stringType = DbTypes.TypeOfString;
  1161. Type intType = DbTypes.TypeOfInt32;
  1162. Type typeType = DbTypes.TypeOfType;
  1163. Type shortType = DbTypes.TypeOfInt16;
  1164. DataTable schemaTable = new DataTable ("SchemaTable");
  1165. schemaTable.Columns.Add ("ColumnName", stringType);
  1166. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  1167. schemaTable.Columns.Add ("ColumnSize", intType);
  1168. schemaTable.Columns.Add ("NumericPrecision", shortType);
  1169. schemaTable.Columns.Add ("NumericScale", shortType);
  1170. schemaTable.Columns.Add ("IsUnique", booleanType);
  1171. schemaTable.Columns.Add ("IsKey", booleanType);
  1172. schemaTable.Columns.Add ("BaseServerName", stringType);
  1173. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  1174. schemaTable.Columns.Add ("BaseColumnName", stringType);
  1175. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  1176. schemaTable.Columns.Add ("BaseTableName", stringType);
  1177. schemaTable.Columns.Add ("DataType", typeType);
  1178. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  1179. schemaTable.Columns.Add ("ProviderType", intType);
  1180. schemaTable.Columns.Add ("IsAliased", booleanType);
  1181. schemaTable.Columns.Add ("IsExpression", booleanType);
  1182. schemaTable.Columns.Add ("IsIdentity", booleanType);
  1183. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  1184. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  1185. schemaTable.Columns.Add ("IsHidden", booleanType);
  1186. schemaTable.Columns.Add ("IsLong", booleanType);
  1187. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  1188. return schemaTable;
  1189. }
  1190. #endregion // Methods
  1191. }
  1192. }