SqlMetaData.cs 24 KB

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