AbstractDataReader.cs 39 KB

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