AbstractDataReader.cs 39 KB

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