SqlMetaData.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. //
  2. // Microsoft.SqlServer.Server.SqlMetaData
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2003
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Data;
  34. using System.Threading;
  35. using System.Data.SqlTypes;
  36. namespace Microsoft.SqlServer.Server {
  37. public sealed class SqlMetaData
  38. {
  39. #region Fields
  40. SqlCompareOptions compareOptions = SqlCompareOptions.None;
  41. string databaseName = null;
  42. long _localeId = 0L;
  43. long maxLength = 0L;
  44. string name;
  45. byte precision = 10;
  46. byte scale = 0;
  47. string owningSchema = null;
  48. string objectName = null;
  49. SqlDbType _sqlDbType = SqlDbType.NVarChar;
  50. DbType _dbType = DbType.String;
  51. Type type = typeof (string);
  52. #endregion // Fields
  53. #region Constructors
  54. public SqlMetaData (string name, SqlDbType dbType)
  55. {
  56. if (name == null)
  57. throw new ArgumentNullException ("name can not be null");
  58. switch (dbType) {
  59. case SqlDbType.Bit:
  60. maxLength = 1;
  61. precision = 1;
  62. scale = 0;
  63. _localeId = 0;
  64. compareOptions = SqlCompareOptions.None;
  65. _dbType = DbType.Boolean;
  66. type = typeof (bool);
  67. break;
  68. case SqlDbType.BigInt:
  69. maxLength = 8;
  70. precision = 19;
  71. scale = 0;
  72. _localeId = 0;
  73. compareOptions = SqlCompareOptions.None;
  74. _dbType = DbType.Int64;
  75. type = typeof (long);
  76. break;
  77. case SqlDbType.DateTime:
  78. maxLength = 8;
  79. precision = 23;
  80. scale = 3;
  81. _localeId = 0;
  82. compareOptions = SqlCompareOptions.None;
  83. _dbType = DbType.DateTime;
  84. type = typeof (DateTime);
  85. break;
  86. case SqlDbType.Decimal:
  87. maxLength = 9;
  88. precision = 18;
  89. scale = 0;
  90. _localeId = 0;
  91. compareOptions = SqlCompareOptions.None;
  92. _dbType = DbType.Decimal;
  93. type = typeof (decimal);
  94. break;
  95. case SqlDbType.Float:
  96. maxLength = 8;
  97. precision = 53;
  98. scale = 0;
  99. _localeId = 0;
  100. compareOptions = SqlCompareOptions.None;
  101. _dbType = DbType.Double;
  102. type = typeof (float);
  103. break;
  104. case SqlDbType.Int:
  105. maxLength = 4;
  106. precision = 10;
  107. scale = 0;
  108. _localeId = 0;
  109. compareOptions = SqlCompareOptions.None;
  110. _dbType = DbType.Int32;
  111. type = typeof (int);
  112. break;
  113. case SqlDbType.Money:
  114. maxLength = 8;
  115. precision = 19;
  116. scale = 4;
  117. _localeId = 0;
  118. compareOptions = SqlCompareOptions.None;
  119. _dbType = DbType.Currency;
  120. type = typeof (double);
  121. break;
  122. /*
  123. case SqlDbType.Numeric:
  124. maxLength = ;
  125. precision = ;
  126. scale = ;
  127. localeId = 0;
  128. compareOptions = SqlCompareOptions.None;
  129. break;
  130. */
  131. case SqlDbType.SmallDateTime:
  132. maxLength = 4;
  133. precision = 16;
  134. scale = 0;
  135. _localeId = 0;
  136. compareOptions = SqlCompareOptions.None;
  137. _dbType = DbType.DateTime;
  138. type = typeof (DateTime);
  139. break;
  140. case SqlDbType.SmallInt:
  141. maxLength = 2;
  142. precision = 5;
  143. scale = 0;
  144. _localeId = 0;
  145. compareOptions = SqlCompareOptions.None;
  146. _dbType = DbType.Int16;
  147. type = typeof (short);
  148. break;
  149. case SqlDbType.SmallMoney:
  150. maxLength = 4;
  151. precision = 10;
  152. scale = 4;
  153. _localeId = 0;
  154. compareOptions = SqlCompareOptions.None;
  155. _dbType = DbType.Currency;
  156. type = typeof (double);
  157. break;
  158. case SqlDbType.Timestamp:
  159. maxLength = 8;
  160. precision = 0;
  161. scale = 0;
  162. _localeId = 0;
  163. compareOptions = SqlCompareOptions.None;
  164. _dbType = DbType.DateTime;
  165. type = typeof (DateTime);
  166. break;
  167. case SqlDbType.TinyInt:
  168. maxLength = 1;
  169. precision = 3;
  170. scale = 0;
  171. _localeId = 0;
  172. compareOptions = SqlCompareOptions.None;
  173. _dbType = DbType.Int16;
  174. type = typeof (short);
  175. break;
  176. case SqlDbType.UniqueIdentifier:
  177. maxLength = 16;
  178. precision = 0;
  179. scale = 0;
  180. _localeId = 0;
  181. compareOptions = SqlCompareOptions.None;
  182. _dbType = DbType.Guid;
  183. type = typeof (Guid);
  184. break;
  185. case SqlDbType.Xml:
  186. maxLength = -1;
  187. precision = 0;
  188. scale = 0;
  189. _localeId = 0;
  190. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  191. _dbType = DbType.Xml;
  192. type = typeof (string);
  193. break;
  194. default:
  195. throw new ArgumentException ("SqlDbType not supported");
  196. }
  197. this.name = name;
  198. this._sqlDbType = dbType;
  199. }
  200. public SqlMetaData (string name, SqlDbType dbType, long maxLength)
  201. {
  202. if (name == null)
  203. throw new ArgumentNullException ("name can not be null");
  204. switch (dbType) {
  205. case SqlDbType.Binary:
  206. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  207. _dbType = DbType.Binary;
  208. type = typeof (byte []);
  209. break;
  210. case SqlDbType.Char:
  211. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  212. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  213. _dbType = DbType.AnsiStringFixedLength;
  214. type = typeof (string);
  215. break;
  216. case SqlDbType.Image:
  217. maxLength = -1;
  218. precision = 0;
  219. scale = 0;
  220. _localeId = 0;
  221. compareOptions = SqlCompareOptions.None;
  222. _dbType = DbType.Binary;
  223. type = typeof (byte []);
  224. break;
  225. case SqlDbType.NChar:
  226. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  227. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  228. _dbType = DbType.String;
  229. type = typeof (string);
  230. break;
  231. case SqlDbType.NText:
  232. maxLength = -1;
  233. precision = 0;
  234. scale = 0;
  235. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  236. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  237. _dbType = DbType.String;
  238. type = typeof (string);
  239. break;
  240. case SqlDbType.NVarChar:
  241. maxLength = -1;
  242. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  243. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  244. _dbType = DbType.String;
  245. type = typeof (string);
  246. break;
  247. case SqlDbType.Text:
  248. maxLength = -1;
  249. precision = 0;
  250. scale = 0;
  251. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  252. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  253. _dbType = DbType.String;
  254. type = typeof (char []);
  255. break;
  256. case SqlDbType.VarBinary:
  257. maxLength = -1;
  258. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  259. _dbType = DbType.Binary;
  260. type = typeof (byte []);
  261. break;
  262. case SqlDbType.VarChar:
  263. maxLength = -1;
  264. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  265. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  266. _dbType = DbType.String;
  267. type = typeof (char []);
  268. break;
  269. default:
  270. throw new ArgumentException ("SqlDbType not supported");
  271. }
  272. this.maxLength = maxLength;
  273. this.name = name;
  274. this._sqlDbType = dbType;
  275. }
  276. [MonoTODO]
  277. public SqlMetaData (string name, SqlDbType dbType, Type userDefinedType)
  278. {
  279. if (name == null)
  280. throw new ArgumentNullException ("name can not be null");
  281. switch (dbType) {
  282. case SqlDbType.Udt:
  283. maxLength = -1;
  284. precision = 0;
  285. scale = 0;
  286. _localeId = 0;
  287. compareOptions = SqlCompareOptions.None;
  288. _dbType = DbType.Guid;
  289. type = typeof (Guid);
  290. break;
  291. default:
  292. throw new ArgumentException ("SqlDbType not supported");
  293. }
  294. this.name = name;
  295. // FIXME:
  296. //this.sqlDbType = userDefinedType;
  297. throw new NotImplementedException ();
  298. }
  299. public SqlMetaData (string name, SqlDbType dbType, byte precision, byte scale)
  300. {
  301. if (name == null)
  302. throw new ArgumentNullException ("name can not be null");
  303. switch (dbType) {
  304. case SqlDbType.Decimal:
  305. maxLength = 9;
  306. this.precision = precision;
  307. this.scale = scale;
  308. _localeId = 0;
  309. compareOptions = SqlCompareOptions.None;
  310. _dbType = DbType.Decimal;
  311. type = typeof (decimal);
  312. break;
  313. default:
  314. throw new ArgumentException ("SqlDbType not supported");
  315. }
  316. this.name = name;
  317. this._sqlDbType = dbType;
  318. }
  319. public SqlMetaData (string name, SqlDbType dbType, long maxLength, long locale, SqlCompareOptions compareOptions)
  320. {
  321. if (name == null)
  322. throw new ArgumentNullException ("name can not be null");
  323. switch (dbType) {
  324. case SqlDbType.Char:
  325. _dbType = DbType.AnsiStringFixedLength;
  326. type = typeof (char []);
  327. break;
  328. case SqlDbType.NChar:
  329. _dbType = DbType.StringFixedLength;
  330. type = typeof (char []);
  331. break;
  332. case SqlDbType.NText:
  333. case SqlDbType.NVarChar:
  334. _dbType = DbType.String;
  335. type = typeof (string);
  336. break;
  337. case SqlDbType.Text:
  338. case SqlDbType.VarChar:
  339. _dbType = DbType.AnsiString;
  340. type = typeof (char []);
  341. break;
  342. default:
  343. throw new ArgumentException ("SqlDbType not supported");
  344. }
  345. this.compareOptions = compareOptions;
  346. this._localeId = locale;
  347. this.maxLength = maxLength;
  348. this.name = name;
  349. this._sqlDbType = dbType;
  350. }
  351. public SqlMetaData (string name, SqlDbType dbType, string database, string owningSchema, string objectName)
  352. {
  353. if ((name == null || objectName == null) && database != null && owningSchema != null)
  354. throw new ArgumentNullException ("name can not be null");
  355. switch (dbType) {
  356. case SqlDbType.Xml:
  357. maxLength = -1;
  358. precision = 0;
  359. scale = 0;
  360. _localeId = 0;
  361. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  362. _dbType = DbType.String;
  363. type = typeof (string);
  364. break;
  365. default:
  366. throw new ArgumentException ("SqlDbType not supported");
  367. }
  368. this.name = name;
  369. this._sqlDbType = dbType;
  370. databaseName = database;
  371. this.owningSchema = owningSchema;
  372. this.objectName = objectName;
  373. }
  374. public SqlMetaData (string name, SqlDbType dbType, long maxLength, byte precision,
  375. byte scale, long locale, SqlCompareOptions compareOptions,
  376. Type userDefinedType)
  377. {
  378. if (name == null)
  379. throw new ArgumentNullException ("name can not be null");
  380. this.compareOptions = compareOptions;
  381. this._localeId = locale;
  382. this.maxLength = maxLength;
  383. this.precision = precision;
  384. this.scale = scale;
  385. switch (dbType) {
  386. case SqlDbType.Bit:
  387. maxLength = 1;
  388. precision = 1;
  389. scale = 0;
  390. locale = 0;
  391. compareOptions = SqlCompareOptions.None;
  392. _dbType = DbType.Boolean;
  393. type = typeof (bool);
  394. break;
  395. case SqlDbType.BigInt:
  396. maxLength = 8;
  397. precision = 19;
  398. scale = 0;
  399. locale = 0;
  400. compareOptions = SqlCompareOptions.None;
  401. _dbType = DbType.Int64;
  402. type = typeof (long);
  403. break;
  404. case SqlDbType.DateTime:
  405. maxLength = 8;
  406. precision = 23;
  407. scale = 3;
  408. locale = 0;
  409. compareOptions = SqlCompareOptions.None;
  410. _dbType = DbType.DateTime;
  411. type = typeof (DateTime);
  412. break;
  413. case SqlDbType.Decimal:
  414. maxLength = 9;
  415. precision = 18;
  416. scale = 0;
  417. locale = 0;
  418. compareOptions = SqlCompareOptions.None;
  419. _dbType = DbType.Decimal;
  420. type = typeof (decimal);
  421. break;
  422. case SqlDbType.Float:
  423. maxLength = 8;
  424. precision = 53;
  425. scale = 0;
  426. locale = 0;
  427. compareOptions = SqlCompareOptions.None;
  428. _dbType = DbType.Decimal;
  429. type = typeof (float);
  430. break;
  431. case SqlDbType.Image:
  432. maxLength = -1;
  433. precision = 0;
  434. scale = 0;
  435. locale = 0;
  436. compareOptions = SqlCompareOptions.None;
  437. _dbType = DbType.Binary;
  438. type = typeof (byte []);
  439. break;
  440. case SqlDbType.Int:
  441. maxLength = 4;
  442. precision = 10;
  443. scale = 0;
  444. locale = 0;
  445. compareOptions = SqlCompareOptions.None;
  446. _dbType = DbType.Int32;
  447. type = typeof (int);
  448. break;
  449. case SqlDbType.Money:
  450. maxLength = 8;
  451. precision = 19;
  452. scale = 4;
  453. locale = 0;
  454. compareOptions = SqlCompareOptions.None;
  455. _dbType = DbType.Currency;
  456. type = typeof (decimal);
  457. break;
  458. case SqlDbType.NText:
  459. maxLength = -1;
  460. precision = 0;
  461. scale = 0;
  462. locale = Thread.CurrentThread.CurrentCulture.LCID;
  463. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  464. _dbType = DbType.String;
  465. type = typeof (string);
  466. break;
  467. /*
  468. case SqlDbType.Numeric:
  469. maxLength = ;
  470. precision = ;
  471. scale = ;
  472. localeId = 0;
  473. compareOptions = SqlCompareOptions.None;
  474. break;
  475. */
  476. case SqlDbType.Real:
  477. maxLength = 4;
  478. precision = 24;
  479. scale = 0;
  480. locale = 0;
  481. compareOptions = SqlCompareOptions.None;
  482. _dbType = DbType.Single;
  483. type = typeof (Single);
  484. break;
  485. case SqlDbType.SmallDateTime:
  486. maxLength = 4;
  487. precision = 16;
  488. scale = 0;
  489. locale = 0;
  490. compareOptions = SqlCompareOptions.None;
  491. _dbType = DbType.DateTime;
  492. type = typeof (DateTime);
  493. break;
  494. case SqlDbType.SmallInt:
  495. maxLength = 2;
  496. precision = 5;
  497. scale = 0;
  498. locale = 0;
  499. compareOptions = SqlCompareOptions.None;
  500. _dbType = DbType.Int16;
  501. type = typeof (short);
  502. break;
  503. case SqlDbType.SmallMoney:
  504. maxLength = 4;
  505. precision = 10;
  506. scale = 4;
  507. locale = 0;
  508. compareOptions = SqlCompareOptions.None;
  509. _dbType = DbType.Currency;
  510. type = typeof (decimal);
  511. break;
  512. case SqlDbType.Text:
  513. maxLength = -1;
  514. precision = 0;
  515. scale = 0;
  516. locale = Thread.CurrentThread.CurrentCulture.LCID;
  517. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  518. _dbType = DbType.AnsiString;
  519. type = typeof (char []);
  520. break;
  521. case SqlDbType.Timestamp:
  522. maxLength = 8;
  523. precision = 0;
  524. scale = 0;
  525. locale = 0;
  526. compareOptions = SqlCompareOptions.None;
  527. _dbType = DbType.Byte;
  528. type = typeof (byte []);
  529. break;
  530. case SqlDbType.TinyInt:
  531. maxLength = 1;
  532. precision = 3;
  533. scale = 0;
  534. locale = 0;
  535. compareOptions = SqlCompareOptions.None;
  536. _dbType = DbType.Int16;
  537. type = typeof (short);
  538. break;
  539. case SqlDbType.UniqueIdentifier:
  540. maxLength = 16;
  541. precision = 0;
  542. scale = 0;
  543. locale = 0;
  544. compareOptions = SqlCompareOptions.None;
  545. _dbType = DbType.Guid;
  546. type = typeof (Guid);
  547. break;
  548. case SqlDbType.Udt:
  549. maxLength = -1;
  550. precision = 0;
  551. scale = 0;
  552. locale = 0;
  553. compareOptions = SqlCompareOptions.None;
  554. _dbType = DbType.Object;
  555. type = typeof (object);
  556. break;
  557. case SqlDbType.Variant:
  558. maxLength = 8016;
  559. precision = 0;
  560. scale = 0;
  561. locale = 0;
  562. compareOptions = SqlCompareOptions.None;
  563. _dbType = DbType.Object;
  564. type = typeof (object);
  565. break;
  566. case SqlDbType.Xml:
  567. maxLength = -1;
  568. precision = 0;
  569. scale = 0;
  570. locale = 0;
  571. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  572. _dbType = DbType.Xml;
  573. type = typeof (string);
  574. break;
  575. default:
  576. /*
  577. if (typeof (DbType.Row) == typeof (userDefinedType)) {
  578. // FIXME:
  579. // maxLength = Number of columns;
  580. precision = 0;
  581. scale = 0;
  582. localeId = 0;
  583. compareOptions = SqlCompareOptions.None;
  584. } else
  585. */
  586. throw new ArgumentException ("SqlDbType not supported");
  587. }
  588. this.name = name;
  589. this._sqlDbType = dbType;
  590. }
  591. #endregion // Constructors
  592. #region Properties
  593. public SqlCompareOptions CompareOptions {
  594. get { return compareOptions; }
  595. }
  596. public DbType DbType {
  597. get { return _dbType; }
  598. }
  599. public long LocaleId {
  600. get { return _localeId; }
  601. }
  602. public static long Max {
  603. get { return -1; }
  604. }
  605. public long MaxLength {
  606. get { return maxLength; }
  607. }
  608. public string Name {
  609. get { return name; }
  610. }
  611. public byte Precision {
  612. get { return precision; }
  613. }
  614. public byte Scale {
  615. get { return scale; }
  616. }
  617. public SqlDbType SqlDbType {
  618. get { return _sqlDbType; }
  619. }
  620. public string XmlSchemaCollectionDatabase {
  621. get { return databaseName; }
  622. }
  623. public string XmlSchemaCollectionName {
  624. get { return objectName; }
  625. }
  626. public string XmlSchemaCollectionOwningSchema {
  627. get { return owningSchema; }
  628. }
  629. [MonoTODO]
  630. public string TypeName {
  631. get { throw new NotImplementedException (); }
  632. }
  633. #endregion // Properties
  634. #region Methods
  635. public bool Adjust (bool value)
  636. {
  637. if (type != typeof (bool))
  638. throw new ArgumentException ("Value does not match the SqlMetaData type");
  639. return value;
  640. }
  641. public byte Adjust (byte value)
  642. {
  643. if (type != typeof (byte))
  644. throw new ArgumentException ("Value does not match the SqlMetaData type");
  645. return value;
  646. }
  647. public byte[] Adjust (byte[] value)
  648. {
  649. if (type != typeof (byte []))
  650. throw new ArgumentException ("Value does not match the SqlMetaData type");
  651. return value;
  652. }
  653. public char Adjust (char value)
  654. {
  655. if (type != typeof (char))
  656. throw new ArgumentException ("Value does not match the SqlMetaData type");
  657. return value;
  658. }
  659. public char[] Adjust (char[] value)
  660. {
  661. if (type != typeof (char []))
  662. throw new ArgumentException ("Value does not match the SqlMetaData type");
  663. return value;
  664. }
  665. public DateTime Adjust (DateTime value)
  666. {
  667. if (type != typeof (DateTime))
  668. throw new ArgumentException ("Value does not match the SqlMetaData type");
  669. return value;
  670. }
  671. public decimal Adjust (decimal value)
  672. {
  673. if (type != typeof (decimal))
  674. throw new ArgumentException ("Value does not match the SqlMetaData type");
  675. return value;
  676. }
  677. public double Adjust (double value)
  678. {
  679. if (type != typeof (double))
  680. throw new ArgumentException ("Value does not match the SqlMetaData type");
  681. return value;
  682. }
  683. public Guid Adjust (Guid value)
  684. {
  685. if (type != typeof (Guid))
  686. throw new ArgumentException ("Value does not match the SqlMetaData type");
  687. return value;
  688. }
  689. public short Adjust (short value)
  690. {
  691. if (type != typeof (short))
  692. throw new ArgumentException ("Value does not match the SqlMetaData type");
  693. return value;
  694. }
  695. public int Adjust (int value)
  696. {
  697. if (type != typeof (int))
  698. throw new ArgumentException ("Value does not match the SqlMetaData type");
  699. return value;
  700. }
  701. public long Adjust (long value)
  702. {
  703. if (type != typeof (long))
  704. throw new ArgumentException ("Value does not match the SqlMetaData type");
  705. return value;
  706. }
  707. public object Adjust (object value)
  708. {
  709. if (type != typeof (object))
  710. throw new ArgumentException ("Value does not match the SqlMetaData type");
  711. return value;
  712. }
  713. public float Adjust (float value)
  714. {
  715. if (type != typeof (float))
  716. throw new ArgumentException ("Value does not match the SqlMetaData type");
  717. return value;
  718. }
  719. public SqlBinary Adjust (SqlBinary value)
  720. {
  721. if (type != typeof (byte []))
  722. throw new ArgumentException ("Value does not match the SqlMetaData type");
  723. return value;
  724. }
  725. public SqlBoolean Adjust (SqlBoolean value)
  726. {
  727. if (type != typeof (bool))
  728. throw new ArgumentException ("Value does not match the SqlMetaData type");
  729. return value;
  730. }
  731. public SqlByte Adjust (SqlByte value)
  732. {
  733. if (type != typeof (byte))
  734. throw new ArgumentException ("Value does not match the SqlMetaData type");
  735. return value;
  736. }
  737. public SqlBytes Adjust (SqlBytes value)
  738. {
  739. if (type != typeof (byte []))
  740. throw new ArgumentException ("Value does not match the SqlMetaData type");
  741. return value;
  742. }
  743. public SqlChars Adjust (SqlChars value)
  744. {
  745. if (type != typeof (char []))
  746. throw new ArgumentException ("Value does not match the SqlMetaData type");
  747. return value;
  748. }
  749. public SqlDateTime Adjust (SqlDateTime value)
  750. {
  751. if (type != typeof (DateTime))
  752. throw new ArgumentException ("Value does not match the SqlMetaData type");
  753. return value;
  754. }
  755. public SqlDecimal Adjust (SqlDecimal value)
  756. {
  757. if (type != typeof (decimal))
  758. throw new ArgumentException ("Value does not match the SqlMetaData type");
  759. return value;
  760. }
  761. public SqlDouble Adjust (SqlDouble value)
  762. {
  763. if (type != typeof (double))
  764. throw new ArgumentException ("Value does not match the SqlMetaData type");
  765. return value;
  766. }
  767. public SqlGuid Adjust (SqlGuid value)
  768. {
  769. if (type != typeof (Guid))
  770. throw new ArgumentException ("Value does not match the SqlMetaData type");
  771. return value;
  772. }
  773. public SqlInt16 Adjust (SqlInt16 value)
  774. {
  775. if (type != typeof (short))
  776. throw new ArgumentException ("Value does not match the SqlMetaData type");
  777. return value;
  778. }
  779. public SqlInt32 Adjust (SqlInt32 value)
  780. {
  781. if (type != typeof (int))
  782. throw new ArgumentException ("Value does not match the SqlMetaData type");
  783. return value;
  784. }
  785. public SqlInt64 Adjust (SqlInt64 value)
  786. {
  787. if (type != typeof (long))
  788. throw new ArgumentException ("Value does not match the SqlMetaData type");
  789. return value;
  790. }
  791. public SqlMoney Adjust (SqlMoney value)
  792. {
  793. if (type != typeof (decimal))
  794. throw new ArgumentException ("Value does not match the SqlMetaData type");
  795. return value;
  796. }
  797. public SqlSingle Adjust (SqlSingle value)
  798. {
  799. if (type != typeof (Single))
  800. throw new ArgumentException ("Value does not match the SqlMetaData type");
  801. return value;
  802. }
  803. public SqlString Adjust (SqlString value)
  804. {
  805. if (type != typeof (string))
  806. throw new ArgumentException ("Value does not match the SqlMetaData type");
  807. return value;
  808. }
  809. public string Adjust (string value)
  810. {
  811. if (type != typeof (string))
  812. throw new ArgumentException ("Value does not match the SqlMetaData type");
  813. return value;
  814. }
  815. public static SqlMetaData InferFromValue (object value, string name)
  816. {
  817. if (name == null)
  818. throw new ArgumentNullException ("name can not be null");
  819. if (value == null)
  820. throw new ArgumentException ("value can not be null");
  821. SqlMetaData sqlMetaData = null;
  822. switch (value.GetType ().ToString ()) {
  823. case "System.Boolean":
  824. sqlMetaData = new SqlMetaData (name, SqlDbType.Bit);
  825. break;
  826. case "System.Byte":
  827. sqlMetaData = new SqlMetaData (name, SqlDbType.Binary);
  828. break;
  829. case "System.Byte[]":
  830. sqlMetaData = new SqlMetaData (name, SqlDbType.VarBinary);
  831. break;
  832. case "System.Char":
  833. sqlMetaData = new SqlMetaData (name, SqlDbType.Char);
  834. break;
  835. case "System.Char[]":
  836. sqlMetaData = new SqlMetaData (name, SqlDbType.VarChar);
  837. break;
  838. case "System.DateTime":
  839. sqlMetaData = new SqlMetaData (name, SqlDbType.DateTime);
  840. break;
  841. case "System.Decimal":
  842. sqlMetaData = new SqlMetaData (name, SqlDbType.Decimal);
  843. break;
  844. case "System.Double":
  845. sqlMetaData = new SqlMetaData (name, SqlDbType.Float);
  846. break;
  847. case "System.Guid":
  848. sqlMetaData = new SqlMetaData (name, SqlDbType.UniqueIdentifier);
  849. break;
  850. case "System.Int16":
  851. sqlMetaData = new SqlMetaData (name, SqlDbType.SmallInt);
  852. break;
  853. case "System.Int32":
  854. sqlMetaData = new SqlMetaData (name, SqlDbType.Int);
  855. break;
  856. case "System.Int64":
  857. sqlMetaData = new SqlMetaData (name, SqlDbType.BigInt);
  858. break;
  859. case "System.Single":
  860. sqlMetaData = new SqlMetaData (name, SqlDbType.Real);
  861. break;
  862. case "System.String":
  863. sqlMetaData = new SqlMetaData (name, SqlDbType.NVarChar);
  864. break;
  865. case "System.Object":
  866. default:
  867. sqlMetaData = new SqlMetaData (name, SqlDbType.Variant);
  868. break;
  869. }
  870. return sqlMetaData;
  871. }
  872. #endregion // Methods
  873. }
  874. }
  875. #endif