aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim
diff options
context:
space:
mode:
authorMelanie2009-11-15 20:19:19 +0000
committerMelanie2009-11-15 20:19:19 +0000
commit99977902119d0bb981f507a5a6a96c1148766e77 (patch)
tree5fc065cec208e10db9adde20e55cbd2a10746959 /OpenSim
parentMerge branch 'master' into vehicles (diff)
downloadopensim-SC_OLD-99977902119d0bb981f507a5a6a96c1148766e77.zip
opensim-SC_OLD-99977902119d0bb981f507a5a6a96c1148766e77.tar.gz
opensim-SC_OLD-99977902119d0bb981f507a5a6a96c1148766e77.tar.bz2
opensim-SC_OLD-99977902119d0bb981f507a5a6a96c1148766e77.tar.xz
Committing the incomplete table handler to get it into the tree. No user
functionality yet
Diffstat (limited to 'OpenSim')
-rw-r--r--OpenSim/Data/MySQL/MySQLGenericTableHandler.cs200
1 files changed, 200 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs
new file mode 100644
index 0000000..4eb4a24
--- /dev/null
+++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs
@@ -0,0 +1,200 @@
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
28using System;
29using System.Collections.Generic;
30using System.Data;
31using System.Reflection;
32using log4net;
33using MySql.Data.MySqlClient;
34using OpenMetaverse;
35using OpenSim.Framework;
36using OpenSim.Region.Framework.Interfaces;
37
38namespace OpenSim.Data.MySQL
39{
40 public class MySQLGenericTableHandler<T> : MySqlFramework where T: struct
41 {
42 private static readonly ILog m_log =
43 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44
45
46 protected Dictionary<string, FieldInfo> m_Fields =
47 new Dictionary<string, FieldInfo>();
48
49 protected List<string> m_ColumnNames = null;
50 protected string m_Realm;
51 protected FieldInfo m_DataField = null;
52
53 public MySQLGenericTableHandler(string connectionString,
54 string realm, string storeName) : base(connectionString)
55 {
56 m_Realm = realm;
57 if (storeName != String.Empty)
58 {
59 Assembly assem = GetType().Assembly;
60
61 Migration m = new Migration(m_Connection, assem, storeName);
62 m.Update();
63 }
64
65 Type t = typeof(T);
66 FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
67 BindingFlags.Instance |
68 BindingFlags.DeclaredOnly);
69
70 if (fields.Length == 0)
71 return;
72
73 foreach (FieldInfo f in fields)
74 {
75 if (f.Name != "Data")
76 m_Fields[f.Name] = f;
77 else
78 m_DataField = f;
79 }
80 }
81
82 private void CheckColumnNames(IDataReader reader)
83 {
84 if (m_ColumnNames != null)
85 return;
86
87 m_ColumnNames = new List<string>();
88
89 DataTable schemaTable = reader.GetSchemaTable();
90 foreach (DataRow row in schemaTable.Rows)
91 {
92 if (row["ColumnName"] != null &&
93 (!m_Fields.ContainsKey(row["ColumnName"].ToString())))
94 m_ColumnNames.Add(row["ColumnName"].ToString());
95 }
96 }
97
98 public T[] Get(string field, string key)
99 {
100 return Get(new string[] { field }, new string[] { key });
101 }
102
103 public T[] Get(string[] fields, string[] keys)
104 {
105 if (fields.Length != keys.Length)
106 return new T[0];
107
108 List<string> terms = new List<string>();
109
110 MySqlCommand cmd = new MySqlCommand();
111
112 for (int i = 0 ; i < fields.Length ; i++)
113 {
114 cmd.Parameters.AddWithValue(fields[i], keys[i]);
115 terms.Add(fields[i] + " = ?" + fields[i]);
116 }
117
118 string where = String.Join(" and ", terms.ToArray());
119
120 string query = String.Format("select * from {0} where {1}",
121 m_Realm, where);
122
123 cmd.CommandText = query;
124
125 return DoQuery(cmd);
126 }
127
128 protected T[] DoQuery(MySqlCommand cmd)
129 {
130 IDataReader reader = ExecuteReader(cmd);
131 if (reader == null)
132 return new T[0];
133
134 CheckColumnNames(reader);
135
136 List<T> result = new List<T>();
137
138 while(reader.Read())
139 {
140 T row = new T();
141
142 foreach (string name in m_Fields.Keys)
143 {
144 if (m_Fields[name].GetValue(row) is bool)
145 {
146 int v = Convert.ToInt32(reader[name]);
147 m_Fields[name].SetValue(row, v != 0 ? true : false);
148 }
149 else if(m_Fields[name].GetValue(row) is UUID)
150 {
151 UUID uuid = UUID.Zero;
152
153 UUID.TryParse(reader[name].ToString(), out uuid);
154 m_Fields[name].SetValue(row, uuid);
155 }
156 else
157 {
158 m_Fields[name].SetValue(row, reader[name]);
159 }
160 }
161
162 if (m_DataField != null)
163 {
164 Dictionary<string, string> data =
165 new Dictionary<string, string>();
166
167 foreach (string col in m_ColumnNames)
168 data[col] = reader[col].ToString();
169
170 m_DataField.SetValue(row, data);
171 }
172
173 result.Add(row);
174 }
175
176 CloseReaderCommand(cmd);
177
178 return result.ToArray();
179 }
180
181 public T[] Get(string where)
182 {
183 MySqlCommand cmd = new MySqlCommand();
184
185 string query = String.Format("select * from {0} where {1}",
186 m_Realm, where);
187
188 cmd.CommandText = query;
189
190 return DoQuery(cmd);
191 }
192
193 public void Store(T row)
194 {
195 MySqlCommand cmd = new MySqlCommand();
196
197 string query = "";
198 }
199 }
200}