PageRenderTime 86ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data/Test/DataProviderTests/setup/Setup.cs

https://github.com/pruiz/mono
C# | 321 lines | 228 code | 60 blank | 33 comment | 39 complexity | 81c97c3f423ad0121bf3923d7584f554 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Setup.cs : Setup class for
  3. // - creating, dropping, insert data into database tables
  4. // - Setup/teardown of stored procedures
  5. //
  6. //
  7. // Author:
  8. // Satya Sudha K (ksathyasudha@novell.com)
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.IO;
  34. using System.Xml;
  35. using System.Data;
  36. using System.Data.OracleClient;
  37. using System.Data.SqlClient;
  38. using System.Configuration;
  39. namespace MonoTests.System.Data {
  40. public class Setup {
  41. string [] databases;
  42. IDbConnection con;
  43. IDbCommand cmd;
  44. XmlNode node;
  45. string curDatabase;
  46. TableInfo [] tabinfo;
  47. string createStoredProc;
  48. string deleteTables;
  49. string createTables;
  50. string insertData;
  51. public Setup (string [] listOfDbs)
  52. {
  53. if (listOfDbs.Length == 0) {
  54. string dbList = ConfigurationSettings.AppSettings ["Databases"];
  55. databases = dbList.Split (';');
  56. } else
  57. databases = (string []) listOfDbs.Clone ();
  58. Reset ();
  59. }
  60. void Reset ()
  61. {
  62. if (con != null)
  63. con.Close ();
  64. con = null;
  65. cmd = null;
  66. node = null;
  67. tabinfo = null;
  68. curDatabase = null;
  69. createStoredProc = null;
  70. deleteTables = null;
  71. createTables = null;
  72. insertData = null;
  73. }
  74. bool Initialize (string database)
  75. {
  76. curDatabase = database;
  77. node = (XmlNode) ConfigurationSettings.GetConfig (database);
  78. con = GetConnection (database);
  79. try {
  80. con.Open ();
  81. } catch (Exception e) {
  82. createStoredProc = deleteTables = createTables = insertData = null;
  83. Console.WriteLine (e.Message);
  84. Reset ();
  85. return false;
  86. }
  87. cmd = con.CreateCommand ();
  88. createStoredProc = ConfigClass.GetElement (node, "createStoredProc");
  89. deleteTables = ConfigClass.GetElement (node, "deleteTables");
  90. createTables = ConfigClass.GetElement (node, "createTables");
  91. insertData = ConfigClass.GetElement (node, "insertData");
  92. string noOfTables = ConfigClass.GetElement (node, "tables", "numTables");
  93. int numTables = Convert.ToInt32 (noOfTables);
  94. tabinfo = new TableInfo [numTables];
  95. for (int i = 1; i <= numTables; i++)
  96. tabinfo [i - 1].Initialize (node, i);
  97. return true;
  98. }
  99. public void SetupDatabase ()
  100. {
  101. foreach (string db in databases) {
  102. bool hasErrors = false;
  103. Console.WriteLine ("\n ******** Doing setup for {0} database ********\n", db);
  104. if (Initialize (db) != true) {
  105. Console.WriteLine ("Failed to do the initialisation for {0} database", db);
  106. Console.WriteLine ("Skipping setup for " + db);
  107. hasErrors = true;
  108. continue;
  109. }
  110. Console.WriteLine (" *** Running the following queries ***\n");
  111. if (deleteTables.Equals ("Y")) {
  112. if (DeleteTables ()!= true)
  113. hasErrors = true;
  114. }
  115. if (createTables.Equals ("Y")) {
  116. if (CreateTables () != true)
  117. hasErrors = true;
  118. }
  119. if (insertData.Equals ("Y")) {
  120. if (InsertData () != true) {
  121. hasErrors = true;
  122. }
  123. }
  124. if (createStoredProc.Equals ("Y")) {
  125. int numStoredProc = Convert.ToInt32 (ConfigClass.GetElement (node,
  126. "StoredProc", "NumStoredProc"));
  127. for (int i = 1; i <= numStoredProc; i++) {
  128. if (CreateStoredProc (i) != true)
  129. hasErrors = true;
  130. }
  131. if (hasErrors == true)
  132. Console.WriteLine ("There were errors while setting up the {0} database", db);
  133. else
  134. Console.WriteLine ("Successfully set up the {0} database", db);
  135. }
  136. }
  137. }
  138. bool CreateTables ()
  139. {
  140. string createQuery;
  141. for (int i = 1; i<= tabinfo.Length; i++) {
  142. string [] constraints = ConfigClass.GetColumnDetails (node, i, "constraint");
  143. createQuery = "create table " + tabinfo [i - 1].name;
  144. createQuery += "(";
  145. for (int col = 1; col <= tabinfo [i - 1].columns.Length; col++) {
  146. createQuery += tabinfo [i - 1].columns [col - 1];
  147. createQuery += " " ;
  148. createQuery += tabinfo [i - 1].types [col - 1];
  149. createQuery += " " + constraints [col - 1];
  150. createQuery += ",";
  151. }
  152. createQuery = createQuery.Trim (',');
  153. createQuery += ")";
  154. Console.WriteLine (createQuery);
  155. cmd.CommandText = createQuery;
  156. cmd.ExecuteNonQuery ();
  157. }
  158. return true;
  159. }
  160. bool InsertData ()
  161. {
  162. int numTables = Convert.ToInt32 (ConfigClass.GetElement (node, "values", "numTables"));
  163. string tableName;
  164. for (int i = 1; i <= numTables; i++) {
  165. string tableTag = "table" + i;
  166. tableName = ConfigClass.GetElement (node, "values", tableTag, "tableName");
  167. int numRows = Convert.ToInt32 (ConfigClass.GetElement (node, "values", tableTag, "numRows"));
  168. int numCols = Convert.ToInt32 (ConfigClass.GetElement (node, "values", tableTag, "numCols"));
  169. for (int j = 1; j <= numRows; j++) {
  170. string rowTag = "row" + j;
  171. string insertQuery = "Insert into " + tableName + " values (";
  172. for (int k = 1; k <= numCols; k++) {
  173. string colTag = "column"+k;
  174. insertQuery += ConfigClass.GetElement (node, "values", tableTag, rowTag, colTag);
  175. insertQuery += ",";
  176. }
  177. insertQuery = insertQuery.Trim (',');
  178. insertQuery += ")";
  179. Console.WriteLine (insertQuery);
  180. cmd.CommandText = insertQuery;
  181. try {
  182. cmd.ExecuteNonQuery ();
  183. } catch (Exception e) {
  184. Console.WriteLine ("Failed to insert row into the table:" +
  185. tableName + " " + e.Message);
  186. return false;
  187. }
  188. }
  189. }
  190. return true;
  191. }
  192. bool DeleteTables ()
  193. {
  194. string deleteQuery;
  195. bool retval = true;
  196. for (int i = 1; i <= tabinfo.Length; i++) {
  197. deleteQuery = "drop table " + tabinfo [i - 1].name;
  198. Console.WriteLine (deleteQuery);
  199. cmd.CommandText = deleteQuery;
  200. try {
  201. cmd.ExecuteNonQuery ();
  202. } catch (Exception e) {
  203. Console.WriteLine ("Unable to drop table :" + tabinfo [i - 1].name + ":" + e.Message);
  204. retval = false;
  205. }
  206. }
  207. return retval;
  208. }
  209. bool CreateStoredProc (int storedProcNum)
  210. {
  211. string name = ConfigClass.GetElement (node, "StoredProc", "StoredProc" + storedProcNum, "name");
  212. string type = ConfigClass.GetElement (node, "StoredProc", "StoredProc" + storedProcNum, "type");
  213. int numStatements = Convert.ToInt32 (ConfigClass.GetElement (node, "StoredProc",
  214. "StoredProc" + storedProcNum, "template", "numStmts"));
  215. string [] templates = new string [numStatements];
  216. for (int i = 1; i <= numStatements; i++)
  217. templates [i - 1] = ConfigClass.GetElement (node, "StoredProc", "StoredProc" + storedProcNum, "template", "stmt"+i);
  218. if (type.Equals ("generic")) {
  219. // To be created for all tables
  220. for (int tableNum = 0; tableNum < tabinfo.Length; tableNum ++) {
  221. string storedProcName = name.Replace ("{{TABLE}}", tabinfo [tableNum].name);
  222. Console.WriteLine ("Creating : " + storedProcName);
  223. for (int index = 1; index <= numStatements; index++) {
  224. string SPtemplate = templates [index - 1];
  225. SPtemplate = SPtemplate.Replace ("{{TABLE}}", tabinfo [tableNum].name);
  226. string listOfColumns = String.Join (",", tabinfo [tableNum].columns);
  227. SPtemplate = SPtemplate.Replace ("{{TABLE}}", tabinfo [tableNum].name);
  228. SPtemplate = SPtemplate.Replace ("{{COLUMNS}}", listOfColumns);
  229. int beg = 0;
  230. while ((beg = SPtemplate.IndexOf ("{{COLUMN_")) >= 0) {
  231. int end = SPtemplate.IndexOf ("}}", beg + 9);
  232. string strToBeReplaced = SPtemplate.Substring (beg, end - beg + 2);
  233. beg += 9;
  234. int columnNum = Convert.ToInt32 (SPtemplate.Substring (beg, end - beg));
  235. SPtemplate = SPtemplate.Replace (strToBeReplaced,
  236. tabinfo [tableNum].columns [columnNum]);
  237. }
  238. SPtemplate = SPtemplate.Replace ("\r", "");
  239. cmd.CommandText = SPtemplate;
  240. Console.WriteLine (SPtemplate);
  241. cmd.ExecuteNonQuery ();
  242. }
  243. }
  244. } else {
  245. // To be implemented
  246. }
  247. return true;
  248. }
  249. IDbConnection GetConnection (string database)
  250. {
  251. IDbConnection con = null;
  252. string connStr = ConfigClass.GetElement (node, "database", "connectionString");
  253. if (database == "oracle") {
  254. con = new OracleConnection (connStr);
  255. } else if (database == "mssql") {
  256. con = new SqlConnection (connStr);
  257. }
  258. return con;
  259. }
  260. }
  261. }