AbstractDataReader.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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.ProviderBase;
  34. using java.io;
  35. using java.sql;
  36. namespace System.Data.Common
  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. 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. DbTypes.JavaSqlTypes javaSqlType = (DbTypes.JavaSqlTypes)rsMetaData.getColumnType(1);
  171. if (javaSqlType == DbTypes.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. byte[] byteArr = ((BytesReaderCacheContainer)ReaderCache[columnIndex]).GetBytes();
  376. if (byteArr == null)
  377. return 0;
  378. if (buffer == null)
  379. return byteArr.LongLength;
  380. long actualLength = ((dataIndex + length) >= byteArr.Length) ? (byteArr.Length - dataIndex) : length;
  381. Array.Copy(byteArr,dataIndex,buffer,bufferIndex,actualLength);
  382. return actualLength;
  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. char[] charArr = ((CharsReaderCacheContainer)ReaderCache[columnIndex]).GetChars();
  416. long actualLength = ((dataIndex + length) >= charArr.Length) ? (charArr.Length - dataIndex) : length;
  417. Array.Copy(charArr,dataIndex,buffer,bufferIndex,actualLength);
  418. return actualLength;
  419. }
  420. public override string GetDataTypeName(int columnIndex)
  421. {
  422. try {
  423. if (ResultsMetaData == null) {
  424. return String.Empty;
  425. }
  426. return ResultsMetaData.getColumnTypeName(columnIndex + 1);
  427. }
  428. catch (SQLException exp) {
  429. throw CreateException(exp);
  430. }
  431. }
  432. public override DateTime GetDateTime(int columnIndex)
  433. {
  434. FillReaderCache(columnIndex);
  435. return ((DateTimeReaderCacheContainer)ReaderCache[columnIndex]).GetDateTime();
  436. }
  437. public DateTime GetDateTimeSafe(int columnIndex)
  438. {
  439. if (ReaderCache[columnIndex] is DateTimeReaderCacheContainer) {
  440. return GetDateTime(columnIndex);
  441. }
  442. else {
  443. return Convert.ToDateTime(GetValue(columnIndex));
  444. }
  445. }
  446. public virtual TimeSpan GetTimeSpan(int columnIndex)
  447. {
  448. FillReaderCache(columnIndex);
  449. return ((TimeSpanReaderCacheContainer)ReaderCache[columnIndex]).GetTimeSpan();
  450. }
  451. public override Guid GetGuid(int columnIndex)
  452. {
  453. FillReaderCache(columnIndex);
  454. return ((GuidReaderCacheContainer)ReaderCache[columnIndex]).GetGuid();
  455. }
  456. public override decimal GetDecimal(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 GetDecimal(columnIndex);
  465. }
  466. else {
  467. return Convert.ToDecimal(GetValue(columnIndex));
  468. }
  469. }
  470. public override double GetDouble(int columnIndex)
  471. {
  472. FillReaderCache(columnIndex);
  473. return ((DoubleReaderCacheContainer)ReaderCache[columnIndex]).GetDouble();
  474. }
  475. public double GetDoubleSafe(int columnIndex)
  476. {
  477. if (ReaderCache[columnIndex] is DoubleReaderCacheContainer) {
  478. return GetDouble(columnIndex);
  479. }
  480. else {
  481. return Convert.ToDouble(GetValue(columnIndex));
  482. }
  483. }
  484. public override float GetFloat(int columnIndex)
  485. {
  486. FillReaderCache(columnIndex);
  487. return ((FloatReaderCacheContainer)ReaderCache[columnIndex]).GetFloat();
  488. }
  489. public float GetFloatSafe(int columnIndex)
  490. {
  491. if (ReaderCache[columnIndex] is FloatReaderCacheContainer) {
  492. return GetFloat(columnIndex);
  493. }
  494. else {
  495. return Convert.ToSingle(GetValue(columnIndex));
  496. }
  497. }
  498. public override short GetInt16(int columnIndex)
  499. {
  500. FillReaderCache(columnIndex);
  501. return ((Int16ReaderCacheContainer)ReaderCache[columnIndex]).GetInt16();
  502. }
  503. public short GetInt16Safe(int columnIndex)
  504. {
  505. if (ReaderCache[columnIndex] is Int16ReaderCacheContainer) {
  506. return GetInt16(columnIndex);
  507. }
  508. else {
  509. return Convert.ToInt16(GetValue(columnIndex));
  510. }
  511. }
  512. public override int GetInt32(int columnIndex)
  513. {
  514. FillReaderCache(columnIndex);
  515. return ((Int32ReaderCacheContainer)ReaderCache[columnIndex]).GetInt32();
  516. }
  517. public int GetInt32Safe(int columnIndex)
  518. {
  519. if (ReaderCache[columnIndex] is Int32ReaderCacheContainer) {
  520. return GetInt32(columnIndex);
  521. }
  522. else {
  523. return Convert.ToInt32(GetValue(columnIndex));
  524. }
  525. }
  526. public override long GetInt64(int columnIndex)
  527. {
  528. FillReaderCache(columnIndex);
  529. return ((Int64ReaderCacheContainer)ReaderCache[columnIndex]).GetInt64();
  530. }
  531. public long GetInt64Safe(int columnIndex)
  532. {
  533. if (ReaderCache[columnIndex] is Int64ReaderCacheContainer) {
  534. return GetInt64(columnIndex);
  535. }
  536. else {
  537. return Convert.ToInt64(GetValue(columnIndex));
  538. }
  539. }
  540. public override string GetName(int columnIndex)
  541. {
  542. try {
  543. if (ResultsMetaData == null) {
  544. return String.Empty;
  545. }
  546. return ResultsMetaData.getColumnName(columnIndex + 1);
  547. }
  548. catch (SQLException exp) {
  549. throw new IndexOutOfRangeException(exp.Message, exp);
  550. }
  551. }
  552. public override int GetOrdinal(String columnName)
  553. {
  554. try {
  555. int retVal = Results.findColumn(columnName);
  556. if(retVal != -1) {
  557. retVal -= 1;
  558. }
  559. return retVal;
  560. }
  561. catch (SQLException exp) {
  562. throw new IndexOutOfRangeException(exp.Message, exp);
  563. }
  564. }
  565. public override string GetString(int columnIndex)
  566. {
  567. FillReaderCache(columnIndex);
  568. return ((StringReaderCacheContainer)ReaderCache[columnIndex]).GetString();
  569. }
  570. public string GetStringSafe(int columnIndex) {
  571. if (ReaderCache[columnIndex] is StringReaderCacheContainer) {
  572. return GetString(columnIndex);
  573. }
  574. else {
  575. return Convert.ToString(GetValue(columnIndex));
  576. }
  577. }
  578. public override object GetValue(int columnIndex)
  579. {
  580. FillReaderCache(columnIndex);
  581. if (ReaderCache[columnIndex].IsNull()) {
  582. return DBNull.Value;
  583. }
  584. return ReaderCache[columnIndex].GetValue();
  585. }
  586. public override int GetValues(Object[] values)
  587. {
  588. int columnCount = FieldCount;
  589. int i = 0;
  590. for (; i < values.Length && i < columnCount; i++) {
  591. values[i] = GetValue(i);
  592. }
  593. return i;
  594. }
  595. private void FillReaderCache(int columnIndex)
  596. {
  597. try {
  598. IReaderCacheContainer[] readerCache = ReaderCache;
  599. if ((Behavior & CommandBehavior.SequentialAccess) == 0) {
  600. while (_currentCacheFilledPosition < columnIndex) {
  601. _currentCacheFilledPosition++;
  602. readerCache[_currentCacheFilledPosition].Fetch(Results,_currentCacheFilledPosition);
  603. }
  604. }
  605. else {
  606. readerCache[columnIndex].Fetch(Results,columnIndex);
  607. }
  608. }
  609. catch(SQLException e) {
  610. _currentCacheFilledPosition = -1;
  611. throw CreateException(e);
  612. }
  613. catch (IOException e) {
  614. _currentCacheFilledPosition = -1;
  615. throw CreateException(e);
  616. }
  617. }
  618. private IReaderCacheContainer[] CreateReaderCache()
  619. {
  620. try {
  621. IReaderCacheContainer[] readerCache = new IReaderCacheContainer[FieldCount];
  622. for(int i=0; i < readerCache.Length; i++) {
  623. DbTypes.JavaSqlTypes javaSqlType = (DbTypes.JavaSqlTypes) ResultsMetaData.getColumnType(i + 1);
  624. switch (javaSqlType) {
  625. case DbTypes.JavaSqlTypes.ARRAY :
  626. readerCache[i] = new ArrayReaderCacheContainer();
  627. break;
  628. case DbTypes.JavaSqlTypes.BIGINT :
  629. readerCache[i] = new Int64ReaderCacheContainer();
  630. break;
  631. case DbTypes.JavaSqlTypes.BINARY :
  632. case DbTypes.JavaSqlTypes.VARBINARY :
  633. case DbTypes.JavaSqlTypes.LONGVARBINARY :
  634. readerCache[i] = new BytesReaderCacheContainer();
  635. break;
  636. case DbTypes.JavaSqlTypes.BIT :
  637. readerCache[i] = new BooleanReaderCacheContainer();
  638. break;
  639. case DbTypes.JavaSqlTypes.BLOB :
  640. readerCache[i] = new BlobReaderCacheContainer();
  641. break;
  642. case DbTypes.JavaSqlTypes.CHAR :
  643. if ("uniqueidentifier".Equals(ResultsMetaData.getColumnTypeName(i + 1))) {
  644. readerCache[i] = new GuidReaderCacheContainer();
  645. }
  646. else {
  647. readerCache[i] = new StringReaderCacheContainer();
  648. }
  649. break;
  650. case DbTypes.JavaSqlTypes.CLOB :
  651. readerCache[i] = new ClobReaderCacheContainer();
  652. break;
  653. case DbTypes.JavaSqlTypes.TIME :
  654. readerCache[i] = new TimeSpanReaderCacheContainer();
  655. break;
  656. case DbTypes.JavaSqlTypes.DATE :
  657. AbstractDBConnection conn = (AbstractDBConnection)((ICloneable)_command.Connection);
  658. string driverName = conn.JdbcConnection.getMetaData().getDriverName();
  659. if (driverName.StartsWith("PostgreSQL")) {
  660. readerCache[i] = new DateTimeReaderCacheContainer();
  661. break;
  662. }
  663. else
  664. goto case DbTypes.JavaSqlTypes.TIMESTAMP;
  665. case DbTypes.JavaSqlTypes.TIMESTAMP :
  666. readerCache[i] = new TimestampReaderCacheContainer();
  667. break;
  668. case DbTypes.JavaSqlTypes.DECIMAL :
  669. case DbTypes.JavaSqlTypes.NUMERIC :
  670. // jdbc driver for oracle identitfies both FLOAT and NUMBEr columns as
  671. // java.sql.Types.NUMERIC (2), columnTypeName NUMBER, columnClassName java.math.BigDecimal
  672. // therefore we relay on scale
  673. int scale = ResultsMetaData.getScale(i + 1);
  674. if (scale == -127) {
  675. // Oracle db type FLOAT
  676. readerCache[i] = new DoubleReaderCacheContainer();
  677. }
  678. else {
  679. readerCache[i] = new DecimalReaderCacheContainer();
  680. }
  681. break;
  682. case DbTypes.JavaSqlTypes.DOUBLE :
  683. case DbTypes.JavaSqlTypes.FLOAT :
  684. readerCache[i] = new DoubleReaderCacheContainer();
  685. break;
  686. case DbTypes.JavaSqlTypes.INTEGER :
  687. readerCache[i] = new Int32ReaderCacheContainer();
  688. break;
  689. case DbTypes.JavaSqlTypes.LONGVARCHAR :
  690. case DbTypes.JavaSqlTypes.VARCHAR :
  691. readerCache[i] = new StringReaderCacheContainer();
  692. break;
  693. case DbTypes.JavaSqlTypes.NULL :
  694. readerCache[i] = new NullReaderCacheContainer();
  695. break;
  696. case DbTypes.JavaSqlTypes.REAL :
  697. readerCache[i] = new FloatReaderCacheContainer();
  698. break;
  699. case DbTypes.JavaSqlTypes.REF :
  700. readerCache[i] = new RefReaderCacheContainer();
  701. break;
  702. case DbTypes.JavaSqlTypes.SMALLINT :
  703. readerCache[i] = new Int16ReaderCacheContainer();
  704. break;
  705. case DbTypes.JavaSqlTypes.TINYINT :
  706. readerCache[i] = new ByteReaderCacheContainer();
  707. break;
  708. case DbTypes.JavaSqlTypes.DISTINCT :
  709. case DbTypes.JavaSqlTypes.JAVA_OBJECT :
  710. case DbTypes.JavaSqlTypes.OTHER :
  711. case DbTypes.JavaSqlTypes.STRUCT :
  712. default :
  713. readerCache[i] = new ObjectReaderCacheContainer();
  714. break;
  715. }
  716. // ((ReaderCacheContainerBase)readerCache[i])._jdbcType = (int) javaSqlType;
  717. }
  718. return readerCache;
  719. }
  720. catch(SQLException e) {
  721. throw CreateException(e);
  722. }
  723. }
  724. public override bool IsDBNull(int columnIndex)
  725. {
  726. FillReaderCache(columnIndex);
  727. return ReaderCache[columnIndex].IsNull();
  728. }
  729. public override Type GetFieldType(int i)
  730. {
  731. try {
  732. int javaSqlType = ResultsMetaData.getColumnType(i + 1);
  733. return DbConvert.JavaSqlTypeToClrType(javaSqlType);
  734. }
  735. catch (SQLException exp) {
  736. throw new IndexOutOfRangeException(exp.Message, exp);
  737. }
  738. }
  739. public IDataReader GetData(int i)
  740. {
  741. throw new NotSupportedException();
  742. }
  743. public override DataTable GetSchemaTable()
  744. {
  745. if (SchemaTable.Rows != null && SchemaTable.Rows.Count > 0) {
  746. return SchemaTable;
  747. }
  748. ResultSetMetaData metaData;
  749. if (Behavior == CommandBehavior.SchemaOnly) {
  750. try {
  751. metaData = ((PreparedStatement)_command.JdbcStatement).getMetaData();
  752. }
  753. catch(SQLException e) {
  754. throw CreateException("CommandBehaviour.SchemaOnly is not supported by the JDBC driver.",e);
  755. }
  756. }
  757. else {
  758. metaData = ResultsMetaData;
  759. }
  760. if (metaData == null) {
  761. return SchemaTable;
  762. }
  763. DatabaseMetaData dbMetaData = null;
  764. AbstractDBConnection clonedConnection = null;
  765. if ((_command.Behavior & CommandBehavior.KeyInfo) != 0) {
  766. clonedConnection = (AbstractDBConnection)((ICloneable)_command.Connection).Clone();
  767. try {
  768. clonedConnection.Open();
  769. dbMetaData = clonedConnection.JdbcConnection.getMetaData();
  770. }
  771. catch {
  772. //suppress
  773. if (clonedConnection != null) {
  774. clonedConnection.Close();
  775. }
  776. }
  777. }
  778. try {
  779. int tmp;
  780. for(int i = 1; i <= metaData.getColumnCount(); i++) {
  781. DataRow row = SchemaTable.NewRow ();
  782. string columnName = metaData.getColumnLabel(i);
  783. string baseColumnName = metaData.getColumnName(i);
  784. row [(int)SCHEMA_TABLE.ColumnName] = columnName; // maybe we should use metaData.getColumnLabel(i);
  785. row [(int)SCHEMA_TABLE.ColumnSize] = metaData.getColumnDisplaySize(i);
  786. row [(int)SCHEMA_TABLE.ColumnOrdinal] = i - 1;
  787. try {
  788. // FIXME : workaround for Oracle JDBC driver bug
  789. // getPrecision on BLOB, CLOB, NCLOB throws NumberFormatException
  790. tmp = metaData.getPrecision(i);
  791. }
  792. catch(java.lang.NumberFormatException e) {
  793. // supress exception
  794. tmp = 255;
  795. }
  796. row [(int)SCHEMA_TABLE.NumericPrecision] = Convert.ToInt16(tmp > 255 ? 255 : tmp);
  797. tmp = metaData.getScale(i);
  798. row [(int)SCHEMA_TABLE.NumericScale] = Convert.ToInt16(tmp > 255 ? 255 : tmp);
  799. row [(int)SCHEMA_TABLE.BaseServerName] = DBNull.Value;
  800. string catalog = null;
  801. try {
  802. catalog = metaData.getCatalogName(i);
  803. }
  804. catch (Exception e) {
  805. // supress exception
  806. }
  807. if (catalog != null && catalog.Length == 0)
  808. catalog = ((AbstractDBConnection)_command.Connection).JdbcConnection.getCatalog();
  809. row [(int)SCHEMA_TABLE.BaseCatalogName] = catalog;
  810. row [(int)SCHEMA_TABLE.BaseColumnName] = baseColumnName;
  811. string schemaName;
  812. string tableName;
  813. try {
  814. tableName = metaData.getTableName(i);
  815. }
  816. catch {
  817. tableName = null;
  818. }
  819. try {
  820. schemaName = metaData.getSchemaName(i);
  821. }
  822. catch {
  823. schemaName = null;
  824. }
  825. if (tableName != null && tableName.Length == 0)
  826. tableName = null;
  827. if (schemaName != null && schemaName.Length == 0)
  828. schemaName = null;
  829. row [(int)SCHEMA_TABLE.BaseSchemaName] = schemaName;
  830. row [(int)SCHEMA_TABLE.BaseTableName] = tableName;
  831. row [(int)SCHEMA_TABLE.AllowDBNull] = Convert.ToBoolean(metaData.isNullable(i));
  832. InitKeyInfo(row, dbMetaData, catalog, schemaName, tableName);
  833. row [(int)SCHEMA_TABLE.IsAliased] = columnName != baseColumnName;
  834. row [(int)SCHEMA_TABLE.IsExpression] = false;
  835. row [(int)SCHEMA_TABLE.IsAutoIncrement] = metaData.isAutoIncrement(i);
  836. row [(int)SCHEMA_TABLE.IsHidden] = false;
  837. row [(int)SCHEMA_TABLE.IsReadOnly] = metaData.isReadOnly(i);
  838. int columnType = metaData.getColumnType(i);
  839. string columnTypeName = metaData.getColumnTypeName(i);
  840. if(columnType == Types.ARRAY) {
  841. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  842. row [(int)SCHEMA_TABLE.DataType] = typeof (java.sql.Array);
  843. row [(int)SCHEMA_TABLE.IsLong] = false;
  844. }
  845. else if(columnType == Types.BIGINT) {
  846. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  847. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfInt64;
  848. row [(int)SCHEMA_TABLE.IsLong] = false;
  849. }
  850. else if(columnType == Types.BINARY) {
  851. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  852. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  853. row [(int)SCHEMA_TABLE.IsLong] = true;
  854. }
  855. else if(columnType == Types.BIT) {
  856. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  857. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfBoolean;
  858. row [(int)SCHEMA_TABLE.IsLong] = false;
  859. }
  860. else if(columnType == Types.BLOB) {
  861. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  862. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  863. row [(int)SCHEMA_TABLE.IsLong] = true;
  864. }
  865. else if(columnType == Types.CHAR) {
  866. // FIXME : specific for Microsoft SQl Server driver
  867. if (columnTypeName.Equals("uniqueidentifier")) {
  868. row [(int)SCHEMA_TABLE.ProviderType] = DbType.Guid;
  869. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfGuid;
  870. row [(int)SCHEMA_TABLE.IsLong] = false;
  871. }
  872. else {
  873. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  874. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;
  875. row [(int)SCHEMA_TABLE.IsLong] = false;
  876. }
  877. }
  878. else if(columnType == Types.CLOB) {
  879. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  880. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString; // instead og .java.sql.Clob
  881. row [(int)SCHEMA_TABLE.IsLong] = true;
  882. }
  883. else if(columnType == Types.DATE) {
  884. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  885. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDateTime;
  886. row [(int)SCHEMA_TABLE.IsLong] = false;
  887. }
  888. else if(columnType == Types.DECIMAL) {
  889. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  890. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDecimal;
  891. row [(int)SCHEMA_TABLE.IsLong] = false;
  892. }
  893. // else if(columnType == Types.DISTINCT)
  894. // {
  895. // row ["ProviderType = (int)GetProviderType(columnType);
  896. // row ["DataType = typeof (?);
  897. // row ["IsLong = false;
  898. // }
  899. else if(columnType == Types.DOUBLE) {
  900. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  901. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDouble; // was typeof(float)
  902. row [(int)SCHEMA_TABLE.IsLong] = false;
  903. }
  904. else if(columnType == Types.FLOAT) {
  905. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  906. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDouble;
  907. row [(int)SCHEMA_TABLE.IsLong] = false;
  908. }
  909. else if(columnType == Types.REAL) {
  910. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  911. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfFloat;
  912. row [(int)SCHEMA_TABLE.IsLong] = false;
  913. }
  914. else if(columnType == Types.INTEGER) {
  915. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  916. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfInt32;
  917. row [(int)SCHEMA_TABLE.IsLong] = false;
  918. }
  919. else if(columnType == Types.JAVA_OBJECT) {
  920. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  921. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfObject;
  922. row [(int)SCHEMA_TABLE.IsLong] = false;
  923. }
  924. else if(columnType == Types.LONGVARBINARY) {
  925. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  926. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  927. row [(int)SCHEMA_TABLE.IsLong] = true;
  928. }
  929. else if(columnType == Types.LONGVARCHAR) {
  930. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  931. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;
  932. row [(int)SCHEMA_TABLE.IsLong] = true;
  933. }
  934. else if(columnType == Types.NUMERIC) {
  935. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  936. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDecimal;
  937. row [(int)SCHEMA_TABLE.IsLong] = false;
  938. }
  939. else if(columnType == Types.REF) {
  940. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  941. row [(int)SCHEMA_TABLE.DataType] = typeof (java.sql.Ref);
  942. row [(int)SCHEMA_TABLE.IsLong] = true;
  943. }
  944. else if(columnType == Types.SMALLINT) {
  945. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  946. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfInt16;
  947. row [(int)SCHEMA_TABLE.IsLong] = false;
  948. }
  949. else if(columnType == Types.STRUCT) {
  950. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  951. row [(int)SCHEMA_TABLE.DataType] = typeof (java.sql.Struct);
  952. row [(int)SCHEMA_TABLE.IsLong] = false;
  953. }
  954. else if(columnType == Types.TIME) {
  955. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  956. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfTimespan;
  957. row [(int)SCHEMA_TABLE.IsLong] = false;
  958. }
  959. else if(columnType == Types.TIMESTAMP) {
  960. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  961. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfDateTime;
  962. row [(int)SCHEMA_TABLE.IsLong] = false;
  963. }
  964. else if(columnType == Types.TINYINT) {
  965. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  966. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByte;
  967. row [(int)SCHEMA_TABLE.IsLong] = false;
  968. }
  969. else if(columnType == Types.VARBINARY) {
  970. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  971. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfByteArray;
  972. row [(int)SCHEMA_TABLE.IsLong] = true;
  973. }
  974. else if(columnType == Types.VARCHAR) {
  975. // FIXME : specific for Microsoft SQl Server driver
  976. if (columnTypeName.Equals("sql_variant")) {
  977. row [(int)SCHEMA_TABLE.ProviderType] = DbType.Object;
  978. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfObject;
  979. row [(int)SCHEMA_TABLE.IsLong] = false;
  980. }
  981. else {
  982. row [(int)SCHEMA_TABLE.ProviderType] = GetProviderType(columnType);
  983. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;// (char[]);
  984. row [(int)SCHEMA_TABLE.IsLong] = false;//true;
  985. }
  986. }
  987. else if(columnType == -8 && columnTypeName.Equals("ROWID")) {
  988. // FIXME : specific for Oracle JDBC driver : OracleTypes.ROWID
  989. row [(int)SCHEMA_TABLE.ProviderType] = DbType.String;
  990. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfString;
  991. row [(int)SCHEMA_TABLE.IsLong] = false;
  992. }
  993. else {
  994. row [(int)SCHEMA_TABLE.ProviderType] = DbType.Object;
  995. row [(int)SCHEMA_TABLE.DataType] = DbTypes.TypeOfObject;
  996. row [(int)SCHEMA_TABLE.IsLong] = true;
  997. }
  998. SchemaTable.Rows.Add (row);
  999. }
  1000. }
  1001. catch (SQLException e) {
  1002. throw CreateException(e);
  1003. }
  1004. finally {
  1005. if (clonedConnection != null) {
  1006. clonedConnection.Close();
  1007. }
  1008. }
  1009. return SchemaTable;
  1010. }
  1011. private void InitKeyInfo(DataRow row, DatabaseMetaData dbMetaData, String catalog, String schema, String table) {
  1012. string column = (string)row [(int)SCHEMA_TABLE.BaseColumnName];
  1013. row [(int)SCHEMA_TABLE.IsUnique] = false;
  1014. row [(int)SCHEMA_TABLE.IsKey] = false;
  1015. row [(int)SCHEMA_TABLE.IsIdentity] = false;
  1016. row [(int)SCHEMA_TABLE.IsRowVersion] = false;
  1017. if ((_command.Behavior & CommandBehavior.KeyInfo) == 0)
  1018. return;
  1019. if(table == null || column == null || dbMetaData == null)
  1020. return;
  1021. ResultSet versionCol = dbMetaData.getVersionColumns(catalog, schema, table);
  1022. try {
  1023. while(versionCol.next()) {
  1024. if(versionCol.getString("COLUMN_NAME") == column) {
  1025. if (DatabaseMetaData__Finals.versionColumnPseudo == versionCol.getShort("PSEUDO_COLUMN")) {
  1026. row [(int)SCHEMA_TABLE.IsIdentity] = true;
  1027. row [(int)SCHEMA_TABLE.IsRowVersion] = true;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. finally {
  1033. versionCol.close();
  1034. }
  1035. ResultSet primaryKeys = dbMetaData.getPrimaryKeys(catalog,schema,table);
  1036. bool primaryKeyExists = false;
  1037. int columnCount = 0;
  1038. try {
  1039. while(primaryKeys.next()) {
  1040. columnCount++;
  1041. if(primaryKeys.getString("COLUMN_NAME") == column) {
  1042. row [(int)SCHEMA_TABLE.IsKey] = true;
  1043. primaryKeyExists = true;
  1044. }
  1045. }
  1046. // column constitutes a key by itself, so it should be marked as unique
  1047. if ((columnCount == 1) && (((bool)row [(int)SCHEMA_TABLE.IsKey]) == true)) {
  1048. row [(int)SCHEMA_TABLE.IsUnique] = true;
  1049. }
  1050. }
  1051. finally {
  1052. primaryKeys.close();
  1053. }
  1054. ResultSet indexInfoRes = dbMetaData.getIndexInfo(catalog,schema,table,true,false);
  1055. string currentIndexName = null;
  1056. columnCount = 0;
  1057. bool belongsToCurrentIndex = false;
  1058. bool atFirstIndex = true;
  1059. bool uniqueKeyExists = false;
  1060. try {
  1061. while(indexInfoRes.next()) {
  1062. if (indexInfoRes.getShort("TYPE") == DatabaseMetaData__Finals.tableIndexStatistic) {
  1063. // index of type tableIndexStatistic identifies table statistics - ignore it
  1064. continue;
  1065. }
  1066. uniqueKeyExists = true;
  1067. string iname = indexInfoRes.getString("INDEX_NAME");
  1068. if (currentIndexName == iname) {
  1069. // we're within the rows of the same index
  1070. columnCount++;
  1071. }
  1072. else {
  1073. // we jump to row of new index
  1074. if (belongsToCurrentIndex && columnCount == 1) {
  1075. // there is a constraint of type UNIQUE that applies only to this column
  1076. row [(int)SCHEMA_TABLE.IsUnique] = true;
  1077. }
  1078. if (currentIndexName != null) {
  1079. atFirstIndex = false;
  1080. }
  1081. currentIndexName = iname;
  1082. columnCount = 1;
  1083. belongsToCurrentIndex = false;
  1084. }
  1085. if(indexInfoRes.getString("COLUMN_NAME") == column) {
  1086. // FIXME : this will cause "spare" columns marked as IsKey. Needs future investigation.
  1087. // only the first index we met should be marked as a key
  1088. //if (atFirstIndex) {
  1089. row [(int)SCHEMA_TABLE.IsKey] = true;
  1090. //}
  1091. belongsToCurrentIndex = true;
  1092. }
  1093. }
  1094. // the column appears in the last index, which is single-column
  1095. if (belongsToCurrentIndex && columnCount == 1) {
  1096. // there is a constraint of type UNIQUE that applies only to this column
  1097. row [(int)SCHEMA_TABLE.IsUnique] = true;
  1098. }
  1099. }
  1100. finally {
  1101. indexInfoRes.close();
  1102. }
  1103. if(!primaryKeyExists && !uniqueKeyExists) {
  1104. ResultSet bestRowId = dbMetaData.getBestRowIdentifier(catalog, schema, table, DatabaseMetaData__Finals.bestRowTemporary, false);
  1105. try {
  1106. while(bestRowId.next()) {
  1107. if(bestRowId.getString("COLUMN_NAME") == column)
  1108. row [(int)SCHEMA_TABLE.IsKey] = true;
  1109. }
  1110. }
  1111. finally {
  1112. bestRowId.close();
  1113. }
  1114. }
  1115. }
  1116. protected static DataTable ConstructSchemaTable ()
  1117. {
  1118. Type booleanType = DbTypes.TypeOfBoolean;
  1119. Type stringType = DbTypes.TypeOfString;
  1120. Type intType = DbTypes.TypeOfInt32;
  1121. Type typeType = DbTypes.TypeOfType;
  1122. Type shortType = DbTypes.TypeOfInt16;
  1123. DataTable schemaTable = new DataTable ("SchemaTable");
  1124. schemaTable.Columns.Add ("ColumnName", stringType);
  1125. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  1126. schemaTable.Columns.Add ("ColumnSize", intType);
  1127. schemaTable.Columns.Add ("NumericPrecision", shortType);
  1128. schemaTable.Columns.Add ("NumericScale", shortType);
  1129. schemaTable.Columns.Add ("IsUnique", booleanType);
  1130. schemaTable.Columns.Add ("IsKey", booleanType);
  1131. schemaTable.Columns.Add ("BaseServerName", stringType);
  1132. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  1133. schemaTable.Columns.Add ("BaseColumnName", stringType);
  1134. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  1135. schemaTable.Columns.Add ("BaseTableName", stringType);
  1136. schemaTable.Columns.Add ("DataType", typeType);
  1137. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  1138. schemaTable.Columns.Add ("ProviderType", intType);
  1139. schemaTable.Columns.Add ("IsAliased", booleanType);
  1140. schemaTable.Columns.Add ("IsExpression", booleanType);
  1141. schemaTable.Columns.Add ("IsIdentity", booleanType);
  1142. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  1143. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  1144. schemaTable.Columns.Add ("IsHidden", booleanType);
  1145. schemaTable.Columns.Add ("IsLong", booleanType);
  1146. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  1147. return schemaTable;
  1148. }
  1149. #endregion // Methods
  1150. }
  1151. }