DbConnection.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. //
  2. // System.Data.Common.DbConnection
  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 || TARGET_JVM
  32. using System.ComponentModel;
  33. using System.Data;
  34. #if NET_2_0 && !TARGET_JVM
  35. using System.Transactions;
  36. #endif
  37. namespace System.Data.Common {
  38. public abstract class DbConnection : Component, IDbConnection, IDisposable
  39. {
  40. #region Constructors
  41. protected DbConnection ()
  42. {
  43. }
  44. #endregion // Constructors
  45. #region Properties
  46. [RecommendedAsConfigurable (true)]
  47. [RefreshProperties (RefreshProperties.All)]
  48. [DefaultValue ("")]
  49. public abstract string ConnectionString { get; set; }
  50. public abstract string Database { get; }
  51. public abstract string DataSource { get; }
  52. [Browsable (false)]
  53. public abstract string ServerVersion { get; }
  54. [Browsable (false)]
  55. public abstract ConnectionState State { get; }
  56. public virtual int ConnectionTimeout {
  57. get { return 15; }
  58. }
  59. #endregion // Properties
  60. #region Methods
  61. protected abstract DbTransaction BeginDbTransaction (IsolationLevel isolationLevel);
  62. public DbTransaction BeginTransaction ()
  63. {
  64. return BeginDbTransaction (IsolationLevel.Unspecified);
  65. }
  66. public DbTransaction BeginTransaction (IsolationLevel isolationLevel)
  67. {
  68. return BeginDbTransaction (isolationLevel);
  69. }
  70. public abstract void ChangeDatabase (string databaseName);
  71. public abstract void Close ();
  72. public DbCommand CreateCommand ()
  73. {
  74. return CreateDbCommand ();
  75. }
  76. protected abstract DbCommand CreateDbCommand ();
  77. #if NET_2_0 && !TARGET_JVM
  78. [MonoTODO]
  79. public virtual void EnlistTransaction (Transaction transaction)
  80. {
  81. throw new NotSupportedException ();
  82. }
  83. #endif
  84. #if NET_2_0
  85. static class DataTypes
  86. {
  87. static readonly ColumnInfo [] columns = {
  88. new ColumnInfo ("TypeName", typeof(string)),
  89. new ColumnInfo ("ProviderDbType", typeof(int)),
  90. new ColumnInfo ("ColumnSize", typeof(long)),
  91. new ColumnInfo ("CreateFormat", typeof(string)),
  92. new ColumnInfo ("CreateParameters", typeof(string)),
  93. new ColumnInfo ("DataType", typeof(string)),
  94. new ColumnInfo ("IsAutoIncrementable", typeof(bool)),
  95. new ColumnInfo ("IsBestMatch", typeof(bool)),
  96. new ColumnInfo ("IsCaseSensitive", typeof(bool)),
  97. new ColumnInfo ("IsFixedLength", typeof(bool)),
  98. new ColumnInfo ("IsFixedPrecisionScale", typeof(bool)),
  99. new ColumnInfo ("IsLong", typeof(bool)),
  100. new ColumnInfo ("IsNullable", typeof(bool)),
  101. new ColumnInfo ("IsSearchable", typeof(bool)),
  102. new ColumnInfo ("IsSearchableWithLike", typeof(bool)),
  103. new ColumnInfo ("IsUnsigned", typeof(bool)),
  104. new ColumnInfo ("MaximumScale", typeof(short)),
  105. new ColumnInfo ("MinimumScale", typeof(short)),
  106. new ColumnInfo ("IsConcurrencyType", typeof(bool)),
  107. new ColumnInfo ("IsLiteralSupported", typeof(bool)),
  108. new ColumnInfo ("LiteralPrefix", typeof(string)),
  109. new ColumnInfo ("LiteralSuffix", typeof(string))
  110. };
  111. static readonly object [][] rows = {
  112. new object [] {"smallint", 16, 5, "smallint", null, "System.Int16", true, true,
  113. false, true, true, false, true, true, false, false, null,
  114. null, false, null, null, null},
  115. new object [] {"int", 8, 10, "int", null, "System.Int32",
  116. true, true, false, true, true, false, true, true, false,
  117. false, null, null, false, null, null, null},
  118. new object [] {"real", 13, 7, "real", null,
  119. "System.Single", false, true, false, true, false, false,
  120. true, true, false, false, null, null, false, null, null, null},
  121. new object [] {"float", 6, 53, "float({0})",
  122. "number of bits used to store the mantissa", "System.Double",
  123. false, true, false, true, false, false, true, true,
  124. false, false, null, null, false, null, null, null},
  125. new object [] {"money", 9, 19, "money", null,
  126. "System.Decimal", false, false, false, true, true,
  127. false, true, true, false, false, null, null, false,
  128. null, null, null},
  129. new object [] {"smallmoney", 17, 10, "smallmoney", null,
  130. "System.Decimal", false, false, false, true, true, false,
  131. true, true, false, false, null, null, false, null, null, null},
  132. new object [] {"bit", 2, 1, "bit", null, "System.Boolean",
  133. false, false, false, true, false, false, true, true,
  134. false, null, null, null, false, null, null, null},
  135. new object [] {"tinyint", 20, 3, "tinyint", null,
  136. "System.SByte", true, true, false, true, true, false,
  137. true, true, false, true, null, null, false, null, null, null},
  138. new object [] {"bigint", 0, 19, "bigint", null,
  139. "System.Int64", true, true, false, true, true, false,
  140. true, true, false, false, null, null, false, null, null, null},
  141. new object [] {"timestamp", 19, 8, "timestamp", null,
  142. "System.Byte[]", false, false, false, true, false, false,
  143. false, true, false, null, null, null, true, null, "0x", null},
  144. new object [] {"binary", 1, 8000, "binary({0})", "length",
  145. "System.Byte[]", false, true, false, true, false, false,
  146. true, true, false, null, null, null, false, null, "0x", null},
  147. new object [] {"image", 7, 2147483647, "image", null,
  148. "System.Byte[]", false, true, false, false, false, true,
  149. true, false, false, null, null, null, false, null, "0x", null},
  150. new object [] {"text", 18, 2147483647, "text", null,
  151. "System.String", false, true, false, false, false, true,
  152. true, false, true, null, null, null, false, null, "'", "'"},
  153. new object [] {"ntext", 11, 1073741823, "ntext", null,
  154. "System.String", false, true, false, false, false, true,
  155. true, false, true, null, null, null, false, null, "N'", "'"},
  156. new object [] {"decimal", 5, 38, "decimal({0}, {1})",
  157. "precision,scale", "System.Decimal", true, true, false,
  158. true, false, false, true, true, false, false, 38, 0,
  159. false, null, null, null},
  160. new object [] {"numeric", 5, 38, "numeric({0}, {1})",
  161. "precision,scale", "System.Decimal", true, true, false,
  162. true, false, false, true, true, false, false, 38, 0,
  163. false, null, null, null},
  164. new object [] {"datetime", 4, 23, "datetime", null,
  165. "System.DateTime", false, true, false, true, false, false,
  166. true, true, true, null, null, null, false, null, "{ts '", "'}"},
  167. new object [] {"smalldatetime", 15, 16, "smalldatetime", null,
  168. "System.DateTime", false, true, false, true, false, false,
  169. true, true, true, null, null, null, false, null, "{ts '", "'}"},
  170. new object [] {"sql_variant", 23, null, "sql_variant",
  171. null, "System.Object", false, true, false, false, false,
  172. false, true, true, false, null, null, null, false, false,
  173. null, null},
  174. new object [] {"xml", 25, 2147483647, "xml", null,
  175. "System.String", false, false, false, false, false, true,
  176. true, false, false, null, null, null, false, false, null, null},
  177. new object [] {"varchar", 22, 2147483647, "varchar({0})",
  178. "max length", "System.String", false, true, false, false,
  179. false, false, true, true, true, null, null, null, false,
  180. null, "'", "'"},
  181. new object [] {"char", 3, 2147483647, "char({0})", "length",
  182. "System.String", false, true, false, true, false, false,
  183. true, true, true, null, null, null, false, null, "'", "'"},
  184. new object [] {"nchar", 10, 1073741823, "nchar({0})", "length",
  185. "System.String", false, true, false, true, false, false,
  186. true, true, true, null, null, null, false, null, "N'", "'"},
  187. new object [] {"nvarchar", 12, 1073741823, "nvarchar({0})", "max length",
  188. "System.String", false, true, false, false, false, false, true, true,
  189. true, null, null, null, false, null, "N'", "'"},
  190. new object [] {"varbinary", 21, 1073741823, "varbinary({0})",
  191. "max length", "System.Byte[]", false, true, false, false,
  192. false, false, true, true, false, null, null, null, false,
  193. null, "0x", null},
  194. new object [] {"uniqueidentifier", 14, 16, "uniqueidentifier", null,
  195. "System.Guid", false, true, false, true, false, false, true,
  196. true, false, null, null, null, false, null, "'", "'"}
  197. };
  198. static DataTable instance;
  199. static public DataTable Instance {
  200. get {
  201. if (instance == null) {
  202. instance = new DataTable ("DataTypes");
  203. foreach (ColumnInfo c in columns)
  204. instance.Columns.Add (c.name, c.type);
  205. foreach (object [] row in rows)
  206. instance.LoadDataRow (row, true);
  207. }
  208. return instance;
  209. }
  210. }
  211. }
  212. struct ColumnInfo {
  213. public string name;
  214. public Type type;
  215. public ColumnInfo (string name, Type type)
  216. {
  217. this.name = name; this.type = type;
  218. }
  219. }
  220. static class MetaDataCollections
  221. {
  222. static readonly ColumnInfo [] columns = {
  223. new ColumnInfo ("CollectionName", typeof (string)),
  224. new ColumnInfo ("NumberOfRestrictions", typeof (int)),
  225. new ColumnInfo ("NumberOfIdentifierParts", typeof (int))
  226. };
  227. static readonly object [][] rows = {
  228. new object [] {"MetaDataCollections", 0, 0},
  229. new object [] {"DataSourceInformation", 0, 0},
  230. new object [] {"DataTypes", 0, 0},
  231. new object [] {"Restrictions", 0, 0},
  232. new object [] {"ReservedWords", 0, 0},
  233. new object [] {"Users", 1, 1},
  234. new object [] {"Databases", 1, 1},
  235. new object [] {"Tables", 4, 3},
  236. new object [] {"Columns", 4, 4},
  237. new object [] {"Views", 3, 3},
  238. new object [] {"ViewColumns", 4, 4},
  239. new object [] {"ProcedureParameters", 4, 1},
  240. new object [] {"Procedures", 4, 3},
  241. new object [] {"ForeignKeys", 4, 3},
  242. new object [] {"IndexColumns", 5, 4},
  243. new object [] {"Indexes", 4, 3},
  244. new object [] {"UserDefinedTypes", 2, 1}
  245. };
  246. static DataTable instance;
  247. static public DataTable Instance {
  248. get {
  249. if (instance == null) {
  250. instance = new DataTable ("GetSchema");
  251. foreach (ColumnInfo c in columns)
  252. instance.Columns.Add (c.name, c.type);
  253. foreach (object [] row in rows)
  254. instance.LoadDataRow (row, true);
  255. }
  256. return instance;
  257. }
  258. }
  259. }
  260. static class Restrictions
  261. {
  262. static readonly ColumnInfo [] columns = {
  263. new ColumnInfo ("CollectionName", typeof (string)),
  264. new ColumnInfo ("RestrictionName", typeof(string)),
  265. new ColumnInfo ("ParameterName", typeof(string)),
  266. new ColumnInfo ("RestrictionDefault", typeof(string)),
  267. new ColumnInfo ("RestrictionNumber", typeof(int))
  268. };
  269. static readonly object [][] rows = {
  270. new object [] {"Users", "User_Name", "@Name", "name", 1},
  271. new object [] {"Databases", "Name", "@Name", "Name", 1},
  272. new object [] {"Tables", "Catalog", "@Catalog", "TABLE_CATALOG", 1},
  273. new object [] {"Tables", "Owner", "@Owner", "TABLE_SCHEMA", 2},
  274. new object [] {"Tables", "Table", "@Name", "TABLE_NAME", 3},
  275. new object [] {"Tables", "TableType", "@TableType", "TABLE_TYPE", 4},
  276. new object [] {"Columns", "Catalog", "@Catalog", "TABLE_CATALOG", 1},
  277. new object [] {"Columns", "Owner", "@Owner", "TABLE_SCHEMA", 2},
  278. new object [] {"Columns", "Table", "@Table", "TABLE_NAME", 3},
  279. new object [] {"Columns", "Column", "@Column", "COLUMN_NAME", 4},
  280. new object [] {"Views", "Catalog", "@Catalog", "TABLE_CATALOG", 1},
  281. new object [] {"Views", "Owner", "@Owner", "TABLE_SCHEMA", 2},
  282. new object [] {"Views", "Table", "@Table", "TABLE_NAME", 3},
  283. new object [] {"ViewColumns", "Catalog", "@Catalog", "VIEW_CATALOG", 1},
  284. new object [] {"ViewColumns", "Owner", "@Owner", "VIEW_SCHEMA", 2},
  285. new object [] {"ViewColumns", "Table", "@Table", "VIEW_NAME", 3},
  286. new object [] {"ViewColumns", "Column", "@Column", "COLUMN_NAME", 4},
  287. new object [] {"ProcedureParameters", "Catalog", "@Catalog", "SPECIFIC_CATALOG", 1},
  288. new object [] {"ProcedureParameters", "Owner", "@Owner", "SPECIFIC_SCHEMA", 2},
  289. new object [] {"ProcedureParameters", "Name", "@Name", "SPECIFIC_NAME", 3},
  290. new object [] {"ProcedureParameters", "Parameter", "@Parameter", "PARAMETER_NAME", 4},
  291. new object [] {"Procedures", "Catalog", "@Catalog", "SPECIFIC_CATALOG", 1},
  292. new object [] {"Procedures", "Owner", "@Owner", "SPECIFIC_SCHEMA", 2},
  293. new object [] {"Procedures", "Name", "@Name", "SPECIFIC_NAME", 3},
  294. new object [] {"Procedures", "Type", "@Type", "ROUTINE_TYPE", 4},
  295. new object [] {"IndexColumns", "Catalog", "@Catalog", "db_name(}", 1},
  296. new object [] {"IndexColumns", "Owner", "@Owner", "user_name(}", 2},
  297. new object [] {"IndexColumns", "Table", "@Table", "o.name", 3},
  298. new object [] {"IndexColumns", "ConstraintName", "@ConstraintName", "x.name", 4},
  299. new object [] {"IndexColumns", "Column", "@Column", "c.name", 5},
  300. new object [] {"Indexes", "Catalog", "@Catalog", "db_name(}", 1},
  301. new object [] {"Indexes", "Owner", "@Owner", "user_name(}", 2},
  302. new object [] {"Indexes", "Table", "@Table", "o.name", 3},
  303. new object [] {"Indexes", "Name", "@Name", "x.name", 4},
  304. new object [] {"UserDefinedTypes", "assembly_name", "@AssemblyName", "assemblies.name", 1},
  305. new object [] {"UserDefinedTypes", "udt_name", "@UDTName", "types.assembly_class", 2},
  306. new object [] {"ForeignKeys", "Catalog", "@Catalog", "CONSTRAINT_CATALOG", 1},
  307. new object [] {"ForeignKeys", "Owner", "@Owner", "CONSTRAINT_SCHEMA", 2},
  308. new object [] {"ForeignKeys", "Table", "@Table", "TABLE_NAME", 3},
  309. new object [] {"ForeignKeys", "Name", "@Name", "CONSTRAINT_NAME", 4}
  310. };
  311. static DataTable instance;
  312. static public DataTable Instance {
  313. get {
  314. if (instance == null) {
  315. instance = new DataTable ("Restrictions");
  316. foreach (ColumnInfo c in columns)
  317. instance.Columns.Add (c.name, c.type);
  318. foreach (object [] row in rows)
  319. instance.LoadDataRow (row, true);
  320. }
  321. return instance;
  322. }
  323. }
  324. }
  325. static class ReservedWords
  326. {
  327. static readonly string [] reservedWords =
  328. {
  329. "ADD", "EXCEPT", "PERCENT", "ALL", "EXEC", "PLAN", "ALTER",
  330. "EXECUTE", "PRECISION", "AND", "EXISTS", "PRIMARY", "ANY",
  331. "EXIT", "PRINT", "AS", "FETCH", "PROC", "ASC", "FILE",
  332. "PROCEDURE", "AUTHORIZATION", "FILLFACTOR", "PUBLIC",
  333. "BACKUP", "FOR", "RAISERROR", "BEGIN", "FOREIGN", "READ",
  334. "BETWEEN", "FREETEXT", "READTEXT", "BREAK", "FREETEXTTABLE",
  335. "RECONFIGURE", "BROWSE", "FROM", "REFERENCES", "BULK",
  336. "FULL", "REPLICATION", "BY", "FUNCTION", "RESTORE",
  337. "CASCADE", "GOTO", "RESTRICT", "CASE", "GRANT", "RETURN",
  338. "CHECK", "GROUP", "REVOKE", "CHECKPOINT", "HAVING", "RIGHT",
  339. "CLOSE", "HOLDLOCK", "ROLLBACK", "CLUSTERED", "IDENTITY",
  340. "ROWCOUNT", "COALESCE", "IDENTITY_INSERT", "ROWGUIDCOL",
  341. "COLLATE", "IDENTITYCOL", "RULE", "COLUMN", "IF", "SAVE",
  342. "COMMIT", "IN", "SCHEMA", "COMPUTE", "INDEX", "SELECT",
  343. "CONSTRAINT", "INNER", "SESSION_USER", "CONTAINS", "INSERT",
  344. "SET", "CONTAINSTABLE", "INTERSECT", "SETUSER", "CONTINUE",
  345. "INTO", "SHUTDOWN", "CONVERT", "IS", "SOME", "CREATE",
  346. "JOIN", "STATISTICS", "CROSS", "KEY", "SYSTEM_USER",
  347. "CURRENT", "KILL", "TABLE", "CURRENT_DATE", "LEFT",
  348. "TEXTSIZE", "CURRENT_TIME", "LIKE", "THEN",
  349. "CURRENT_TIMESTAMP", "LINENO", "TO", "CURRENT_USER", "LOAD",
  350. "TOP", "CURSOR", "NATIONAL", "TRAN", "DATABASE", "NOCHECK",
  351. "TRANSACTION", "DBCC", "NONCLUSTERED", "TRIGGER",
  352. "DEALLOCATE", "NOT", "TRUNCATE", "DECLARE", "NULL",
  353. "TSEQUAL", "DEFAULT", "NULLIF", "UNION", "DELETE", "OF",
  354. "UNIQUE", "DENY", "OFF", "UPDATE", "DESC", "OFFSETS",
  355. "UPDATETEXT", "DISK", "ON", "USE", "DISTINCT", "OPEN",
  356. "USER", "DISTRIBUTED", "OPENDATASOURCE", "VALUES", "DOUBLE",
  357. "OPENQUERY", "VARYING", "DROP", "OPENROWSET", "VIEW",
  358. "DUMMY", "OPENXML", "WAITFOR", "DUMP", "OPTION", "WHEN",
  359. "ELSE", "OR", "WHERE", "END", "ORDER", "WHILE", "ERRLVL",
  360. "OUTER", "WITH", "ESCAPE", "OVER", "WRITETEXT", "ABSOLUTE",
  361. "FOUND", "PRESERVE", "ACTION", "FREE", "PRIOR", "ADMIN",
  362. "GENERAL", "PRIVILEGES", "AFTER", "GET", "READS",
  363. "AGGREGATE", "GLOBAL", "REAL", "ALIAS", "GO", "RECURSIVE",
  364. "ALLOCATE", "GROUPING", "REF", "ARE", "HOST", "REFERENCING",
  365. "ARRAY", "HOUR", "RELATIVE", "ASSERTION", "IGNORE", "RESULT",
  366. "AT", "IMMEDIATE", "RETURNS", "BEFORE", "INDICATOR", "ROLE",
  367. "BINARY", "INITIALIZE", "ROLLUP", "BIT", "INITIALLY",
  368. "ROUTINE", "BLOB", "INOUT", "ROW", "BOOLEAN", "INPUT",
  369. "ROWS", "BOTH", "INT", "SAVEPOINT", "BREADTH", "INTEGER",
  370. "SCROLL", "CALL", "INTERVAL", "SCOPE", "CASCADED",
  371. "ISOLATION", "SEARCH", "CAST", "ITERATE", "SECOND",
  372. "CATALOG", "LANGUAGE", "SECTION", "CHAR", "LARGE",
  373. "SEQUENCE", "CHARACTER", "LAST", "SESSION", "CLASS",
  374. "LATERAL", "SETS", "CLOB", "LEADING", "SIZE", "COLLATION",
  375. "LESS", "SMALLINT", "COMPLETION", "LEVEL", "SPACE",
  376. "CONNECT", "LIMIT", "SPECIFIC", "CONNECTION", "LOCAL",
  377. "SPECIFICTYPE", "CONSTRAINTS", "LOCALTIME", "SQL",
  378. "CONSTRUCTOR", "LOCALTIMESTAMP", "SQLEXCEPTION",
  379. "CORRESPONDING", "LOCATOR", "SQLSTATE", "CUBE", "MAP",
  380. "SQLWARNING", "CURRENT_PATH", "MATCH", "START",
  381. "CURRENT_ROLE", "MINUTE", "STATE", "CYCLE", "MODIFIES",
  382. "STATEMENT", "DATA", "MODIFY", "STATIC", "DATE", "MODULE",
  383. "STRUCTURE", "DAY", "MONTH", "TEMPORARY", "DEC", "NAMES",
  384. "TERMINATE", "DECIMAL", "NATURAL", "THAN", "DEFERRABLE",
  385. "NCHAR", "TIME", "DEFERRED", "NCLOB", "TIMESTAMP", "DEPTH",
  386. "NEW", "TIMEZONE_HOUR", "DEREF", "NEXT", "TIMEZONE_MINUTE",
  387. "DESCRIBE", "NO", "TRAILING", "DESCRIPTOR", "NONE",
  388. "TRANSLATION", "DESTROY", "NUMERIC", "TREAT", "DESTRUCTOR",
  389. "OBJECT", "TRUE", "DETERMINISTIC", "OLD", "UNDER",
  390. "DICTIONARY", "ONLY", "UNKNOWN", "DIAGNOSTICS", "OPERATION",
  391. "UNNEST", "DISCONNECT", "ORDINALITY", "USAGE", "DOMAIN",
  392. "OUT", "USING", "DYNAMIC", "OUTPUT", "VALUE", "EACH",
  393. "PAD", "VARCHAR", "END-EXEC", "PARAMETER", "VARIABLE",
  394. "EQUALS", "PARAMETERS", "WHENEVER", "EVERY", "PARTIAL",
  395. "WITHOUT", "EXCEPTION", "PATH", "WORK", "EXTERNAL",
  396. "POSTFIX", "WRITE", "FALSE", "PREFIX", "YEAR", "FIRST",
  397. "PREORDER", "ZONE", "FLOAT", "PREPARE", "ADA", "AVG",
  398. "BIT_LENGTH", "CHAR_LENGTH", "CHARACTER_LENGTH", "COUNT",
  399. "EXTRACT", "FORTRAN", "INCLUDE", "INSENSITIVE", "LOWER",
  400. "MAX", "MIN", "OCTET_LENGTH", "OVERLAPS", "PASCAL",
  401. "POSITION", "SQLCA", "SQLCODE", "SQLERROR", "SUBSTRING",
  402. "SUM", "TRANSLATE", "TRIM", "UPPER"
  403. };
  404. static DataTable instance;
  405. static public DataTable Instance {
  406. get {
  407. if (instance == null) {
  408. DataRow row = null;
  409. instance = new DataTable ("ReservedWords");
  410. instance.Columns.Add ("ReservedWord", typeof(string));
  411. foreach (string reservedWord in reservedWords)
  412. {
  413. row = instance.NewRow();
  414. row["ReservedWord"] = reservedWord;
  415. instance.Rows.Add(row);
  416. }
  417. }
  418. return instance;
  419. }
  420. }
  421. }
  422. public virtual DataTable GetSchema ()
  423. {
  424. return MetaDataCollections.Instance;
  425. }
  426. public virtual DataTable GetSchema (string collectionName)
  427. {
  428. return GetSchema (collectionName, null);
  429. }
  430. private void AddParameter (DbCommand command, string parameterName, DbType parameterType, int parameterSize)
  431. {
  432. DbParameter parameter = command.CreateParameter ();
  433. parameter.ParameterName = parameterName;
  434. parameter.DbType = parameterType;
  435. parameter.Size = parameterSize;
  436. command.Parameters.Add (parameter);
  437. }
  438. public virtual DataTable GetSchema (string collectionName, string[] restrictionValues)
  439. {
  440. if (collectionName == null)
  441. //LAMESPEC: In MS.NET, if collectionName is null, it throws ArgumentException.
  442. throw new ArgumentException ();
  443. String cName = null;
  444. DataTable schemaTable = MetaDataCollections.Instance;
  445. int length = restrictionValues == null ? 0 : restrictionValues.Length;
  446. foreach (DataRow row in schemaTable.Rows) {
  447. if (String.Compare ((string) row ["CollectionName"], collectionName, true) == 0) {
  448. if (length > (int) row ["NumberOfRestrictions"]) {
  449. throw new ArgumentException ("More restrictions were provided " +
  450. "than the requested schema ('" +
  451. row ["CollectionName"].ToString () + "') supports");
  452. }
  453. cName = row ["CollectionName"].ToString ();
  454. }
  455. }
  456. if (cName == null)
  457. throw new ArgumentException ("The requested collection ('" + collectionName + "') is not defined.");
  458. DbCommand command = null;
  459. DataTable dataTable = new DataTable ();
  460. switch (cName)
  461. {
  462. case "Databases":
  463. command = CreateCommand ();
  464. command.Connection = this;
  465. command.CommandText = "select name as database_name, dbid, crdate as create_date " +
  466. "from master.sys.sysdatabases where (name = @Name or (@Name " +
  467. "is null))";
  468. AddParameter (command, "@Name", DbType.StringFixedLength, 4000);
  469. break;
  470. case "ForeignKeys":
  471. command = CreateCommand ();
  472. command.Connection = this;
  473. command.CommandText = "select CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, " +
  474. "TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, CONSTRAINT_TYPE, " +
  475. "IS_DEFERRABLE, INITIALLY_DEFERRED from " +
  476. "INFORMATION_SCHEMA.TABLE_CONSTRAINTS where (CONSTRAINT_CATALOG" +
  477. " = @Catalog or (@Catalog is null)) and (CONSTRAINT_SCHEMA = " +
  478. "@Owner or (@Owner is null)) and (TABLE_NAME = @Table or (" +
  479. "@Table is null)) and (CONSTRAINT_NAME = @Name or (@Name is null))" +
  480. " and CONSTRAINT_TYPE = 'FOREIGN KEY' order by CONSTRAINT_CATALOG," +
  481. " CONSTRAINT_SCHEMA, CONSTRAINT_NAME";
  482. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  483. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  484. AddParameter (command, "@Table", DbType.StringFixedLength, 4000);
  485. AddParameter (command, "@Name", DbType.StringFixedLength, 4000);
  486. break;
  487. case "Indexes":
  488. command = CreateCommand ();
  489. command.Connection = this;
  490. command.CommandText = "select distinct db_name() as constraint_catalog, " +
  491. "constraint_schema = user_name (o.uid), " +
  492. "constraint_name = x.name, table_catalog = db_name (), " +
  493. "table_schema = user_name (o.uid), table_name = o.name, " +
  494. "index_name = x.name from sysobjects o, sysindexes x, " +
  495. "sysindexkeys xk where o.type in ('U') and x.id = o.id and " +
  496. "o.id = xk.id and x.indid = xk.indid and xk.keyno = x.keycnt " +
  497. "and (db_name() = @Catalog or (@Catalog is null)) and " +
  498. "(user_name() = @Owner or (@Owner is null)) and (o.name = " +
  499. "@Table or (@Table is null)) and (x.name = @Name or (@Name is null))" +
  500. "order by table_name, index_name";
  501. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  502. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  503. AddParameter (command, "@Table", DbType.StringFixedLength, 4000);
  504. AddParameter (command, "@Name", DbType.StringFixedLength, 4000);
  505. break;
  506. case "IndexColumns":
  507. command = CreateCommand ();
  508. command.Connection = this;
  509. command.CommandText = "select distinct db_name() as constraint_catalog, " +
  510. "constraint_schema = user_name (o.uid), constraint_name = x.name, " +
  511. "table_catalog = db_name (), table_schema = user_name (o.uid), " +
  512. "table_name = o.name, column_name = c.name, " +
  513. "ordinal_position = convert (int, xk.keyno), keyType = c.xtype, " +
  514. "index_name = x.name from sysobjects o, sysindexes x, syscolumns c, " +
  515. "sysindexkeys xk where o.type in ('U') and x.id = o.id and o.id = c.id " +
  516. "and o.id = xk.id and x.indid = xk.indid and c.colid = xk.colid " +
  517. "and xk.keyno <= x.keycnt and permissions (o.id, c.name) <> 0 " +
  518. "and (db_name() = @Catalog or (@Catalog is null)) and (user_name() " +
  519. "= @Owner or (@Owner is null)) and (o.name = @Table or (@Table is" +
  520. " null)) and (x.name = @ConstraintName or (@ConstraintName is null)) " +
  521. "and (c.name = @Column or (@Column is null)) order by table_name, " +
  522. "index_name";
  523. AddParameter (command, "@Catalog", DbType.StringFixedLength, 8);
  524. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  525. AddParameter (command, "@Table", DbType.StringFixedLength, 13);
  526. AddParameter (command, "@ConstraintName", DbType.StringFixedLength, 4000);
  527. AddParameter (command, "@Column", DbType.StringFixedLength, 4000);
  528. break;
  529. case "Procedures":
  530. command = CreateCommand ();
  531. command.Connection = this;
  532. command.CommandText = "select SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME, " +
  533. "ROUTINE_CATALOG, ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, " +
  534. "CREATED, LAST_ALTERED from INFORMATION_SCHEMA.ROUTINES where " +
  535. "(SPECIFIC_CATALOG = @Catalog or (@Catalog is null)) and " +
  536. "(SPECIFIC_SCHEMA = @Owner or (@Owner is null)) and (SPECIFIC_NAME" +
  537. " = @Name or (@Name is null)) and (ROUTINE_TYPE = @Type or (@Type " +
  538. "is null)) order by SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME";
  539. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  540. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  541. AddParameter (command, "@Name", DbType.StringFixedLength, 4000);
  542. AddParameter (command, "@Type", DbType.StringFixedLength, 4000);
  543. break;
  544. case "ProcedureParameters":
  545. command = CreateCommand ();
  546. command.Connection = this;
  547. command.CommandText = "select SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME, " +
  548. "ORDINAL_POSITION, PARAMETER_MODE, IS_RESULT, AS_LOCATOR, " +
  549. "PARAMETER_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, " +
  550. "CHARACTER_OCTET_LENGTH, COLLATION_CATALOG, COLLATION_SCHEMA, " +
  551. "COLLATION_NAME, CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, " +
  552. "CHARACTER_SET_NAME, NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, " +
  553. "NUMERIC_SCALE, DATETIME_PRECISION, INTERVAL_TYPE, " +
  554. "INTERVAL_PRECISION from INFORMATION_SCHEMA.PARAMETERS where " +
  555. "(SPECIFIC_CATALOG = @Catalog or (@Catalog is null)) and " +
  556. "(SPECIFIC_SCHEMA = @Owner or (@Owner is null)) and (SPECIFIC_NAME = " +
  557. "@Name or (@Name is null)) and (PARAMETER_NAME = @Parameter or (" +
  558. "@Parameter is null)) order by SPECIFIC_CATALOG, SPECIFIC_SCHEMA," +
  559. " SPECIFIC_NAME, PARAMETER_NAME";
  560. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  561. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  562. AddParameter (command, "@Name", DbType.StringFixedLength, 4000);
  563. AddParameter (command, "@Parameter", DbType.StringFixedLength, 4000);
  564. break;
  565. case "Tables":
  566. command = CreateCommand ();
  567. command.Connection = this;
  568. command.CommandText = "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE " +
  569. "from INFORMATION_SCHEMA.TABLES where" +
  570. " (TABLE_CATALOG = @catalog or (@catalog is null)) and " +
  571. "(TABLE_SCHEMA = @owner or (@owner is null))and " +
  572. "(TABLE_NAME = @name or (@name is null)) and " +
  573. "(TABLE_TYPE = @table_type or (@table_type is null))";
  574. AddParameter (command, "@catalog", DbType.StringFixedLength, 8);
  575. AddParameter (command, "@owner", DbType.StringFixedLength, 3);
  576. AddParameter (command, "@name", DbType.StringFixedLength, 11);
  577. AddParameter (command, "@table_type", DbType.StringFixedLength, 10);
  578. break;
  579. case "Columns":
  580. command = CreateCommand ();
  581. command.Connection = this;
  582. command.CommandText = "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, " +
  583. "ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, " +
  584. "CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, " +
  585. "NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE, " +
  586. "DATETIME_PRECISION, CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, " +
  587. "CHARACTER_SET_NAME, COLLATION_CATALOG from INFORMATION_SCHEMA.COLUMNS" +
  588. " where (TABLE_CATALOG = @Catalog or (@Catalog is null)) and (" +
  589. "TABLE_SCHEMA = @Owner or (@Owner is null)) and (TABLE_NAME = @table" +
  590. " or (@Table is null)) and (COLUMN_NAME = @column or (@Column is null" +
  591. ")) order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME";
  592. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  593. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  594. AddParameter (command, "@Table", DbType.StringFixedLength, 4000);
  595. AddParameter (command, "@Column", DbType.StringFixedLength, 4000);
  596. break;
  597. case "Users":
  598. command = CreateCommand ();
  599. command.Connection = this;
  600. command.CommandText = "select uid, name as user_name, createdate, updatedate from sysusers" +
  601. " where (name = @Name or (@Name is null))";
  602. AddParameter (command, "@Name", DbType.StringFixedLength, 4000);
  603. break;
  604. case "Views":
  605. command = CreateCommand ();
  606. command.Connection = this;
  607. command.CommandText = "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, CHECK_OPTION, " +
  608. "IS_UPDATABLE from INFORMATION_SCHEMA.VIEWS where (TABLE_CATALOG" +
  609. " = @Catalog or (@Catalog is null)) TABLE_SCHEMA = @Owner or " +
  610. "(@Owner is null)) and (TABLE_NAME = @table or (@Table is null))" +
  611. " order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME";
  612. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  613. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  614. AddParameter (command, "@Table", DbType.StringFixedLength, 4000);
  615. break;
  616. case "ViewColumns":
  617. command = CreateCommand ();
  618. command.Connection = this;
  619. command.CommandText = "select VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME, TABLE_CATALOG, " +
  620. "TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from " +
  621. "INFORMATION_SCHEMA.VIEW_COLUMN_USAGE where (VIEW_CATALOG = " +
  622. "@Catalog (@Catalog is null)) and (VIEW_SCHEMA = @Owner (@Owner" +
  623. " is null)) and (VIEW_NAME = @Table or (@Table is null)) and " +
  624. "(COLUMN_NAME = @Column or (@Column is null)) order by " +
  625. "VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME";
  626. AddParameter (command, "@Catalog", DbType.StringFixedLength, 4000);
  627. AddParameter (command, "@Owner", DbType.StringFixedLength, 4000);
  628. AddParameter (command, "@Table", DbType.StringFixedLength, 4000);
  629. AddParameter (command, "@Column", DbType.StringFixedLength, 4000);
  630. break;
  631. case "UserDefinedTypes":
  632. command = CreateCommand ();
  633. command.Connection = this;
  634. command.CommandText = "select assemblies.name as assembly_name, types.assembly_class " +
  635. "as udt_name, ASSEMBLYPROPERTY(assemblies.name, 'VersionMajor') " +
  636. "as version_major, ASSEMBLYPROPERTY(assemblies.name, 'VersionMinor') " +
  637. "as version_minor, ASSEMBLYPROPERTY(assemblies.name, 'VersionBuild') " +
  638. "as version_build, ASSEMBLYPROPERTY(assemblies.name, 'VersionRevision') " +
  639. "as version_revision, ASSEMBLYPROPERTY(assemblies.name, 'CultureInfo') " +
  640. "as culture_info, ASSEMBLYPROPERTY(assemblies.name, 'PublicKey') " +
  641. "as public_key, is_fixed_length, max_length, Create_Date, " +
  642. "Permission_set_desc from sys.assemblies as assemblies join " +
  643. "sys.assembly_types as types on assemblies.assembly_id = types.assembly_id" +
  644. " where (assemblies.name = @AssemblyName or (@AssemblyName is null)) and " +
  645. "(types.assembly_class = @UDTName or (@UDTName is null))";
  646. AddParameter (command, "@AssemblyName", DbType.StringFixedLength, 4000);
  647. AddParameter (command, "@UDTName", DbType.StringFixedLength, 4000);
  648. break;
  649. case "MetaDataCollections":
  650. return MetaDataCollections.Instance;
  651. case "DataSourceInformation":
  652. throw new NotImplementedException ();
  653. case "DataTypes":
  654. return DataTypes.Instance;
  655. case "ReservedWords":
  656. return ReservedWords.Instance;
  657. case "Restrictions":
  658. return Restrictions.Instance;
  659. }
  660. for (int i = 0; i < length; i++) {
  661. command.Parameters [i].Value = restrictionValues [i];
  662. }
  663. DbProviderFactory dbProvider = DbProviderFactories.GetFactory (this.GetType (). ToString ());
  664. DbDataAdapter dataAdapter = dbProvider.CreateDataAdapter ();
  665. dataAdapter.SelectCommand = command;
  666. dataAdapter.Fill (dataTable);
  667. return dataTable;
  668. }
  669. #endif
  670. IDbTransaction IDbConnection.BeginTransaction ()
  671. {
  672. return BeginTransaction ();
  673. }
  674. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel il)
  675. {
  676. return BeginTransaction (il);
  677. }
  678. IDbCommand IDbConnection.CreateCommand ()
  679. {
  680. return CreateCommand ();
  681. }
  682. public abstract void Open ();
  683. protected virtual void OnStateChange (StateChangeEventArgs stateChanged)
  684. {
  685. if (StateChange != null)
  686. StateChange (this, stateChanged);
  687. }
  688. #endregion // Methods
  689. public virtual event StateChangeEventHandler StateChange;
  690. }
  691. }
  692. #endif