diff options
Merge branch 'master' into presence-refactor
This was a large, heavily conflicted merge and things MAY have got broken.
Please check!
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLGenericTableHandler.cs')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | 217 |
1 files changed, 116 insertions, 101 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs index 2269d20..6ad59f6 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,149 +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 | reader.Close(); | ||
186 | |||
187 | CloseReaderCommand(cmd); | ||
188 | |||
189 | return result.ToArray(); | 198 | return result.ToArray(); |
190 | } | 199 | } |
191 | 200 | ||
192 | public virtual T[] Get(string where) | 201 | public virtual T[] Get(string where) |
193 | { | 202 | { |
194 | MySqlCommand cmd = new MySqlCommand(); | 203 | using (MySqlCommand cmd = new MySqlCommand()) |
195 | 204 | { | |
196 | string query = String.Format("select * from {0} where {1}", | 205 | |
197 | m_Realm, where); | 206 | string query = String.Format("select * from {0} where {1}", |
198 | 207 | m_Realm, where); | |
199 | cmd.CommandText = query; | 208 | |
200 | 209 | cmd.CommandText = query; | |
201 | return DoQuery(cmd); | 210 | |
211 | return DoQuery(cmd); | ||
212 | } | ||
202 | } | 213 | } |
203 | 214 | ||
204 | public virtual bool Store(T row) | 215 | public virtual bool Store(T row) |
205 | { | 216 | { |
206 | MySqlCommand cmd = new MySqlCommand(); | 217 | using (MySqlCommand cmd = new MySqlCommand()) |
218 | { | ||
207 | 219 | ||
208 | string query = ""; | 220 | string query = ""; |
209 | List<String> names = new List<String>(); | 221 | List<String> names = new List<String>(); |
210 | List<String> values = new List<String>(); | 222 | List<String> values = new List<String>(); |
211 | 223 | ||
212 | foreach (FieldInfo fi in m_Fields.Values) | 224 | foreach (FieldInfo fi in m_Fields.Values) |
213 | { | 225 | { |
214 | names.Add(fi.Name); | 226 | names.Add(fi.Name); |
215 | values.Add("?" + fi.Name); | 227 | values.Add("?" + fi.Name); |
216 | cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString()); | 228 | cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString()); |
217 | } | 229 | } |
218 | 230 | ||
219 | if (m_DataField != null) | 231 | if (m_DataField != null) |
220 | { | 232 | { |
221 | Dictionary<string, string> data = | 233 | Dictionary<string, string> data = |
222 | (Dictionary<string, string>)m_DataField.GetValue(row); | 234 | (Dictionary<string, string>)m_DataField.GetValue(row); |
223 | 235 | ||
224 | foreach (KeyValuePair<string, string> kvp in data) | 236 | foreach (KeyValuePair<string, string> kvp in data) |
225 | { | 237 | { |
226 | names.Add(kvp.Key); | 238 | names.Add(kvp.Key); |
227 | values.Add("?" + kvp.Key); | 239 | values.Add("?" + kvp.Key); |
228 | cmd.Parameters.AddWithValue("?" + kvp.Key, kvp.Value); | 240 | cmd.Parameters.AddWithValue("?" + kvp.Key, kvp.Value); |
241 | } | ||
229 | } | 242 | } |
230 | } | ||
231 | 243 | ||
232 | 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()) + ")"; |
233 | 245 | ||
234 | cmd.CommandText = query; | 246 | cmd.CommandText = query; |
235 | 247 | ||
236 | if (ExecuteNonQuery(cmd) > 0) | 248 | if (ExecuteNonQuery(cmd) > 0) |
237 | return true; | 249 | return true; |
238 | 250 | ||
239 | return false; | 251 | return false; |
252 | } | ||
240 | } | 253 | } |
241 | 254 | ||
242 | public virtual bool Delete(string field, string val) | 255 | public virtual bool Delete(string field, string val) |
243 | { | 256 | { |
244 | MySqlCommand cmd = new MySqlCommand(); | 257 | using (MySqlCommand cmd = new MySqlCommand()) |
258 | { | ||
245 | 259 | ||
246 | 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); |
247 | cmd.Parameters.AddWithValue(field, val); | 261 | cmd.Parameters.AddWithValue(field, val); |
248 | 262 | ||
249 | if (ExecuteNonQuery(cmd) > 0) | 263 | if (ExecuteNonQuery(cmd) > 0) |
250 | return true; | 264 | return true; |
251 | 265 | ||
252 | return false; | 266 | return false; |
267 | } | ||
253 | } | 268 | } |
254 | } | 269 | } |
255 | } | 270 | } |