aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MSSQL/MSSQLManager.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/MSSQL/MSSQLManager.cs225
1 files changed, 4 insertions, 221 deletions
diff --git a/OpenSim/Data/MSSQL/MSSQLManager.cs b/OpenSim/Data/MSSQL/MSSQLManager.cs
index 3d7a768..4309b42 100644
--- a/OpenSim/Data/MSSQL/MSSQLManager.cs
+++ b/OpenSim/Data/MSSQL/MSSQLManager.cs
@@ -46,22 +46,7 @@ namespace OpenSim.Data.MSSQL
46 /// <summary> 46 /// <summary>
47 /// Connection string for ADO.net 47 /// Connection string for ADO.net
48 /// </summary> 48 /// </summary>
49 private readonly string connectionString; 49 private readonly string connectionString;
50
51 public MSSQLManager(string dataSource, string initialCatalog, string persistSecurityInfo, string userId,
52 string password)
53 {
54 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
55
56 builder.DataSource = dataSource;
57 builder.InitialCatalog = initialCatalog;
58 builder.PersistSecurityInfo = Convert.ToBoolean(persistSecurityInfo);
59 builder.UserID = userId;
60 builder.Password = password;
61 builder.ApplicationName = Assembly.GetEntryAssembly().Location;
62
63 connectionString = builder.ToString();
64 }
65 50
66 /// <summary> 51 /// <summary>
67 /// Initialize the manager and set the connectionstring 52 /// Initialize the manager and set the connectionstring
@@ -72,94 +57,6 @@ namespace OpenSim.Data.MSSQL
72 connectionString = connection; 57 connectionString = connection;
73 } 58 }
74 59
75 public SqlConnection DatabaseConnection()
76 {
77 SqlConnection conn = new SqlConnection(connectionString);
78
79 //TODO is this good??? Opening connection here
80 conn.Open();
81
82 return conn;
83 }
84
85 #region Obsolete functions, can be removed!
86
87 /// <summary>
88 ///
89 /// </summary>
90 /// <param name="dt"></param>
91 /// <param name="name"></param>
92 /// <param name="type"></param>
93 [Obsolete("Do not use!")]
94 protected static void createCol(DataTable dt, string name, Type type)
95 {
96 DataColumn col = new DataColumn(name, type);
97 dt.Columns.Add(col);
98 }
99
100 /// <summary>
101 /// Define Table function
102 /// </summary>
103 /// <param name="dt"></param>
104 /// <returns></returns>
105/*
106 [Obsolete("Do not use!")]
107 protected static string defineTable(DataTable dt)
108 {
109 string sql = "create table " + dt.TableName + "(";
110 string subsql = String.Empty;
111 foreach (DataColumn col in dt.Columns)
112 {
113 if (subsql.Length > 0)
114 {
115 // a map function would rock so much here
116 subsql += ",\n";
117 }
118
119 subsql += col.ColumnName + " " + SqlType(col.DataType);
120 if (col == dt.PrimaryKey[0])
121 {
122 subsql += " primary key";
123 }
124 }
125 sql += subsql;
126 sql += ")";
127 return sql;
128 }
129*/
130
131 #endregion
132
133 /// <summary>
134 /// Type conversion function
135 /// </summary>
136 /// <param name="type">a type</param>
137 /// <returns>a sqltype</returns>
138 /// <remarks>this is something we'll need to implement for each db slightly differently.</remarks>
139/*
140 [Obsolete("Used by a obsolete methods")]
141 public static string SqlType(Type type)
142 {
143 if (type == typeof(String))
144 {
145 return "varchar(255)";
146 }
147 if (type == typeof(Int32))
148 {
149 return "integer";
150 }
151 if (type == typeof(Double))
152 {
153 return "float";
154 }
155 if (type == typeof(Byte[]))
156 {
157 return "image";
158 }
159 return "varchar(255)";
160 }
161*/
162
163 /// <summary> 60 /// <summary>
164 /// Type conversion to a SQLDbType functions 61 /// Type conversion to a SQLDbType functions
165 /// </summary> 62 /// </summary>
@@ -286,134 +183,20 @@ namespace OpenSim.Data.MSSQL
286 private static readonly Dictionary<string, string> emptyDictionary = new Dictionary<string, string>(); 183 private static readonly Dictionary<string, string> emptyDictionary = new Dictionary<string, string>();
287 184
288 /// <summary> 185 /// <summary>
289 /// Run a query and return a sql db command
290 /// </summary>
291 /// <param name="sql">The SQL query.</param>
292 /// <returns></returns>
293 internal AutoClosingSqlCommand Query(string sql)
294 {
295 return Query(sql, emptyDictionary);
296 }
297
298 /// <summary>
299 /// Runs a query with protection against SQL Injection by using parameterised input.
300 /// </summary>
301 /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
302 /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
303 /// <returns>A Sql DB Command</returns>
304 internal AutoClosingSqlCommand Query(string sql, Dictionary<string, string> parameters)
305 {
306 SqlCommand dbcommand = DatabaseConnection().CreateCommand();
307 dbcommand.CommandText = sql;
308 foreach (KeyValuePair<string, string> param in parameters)
309 {
310 dbcommand.Parameters.AddWithValue(param.Key, param.Value);
311 }
312
313 return new AutoClosingSqlCommand(dbcommand);
314 }
315
316 /// <summary>
317 /// Runs a query with protection against SQL Injection by using parameterised input.
318 /// </summary>
319 /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
320 /// <param name="sqlParameter">A parameter - use createparameter to create parameter</param>
321 /// <returns></returns>
322 internal AutoClosingSqlCommand Query(string sql, SqlParameter sqlParameter)
323 {
324 SqlCommand dbcommand = DatabaseConnection().CreateCommand();
325 dbcommand.CommandText = sql;
326 dbcommand.Parameters.Add(sqlParameter);
327
328 return new AutoClosingSqlCommand(dbcommand);
329 }
330
331 /// <summary>
332 /// Checks if we need to do some migrations to the database 186 /// Checks if we need to do some migrations to the database
333 /// </summary> 187 /// </summary>
334 /// <param name="migrationStore">migrationStore.</param> 188 /// <param name="migrationStore">migrationStore.</param>
335 public void CheckMigration(string migrationStore) 189 public void CheckMigration(string migrationStore)
336 { 190 {
337 using (SqlConnection connection = DatabaseConnection()) 191 using (SqlConnection connection = new SqlConnection(connectionString))
338 { 192 {
193 connection.Open();
339 Assembly assem = GetType().Assembly; 194 Assembly assem = GetType().Assembly;
340 MSSQLMigration migration = new MSSQLMigration(connection, assem, migrationStore); 195 MSSQLMigration migration = new MSSQLMigration(connection, assem, migrationStore);
341 196
342 migration.Update(); 197 migration.Update();
343
344 connection.Close();
345 } 198 }
346 } 199 }
347
348 #region Old Testtable functions
349
350 /// <summary>
351 /// Execute a SQL statement stored in a resource, as a string
352 /// </summary>
353 /// <param name="name">the ressource string</param>
354 public void ExecuteResourceSql(string name)
355 {
356 using (IDbCommand cmd = Query(getResourceString(name), new Dictionary<string, string>()))
357 {
358 cmd.ExecuteNonQuery();
359 }
360 }
361
362 /// <summary>
363 /// Given a list of tables, return the version of the tables, as seen in the database
364 /// </summary>
365 /// <param name="tableList"></param>
366 public void GetTableVersion(Dictionary<string, string> tableList)
367 {
368 Dictionary<string, string> param = new Dictionary<string, string>();
369 param["dbname"] = new SqlConnectionStringBuilder(connectionString).InitialCatalog;
370
371 using (IDbCommand tablesCmd =
372 Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_CATALOG=@dbname", param))
373 using (IDataReader tables = tablesCmd.ExecuteReader())
374 {
375 while (tables.Read())
376 {
377 try
378 {
379 string tableName = (string)tables["TABLE_NAME"];
380 if (tableList.ContainsKey(tableName))
381 tableList[tableName] = tableName;
382 }
383 catch (Exception e)
384 {
385 m_log.Error(e.ToString());
386 }
387 }
388 tables.Close();
389 }
390
391 }
392
393 /// <summary>
394 ///
395 /// </summary>
396 /// <param name="name"></param>
397 /// <returns></returns>
398 private string getResourceString(string name)
399 {
400 Assembly assem = GetType().Assembly;
401 string[] names = assem.GetManifestResourceNames();
402
403 foreach (string s in names)
404 if (s.EndsWith(name))
405 using (Stream resource = assem.GetManifestResourceStream(s))
406 {
407 using (StreamReader resourceReader = new StreamReader(resource))
408 {
409 string resourceString = resourceReader.ReadToEnd();
410 return resourceString;
411 }
412 }
413 throw new Exception(string.Format("Resource '{0}' was not found", name));
414 }
415
416 #endregion
417 200
418 /// <summary> 201 /// <summary>
419 /// Returns the version of this DB provider 202 /// Returns the version of this DB provider