2
0

AbstractDataReader.cs 38 KB

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