diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLGenericHandler.cs | 353 | ||||
-rw-r--r-- | OpenSim/Data/MySQL/MySQLRaw.cs | 142 |
2 files changed, 142 insertions, 353 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericHandler.cs b/OpenSim/Data/MySQL/MySQLGenericHandler.cs deleted file mode 100644 index 4f84641..0000000 --- a/OpenSim/Data/MySQL/MySQLGenericHandler.cs +++ /dev/null | |||
@@ -1,353 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using MySql.Data.MySqlClient; | ||
34 | using OpenMetaverse; | ||
35 | using OpenSim.Framework; | ||
36 | using OpenSim.Region.Framework.Interfaces; | ||
37 | |||
38 | namespace OpenSim.Data.MySQL | ||
39 | { | ||
40 | // public class MySQLGenericHandler<T> : MySqlFramework where T: class, new() | ||
41 | public class MySQLGenericHandler : MySqlFramework | ||
42 | { | ||
43 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | |||
45 | protected virtual Assembly Assembly | ||
46 | { | ||
47 | get { return GetType().Assembly; } | ||
48 | } | ||
49 | |||
50 | public MySQLGenericHandler(MySqlTransaction trans) : base(trans) | ||
51 | { | ||
52 | CommonConstruct(); | ||
53 | } | ||
54 | |||
55 | public MySQLGenericHandler(string connectionString) : base(connectionString) | ||
56 | { | ||
57 | CommonConstruct(); | ||
58 | } | ||
59 | |||
60 | protected void CommonConstruct() | ||
61 | { | ||
62 | // We always use a new connection for any Migrations | ||
63 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
64 | { | ||
65 | dbcon.Open(); | ||
66 | } | ||
67 | } | ||
68 | /* | ||
69 | private void CheckColumnNames(IDataReader reader) | ||
70 | { | ||
71 | if (m_ColumnNames != null) | ||
72 | return; | ||
73 | |||
74 | List<string> columnNames = new List<string>(); | ||
75 | |||
76 | DataTable schemaTable = reader.GetSchemaTable(); | ||
77 | foreach (DataRow row in schemaTable.Rows) | ||
78 | { | ||
79 | if (row["ColumnName"] != null) | ||
80 | columnNames.Add(row["ColumnName"].ToString()); | ||
81 | } | ||
82 | |||
83 | m_ColumnNames = columnNames; | ||
84 | } | ||
85 | */ | ||
86 | /* | ||
87 | public virtual T[] Get(string table, string field, string key) | ||
88 | { | ||
89 | return Get(table, new string[] { field }, new string[] { key }); | ||
90 | } | ||
91 | |||
92 | public virtual T[] Get(string table, string[] fields, string[] keys) | ||
93 | { | ||
94 | return Get(table, fields, keys, String.Empty); | ||
95 | } | ||
96 | |||
97 | public virtual T[] Get(string table, string[] fields, string[] keys, string options) | ||
98 | { | ||
99 | if (fields.Length != keys.Length) | ||
100 | return new T[0]; | ||
101 | |||
102 | List<string> terms = new List<string>(); | ||
103 | |||
104 | using (MySqlCommand cmd = new MySqlCommand()) | ||
105 | { | ||
106 | for (int i = 0 ; i < fields.Length ; i++) | ||
107 | { | ||
108 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | ||
109 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | ||
110 | } | ||
111 | |||
112 | string where = String.Join(" and ", terms.ToArray()); | ||
113 | |||
114 | string query = String.Format("select * from {0} where {1} {2}", | ||
115 | table, where, options); | ||
116 | |||
117 | cmd.CommandText = query; | ||
118 | |||
119 | return DoQuery(cmd); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | protected T[] DoQuery(MySqlCommand cmd) | ||
124 | { | ||
125 | if (m_trans == null) | ||
126 | { | ||
127 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
128 | { | ||
129 | dbcon.Open(); | ||
130 | T[] ret = DoQueryWithConnection(cmd, dbcon); | ||
131 | dbcon.Close(); | ||
132 | return ret; | ||
133 | } | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | return DoQueryWithTransaction(cmd, m_trans); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | protected T[] DoQueryWithTransaction(MySqlCommand cmd, MySqlTransaction trans) | ||
142 | { | ||
143 | cmd.Transaction = trans; | ||
144 | |||
145 | return DoQueryWithConnection(cmd, trans.Connection); | ||
146 | } | ||
147 | |||
148 | protected T[] DoQueryWithConnection(MySqlCommand cmd, MySqlConnection dbcon) | ||
149 | { | ||
150 | List<T> result = new List<T>(); | ||
151 | |||
152 | cmd.Connection = dbcon; | ||
153 | |||
154 | using (IDataReader reader = cmd.ExecuteReader()) | ||
155 | { | ||
156 | if (reader == null) | ||
157 | return new T[0]; | ||
158 | /* | ||
159 | |||
160 | CheckColumnNames(reader); | ||
161 | |||
162 | while (reader.Read()) | ||
163 | { | ||
164 | T row = new T(); | ||
165 | |||
166 | foreach (string name in m_Fields.Keys) | ||
167 | { | ||
168 | if (reader[name] is DBNull) | ||
169 | { | ||
170 | continue; | ||
171 | } | ||
172 | if (m_Fields[name].FieldType == typeof(bool)) | ||
173 | { | ||
174 | int v = Convert.ToInt32(reader[name]); | ||
175 | m_Fields[name].SetValue(row, v != 0 ? true : false); | ||
176 | } | ||
177 | else if (m_Fields[name].FieldType == typeof(UUID)) | ||
178 | { | ||
179 | m_Fields[name].SetValue(row, DBGuid.FromDB(reader[name])); | ||
180 | } | ||
181 | else if (m_Fields[name].FieldType == typeof(int)) | ||
182 | { | ||
183 | int v = Convert.ToInt32(reader[name]); | ||
184 | m_Fields[name].SetValue(row, v); | ||
185 | } | ||
186 | else if (m_Fields[name].FieldType == typeof(uint)) | ||
187 | { | ||
188 | uint v = Convert.ToUInt32(reader[name]); | ||
189 | m_Fields[name].SetValue(row, v); | ||
190 | } | ||
191 | else | ||
192 | { | ||
193 | m_Fields[name].SetValue(row, reader[name]); | ||
194 | } | ||
195 | } | ||
196 | |||
197 | if (m_DataField != null) | ||
198 | { | ||
199 | Dictionary<string, string> data = | ||
200 | new Dictionary<string, string>(); | ||
201 | |||
202 | foreach (string col in m_ColumnNames) | ||
203 | { | ||
204 | data[col] = reader[col].ToString(); | ||
205 | if (data[col] == null) | ||
206 | data[col] = String.Empty; | ||
207 | } | ||
208 | |||
209 | m_DataField.SetValue(row, data); | ||
210 | } | ||
211 | result.Add(row); | ||
212 | } | ||
213 | */ | ||
214 | /* | ||
215 | } | ||
216 | cmd.Connection = null; | ||
217 | return result.ToArray(); | ||
218 | } | ||
219 | |||
220 | public virtual T[] Get(string table, string where) | ||
221 | { | ||
222 | using (MySqlCommand cmd = new MySqlCommand()) | ||
223 | { | ||
224 | string query = String.Format("select * from {0} where {1}", | ||
225 | table, where); | ||
226 | |||
227 | cmd.CommandText = query; | ||
228 | |||
229 | return DoQuery(cmd); | ||
230 | } | ||
231 | } | ||
232 | */ | ||
233 | public virtual bool Delete(string table, string field, string key) | ||
234 | { | ||
235 | return Delete(table, new string[] { field }, new string[] { key }); | ||
236 | } | ||
237 | |||
238 | public virtual bool Delete(string table, string[] fields, string[] keys) | ||
239 | { | ||
240 | // m_log.DebugFormat( | ||
241 | // "[MYSQL GENERIC TABLE HANDLER]: Delete(string[] fields, string[] keys) invoked with {0}:{1}", | ||
242 | // string.Join(",", fields), string.Join(",", keys)); | ||
243 | |||
244 | if (fields.Length != keys.Length) | ||
245 | return false; | ||
246 | |||
247 | List<string> terms = new List<string>(); | ||
248 | |||
249 | using (MySqlCommand cmd = new MySqlCommand()) | ||
250 | { | ||
251 | for (int i = 0 ; i < fields.Length ; i++) | ||
252 | { | ||
253 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | ||
254 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | ||
255 | } | ||
256 | |||
257 | string where = String.Join(" and ", terms.ToArray()); | ||
258 | |||
259 | string query = String.Format("delete from {0} where {1}", table, where); | ||
260 | |||
261 | cmd.CommandText = query; | ||
262 | |||
263 | return ExecuteNonQuery(cmd) > 0; | ||
264 | } | ||
265 | } | ||
266 | |||
267 | public long GetCount(string table, string field, string key) | ||
268 | { | ||
269 | return GetCount(table, new string[] { field }, new string[] { key }); | ||
270 | } | ||
271 | |||
272 | public long GetCount(string table, string[] fields, string[] keys) | ||
273 | { | ||
274 | if (fields.Length != keys.Length) | ||
275 | return 0; | ||
276 | |||
277 | List<string> terms = new List<string>(); | ||
278 | |||
279 | using (MySqlCommand cmd = new MySqlCommand()) | ||
280 | { | ||
281 | for (int i = 0; i < fields.Length; i++) | ||
282 | { | ||
283 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | ||
284 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | ||
285 | } | ||
286 | |||
287 | string where = String.Join(" and ", terms.ToArray()); | ||
288 | |||
289 | string query = String.Format("select count(*) from {0} where {1}", | ||
290 | table, where); | ||
291 | |||
292 | cmd.CommandText = query; | ||
293 | |||
294 | Object result = DoQueryScalar(cmd); | ||
295 | |||
296 | return Convert.ToInt64(result); | ||
297 | } | ||
298 | } | ||
299 | |||
300 | public long GetCount(string table, string where) | ||
301 | { | ||
302 | using (MySqlCommand cmd = new MySqlCommand()) | ||
303 | { | ||
304 | string query = String.Format("select count(*) from {0} where {1}", | ||
305 | table, where); | ||
306 | |||
307 | cmd.CommandText = query; | ||
308 | |||
309 | object result = DoQueryScalar(cmd); | ||
310 | |||
311 | return Convert.ToInt64(result); | ||
312 | } | ||
313 | } | ||
314 | |||
315 | public long GetCount(string table) | ||
316 | { | ||
317 | using (MySqlCommand cmd = new MySqlCommand()) | ||
318 | { | ||
319 | string query = String.Format("select count(*) from {0}", table); | ||
320 | |||
321 | cmd.CommandText = query; | ||
322 | |||
323 | object result = DoQueryScalar(cmd); | ||
324 | |||
325 | return Convert.ToInt64(result); | ||
326 | } | ||
327 | } | ||
328 | |||
329 | public object DoQueryScalar(MySqlCommand cmd) | ||
330 | { | ||
331 | if (m_trans == null) | ||
332 | { | ||
333 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
334 | { | ||
335 | dbcon.Open(); | ||
336 | cmd.Connection = dbcon; | ||
337 | |||
338 | Object ret = cmd.ExecuteScalar(); | ||
339 | cmd.Connection = null; | ||
340 | dbcon.Close(); | ||
341 | return ret; | ||
342 | } | ||
343 | } | ||
344 | else | ||
345 | { | ||
346 | cmd.Connection = m_trans.Connection; | ||
347 | cmd.Transaction = m_trans; | ||
348 | |||
349 | return cmd.ExecuteScalar(); | ||
350 | } | ||
351 | } | ||
352 | } | ||
353 | } | ||
diff --git a/OpenSim/Data/MySQL/MySQLRaw.cs b/OpenSim/Data/MySQL/MySQLRaw.cs new file mode 100644 index 0000000..4be6d90 --- /dev/null +++ b/OpenSim/Data/MySQL/MySQLRaw.cs | |||
@@ -0,0 +1,142 @@ | |||
1 | // https://dev.mysql.com/doc/connector-net/en/ | ||
2 | |||
3 | |||
4 | using System; | ||
5 | using System.Collections; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Data; | ||
8 | using System.Reflection; | ||
9 | using System.Text; | ||
10 | using log4net; | ||
11 | using MySql.Data.MySqlClient; | ||
12 | |||
13 | namespace OpenSim.Data.MySQL | ||
14 | { | ||
15 | public class MySQLRaw | ||
16 | { | ||
17 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
18 | private string m_connectString; | ||
19 | |||
20 | public MySQLRaw(string connect) | ||
21 | { | ||
22 | m_connectString = connect; | ||
23 | } | ||
24 | |||
25 | public int Count(string table) | ||
26 | { | ||
27 | return Count(table, ""); | ||
28 | } | ||
29 | public int Count(string table, string wher) | ||
30 | { | ||
31 | string query = "SELECT Count(*) FROM " + table; | ||
32 | if ("" != wher) | ||
33 | query = query + " WHERE " + wher; | ||
34 | int result = -1; | ||
35 | |||
36 | object r = doScalarQuery(query); | ||
37 | if (r != null) | ||
38 | result = Convert.ToInt32(r); | ||
39 | |||
40 | return result; | ||
41 | } | ||
42 | |||
43 | public List< Hashtable > Select(string table, string wher) | ||
44 | { | ||
45 | string query = "SELECT * FROM " + table; | ||
46 | if ("" != wher) | ||
47 | query = query + " WHERE " + wher; | ||
48 | |||
49 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
50 | { | ||
51 | dbcon.Open(); | ||
52 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
53 | MySqlDataReader rdr = cmd.ExecuteReader(); | ||
54 | List<string> names = new List<string>(); | ||
55 | DataTable schema = rdr.GetSchemaTable(); | ||
56 | List< Hashtable > list = new List< Hashtable >(); | ||
57 | |||
58 | foreach (DataRow row in schema.Rows) | ||
59 | { | ||
60 | string tbl = ""; | ||
61 | string nm = ""; | ||
62 | string tp = ""; | ||
63 | foreach (DataColumn col in schema.Columns) | ||
64 | { | ||
65 | if ("BaseTableName" == col.ColumnName) tbl = row[col].ToString(); | ||
66 | if ("ColumnName" == col.ColumnName) nm = row[col].ToString(); | ||
67 | if ("DataType" == col.ColumnName) tp = row[col].ToString(); | ||
68 | } | ||
69 | names.Add(nm); | ||
70 | } | ||
71 | |||
72 | while (rdr.Read()) | ||
73 | { | ||
74 | Hashtable r = new Hashtable(); | ||
75 | foreach (string name in names) | ||
76 | { | ||
77 | r[name] = rdr[name]; | ||
78 | } | ||
79 | list.Add(r); | ||
80 | } | ||
81 | |||
82 | rdr.Close(); | ||
83 | dbcon.Close(); | ||
84 | return list; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | private object doScalarQuery(string query) | ||
89 | { | ||
90 | try | ||
91 | { | ||
92 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
93 | { | ||
94 | dbcon.Open(); | ||
95 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
96 | Object ret = cmd.ExecuteScalar(); | ||
97 | dbcon.Close(); | ||
98 | return ret; | ||
99 | } | ||
100 | } | ||
101 | catch (MySqlException e) | ||
102 | { | ||
103 | m_log.ErrorFormat("[MYSQL RAW]: Problem connecting to the database {0}", e.Message); | ||
104 | return null; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | private void doNonQuery(string query) | ||
109 | { | ||
110 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
111 | { | ||
112 | dbcon.Open(); | ||
113 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
114 | cmd.ExecuteNonQuery(); | ||
115 | dbcon.Close(); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | public void Insert(string table) | ||
120 | { | ||
121 | string query = "INSERT INTO " + table + " (name, age) VALUES('John Smith', '33')"; | ||
122 | doNonQuery(query); | ||
123 | } | ||
124 | |||
125 | public void Update(string table, string wher) | ||
126 | { | ||
127 | string query = "UPDATE " + table + " SET name='Joe', age='22'"; | ||
128 | if ("" != wher) | ||
129 | query = query + " WHERE " + wher; | ||
130 | doNonQuery(query); | ||
131 | } | ||
132 | |||
133 | public void Delete(string table, string wher) | ||
134 | { | ||
135 | string query = "DELETE FROM " + table; | ||
136 | if ("" != wher) | ||
137 | query = query + " WHERE " + wher; | ||
138 | doNonQuery(query); | ||
139 | } | ||
140 | |||
141 | } | ||
142 | } | ||