AbstractDataReader.cs 39 KB

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