diff options
author | Kitto Flora | 2010-02-12 14:08:32 -0500 |
---|---|---|
committer | Kitto Flora | 2010-02-12 14:08:32 -0500 |
commit | 6a70349512a612948918db2f2f485e87b0bc87e6 (patch) | |
tree | bdbb11f3ec840c7a14402f0acdc302b04d5a0692 /OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | |
parent | Merge branch 'master' of ssh://3dhosting.de/var/git/careminster (diff) | |
parent | Resolve a merge issue (diff) | |
download | opensim-SC_OLD-6a70349512a612948918db2f2f485e87b0bc87e6.zip opensim-SC_OLD-6a70349512a612948918db2f2f485e87b0bc87e6.tar.gz opensim-SC_OLD-6a70349512a612948918db2f2f485e87b0bc87e6.tar.bz2 opensim-SC_OLD-6a70349512a612948918db2f2f485e87b0bc87e6.tar.xz |
Merge branch 'master' of ssh://3dhosting.de/var/git/careminster
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | 215 |
1 files changed, 116 insertions, 99 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs index fdb98eb..698bf52 100644 --- a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs +++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | |||
@@ -54,12 +54,16 @@ namespace OpenSim.Data.MySQL | |||
54 | string realm, string storeName) : base(connectionString) | 54 | string realm, string storeName) : base(connectionString) |
55 | { | 55 | { |
56 | m_Realm = realm; | 56 | m_Realm = realm; |
57 | m_connectionString = connectionString; | ||
58 | |||
57 | if (storeName != String.Empty) | 59 | if (storeName != String.Empty) |
58 | { | 60 | { |
59 | Assembly assem = GetType().Assembly; | 61 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
60 | 62 | { | |
61 | Migration m = new Migration(m_Connection, assem, storeName); | 63 | dbcon.Open(); |
62 | m.Update(); | 64 | Migration m = new Migration(dbcon, GetType().Assembly, storeName); |
65 | m.Update(); | ||
66 | } | ||
63 | } | 67 | } |
64 | 68 | ||
65 | Type t = typeof(T); | 69 | Type t = typeof(T); |
@@ -107,147 +111,160 @@ namespace OpenSim.Data.MySQL | |||
107 | 111 | ||
108 | List<string> terms = new List<string>(); | 112 | List<string> terms = new List<string>(); |
109 | 113 | ||
110 | MySqlCommand cmd = new MySqlCommand(); | 114 | using (MySqlCommand cmd = new MySqlCommand()) |
111 | |||
112 | for (int i = 0 ; i < fields.Length ; i++) | ||
113 | { | 115 | { |
114 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | 116 | for (int i = 0 ; i < fields.Length ; i++) |
115 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | 117 | { |
116 | } | 118 | cmd.Parameters.AddWithValue(fields[i], keys[i]); |
117 | 119 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | |
118 | string where = String.Join(" and ", terms.ToArray()); | 120 | } |
119 | 121 | ||
120 | string query = String.Format("select * from {0} where {1}", | 122 | string where = String.Join(" and ", terms.ToArray()); |
121 | m_Realm, where); | ||
122 | 123 | ||
123 | cmd.CommandText = query; | 124 | string query = String.Format("select * from {0} where {1}", |
125 | m_Realm, where); | ||
124 | 126 | ||
125 | return DoQuery(cmd); | 127 | cmd.CommandText = query; |
128 | |||
129 | return DoQuery(cmd); | ||
130 | } | ||
126 | } | 131 | } |
127 | 132 | ||
128 | protected T[] DoQuery(MySqlCommand cmd) | 133 | protected T[] DoQuery(MySqlCommand cmd) |
129 | { | 134 | { |
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>(); | 135 | List<T> result = new List<T>(); |
137 | 136 | ||
138 | while (reader.Read()) | 137 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
139 | { | 138 | { |
140 | T row = new T(); | 139 | dbcon.Open(); |
140 | cmd.Connection = dbcon; | ||
141 | 141 | ||
142 | foreach (string name in m_Fields.Keys) | 142 | using (IDataReader reader = cmd.ExecuteReader()) |
143 | { | 143 | { |
144 | if (m_Fields[name].GetValue(row) is bool) | 144 | if (reader == null) |
145 | { | 145 | return new T[0]; |
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 | 146 | ||
153 | UUID.TryParse(reader[name].ToString(), out uuid); | 147 | CheckColumnNames(reader); |
154 | m_Fields[name].SetValue(row, uuid); | ||
155 | } | ||
156 | else if (m_Fields[name].GetValue(row) is int) | ||
157 | { | ||
158 | int v = Convert.ToInt32(reader[name]); | ||
159 | m_Fields[name].SetValue(row, v); | ||
160 | } | ||
161 | else | ||
162 | { | ||
163 | m_Fields[name].SetValue(row, reader[name]); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | if (m_DataField != null) | ||
168 | { | ||
169 | Dictionary<string, string> data = | ||
170 | new Dictionary<string, string>(); | ||
171 | 148 | ||
172 | foreach (string col in m_ColumnNames) | 149 | while (reader.Read()) |
173 | { | 150 | { |
174 | data[col] = reader[col].ToString(); | 151 | T row = new T(); |
175 | if (data[col] == null) | 152 | |
176 | data[col] = String.Empty; | 153 | foreach (string name in m_Fields.Keys) |
154 | { | ||
155 | if (m_Fields[name].GetValue(row) is bool) | ||
156 | { | ||
157 | int v = Convert.ToInt32(reader[name]); | ||
158 | m_Fields[name].SetValue(row, v != 0 ? true : false); | ||
159 | } | ||
160 | else if (m_Fields[name].GetValue(row) is UUID) | ||
161 | { | ||
162 | UUID uuid = UUID.Zero; | ||
163 | |||
164 | UUID.TryParse(reader[name].ToString(), out uuid); | ||
165 | m_Fields[name].SetValue(row, uuid); | ||
166 | } | ||
167 | else if (m_Fields[name].GetValue(row) is int) | ||
168 | { | ||
169 | int v = Convert.ToInt32(reader[name]); | ||
170 | m_Fields[name].SetValue(row, v); | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | m_Fields[name].SetValue(row, reader[name]); | ||
175 | } | ||
176 | } | ||
177 | |||
178 | if (m_DataField != null) | ||
179 | { | ||
180 | Dictionary<string, string> data = | ||
181 | new Dictionary<string, string>(); | ||
182 | |||
183 | foreach (string col in m_ColumnNames) | ||
184 | { | ||
185 | data[col] = reader[col].ToString(); | ||
186 | if (data[col] == null) | ||
187 | data[col] = String.Empty; | ||
188 | } | ||
189 | |||
190 | m_DataField.SetValue(row, data); | ||
191 | } | ||
192 | |||
193 | result.Add(row); | ||
177 | } | 194 | } |
178 | |||
179 | m_DataField.SetValue(row, data); | ||
180 | } | 195 | } |
181 | |||
182 | result.Add(row); | ||
183 | } | 196 | } |
184 | 197 | ||
185 | CloseReaderCommand(cmd); | ||
186 | |||
187 | return result.ToArray(); | 198 | return result.ToArray(); |
188 | } | 199 | } |
189 | 200 | ||
190 | public T[] Get(string where) | 201 | public T[] Get(string where) |
191 | { | 202 | { |
192 | MySqlCommand cmd = new MySqlCommand(); | 203 | using (MySqlCommand cmd = new MySqlCommand()) |
193 | 204 | { | |
194 | string query = String.Format("select * from {0} where {1}", | 205 | |
195 | m_Realm, where); | 206 | string query = String.Format("select * from {0} where {1}", |
196 | 207 | m_Realm, where); | |
197 | cmd.CommandText = query; | 208 | |
198 | 209 | cmd.CommandText = query; | |
199 | return DoQuery(cmd); | 210 | |
211 | return DoQuery(cmd); | ||
212 | } | ||
200 | } | 213 | } |
201 | 214 | ||
202 | public bool Store(T row) | 215 | public bool Store(T row) |
203 | { | 216 | { |
204 | MySqlCommand cmd = new MySqlCommand(); | 217 | using (MySqlCommand cmd = new MySqlCommand()) |
218 | { | ||
205 | 219 | ||
206 | string query = ""; | 220 | string query = ""; |
207 | List<String> names = new List<String>(); | 221 | List<String> names = new List<String>(); |
208 | List<String> values = new List<String>(); | 222 | List<String> values = new List<String>(); |
209 | 223 | ||
210 | foreach (FieldInfo fi in m_Fields.Values) | 224 | foreach (FieldInfo fi in m_Fields.Values) |
211 | { | 225 | { |
212 | names.Add(fi.Name); | 226 | names.Add(fi.Name); |
213 | values.Add("?" + fi.Name); | 227 | values.Add("?" + fi.Name); |
214 | cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString()); | 228 | cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString()); |
215 | } | 229 | } |
216 | 230 | ||
217 | if (m_DataField != null) | 231 | if (m_DataField != null) |
218 | { | 232 | { |
219 | Dictionary<string, string> data = | 233 | Dictionary<string, string> data = |
220 | (Dictionary<string, string>)m_DataField.GetValue(row); | 234 | (Dictionary<string, string>)m_DataField.GetValue(row); |
221 | 235 | ||
222 | foreach (KeyValuePair<string, string> kvp in data) | 236 | foreach (KeyValuePair<string, string> kvp in data) |
223 | { | 237 | { |
224 | names.Add(kvp.Key); | 238 | names.Add(kvp.Key); |
225 | values.Add("?" + kvp.Key); | 239 | values.Add("?" + kvp.Key); |
226 | cmd.Parameters.AddWithValue("?" + kvp.Key, kvp.Value); | 240 | cmd.Parameters.AddWithValue("?" + kvp.Key, kvp.Value); |
241 | } | ||
227 | } | 242 | } |
228 | } | ||
229 | 243 | ||
230 | query = String.Format("replace into {0} (`", m_Realm) + String.Join("`,`", names.ToArray()) + "`) values (" + String.Join(",", values.ToArray()) + ")"; | 244 | query = String.Format("replace into {0} (`", m_Realm) + String.Join("`,`", names.ToArray()) + "`) values (" + String.Join(",", values.ToArray()) + ")"; |
231 | 245 | ||
232 | cmd.CommandText = query; | 246 | cmd.CommandText = query; |
233 | 247 | ||
234 | if (ExecuteNonQuery(cmd) > 0) | 248 | if (ExecuteNonQuery(cmd) > 0) |
235 | return true; | 249 | return true; |
236 | 250 | ||
237 | return false; | 251 | return false; |
252 | } | ||
238 | } | 253 | } |
239 | 254 | ||
240 | public bool Delete(string field, string val) | 255 | public bool Delete(string field, string val) |
241 | { | 256 | { |
242 | MySqlCommand cmd = new MySqlCommand(); | 257 | using (MySqlCommand cmd = new MySqlCommand()) |
258 | { | ||
243 | 259 | ||
244 | cmd.CommandText = String.Format("delete from {0} where `{1}` = ?{1}", m_Realm, field); | 260 | cmd.CommandText = String.Format("delete from {0} where `{1}` = ?{1}", m_Realm, field); |
245 | cmd.Parameters.AddWithValue(field, val); | 261 | cmd.Parameters.AddWithValue(field, val); |
246 | 262 | ||
247 | if (ExecuteNonQuery(cmd) > 0) | 263 | if (ExecuteNonQuery(cmd) > 0) |
248 | return true; | 264 | return true; |
249 | 265 | ||
250 | return false; | 266 | return false; |
267 | } | ||
251 | } | 268 | } |
252 | } | 269 | } |
253 | } | 270 | } |