DataContainer.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. namespace System.Data.Common
  26. {
  27. internal abstract class AbstractDataContainer
  28. {
  29. #region Fields
  30. BitArray _nullValues;
  31. System.Type _type;
  32. DataColumn _column;
  33. #endregion //Fields
  34. #region Properties
  35. internal abstract object this[int index] {
  36. get;
  37. set;
  38. }
  39. internal virtual int Capacity {
  40. get {
  41. return (_nullValues != null) ? _nullValues.Count : 0;
  42. }
  43. set {
  44. if (_nullValues == null) {
  45. _nullValues = new BitArray(value);
  46. }
  47. else {
  48. _nullValues.Length = value;
  49. }
  50. }
  51. }
  52. internal Type Type {
  53. get {
  54. return _type;
  55. }
  56. }
  57. protected DataColumn Column {
  58. get {
  59. return _column;
  60. }
  61. }
  62. #endregion //Properties
  63. #region Methods
  64. internal static AbstractDataContainer CreateInstance(Type type, DataColumn column)
  65. {
  66. AbstractDataContainer container;
  67. switch (Type.GetTypeCode(type)) {
  68. case TypeCode.Int16 :
  69. container = new Int16DataContainer();
  70. break;
  71. case TypeCode.Int32 :
  72. container = new Int32DataContainer();
  73. break;
  74. case TypeCode.Int64 :
  75. container = new Int64DataContainer();
  76. break;
  77. case TypeCode.String :
  78. container = new StringDataContainer();
  79. break;
  80. case TypeCode.Boolean:
  81. container = new BitDataContainer();
  82. break;
  83. case TypeCode.Byte :
  84. container = new ByteDataContainer();
  85. break;
  86. //case TypeCode.Char :
  87. case TypeCode.DateTime :
  88. container = new DateTimeDataContainer();
  89. break;
  90. //case TypeCode.Decimal :
  91. case TypeCode.Double :
  92. container = new DoubleDataContainer();
  93. break;
  94. //case TypeCode.SByte :
  95. case TypeCode.Single :
  96. container = new SingleDataContainer();
  97. break;
  98. //case TypeCode.UInt16 :
  99. //case TypeCode.UInt32 :
  100. //case TypeCode.UInt64 :
  101. default :
  102. container = new ObjectDataContainer();
  103. break;
  104. }
  105. container._type = type;
  106. container._column = column;
  107. return container;
  108. }
  109. internal bool IsNull(int index)
  110. {
  111. return (_nullValues != null) ? _nullValues[index] : true;
  112. }
  113. internal void SetNullBit(int index,bool isNull)
  114. {
  115. _nullValues[index] = isNull;
  116. }
  117. protected void SetNull(int index,bool isNull,bool isDbNull)
  118. {
  119. SetNullBit(index,isDbNull);
  120. // this method must be called after setting the value into value array
  121. // otherwise the dafault value will be overriden
  122. if ( isNull ) {
  123. // set the value to default
  124. CopyValue(Column.Table.DefaultValuesRowIndex,index);
  125. }
  126. }
  127. internal void FillValues(int fromIndex)
  128. {
  129. for(int i=0; i < Capacity; i++) {
  130. CopyValue(fromIndex,i);
  131. _nullValues[i] = _nullValues[fromIndex];
  132. }
  133. }
  134. internal virtual void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  135. {
  136. _nullValues[toIndex] = fromContainer._nullValues[fromIndex];
  137. }
  138. internal virtual void CopyValue(int fromIndex, int toIndex)
  139. {
  140. _nullValues[toIndex] = _nullValues[fromIndex];
  141. }
  142. internal virtual void SetItemFromDataRecord(int index, IDataRecord record, int field)
  143. {
  144. bool isDbNull = record.IsDBNull(field);
  145. SetNull(index,false,isDbNull);
  146. }
  147. protected bool CheckAndSetNull( int index, IDataRecord record, int field)
  148. {
  149. bool isDbNull = record.IsDBNull(field);
  150. SetNull(index,false,isDbNull);
  151. return isDbNull;
  152. }
  153. protected int CompareNulls(int index1, int index2)
  154. {
  155. bool null1 = IsNull(index1);
  156. bool null2 = IsNull(index2);
  157. if ( null1 ^ null2 ) {
  158. return null1 ? -1 : 1;
  159. }
  160. else {
  161. return 0;
  162. }
  163. }
  164. internal abstract int CompareValues(int index1, int index2);
  165. internal abstract long GetInt64(int index);
  166. #endregion //Methods
  167. sealed class Int16DataContainer : AbstractDataContainer
  168. {
  169. #region Fields
  170. short[] _values;
  171. #endregion //Fields
  172. #region Properties
  173. internal override object this[int index] {
  174. get {
  175. if (IsNull(index)) {
  176. return DBNull.Value;
  177. }
  178. else {
  179. return _values[index];
  180. }
  181. }
  182. set {
  183. bool isDbNull = (value == DBNull.Value);
  184. if (value == null || isDbNull) {
  185. SetValue(index,0);
  186. }
  187. else if( value is short ) {
  188. SetValue(index,(short)value);
  189. }
  190. else {
  191. SetValue(index,Convert.ToInt16(value));
  192. }
  193. SetNull(index,value == null,isDbNull);
  194. }
  195. }
  196. internal override int Capacity {
  197. set {
  198. base.Capacity = value;
  199. if (_values == null) {
  200. _values = new short[value];
  201. }
  202. else {
  203. short[] tmp = new short[value];
  204. Array.Copy(_values,0,tmp,0,_values.Length);
  205. _values = tmp;
  206. }
  207. }
  208. }
  209. #endregion //Properties
  210. #region Methods
  211. private void SetValue(int index, short value)
  212. {
  213. _values[index] = value;
  214. }
  215. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  216. {
  217. // if exception thrown, it should be caught
  218. // in the caller method
  219. if (!CheckAndSetNull ( index, record,field))
  220. SetValue(index,record.GetInt16(field));
  221. }
  222. internal override void CopyValue(int fromIndex, int toIndex)
  223. {
  224. base.CopyValue(fromIndex, toIndex);
  225. _values[toIndex] = _values[fromIndex];
  226. }
  227. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  228. {
  229. base.CopyValue(fromContainer, fromIndex, toIndex);
  230. _values[toIndex] = ((Int16DataContainer)fromContainer)._values[fromIndex];
  231. }
  232. internal override int CompareValues(int index1, int index2)
  233. {
  234. short s1 = _values[index1];
  235. short s2 = _values[index2];
  236. if ( s1 == 0 && s2 == 0 ) {
  237. int cn = CompareNulls(index1, index2);
  238. return cn;
  239. }
  240. bool b1 = IsNull(index1);
  241. bool b2 = IsNull(index2);
  242. if ( s1 == 0 && b1 ) {
  243. return -1;
  244. }
  245. if ( s2 == 0 && b2 ) {
  246. return 1;
  247. }
  248. if ( s1 <= s2 ) {
  249. return ( s1 != s2 ) ? -1 : 0;
  250. }
  251. return 1;
  252. }
  253. internal override long GetInt64(int index)
  254. {
  255. return (long) _values[index];
  256. }
  257. #endregion //Methods
  258. }
  259. sealed class Int32DataContainer : AbstractDataContainer
  260. {
  261. #region Fields
  262. int[] _values;
  263. #endregion //Fields
  264. #region Properties
  265. internal override object this[int index] {
  266. get {
  267. if (IsNull(index)) {
  268. return DBNull.Value;
  269. }
  270. else {
  271. return _values[index];
  272. }
  273. }
  274. set {
  275. bool isDbNull = (value == DBNull.Value);
  276. if (value == null || isDbNull) {
  277. SetValue(index,0);
  278. }
  279. else if( value is int ) {
  280. SetValue(index,(int)value);
  281. }
  282. else {
  283. SetValue(index,Convert.ToInt32(value));
  284. }
  285. SetNull(index,value == null,isDbNull);
  286. }
  287. }
  288. internal override int Capacity {
  289. set {
  290. base.Capacity = value;
  291. if (_values == null) {
  292. _values = new int[value];
  293. }
  294. else {
  295. int[] tmp = new int[value];
  296. Array.Copy(_values,0,tmp,0,_values.Length);
  297. _values = tmp;
  298. }
  299. }
  300. }
  301. #endregion //Properties
  302. #region Methods
  303. private void SetValue(int index, int value)
  304. {
  305. _values[index] = value;
  306. }
  307. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  308. {
  309. // if exception thrown, it should be caught
  310. // in the caller method
  311. if (!CheckAndSetNull ( index, record,field))
  312. SetValue(index,record.GetInt32(field));
  313. }
  314. internal override void CopyValue(int fromIndex, int toIndex)
  315. {
  316. base.CopyValue(fromIndex, toIndex);
  317. _values[toIndex] = _values[fromIndex];
  318. }
  319. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  320. {
  321. base.CopyValue(fromContainer, fromIndex, toIndex);
  322. _values[toIndex] = ((Int32DataContainer)fromContainer)._values[fromIndex];
  323. }
  324. internal override int CompareValues(int index1, int index2)
  325. {
  326. int i1 = _values[index1];
  327. int i2 = _values[index2];
  328. if ( i1 == 0 && i2 == 0 ) {
  329. int cn = CompareNulls(index1, index2);
  330. return cn;
  331. }
  332. bool b1 = IsNull(index1);
  333. bool b2 = IsNull(index2);
  334. if ( i1 == 0 && b1 ) {
  335. return -1;
  336. }
  337. if ( i2 == 0 && b2 ) {
  338. return 1;
  339. }
  340. if ( i1 <= i2 ) {
  341. return ( i1 != i2 ) ? -1 : 0;
  342. }
  343. return 1;
  344. }
  345. internal override long GetInt64(int index)
  346. {
  347. return (long) _values[index];
  348. }
  349. #endregion //Methods
  350. }
  351. sealed class Int64DataContainer : AbstractDataContainer
  352. {
  353. #region Fields
  354. long[] _values;
  355. #endregion //Fields
  356. #region Properties
  357. internal override object this[int index] {
  358. get {
  359. if (IsNull(index)) {
  360. return DBNull.Value;
  361. }
  362. else {
  363. return _values[index];
  364. }
  365. }
  366. set {
  367. bool isDbNull = (value == DBNull.Value);
  368. if (value == null || isDbNull) {
  369. SetValue(index,0);
  370. }
  371. else if( value is long ) {
  372. SetValue(index,(long)value);
  373. }
  374. else {
  375. SetValue(index,Convert.ToInt64(value));
  376. }
  377. SetNull(index,value == null,isDbNull);
  378. }
  379. }
  380. internal override int Capacity {
  381. set {
  382. base.Capacity = value;
  383. if (_values == null) {
  384. _values = new long[value];
  385. }
  386. else {
  387. long[] tmp = new long[value];
  388. Array.Copy(_values,0,tmp,0,_values.Length);
  389. _values = tmp;
  390. }
  391. }
  392. }
  393. #endregion //Properties
  394. #region Methods
  395. private void SetValue(int index, long value)
  396. {
  397. _values[index] = value;
  398. }
  399. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  400. {
  401. // if exception thrown, it should be caught
  402. // in the caller method
  403. if (!CheckAndSetNull ( index, record,field))
  404. SetValue(index,record.GetInt64(field));
  405. }
  406. internal override void CopyValue(int fromIndex, int toIndex)
  407. {
  408. base.CopyValue(fromIndex, toIndex);
  409. _values[toIndex] = _values[fromIndex];
  410. }
  411. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  412. {
  413. base.CopyValue(fromContainer, fromIndex, toIndex);
  414. _values[toIndex] = ((Int64DataContainer)fromContainer)._values[fromIndex];
  415. }
  416. internal override int CompareValues(int index1, int index2)
  417. {
  418. long l1 = _values[index1];
  419. long l2 = _values[index2];
  420. if ( l1 == 0 || l2 == 0 ) {
  421. int cn = CompareNulls(index1, index2);
  422. if (cn != 0) {
  423. return cn;
  424. }
  425. }
  426. if ( l1 <= l2 ) {
  427. return ( l1 != l2 ) ? -1 : 0;
  428. }
  429. return 1;
  430. }
  431. internal override long GetInt64(int index)
  432. {
  433. return _values[index];
  434. }
  435. #endregion //Methods
  436. }
  437. sealed class SingleDataContainer : AbstractDataContainer
  438. {
  439. #region Fields
  440. float[] _values;
  441. #endregion //Fields
  442. #region Properties
  443. internal override object this[int index] {
  444. get {
  445. if (IsNull(index)) {
  446. return DBNull.Value;
  447. }
  448. else {
  449. return _values[index];
  450. }
  451. }
  452. set {
  453. bool isDbNull = (value == DBNull.Value);
  454. if (value == null || isDbNull) {
  455. SetValue(index,0);
  456. }
  457. else if( value is float ) {
  458. SetValue(index,(float)value);
  459. }
  460. else {
  461. SetValue(index,Convert.ToSingle(value));
  462. }
  463. SetNull(index,value == null,isDbNull);
  464. }
  465. }
  466. internal override int Capacity {
  467. set {
  468. base.Capacity = value;
  469. if (_values == null) {
  470. _values = new float[value];
  471. }
  472. else {
  473. float[] tmp = new float[value];
  474. Array.Copy(_values,0,tmp,0,_values.Length);
  475. _values = tmp;
  476. }
  477. }
  478. }
  479. #endregion //Properties
  480. #region Methods
  481. private void SetValue(int index, float value)
  482. {
  483. _values[index] = value;
  484. }
  485. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  486. {
  487. // if exception thrown, it should be caught
  488. // in the caller method
  489. if (!CheckAndSetNull ( index, record,field))
  490. SetValue(index,record.GetFloat(field));
  491. }
  492. internal override void CopyValue(int fromIndex, int toIndex)
  493. {
  494. base.CopyValue(fromIndex, toIndex);
  495. _values[toIndex] = _values[fromIndex];
  496. }
  497. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  498. {
  499. base.CopyValue(fromContainer, fromIndex, toIndex);
  500. _values[toIndex] = ((SingleDataContainer)fromContainer)._values[fromIndex];
  501. }
  502. internal override int CompareValues(int index1, int index2)
  503. {
  504. float f1 = _values[index1];
  505. float f2 = _values[index2];
  506. if ( f1 == 0 || f2 == 0 ) {
  507. int cn = CompareNulls(index1, index2);
  508. if (cn != 0) {
  509. return cn;
  510. }
  511. }
  512. if ( f1 <= f2 ) {
  513. return ( f1 != f2 ) ? -1 : 0;
  514. }
  515. return 1;
  516. }
  517. internal override long GetInt64(int index)
  518. {
  519. return Convert.ToInt64(_values[index]);
  520. }
  521. #endregion //Methods
  522. }
  523. sealed class DoubleDataContainer : AbstractDataContainer
  524. {
  525. #region Fields
  526. double[] _values;
  527. #endregion //Fields
  528. #region Properties
  529. internal override object this[int index] {
  530. get {
  531. if (IsNull(index)) {
  532. return DBNull.Value;
  533. }
  534. else {
  535. return _values[index];
  536. }
  537. }
  538. set {
  539. bool isDbNull = (value == DBNull.Value);
  540. if (value == null || isDbNull) {
  541. SetValue(index,0);
  542. }
  543. else if( value is double ) {
  544. SetValue(index,(double)value);
  545. }
  546. else {
  547. SetValue(index,Convert.ToDouble(value));
  548. }
  549. SetNull(index,value == null,isDbNull);
  550. }
  551. }
  552. internal override int Capacity {
  553. set {
  554. base.Capacity = value;
  555. if (_values == null) {
  556. _values = new double[value];
  557. }
  558. else {
  559. double[] tmp = new double[value];
  560. Array.Copy(_values,0,tmp,0,_values.Length);
  561. _values = tmp;
  562. }
  563. }
  564. }
  565. #endregion //Properties
  566. #region Methods
  567. private void SetValue(int index, double value)
  568. {
  569. _values[index] = value;
  570. }
  571. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  572. {
  573. // if exception thrown, it should be caught
  574. // in the caller method
  575. if (!CheckAndSetNull ( index, record,field))
  576. SetValue(index,record.GetDouble(field));
  577. }
  578. internal override void CopyValue(int fromIndex, int toIndex)
  579. {
  580. base.CopyValue(fromIndex, toIndex);
  581. _values[toIndex] = _values[fromIndex];
  582. }
  583. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  584. {
  585. base.CopyValue(fromContainer, fromIndex, toIndex);
  586. _values[toIndex] = ((DoubleDataContainer)fromContainer)._values[fromIndex];
  587. }
  588. internal override int CompareValues(int index1, int index2)
  589. {
  590. double d1 = _values[index1];
  591. double d2 = _values[index2];
  592. if ( d1 == 0 || d2 == 0 ) {
  593. int cn = CompareNulls(index1, index2);
  594. if (cn != 0) {
  595. return cn;
  596. }
  597. }
  598. if ( d1 <= d2 ) {
  599. return ( d1 != d2 ) ? -1 : 0;
  600. }
  601. return 1;
  602. }
  603. internal override long GetInt64(int index)
  604. {
  605. return Convert.ToInt64(_values[index]);
  606. }
  607. #endregion //Methods
  608. }
  609. sealed class ByteDataContainer : AbstractDataContainer
  610. {
  611. #region Fields
  612. byte[] _values;
  613. #endregion //Fields
  614. #region Properties
  615. internal override object this[int index] {
  616. get {
  617. if (IsNull(index)) {
  618. return DBNull.Value;
  619. }
  620. else {
  621. return _values[index];
  622. }
  623. }
  624. set {
  625. bool isDbNull = (value == DBNull.Value);
  626. if (value == null || isDbNull) {
  627. SetValue(index,0);
  628. }
  629. else if( value is byte ) {
  630. SetValue(index,(byte)value);
  631. }
  632. else {
  633. SetValue(index,Convert.ToByte(value));
  634. }
  635. SetNull(index,value == null,isDbNull);
  636. }
  637. }
  638. internal override int Capacity {
  639. set {
  640. base.Capacity = value;
  641. if (_values == null) {
  642. _values = new byte[value];
  643. }
  644. else {
  645. byte[] tmp = new byte[value];
  646. Array.Copy(_values,0,tmp,0,_values.Length);
  647. _values = tmp;
  648. }
  649. }
  650. }
  651. #endregion //Properties
  652. #region Methods
  653. private void SetValue(int index, byte value)
  654. {
  655. _values[index] = value;
  656. }
  657. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  658. {
  659. // if exception thrown, it should be caught
  660. // in the caller method
  661. if (!CheckAndSetNull ( index, record,field))
  662. SetValue(index,record.GetByte(field));
  663. }
  664. internal override void CopyValue(int fromIndex, int toIndex)
  665. {
  666. base.CopyValue(fromIndex, toIndex);
  667. _values[toIndex] = _values[fromIndex];
  668. }
  669. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  670. {
  671. base.CopyValue(fromContainer, fromIndex, toIndex);
  672. _values[toIndex] = ((ByteDataContainer)fromContainer)._values[fromIndex];
  673. }
  674. internal override int CompareValues(int index1, int index2)
  675. {
  676. byte b1 = _values[index1];
  677. byte b2 = _values[index2];
  678. if ( b1 == 0 || b2 == 0 ) {
  679. int cn = CompareNulls(index1, index2);
  680. if (cn != 0) {
  681. return cn;
  682. }
  683. }
  684. if ( b1 <= b2 ) {
  685. return ( b1 != b2 ) ? -1 : 0;
  686. }
  687. return 1;
  688. }
  689. internal override long GetInt64(int index)
  690. {
  691. return (long) _values[index];
  692. }
  693. #endregion //Methods
  694. }
  695. sealed class BitDataContainer : AbstractDataContainer
  696. {
  697. #region Fields
  698. bool[] _values;
  699. #endregion //Fields
  700. #region Properties
  701. internal override object this[int index] {
  702. get {
  703. bool isNull = IsNull(index);
  704. if (isNull) {
  705. return DBNull.Value;
  706. }
  707. else {
  708. return _values[index];
  709. }
  710. }
  711. set {
  712. bool isDbNull = (value == DBNull.Value);
  713. if (value == null || isDbNull) {
  714. SetValue(index,false);
  715. }
  716. else if( value is bool ) {
  717. SetValue(index,(bool)value);
  718. }
  719. else {
  720. SetValue(index,Convert.ToBoolean(value));
  721. }
  722. SetNull(index,value == null,isDbNull);
  723. }
  724. }
  725. internal override int Capacity {
  726. set {
  727. base.Capacity = value;
  728. if (_values == null) {
  729. _values = new bool[value];
  730. }
  731. else {
  732. bool[] tmp = new bool[value];
  733. Array.Copy(_values,0,tmp,0,_values.Length);
  734. _values = tmp;
  735. }
  736. }
  737. }
  738. #endregion //Properties
  739. #region Methods
  740. private void SetValue(int index, bool value)
  741. {
  742. _values[index] = value;
  743. }
  744. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  745. {
  746. // if exception thrown, it should be caught
  747. // in the caller method
  748. if (!CheckAndSetNull ( index, record,field))
  749. SetValue(index,record.GetBoolean(field));
  750. }
  751. internal override void CopyValue(int fromIndex, int toIndex)
  752. {
  753. base.CopyValue(fromIndex, toIndex);
  754. _values[toIndex] = _values[fromIndex];
  755. }
  756. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  757. {
  758. base.CopyValue(fromContainer, fromIndex, toIndex);
  759. _values[toIndex] = ((BitDataContainer)fromContainer)._values[fromIndex];
  760. }
  761. internal override int CompareValues(int index1, int index2)
  762. {
  763. bool b1 = _values[index1];
  764. bool b2 = _values[index2];
  765. if ( b1 ^ b2 ) {
  766. return b1 ? 1 : -1;
  767. }
  768. if ( b1 ) {
  769. return 0;
  770. }
  771. return CompareNulls(index1, index2);
  772. }
  773. internal override long GetInt64(int index)
  774. {
  775. return Convert.ToInt64(_values[index]);
  776. }
  777. #endregion //Methods
  778. }
  779. class ObjectDataContainer : AbstractDataContainer
  780. {
  781. #region Fields
  782. object[] _values;
  783. #endregion //Fields
  784. #region Properties
  785. internal override object this[int index] {
  786. get {
  787. return _values[index];
  788. }
  789. set {
  790. SetValue(index,value);
  791. SetNull(index,value == null,value == DBNull.Value);
  792. }
  793. }
  794. internal override int Capacity {
  795. set {
  796. base.Capacity = value;
  797. if (_values == null) {
  798. _values = new object[value];
  799. }
  800. else {
  801. object[] tmp = new object[value];
  802. Array.Copy(_values,0,tmp,0,_values.Length);
  803. _values = tmp;
  804. }
  805. }
  806. }
  807. #endregion //Properties
  808. #region Methods
  809. protected virtual void SetValue(int index, object value)
  810. {
  811. if(value == null) {
  812. value = Column.DefaultValue;
  813. }
  814. _values[index] = value;
  815. }
  816. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  817. {
  818. // if exception thrown, it should be caught
  819. // in the caller metho
  820. SetValue(index,record.GetValue(field));
  821. base.SetItemFromDataRecord(index,record,field);
  822. }
  823. internal override void CopyValue(int fromIndex, int toIndex)
  824. {
  825. base.CopyValue(fromIndex, toIndex);
  826. _values[toIndex] = _values[fromIndex];
  827. }
  828. internal override void CopyValue(AbstractDataContainer fromContainer, int fromIndex, int toIndex)
  829. {
  830. base.CopyValue(fromContainer, fromIndex, toIndex);
  831. _values[toIndex] = ((ObjectDataContainer)fromContainer)._values[fromIndex];
  832. }
  833. internal override int CompareValues(int index1, int index2)
  834. {
  835. object obj1 = _values[index1];
  836. object obj2 = _values[index2];
  837. if(obj1 == obj2) {
  838. return 0;
  839. }
  840. else if (obj1 is IComparable) {
  841. try {
  842. return ((IComparable)obj1).CompareTo(obj2);
  843. }
  844. catch {
  845. //just suppress
  846. }
  847. if (obj2 is IComparable) {
  848. obj2 = Convert.ChangeType(obj2, Type.GetTypeCode(obj1.GetType()));
  849. return ((IComparable)obj1).CompareTo(obj2);
  850. }
  851. }
  852. return String.Compare(obj1.ToString(), obj2.ToString());
  853. }
  854. internal override long GetInt64(int index)
  855. {
  856. return Convert.ToInt64(_values[index]);
  857. }
  858. #endregion //Methods
  859. }
  860. sealed class StringDataContainer : ObjectDataContainer
  861. {
  862. #region Methods
  863. private void SetValue(int index, string value)
  864. {
  865. if (value != null && Column.MaxLength >= 0 && Column.MaxLength < value.Length ) {
  866. throw new ArgumentException("Cannot set column '" + Column.ColumnName + "' to '" + value + "'. The value violates the MaxLength limit of this column.");
  867. }
  868. base.SetValue(index,value);
  869. }
  870. protected override void SetValue(int index, object value)
  871. {
  872. if ( value != null && value != DBNull.Value ) {
  873. if ( value is string ) {
  874. SetValue(index, (string) value);
  875. }
  876. else {
  877. SetValue(index, Convert.ToString(value));
  878. }
  879. return;
  880. }
  881. base.SetValue(index, value);
  882. }
  883. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  884. {
  885. // if exception thrown, it should be caught
  886. // in the caller method
  887. if (!CheckAndSetNull ( index, record,field))
  888. SetValue(index,record.GetString(field));
  889. }
  890. internal override int CompareValues(int index1, int index2)
  891. {
  892. bool isNull1 = IsNull(index1);
  893. bool isNull2 = IsNull(index2);
  894. if (isNull1) {
  895. return isNull2 ? 0 : -1;
  896. }
  897. else {
  898. if (isNull2) {
  899. return 1;
  900. }
  901. }
  902. return String.Compare((string)this[index1], (string)this[index2], !Column.Table.CaseSensitive);
  903. }
  904. #endregion //Methods
  905. }
  906. sealed class DateTimeDataContainer : ObjectDataContainer
  907. {
  908. #region Methods
  909. protected override void SetValue(int index, object value)
  910. {
  911. if ( value != null && value != DBNull.Value && !(value is DateTime)) {
  912. value = Convert.ToDateTime(value);
  913. }
  914. base.SetValue(index,value);
  915. }
  916. internal override void SetItemFromDataRecord(int index, IDataRecord record, int field)
  917. {
  918. // if exception thrown, it should be caught
  919. // in the caller method
  920. if (!CheckAndSetNull(index,record,field))
  921. base.SetValue(index,record.GetDateTime(field));
  922. }
  923. internal override int CompareValues(int index1, int index2)
  924. {
  925. bool isNull1 = IsNull(index1);
  926. bool isNull2 = IsNull(index2);
  927. if (isNull1) {
  928. return isNull2 ? 0 : -1;
  929. }
  930. else {
  931. if (isNull2) {
  932. return 1;
  933. }
  934. }
  935. return DateTime.Compare((DateTime)this[index1], (DateTime)this[index2]);
  936. }
  937. #endregion //Methods
  938. }
  939. }
  940. }