Setup.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 ([email protected])
  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 ByteFX.Data.MySqlClient;
  39. using Npgsql;
  40. using System.Configuration;
  41. namespace MonoTests.System.Data {
  42. public class Setup {
  43. string [] databases;
  44. IDbConnection con;
  45. IDbCommand cmd;
  46. XmlNode node;
  47. string curDatabase;
  48. TableInfo [] tabinfo;
  49. string createStoredProc;
  50. string deleteTables;
  51. string createTables;
  52. string insertData;
  53. public Setup (string [] listOfDbs)
  54. {
  55. if (listOfDbs.Length == 0) {
  56. string dbList = ConfigurationSettings.AppSettings ["Databases"];
  57. databases = dbList.Split (';');
  58. } else
  59. databases = (string []) listOfDbs.Clone ();
  60. Reset ();
  61. }
  62. void Reset ()
  63. {
  64. if (con != null)
  65. con.Close ();
  66. con = null;
  67. cmd = null;
  68. node = null;
  69. tabinfo = null;
  70. curDatabase = null;
  71. createStoredProc = null;
  72. deleteTables = null;
  73. createTables = null;
  74. insertData = null;
  75. }
  76. bool Initialize (string database)
  77. {
  78. curDatabase = database;
  79. node = (XmlNode) ConfigurationSettings.GetConfig (database);
  80. con = GetConnection (database);
  81. try {
  82. con.Open ();
  83. } catch (Exception e) {
  84. createStoredProc = deleteTables = createTables = insertData = null;
  85. Console.WriteLine (e.Message);
  86. Reset ();
  87. return false;
  88. }
  89. cmd = con.CreateCommand ();
  90. createStoredProc = ConfigClass.GetElement (node, "createStoredProc");
  91. deleteTables = ConfigClass.GetElement (node, "deleteTables");
  92. createTables = ConfigClass.GetElement (node, "createTables");
  93. insertData = ConfigClass.GetElement (node, "insertData");
  94. string noOfTables = ConfigClass.GetElement (node, "tables", "numTables");
  95. int numTables = Convert.ToInt32 (noOfTables);
  96. tabinfo = new TableInfo [numTables];
  97. for (int i = 1; i <= numTables; i++)
  98. tabinfo [i - 1].Initialize (node, i);
  99. return true;
  100. }
  101. public void SetupDatabase ()
  102. {
  103. foreach (string db in databases) {
  104. bool hasErrors = false;
  105. Console.WriteLine ("\n ******** Doing setup for {0} database ********\n", db);
  106. if (Initialize (db) != true) {
  107. Console.WriteLine ("Failed to do the initialisation for {0} database", db);
  108. Console.WriteLine ("Skipping setup for " + db);
  109. hasErrors = true;
  110. continue;
  111. }
  112. Console.WriteLine (" *** Running the following queries ***\n");
  113. if (deleteTables.Equals ("Y")) {
  114. if (DeleteTables ()!= true)
  115. hasErrors = true;
  116. }
  117. if (createTables.Equals ("Y")) {
  118. if (CreateTables () != true)
  119. hasErrors = true;
  120. }
  121. if (insertData.Equals ("Y")) {
  122. if (InsertData () != true) {
  123. hasErrors = true;
  124. }
  125. }
  126. if (createStoredProc.Equals ("Y")) {
  127. int numStoredProc = Convert.ToInt32 (ConfigClass.GetElement (node,
  128. "StoredProc", "NumStoredProc"));
  129. for (int i = 1; i <= numStoredProc; i++) {
  130. if (CreateStoredProc (i) != true)
  131. hasErrors = true;
  132. }
  133. if (hasErrors == true)
  134. Console.WriteLine ("There were errors while setting up the {0} database", db);
  135. else
  136. Console.WriteLine ("Successfully set up the {0} database", db);
  137. }
  138. }
  139. }
  140. bool CreateTables ()
  141. {
  142. string createQuery;
  143. for (int i = 1; i<= tabinfo.Length; i++) {
  144. string [] constraints = ConfigClass.GetColumnDetails (node, i, "constraint");
  145. createQuery = "create table " + tabinfo [i - 1].name;
  146. createQuery += "(";
  147. for (int col = 1; col <= tabinfo [i - 1].columns.Length; col++) {
  148. createQuery += tabinfo [i - 1].columns [col - 1];
  149. createQuery += " " ;
  150. createQuery += tabinfo [i - 1].types [col - 1];
  151. createQuery += " " + constraints [col - 1];
  152. createQuery += ",";
  153. }
  154. createQuery = createQuery.Trim (',');
  155. createQuery += ")";
  156. Console.WriteLine (createQuery);
  157. cmd.CommandText = createQuery;
  158. cmd.ExecuteNonQuery ();
  159. }
  160. return true;
  161. }
  162. bool InsertData ()
  163. {
  164. int numTables = Convert.ToInt32 (ConfigClass.GetElement (node, "values", "numTables"));
  165. string tableName;
  166. for (int i = 1; i <= numTables; i++) {
  167. string tableTag = "table" + i;
  168. tableName = ConfigClass.GetElement (node, "values", tableTag, "tableName");
  169. int numRows = Convert.ToInt32 (ConfigClass.GetElement (node, "values", tableTag, "numRows"));
  170. int numCols = Convert.ToInt32 (ConfigClass.GetElement (node, "values", tableTag, "numCols"));
  171. for (int j = 1; j <= numRows; j++) {
  172. string rowTag = "row" + j;
  173. string insertQuery = "Insert into " + tableName + " values (";
  174. for (int k = 1; k <= numCols; k++) {
  175. string colTag = "column"+k;
  176. insertQuery += ConfigClass.GetElement (node, "values", tableTag, rowTag, colTag);
  177. insertQuery += ",";
  178. }
  179. insertQuery = insertQuery.Trim (',');
  180. insertQuery += ")";
  181. Console.WriteLine (insertQuery);
  182. cmd.CommandText = insertQuery;
  183. try {
  184. cmd.ExecuteNonQuery ();
  185. } catch (Exception e) {
  186. Console.WriteLine ("Failed to insert row into the table:" +
  187. tableName + " " + e.Message);
  188. return false;
  189. }
  190. }
  191. }
  192. return true;
  193. }
  194. bool DeleteTables ()
  195. {
  196. string deleteQuery;
  197. bool retval = true;
  198. for (int i = 1; i <= tabinfo.Length; i++) {
  199. deleteQuery = "drop table " + tabinfo [i - 1].name;
  200. Console.WriteLine (deleteQuery);
  201. cmd.CommandText = deleteQuery;
  202. try {
  203. cmd.ExecuteNonQuery ();
  204. } catch (Exception e) {
  205. Console.WriteLine ("Unable to drop table :" + tabinfo [i - 1].name + ":" + e.Message);
  206. retval = false;
  207. }
  208. }
  209. return retval;
  210. }
  211. bool CreateStoredProc (int storedProcNum)
  212. {
  213. string name = ConfigClass.GetElement (node, "StoredProc", "StoredProc" + storedProcNum, "name");
  214. string type = ConfigClass.GetElement (node, "StoredProc", "StoredProc" + storedProcNum, "type");
  215. int numStatements = Convert.ToInt32 (ConfigClass.GetElement (node, "StoredProc",
  216. "StoredProc" + storedProcNum, "template", "numStmts"));
  217. string [] templates = new string [numStatements];
  218. for (int i = 1; i <= numStatements; i++)
  219. templates [i - 1] = ConfigClass.GetElement (node, "StoredProc", "StoredProc" + storedProcNum, "template", "stmt"+i);
  220. if (type.Equals ("generic")) {
  221. // To be created for all tables
  222. for (int tableNum = 0; tableNum < tabinfo.Length; tableNum ++) {
  223. string storedProcName = name.Replace ("{{TABLE}}", tabinfo [tableNum].name);
  224. Console.WriteLine ("Creating : " + storedProcName);
  225. for (int index = 1; index <= numStatements; index++) {
  226. string SPtemplate = templates [index - 1];
  227. SPtemplate = SPtemplate.Replace ("{{TABLE}}", tabinfo [tableNum].name);
  228. string listOfColumns = String.Join (",", tabinfo [tableNum].columns);
  229. SPtemplate = SPtemplate.Replace ("{{TABLE}}", tabinfo [tableNum].name);
  230. SPtemplate = SPtemplate.Replace ("{{COLUMNS}}", listOfColumns);
  231. int beg = 0;
  232. while ((beg = SPtemplate.IndexOf ("{{COLUMN_")) >= 0) {
  233. int end = SPtemplate.IndexOf ("}}", beg + 9);
  234. string strToBeReplaced = SPtemplate.Substring (beg, end - beg + 2);
  235. beg += 9;
  236. int columnNum = Convert.ToInt32 (SPtemplate.Substring (beg, end - beg));
  237. SPtemplate = SPtemplate.Replace (strToBeReplaced,
  238. tabinfo [tableNum].columns [columnNum]);
  239. }
  240. SPtemplate = SPtemplate.Replace ("\r", "");
  241. cmd.CommandText = SPtemplate;
  242. Console.WriteLine (SPtemplate);
  243. cmd.ExecuteNonQuery ();
  244. }
  245. }
  246. } else {
  247. // To be implemented
  248. }
  249. return true;
  250. }
  251. IDbConnection GetConnection (string database)
  252. {
  253. IDbConnection con = null;
  254. string connStr = ConfigClass.GetElement (node, "database", "connectionString");
  255. if (database == "oracle") {
  256. con = new OracleConnection (connStr);
  257. } else if (database == "mysql") {
  258. con = new MySqlConnection (connStr);
  259. } else if (database == "mssql") {
  260. con = new SqlConnection (connStr);
  261. } else if (database == "postgres") {
  262. con = new NpgsqlConnection (connStr);
  263. }
  264. return con;
  265. }
  266. }
  267. }