MemoryMappedFile support on Windows
[mono.git] / mcs / class / System.Data / Test / MySqlTestBed.cs
1 //
2 // MySqlTestBed.cs : This is base class which manages the connections to 
3 //                    mysql database. This serves as a base class for all
4 //                    mysql database dependant tests.
5 //
6 // To run :
7 //  * create a test database in mysql server.
8 //  * create an DNS entry.
9 //  * update the MySqlTestBed.config with the DNS names and
10 //    username, password for connection in the configuration key
11 //    MySql-DSN
12 //  * compile using following command
13 //      mcs /r:System.Data.dll,NUnit.Framework.dll /t:library /debug
14 //      /out:MySqlTestBed.dll MySqlTestBed.cs System.Data.Odbc/*.cs
15 //  * To run the tests
16 //      mono /usr/local/bin/nunit-console.exe MySqlTestBed.dll
17 //
18 // Author:
19 //      Sureshkumar T (TSureshkumar@novell.com)
20 //
21 // Copyright (c) 2004 Novell Inc., and the individuals listed
22 // on the ChangeLog entries.
23 // 
24 // Permission is hereby granted, free of charge, to any person obtaining
25 // a copy of this software and associated documentation files (the
26 // "Software"), to deal in the Software without restriction, including
27 // without limitation the rights to use, copy, modify, merge, publish,
28 // distribute, sublicense, and/or sell copies of the Software, and to
29 // permit persons to whom the Software is furnished to do so, subject to
30 // the following conditions:
31 // 
32 // The above copyright notice and this permission notice shall be
33 // included in all copies or substantial portions of the Software.
34 // 
35 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
39 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
40 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
41 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 //
43
44 using System;
45 using System.Data;
46 using System.Configuration;
47 using System.Data.Odbc;
48 using System.Collections.Specialized;
49
50 namespace MonoTests.System.Data 
51 {
52         public class MySqlOdbcBaseClient  
53         {
54                 #region protected members
55                 protected string connectionString = null;
56                 protected OdbcConnection conn = null;
57                 protected bool isConnAlive = false;
58                 #endregion
59
60                 public MySqlOdbcBaseClient ()
61                 {
62                         //Connection String with DSN.
63                         NameValueCollection appSettings = ConfigurationSettings.AppSettings ;
64                         connectionString = appSettings ["MySql-DSN"];
65                         conn = new OdbcConnection (connectionString);
66                 }
67
68                 protected void OpenConnection () 
69                 {
70                         conn.ConnectionString = connectionString;
71                         conn.Open ();
72                         // run tests only if the connection is open,
73                         // otherwise make it fail, to setup with correct
74                         // database settings
75                         if (conn != null && conn.State != ConnectionState.Closed) 
76                                 isConnAlive = true;
77                 }
78
79                 protected void CloseConnection () 
80                 {
81                         if (conn != null && conn.State != ConnectionState.Closed) {
82                                 conn.Close ();
83                                 isConnAlive = false;
84                         }
85                 }
86
87                 protected void CreateTestSetup ()
88                 {
89                         if (!isConnAlive)
90                                 return ;
91                         // Create test database & tables
92                         // mysql odbc does not supports batch sql statements
93                         string createQuery = "DROP TABLE IF EXISTS test;" ;
94                         ExecuteQuery (createQuery);
95                         createQuery = "CREATE TABLE test (" + 
96                                           "pk_tint TINYINT NOT NULL PRIMARY KEY," + 
97                                           "col_char CHAR(20)," + 
98                                           "col_int INT," + 
99                                           "col_blob TINYBLOB," + 
100                                           "col_datetime DATETIME," + 
101                                           "col_date DATE," + 
102                                           "col_time TIME" + 
103                                           ");";  
104                         ExecuteQuery (createQuery);
105                         createQuery = "INSERT INTO test VALUES (1, 'mono test" +
106                                       "#1', 255, 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00' );" ; 
107                         ExecuteQuery (createQuery);
108                         createQuery = "INSERT INTO test VALUES (2, 'mono test" +
109                                       "#2', 256, NULL, NULL, NULL, NULL );";
110                         ExecuteQuery (createQuery);
111                         createQuery = "INSERT INTO test VALUES (3, 'mono test" +
112                                       "#3', 257 , 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00');" ; 
113                         ExecuteQuery (createQuery);
114                         createQuery = "INSERT INTO test VALUES (4, 'mono test" +
115                                       "#4', 258 , 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00');" ; 
116                         ExecuteQuery (createQuery);
117                         createQuery = "INSERT INTO test VALUES (5, 'mono test" +
118                                       "#5', 259, 127123645917568585638457243856234985, '2004-08-22', '2004-08-22', '12:00:00' );" ;
119                         ExecuteQuery (createQuery);
120                 }
121
122                 private void ExecuteQuery (string query) 
123                 {
124                         OdbcCommand cmd = new OdbcCommand ();
125                         cmd.Connection = conn;
126                         cmd.CommandText = query;
127                         try {
128                                 int recordsAff = cmd.ExecuteNonQuery ();
129                         } catch (Exception e) {
130                         }
131                 }
132
133                 protected void CleanTestSetup ()
134                 {
135                         if (!isConnAlive)
136                                 return;
137                         // delete test database 
138                         string dropQuery = "DROP table IF EXISTS test";
139                         //ExecuteQuery(dropQuery);
140                 }
141         }
142 }