SqlMetaData.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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.Data.SqlClient;
  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. [MonoTODO]
  201. public SqlMetaData (string name, SqlDbType dbType, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
  202. {
  203. throw new NotImplementedException ();
  204. }
  205. [MonoTODO]
  206. public SqlMetaData (string name, SqlDbType dbType, long maxLength, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. [MonoTODO]
  211. public SqlMetaData (string name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. [MonoTODO]
  216. public SqlMetaData (string name, SqlDbType dbType, long maxLength, long locale, SqlCompareOptions compareOptions, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. [MonoTODO]
  221. public SqlMetaData (string name, SqlDbType dbType, string database, string owningSchema, string objectName, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. [MonoTODO]
  226. public SqlMetaData (string name, SqlDbType dbType, long maxLength, byte precision, byte scale, long localeId, SqlCompareOptions compareOptions, System.Type userDefinedType, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
  227. {
  228. throw new NotImplementedException ();
  229. }
  230. public SqlMetaData (string name, SqlDbType dbType, long maxLength)
  231. {
  232. if (name == null)
  233. throw new ArgumentNullException ("name can not be null");
  234. switch (dbType) {
  235. case SqlDbType.Binary:
  236. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  237. _dbType = DbType.Binary;
  238. type = typeof (byte []);
  239. break;
  240. case SqlDbType.Char:
  241. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  242. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  243. _dbType = DbType.AnsiStringFixedLength;
  244. type = typeof (string);
  245. break;
  246. case SqlDbType.Image:
  247. maxLength = -1;
  248. precision = 0;
  249. scale = 0;
  250. _localeId = 0;
  251. compareOptions = SqlCompareOptions.None;
  252. _dbType = DbType.Binary;
  253. type = typeof (byte []);
  254. break;
  255. case SqlDbType.NChar:
  256. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  257. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  258. _dbType = DbType.String;
  259. type = typeof (string);
  260. break;
  261. case SqlDbType.NText:
  262. maxLength = -1;
  263. precision = 0;
  264. scale = 0;
  265. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  266. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  267. _dbType = DbType.String;
  268. type = typeof (string);
  269. break;
  270. case SqlDbType.NVarChar:
  271. maxLength = -1;
  272. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  273. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  274. _dbType = DbType.String;
  275. type = typeof (string);
  276. break;
  277. case SqlDbType.Text:
  278. maxLength = -1;
  279. precision = 0;
  280. scale = 0;
  281. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  282. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  283. _dbType = DbType.String;
  284. type = typeof (char []);
  285. break;
  286. case SqlDbType.VarBinary:
  287. maxLength = -1;
  288. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  289. _dbType = DbType.Binary;
  290. type = typeof (byte []);
  291. break;
  292. case SqlDbType.VarChar:
  293. maxLength = -1;
  294. _localeId = Thread.CurrentThread.CurrentCulture.LCID;
  295. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  296. _dbType = DbType.String;
  297. type = typeof (char []);
  298. break;
  299. default:
  300. throw new ArgumentException ("SqlDbType not supported");
  301. }
  302. this.maxLength = maxLength;
  303. this.name = name;
  304. this._sqlDbType = dbType;
  305. }
  306. [MonoTODO]
  307. public SqlMetaData (string name, SqlDbType dbType, Type userDefinedType)
  308. {
  309. if (name == null)
  310. throw new ArgumentNullException ("name can not be null");
  311. switch (dbType) {
  312. case SqlDbType.Udt:
  313. maxLength = -1;
  314. precision = 0;
  315. scale = 0;
  316. _localeId = 0;
  317. compareOptions = SqlCompareOptions.None;
  318. _dbType = DbType.Guid;
  319. type = typeof (Guid);
  320. break;
  321. default:
  322. throw new ArgumentException ("SqlDbType not supported");
  323. }
  324. this.name = name;
  325. // FIXME:
  326. //this.sqlDbType = userDefinedType;
  327. throw new NotImplementedException ();
  328. }
  329. public SqlMetaData (string name, SqlDbType dbType, byte precision, byte scale)
  330. {
  331. if (name == null)
  332. throw new ArgumentNullException ("name can not be null");
  333. switch (dbType) {
  334. case SqlDbType.Decimal:
  335. maxLength = 9;
  336. this.precision = precision;
  337. this.scale = scale;
  338. _localeId = 0;
  339. compareOptions = SqlCompareOptions.None;
  340. _dbType = DbType.Decimal;
  341. type = typeof (decimal);
  342. break;
  343. default:
  344. throw new ArgumentException ("SqlDbType not supported");
  345. }
  346. this.name = name;
  347. this._sqlDbType = dbType;
  348. }
  349. public SqlMetaData (string name, SqlDbType dbType, long maxLength, long locale, SqlCompareOptions compareOptions)
  350. {
  351. if (name == null)
  352. throw new ArgumentNullException ("name can not be null");
  353. switch (dbType) {
  354. case SqlDbType.Char:
  355. _dbType = DbType.AnsiStringFixedLength;
  356. type = typeof (char []);
  357. break;
  358. case SqlDbType.NChar:
  359. _dbType = DbType.StringFixedLength;
  360. type = typeof (char []);
  361. break;
  362. case SqlDbType.NText:
  363. case SqlDbType.NVarChar:
  364. _dbType = DbType.String;
  365. type = typeof (string);
  366. break;
  367. case SqlDbType.Text:
  368. case SqlDbType.VarChar:
  369. _dbType = DbType.AnsiString;
  370. type = typeof (char []);
  371. break;
  372. default:
  373. throw new ArgumentException ("SqlDbType not supported");
  374. }
  375. this.compareOptions = compareOptions;
  376. this._localeId = locale;
  377. this.maxLength = maxLength;
  378. this.name = name;
  379. this._sqlDbType = dbType;
  380. }
  381. public SqlMetaData (string name, SqlDbType dbType, string database, string owningSchema, string objectName)
  382. {
  383. if ((name == null || objectName == null) && database != null && owningSchema != null)
  384. throw new ArgumentNullException ("name can not be null");
  385. switch (dbType) {
  386. case SqlDbType.Xml:
  387. maxLength = -1;
  388. precision = 0;
  389. scale = 0;
  390. _localeId = 0;
  391. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  392. _dbType = DbType.String;
  393. type = typeof (string);
  394. break;
  395. default:
  396. throw new ArgumentException ("SqlDbType not supported");
  397. }
  398. this.name = name;
  399. this._sqlDbType = dbType;
  400. databaseName = database;
  401. this.owningSchema = owningSchema;
  402. this.objectName = objectName;
  403. }
  404. public SqlMetaData (string name, SqlDbType dbType, long maxLength, byte precision,
  405. byte scale, long locale, SqlCompareOptions compareOptions,
  406. Type userDefinedType)
  407. {
  408. if (name == null)
  409. throw new ArgumentNullException ("name can not be null");
  410. this.compareOptions = compareOptions;
  411. this._localeId = locale;
  412. this.maxLength = maxLength;
  413. this.precision = precision;
  414. this.scale = scale;
  415. switch (dbType) {
  416. case SqlDbType.Bit:
  417. maxLength = 1;
  418. precision = 1;
  419. scale = 0;
  420. locale = 0;
  421. compareOptions = SqlCompareOptions.None;
  422. _dbType = DbType.Boolean;
  423. type = typeof (bool);
  424. break;
  425. case SqlDbType.BigInt:
  426. maxLength = 8;
  427. precision = 19;
  428. scale = 0;
  429. locale = 0;
  430. compareOptions = SqlCompareOptions.None;
  431. _dbType = DbType.Int64;
  432. type = typeof (long);
  433. break;
  434. case SqlDbType.DateTime:
  435. maxLength = 8;
  436. precision = 23;
  437. scale = 3;
  438. locale = 0;
  439. compareOptions = SqlCompareOptions.None;
  440. _dbType = DbType.DateTime;
  441. type = typeof (DateTime);
  442. break;
  443. case SqlDbType.Decimal:
  444. maxLength = 9;
  445. precision = 18;
  446. scale = 0;
  447. locale = 0;
  448. compareOptions = SqlCompareOptions.None;
  449. _dbType = DbType.Decimal;
  450. type = typeof (decimal);
  451. break;
  452. case SqlDbType.Float:
  453. maxLength = 8;
  454. precision = 53;
  455. scale = 0;
  456. locale = 0;
  457. compareOptions = SqlCompareOptions.None;
  458. _dbType = DbType.Decimal;
  459. type = typeof (float);
  460. break;
  461. case SqlDbType.Image:
  462. maxLength = -1;
  463. precision = 0;
  464. scale = 0;
  465. locale = 0;
  466. compareOptions = SqlCompareOptions.None;
  467. _dbType = DbType.Binary;
  468. type = typeof (byte []);
  469. break;
  470. case SqlDbType.Int:
  471. maxLength = 4;
  472. precision = 10;
  473. scale = 0;
  474. locale = 0;
  475. compareOptions = SqlCompareOptions.None;
  476. _dbType = DbType.Int32;
  477. type = typeof (int);
  478. break;
  479. case SqlDbType.Money:
  480. maxLength = 8;
  481. precision = 19;
  482. scale = 4;
  483. locale = 0;
  484. compareOptions = SqlCompareOptions.None;
  485. _dbType = DbType.Currency;
  486. type = typeof (decimal);
  487. break;
  488. case SqlDbType.NText:
  489. maxLength = -1;
  490. precision = 0;
  491. scale = 0;
  492. locale = Thread.CurrentThread.CurrentCulture.LCID;
  493. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  494. _dbType = DbType.String;
  495. type = typeof (string);
  496. break;
  497. /*
  498. case SqlDbType.Numeric:
  499. maxLength = ;
  500. precision = ;
  501. scale = ;
  502. localeId = 0;
  503. compareOptions = SqlCompareOptions.None;
  504. break;
  505. */
  506. case SqlDbType.Real:
  507. maxLength = 4;
  508. precision = 24;
  509. scale = 0;
  510. locale = 0;
  511. compareOptions = SqlCompareOptions.None;
  512. _dbType = DbType.Single;
  513. type = typeof (Single);
  514. break;
  515. case SqlDbType.SmallDateTime:
  516. maxLength = 4;
  517. precision = 16;
  518. scale = 0;
  519. locale = 0;
  520. compareOptions = SqlCompareOptions.None;
  521. _dbType = DbType.DateTime;
  522. type = typeof (DateTime);
  523. break;
  524. case SqlDbType.SmallInt:
  525. maxLength = 2;
  526. precision = 5;
  527. scale = 0;
  528. locale = 0;
  529. compareOptions = SqlCompareOptions.None;
  530. _dbType = DbType.Int16;
  531. type = typeof (short);
  532. break;
  533. case SqlDbType.SmallMoney:
  534. maxLength = 4;
  535. precision = 10;
  536. scale = 4;
  537. locale = 0;
  538. compareOptions = SqlCompareOptions.None;
  539. _dbType = DbType.Currency;
  540. type = typeof (decimal);
  541. break;
  542. case SqlDbType.Text:
  543. maxLength = -1;
  544. precision = 0;
  545. scale = 0;
  546. locale = Thread.CurrentThread.CurrentCulture.LCID;
  547. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  548. _dbType = DbType.AnsiString;
  549. type = typeof (char []);
  550. break;
  551. case SqlDbType.Timestamp:
  552. maxLength = 8;
  553. precision = 0;
  554. scale = 0;
  555. locale = 0;
  556. compareOptions = SqlCompareOptions.None;
  557. _dbType = DbType.Byte;
  558. type = typeof (byte []);
  559. break;
  560. case SqlDbType.TinyInt:
  561. maxLength = 1;
  562. precision = 3;
  563. scale = 0;
  564. locale = 0;
  565. compareOptions = SqlCompareOptions.None;
  566. _dbType = DbType.Int16;
  567. type = typeof (short);
  568. break;
  569. case SqlDbType.UniqueIdentifier:
  570. maxLength = 16;
  571. precision = 0;
  572. scale = 0;
  573. locale = 0;
  574. compareOptions = SqlCompareOptions.None;
  575. _dbType = DbType.Guid;
  576. type = typeof (Guid);
  577. break;
  578. case SqlDbType.Udt:
  579. maxLength = -1;
  580. precision = 0;
  581. scale = 0;
  582. locale = 0;
  583. compareOptions = SqlCompareOptions.None;
  584. _dbType = DbType.Object;
  585. type = typeof (object);
  586. break;
  587. case SqlDbType.Variant:
  588. maxLength = 8016;
  589. precision = 0;
  590. scale = 0;
  591. locale = 0;
  592. compareOptions = SqlCompareOptions.None;
  593. _dbType = DbType.Object;
  594. type = typeof (object);
  595. break;
  596. case SqlDbType.Xml:
  597. maxLength = -1;
  598. precision = 0;
  599. scale = 0;
  600. locale = 0;
  601. compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
  602. _dbType = DbType.Xml;
  603. type = typeof (string);
  604. break;
  605. default:
  606. /*
  607. if (typeof (DbType.Row) == typeof (userDefinedType)) {
  608. // FIXME:
  609. // maxLength = Number of columns;
  610. precision = 0;
  611. scale = 0;
  612. localeId = 0;
  613. compareOptions = SqlCompareOptions.None;
  614. } else
  615. */
  616. throw new ArgumentException ("SqlDbType not supported");
  617. }
  618. this.name = name;
  619. this._sqlDbType = dbType;
  620. }
  621. #endregion // Constructors
  622. #region Properties
  623. public SqlCompareOptions CompareOptions {
  624. get { return compareOptions; }
  625. }
  626. public DbType DbType {
  627. get { return _dbType; }
  628. }
  629. public long LocaleId {
  630. get { return _localeId; }
  631. }
  632. public static long Max {
  633. get { return -1; }
  634. }
  635. public long MaxLength {
  636. get { return maxLength; }
  637. }
  638. public string Name {
  639. get { return name; }
  640. }
  641. public byte Precision {
  642. get { return precision; }
  643. }
  644. public byte Scale {
  645. get { return scale; }
  646. }
  647. public SqlDbType SqlDbType {
  648. get { return _sqlDbType; }
  649. }
  650. public string XmlSchemaCollectionDatabase {
  651. get { return databaseName; }
  652. }
  653. public string XmlSchemaCollectionName {
  654. get { return objectName; }
  655. }
  656. public string XmlSchemaCollectionOwningSchema {
  657. get { return owningSchema; }
  658. }
  659. [MonoTODO]
  660. public string TypeName {
  661. get { throw new NotImplementedException (); }
  662. }
  663. [MonoTODO]
  664. public bool IsUniqueKey {
  665. get { throw new NotImplementedException (); }
  666. }
  667. [MonoTODO]
  668. public SortOrder SortOrder {
  669. get { throw new NotImplementedException (); }
  670. }
  671. [MonoTODO]
  672. public int SortOrdinal {
  673. get { throw new NotImplementedException (); }
  674. }
  675. [MonoTODO]
  676. public bool UseServerDefault {
  677. get { throw new NotImplementedException (); }
  678. }
  679. #endregion // Properties
  680. #region Methods
  681. public bool Adjust (bool value)
  682. {
  683. if (type != typeof (bool))
  684. throw new ArgumentException ("Value does not match the SqlMetaData type");
  685. return value;
  686. }
  687. public byte Adjust (byte value)
  688. {
  689. if (type != typeof (byte))
  690. throw new ArgumentException ("Value does not match the SqlMetaData type");
  691. return value;
  692. }
  693. public byte[] Adjust (byte[] value)
  694. {
  695. if (type != typeof (byte []))
  696. throw new ArgumentException ("Value does not match the SqlMetaData type");
  697. return value;
  698. }
  699. public char Adjust (char value)
  700. {
  701. if (type != typeof (char))
  702. throw new ArgumentException ("Value does not match the SqlMetaData type");
  703. return value;
  704. }
  705. public char[] Adjust (char[] value)
  706. {
  707. if (type != typeof (char []))
  708. throw new ArgumentException ("Value does not match the SqlMetaData type");
  709. return value;
  710. }
  711. public DateTime Adjust (DateTime value)
  712. {
  713. if (type != typeof (DateTime))
  714. throw new ArgumentException ("Value does not match the SqlMetaData type");
  715. return value;
  716. }
  717. public DateTimeOffset Adjust (DateTimeOffset value)
  718. {
  719. if (type != typeof (DateTimeOffset))
  720. throw new ArgumentException ("Value does not match the SqlMetaData type");
  721. return value;
  722. }
  723. public TimeSpan Adjust (TimeSpan value)
  724. {
  725. if (type != typeof (TimeSpan))
  726. throw new ArgumentException ("Value does not match the SqlMetaData type");
  727. return value;
  728. }
  729. public decimal Adjust (decimal value)
  730. {
  731. if (type != typeof (decimal))
  732. throw new ArgumentException ("Value does not match the SqlMetaData type");
  733. return value;
  734. }
  735. public double Adjust (double value)
  736. {
  737. if (type != typeof (double))
  738. throw new ArgumentException ("Value does not match the SqlMetaData type");
  739. return value;
  740. }
  741. public Guid Adjust (Guid value)
  742. {
  743. if (type != typeof (Guid))
  744. throw new ArgumentException ("Value does not match the SqlMetaData type");
  745. return value;
  746. }
  747. public short Adjust (short value)
  748. {
  749. if (type != typeof (short))
  750. throw new ArgumentException ("Value does not match the SqlMetaData type");
  751. return value;
  752. }
  753. public int Adjust (int value)
  754. {
  755. if (type != typeof (int))
  756. throw new ArgumentException ("Value does not match the SqlMetaData type");
  757. return value;
  758. }
  759. public long Adjust (long value)
  760. {
  761. if (type != typeof (long))
  762. throw new ArgumentException ("Value does not match the SqlMetaData type");
  763. return value;
  764. }
  765. public object Adjust (object value)
  766. {
  767. if (type != typeof (object))
  768. throw new ArgumentException ("Value does not match the SqlMetaData type");
  769. return value;
  770. }
  771. public float Adjust (float value)
  772. {
  773. if (type != typeof (float))
  774. throw new ArgumentException ("Value does not match the SqlMetaData type");
  775. return value;
  776. }
  777. public SqlBinary Adjust (SqlBinary value)
  778. {
  779. if (type != typeof (byte []))
  780. throw new ArgumentException ("Value does not match the SqlMetaData type");
  781. return value;
  782. }
  783. public SqlBoolean Adjust (SqlBoolean value)
  784. {
  785. if (type != typeof (bool))
  786. throw new ArgumentException ("Value does not match the SqlMetaData type");
  787. return value;
  788. }
  789. public SqlByte Adjust (SqlByte value)
  790. {
  791. if (type != typeof (byte))
  792. throw new ArgumentException ("Value does not match the SqlMetaData type");
  793. return value;
  794. }
  795. public SqlBytes Adjust (SqlBytes value)
  796. {
  797. if (type != typeof (byte []))
  798. throw new ArgumentException ("Value does not match the SqlMetaData type");
  799. return value;
  800. }
  801. public SqlChars Adjust (SqlChars value)
  802. {
  803. if (type != typeof (char []))
  804. throw new ArgumentException ("Value does not match the SqlMetaData type");
  805. return value;
  806. }
  807. public SqlDateTime Adjust (SqlDateTime value)
  808. {
  809. if (type != typeof (DateTime))
  810. throw new ArgumentException ("Value does not match the SqlMetaData type");
  811. return value;
  812. }
  813. public SqlDecimal Adjust (SqlDecimal value)
  814. {
  815. if (type != typeof (decimal))
  816. throw new ArgumentException ("Value does not match the SqlMetaData type");
  817. return value;
  818. }
  819. public SqlDouble Adjust (SqlDouble value)
  820. {
  821. if (type != typeof (double))
  822. throw new ArgumentException ("Value does not match the SqlMetaData type");
  823. return value;
  824. }
  825. public SqlGuid Adjust (SqlGuid value)
  826. {
  827. if (type != typeof (Guid))
  828. throw new ArgumentException ("Value does not match the SqlMetaData type");
  829. return value;
  830. }
  831. public SqlInt16 Adjust (SqlInt16 value)
  832. {
  833. if (type != typeof (short))
  834. throw new ArgumentException ("Value does not match the SqlMetaData type");
  835. return value;
  836. }
  837. public SqlInt32 Adjust (SqlInt32 value)
  838. {
  839. if (type != typeof (int))
  840. throw new ArgumentException ("Value does not match the SqlMetaData type");
  841. return value;
  842. }
  843. public SqlInt64 Adjust (SqlInt64 value)
  844. {
  845. if (type != typeof (long))
  846. throw new ArgumentException ("Value does not match the SqlMetaData type");
  847. return value;
  848. }
  849. public SqlMoney Adjust (SqlMoney value)
  850. {
  851. if (type != typeof (decimal))
  852. throw new ArgumentException ("Value does not match the SqlMetaData type");
  853. return value;
  854. }
  855. public SqlSingle Adjust (SqlSingle value)
  856. {
  857. if (type != typeof (Single))
  858. throw new ArgumentException ("Value does not match the SqlMetaData type");
  859. return value;
  860. }
  861. public SqlString Adjust (SqlString value)
  862. {
  863. if (type != typeof (string))
  864. throw new ArgumentException ("Value does not match the SqlMetaData type");
  865. return value;
  866. }
  867. [MonoTODO]
  868. public SqlXml Adjust (SqlXml value)
  869. {
  870. throw new NotImplementedException ();
  871. }
  872. public string Adjust (string value)
  873. {
  874. if (type != typeof (string))
  875. throw new ArgumentException ("Value does not match the SqlMetaData type");
  876. return value;
  877. }
  878. public static SqlMetaData InferFromValue (object value, string name)
  879. {
  880. if (name == null)
  881. throw new ArgumentNullException ("name can not be null");
  882. if (value == null)
  883. throw new ArgumentException ("value can not be null");
  884. SqlMetaData sqlMetaData = null;
  885. switch (value.GetType ().ToString ()) {
  886. case "System.Boolean":
  887. sqlMetaData = new SqlMetaData (name, SqlDbType.Bit);
  888. break;
  889. case "System.Byte":
  890. sqlMetaData = new SqlMetaData (name, SqlDbType.Binary);
  891. break;
  892. case "System.Byte[]":
  893. sqlMetaData = new SqlMetaData (name, SqlDbType.VarBinary);
  894. break;
  895. case "System.Char":
  896. sqlMetaData = new SqlMetaData (name, SqlDbType.Char);
  897. break;
  898. case "System.Char[]":
  899. sqlMetaData = new SqlMetaData (name, SqlDbType.VarChar);
  900. break;
  901. case "System.DateTime":
  902. sqlMetaData = new SqlMetaData (name, SqlDbType.DateTime);
  903. break;
  904. case "System.Decimal":
  905. sqlMetaData = new SqlMetaData (name, SqlDbType.Decimal);
  906. break;
  907. case "System.Double":
  908. sqlMetaData = new SqlMetaData (name, SqlDbType.Float);
  909. break;
  910. case "System.Guid":
  911. sqlMetaData = new SqlMetaData (name, SqlDbType.UniqueIdentifier);
  912. break;
  913. case "System.Int16":
  914. sqlMetaData = new SqlMetaData (name, SqlDbType.SmallInt);
  915. break;
  916. case "System.Int32":
  917. sqlMetaData = new SqlMetaData (name, SqlDbType.Int);
  918. break;
  919. case "System.Int64":
  920. sqlMetaData = new SqlMetaData (name, SqlDbType.BigInt);
  921. break;
  922. case "System.Single":
  923. sqlMetaData = new SqlMetaData (name, SqlDbType.Real);
  924. break;
  925. case "System.String":
  926. sqlMetaData = new SqlMetaData (name, SqlDbType.NVarChar);
  927. break;
  928. case "System.Object":
  929. default:
  930. sqlMetaData = new SqlMetaData (name, SqlDbType.Variant);
  931. break;
  932. }
  933. return sqlMetaData;
  934. }
  935. #endregion // Methods
  936. }
  937. }