aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MySQL/MySQLRegionData.cs
diff options
context:
space:
mode:
authorJohn Hurliman2009-10-04 13:57:51 -0700
committerJohn Hurliman2009-10-04 13:57:51 -0700
commit29a4614529bbda02b9c690d2d1812be1d1e7bbae (patch)
tree89725829b37d502158a114c862a56a075b005b1b /OpenSim/Data/MySQL/MySQLRegionData.cs
parentGuarding a line that is sometimes throwing a null pointer exception. (diff)
downloadopensim-SC-29a4614529bbda02b9c690d2d1812be1d1e7bbae.zip
opensim-SC-29a4614529bbda02b9c690d2d1812be1d1e7bbae.tar.gz
opensim-SC-29a4614529bbda02b9c690d2d1812be1d1e7bbae.tar.bz2
opensim-SC-29a4614529bbda02b9c690d2d1812be1d1e7bbae.tar.xz
* MySQL data tests now pass by fixing a bad fix for a bad cast on the asset Local member in MySQLAssetData
* First pass at applying the using(){} pattern to IDisposable objects. Always use the using pattern on IDisposable objects whenever possible, do not manually call .Close() or .Dispose() unless there is no other way to write the code. This pass mostly covers OpenSim.Data.MySQL, and should have no functional change (tests still pass)
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/MySQL/MySQLRegionData.cs240
1 files changed, 119 insertions, 121 deletions
diff --git a/OpenSim/Data/MySQL/MySQLRegionData.cs b/OpenSim/Data/MySQL/MySQLRegionData.cs
index 06ef624..04b24b6 100644
--- a/OpenSim/Data/MySQL/MySQLRegionData.cs
+++ b/OpenSim/Data/MySQL/MySQLRegionData.cs
@@ -56,12 +56,13 @@ namespace OpenSim.Data.MySQL
56 if (scopeID != UUID.Zero) 56 if (scopeID != UUID.Zero)
57 command += " and ScopeID = ?scopeID"; 57 command += " and ScopeID = ?scopeID";
58 58
59 MySqlCommand cmd = new MySqlCommand(command); 59 using (MySqlCommand cmd = new MySqlCommand(command))
60 60 {
61 cmd.Parameters.AddWithValue("?regionName", regionName); 61 cmd.Parameters.AddWithValue("?regionName", regionName);
62 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); 62 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
63 63
64 return RunCommand(cmd); 64 return RunCommand(cmd);
65 }
65 } 66 }
66 67
67 public RegionData Get(int posX, int posY, UUID scopeID) 68 public RegionData Get(int posX, int posY, UUID scopeID)
@@ -70,17 +71,18 @@ namespace OpenSim.Data.MySQL
70 if (scopeID != UUID.Zero) 71 if (scopeID != UUID.Zero)
71 command += " and ScopeID = ?scopeID"; 72 command += " and ScopeID = ?scopeID";
72 73
73 MySqlCommand cmd = new MySqlCommand(command); 74 using (MySqlCommand cmd = new MySqlCommand(command))
74 75 {
75 cmd.Parameters.AddWithValue("?posX", posX.ToString()); 76 cmd.Parameters.AddWithValue("?posX", posX.ToString());
76 cmd.Parameters.AddWithValue("?posY", posY.ToString()); 77 cmd.Parameters.AddWithValue("?posY", posY.ToString());
77 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); 78 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
78 79
79 List<RegionData> ret = RunCommand(cmd); 80 List<RegionData> ret = RunCommand(cmd);
80 if (ret.Count == 0) 81 if (ret.Count == 0)
81 return null; 82 return null;
82 83
83 return ret[0]; 84 return ret[0];
85 }
84 } 86 }
85 87
86 public RegionData Get(UUID regionID, UUID scopeID) 88 public RegionData Get(UUID regionID, UUID scopeID)
@@ -89,16 +91,17 @@ namespace OpenSim.Data.MySQL
89 if (scopeID != UUID.Zero) 91 if (scopeID != UUID.Zero)
90 command += " and ScopeID = ?scopeID"; 92 command += " and ScopeID = ?scopeID";
91 93
92 MySqlCommand cmd = new MySqlCommand(command); 94 using (MySqlCommand cmd = new MySqlCommand(command))
93 95 {
94 cmd.Parameters.AddWithValue("?regionID", regionID.ToString()); 96 cmd.Parameters.AddWithValue("?regionID", regionID.ToString());
95 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); 97 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
96 98
97 List<RegionData> ret = RunCommand(cmd); 99 List<RegionData> ret = RunCommand(cmd);
98 if (ret.Count == 0) 100 if (ret.Count == 0)
99 return null; 101 return null;
100 102
101 return ret[0]; 103 return ret[0];
104 }
102 } 105 }
103 106
104 public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID) 107 public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
@@ -107,71 +110,70 @@ namespace OpenSim.Data.MySQL
107 if (scopeID != UUID.Zero) 110 if (scopeID != UUID.Zero)
108 command += " and ScopeID = ?scopeID"; 111 command += " and ScopeID = ?scopeID";
109 112
110 MySqlCommand cmd = new MySqlCommand(command); 113 using (MySqlCommand cmd = new MySqlCommand(command))
111 114 {
112 cmd.Parameters.AddWithValue("?startX", startX.ToString()); 115 cmd.Parameters.AddWithValue("?startX", startX.ToString());
113 cmd.Parameters.AddWithValue("?startY", startY.ToString()); 116 cmd.Parameters.AddWithValue("?startY", startY.ToString());
114 cmd.Parameters.AddWithValue("?endX", endX.ToString()); 117 cmd.Parameters.AddWithValue("?endX", endX.ToString());
115 cmd.Parameters.AddWithValue("?endY", endY.ToString()); 118 cmd.Parameters.AddWithValue("?endY", endY.ToString());
116 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); 119 cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
117 120
118 return RunCommand(cmd); 121 return RunCommand(cmd);
122 }
119 } 123 }
120 124
121 public List<RegionData> RunCommand(MySqlCommand cmd) 125 public List<RegionData> RunCommand(MySqlCommand cmd)
122 { 126 {
123 List<RegionData> retList = new List<RegionData>(); 127 List<RegionData> retList = new List<RegionData>();
124 128
125 IDataReader result = ExecuteReader(cmd); 129 using (IDataReader result = ExecuteReader(cmd))
126
127 while (result.Read())
128 { 130 {
129 RegionData ret = new RegionData(); 131 while (result.Read())
130 ret.Data = new Dictionary<string, object>();
131
132 UUID regionID;
133 UUID.TryParse(result["uuid"].ToString(), out regionID);
134 ret.RegionID = regionID;
135 UUID scope;
136 UUID.TryParse(result["ScopeID"].ToString(), out scope);
137 ret.ScopeID = scope;
138 ret.RegionName = result["regionName"].ToString();
139 ret.posX = Convert.ToInt32(result["locX"]);
140 ret.posY = Convert.ToInt32(result["locY"]);
141 ret.sizeX = Convert.ToInt32(result["sizeX"]);
142 ret.sizeY = Convert.ToInt32(result["sizeY"]);
143
144 if (m_ColumnNames == null)
145 { 132 {
146 m_ColumnNames = new List<string>(); 133 RegionData ret = new RegionData();
147 134 ret.Data = new Dictionary<string, object>();
148 DataTable schemaTable = result.GetSchemaTable(); 135
149 foreach (DataRow row in schemaTable.Rows) 136 UUID regionID;
150 m_ColumnNames.Add(row["ColumnName"].ToString()); 137 UUID.TryParse(result["uuid"].ToString(), out regionID);
138 ret.RegionID = regionID;
139 UUID scope;
140 UUID.TryParse(result["ScopeID"].ToString(), out scope);
141 ret.ScopeID = scope;
142 ret.RegionName = result["regionName"].ToString();
143 ret.posX = Convert.ToInt32(result["locX"]);
144 ret.posY = Convert.ToInt32(result["locY"]);
145 ret.sizeX = Convert.ToInt32(result["sizeX"]);
146 ret.sizeY = Convert.ToInt32(result["sizeY"]);
147
148 if (m_ColumnNames == null)
149 {
150 m_ColumnNames = new List<string>();
151
152 DataTable schemaTable = result.GetSchemaTable();
153 foreach (DataRow row in schemaTable.Rows)
154 m_ColumnNames.Add(row["ColumnName"].ToString());
155 }
156
157 foreach (string s in m_ColumnNames)
158 {
159 if (s == "uuid")
160 continue;
161 if (s == "ScopeID")
162 continue;
163 if (s == "regionName")
164 continue;
165 if (s == "locX")
166 continue;
167 if (s == "locY")
168 continue;
169
170 ret.Data[s] = result[s].ToString();
171 }
172
173 retList.Add(ret);
151 } 174 }
152
153 foreach (string s in m_ColumnNames)
154 {
155 if (s == "uuid")
156 continue;
157 if (s == "ScopeID")
158 continue;
159 if (s == "regionName")
160 continue;
161 if (s == "locX")
162 continue;
163 if (s == "locY")
164 continue;
165
166 ret.Data[s] = result[s].ToString();
167 }
168
169 retList.Add(ret);
170 } 175 }
171 176
172 result.Close();
173 CloseReaderCommand(cmd);
174
175 return retList; 177 return retList;
176 } 178 }
177 179
@@ -198,76 +200,72 @@ namespace OpenSim.Data.MySQL
198 200
199 string[] fields = new List<string>(data.Data.Keys).ToArray(); 201 string[] fields = new List<string>(data.Data.Keys).ToArray();
200 202
201 MySqlCommand cmd = new MySqlCommand(); 203 using (MySqlCommand cmd = new MySqlCommand())
202
203 string update = "update `"+m_Realm+"` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
204 foreach (string field in fields)
205 { 204 {
206 update += ", "; 205 string update = "update `" + m_Realm + "` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
207 update += "`" + field + "` = ?"+field; 206 foreach (string field in fields)
208 207 {
209 cmd.Parameters.AddWithValue("?"+field, data.Data[field]); 208 update += ", ";
210 } 209 update += "`" + field + "` = ?" + field;
211
212 update += " where uuid = ?regionID";
213 210
214 if (data.ScopeID != UUID.Zero) 211 cmd.Parameters.AddWithValue("?" + field, data.Data[field]);
215 update += " and ScopeID = ?scopeID"; 212 }
216 213
217 cmd.CommandText = update; 214 update += " where uuid = ?regionID";
218 cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString());
219 cmd.Parameters.AddWithValue("?regionName", data.RegionName);
220 cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
221 cmd.Parameters.AddWithValue("?posX", data.posX.ToString());
222 cmd.Parameters.AddWithValue("?posY", data.posY.ToString());
223 cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString());
224 cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString());
225 215
226 if (ExecuteNonQuery(cmd) < 1) 216 if (data.ScopeID != UUID.Zero)
227 { 217 update += " and ScopeID = ?scopeID";
228 string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
229 String.Join("`, `", fields) +
230 "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")";
231 218
232 cmd.CommandText = insert; 219 cmd.CommandText = update;
220 cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString());
221 cmd.Parameters.AddWithValue("?regionName", data.RegionName);
222 cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
223 cmd.Parameters.AddWithValue("?posX", data.posX.ToString());
224 cmd.Parameters.AddWithValue("?posY", data.posY.ToString());
225 cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString());
226 cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString());
233 227
234 if (ExecuteNonQuery(cmd) < 1) 228 if (ExecuteNonQuery(cmd) < 1)
235 { 229 {
236 cmd.Dispose(); 230 string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
237 return false; 231 String.Join("`, `", fields) +
232 "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")";
233
234 cmd.CommandText = insert;
235
236 if (ExecuteNonQuery(cmd) < 1)
237 {
238 return false;
239 }
238 } 240 }
239 } 241 }
240 242
241 cmd.Dispose();
242
243 return true; 243 return true;
244 } 244 }
245 245
246 public bool SetDataItem(UUID regionID, string item, string value) 246 public bool SetDataItem(UUID regionID, string item, string value)
247 { 247 {
248 MySqlCommand cmd = new MySqlCommand("update `" + m_Realm + 248 using (MySqlCommand cmd = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where uuid = ?UUID"))
249 "` set `" + item + "` = ?" + item + " where uuid = ?UUID"); 249 {
250 250 cmd.Parameters.AddWithValue("?" + item, value);
251 251 cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
252 cmd.Parameters.AddWithValue("?"+item, value);
253 cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
254 252
255 if (ExecuteNonQuery(cmd) > 0) 253 if (ExecuteNonQuery(cmd) > 0)
256 return true; 254 return true;
255 }
257 256
258 return false; 257 return false;
259 } 258 }
260 259
261 public bool Delete(UUID regionID) 260 public bool Delete(UUID regionID)
262 { 261 {
263 MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm + 262 using (MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm + "` where uuid = ?UUID"))
264 "` where uuid = ?UUID"); 263 {
265 264 cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
266
267 cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
268 265
269 if (ExecuteNonQuery(cmd) > 0) 266 if (ExecuteNonQuery(cmd) > 0)
270 return true; 267 return true;
268 }
271 269
272 return false; 270 return false;
273 } 271 }