aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MSSQL/MSSQLEstateData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/MSSQL/MSSQLEstateData.cs')
-rw-r--r--OpenSim/Data/MSSQL/MSSQLEstateData.cs117
1 files changed, 69 insertions, 48 deletions
diff --git a/OpenSim/Data/MSSQL/MSSQLEstateData.cs b/OpenSim/Data/MSSQL/MSSQLEstateData.cs
index 9c54e77..1faa249 100644
--- a/OpenSim/Data/MSSQL/MSSQLEstateData.cs
+++ b/OpenSim/Data/MSSQL/MSSQLEstateData.cs
@@ -148,56 +148,29 @@ namespace OpenSim.Data.MSSQL
148 } 148 }
149 } 149 }
150 150
151
152 if (insertEstate && create) 151 if (insertEstate && create)
153 { 152 {
154 List<string> names = new List<string>(FieldList); 153 DoCreate(es);
155 154 LinkRegion(regionID, (int)es.EstateID);
156 names.Remove("EstateID"); 155 }
157
158 sql = string.Format("insert into estate_settings ({0}) values ( @{1})", String.Join(",", names.ToArray()), String.Join(", @", names.ToArray()));
159
160 //_Log.Debug("[DB ESTATE]: SQL: " + sql);
161 using (SqlConnection conn = new SqlConnection(m_connectionString))
162 using (SqlCommand insertCommand = new SqlCommand(sql, conn))
163 {
164 insertCommand.CommandText = sql + " SET @ID = SCOPE_IDENTITY()";
165 156
166 foreach (string name in names) 157 LoadBanList(es);
167 {
168 insertCommand.Parameters.Add(_Database.CreateParameter("@" + name, _FieldMap[name].GetValue(es)));
169 }
170 SqlParameter idParameter = new SqlParameter("@ID", SqlDbType.Int);
171 idParameter.Direction = ParameterDirection.Output;
172 insertCommand.Parameters.Add(idParameter);
173 conn.Open();
174 insertCommand.ExecuteNonQuery();
175 158
176 es.EstateID = Convert.ToUInt32(idParameter.Value); 159 es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
177 } 160 es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
161 es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
178 162
179 sql = "INSERT INTO [estate_map] ([RegionID] ,[EstateID]) VALUES (@RegionID, @EstateID)"; 163 //Set event
180 using (SqlConnection conn = new SqlConnection(m_connectionString)) 164 es.OnSave += StoreEstateSettings;
181 using (SqlCommand cmd = new SqlCommand(sql, conn)) 165 return es;
182 { 166 }
183 167
184 cmd.Parameters.Add(_Database.CreateParameter("@RegionID", regionID)); 168 public EstateSettings CreateNewEstate()
185 cmd.Parameters.Add(_Database.CreateParameter("@EstateID", es.EstateID)); 169 {
186 // This will throw on dupe key 170 EstateSettings es = new EstateSettings();
187 try 171 es.OnSave += StoreEstateSettings;
188 {
189 conn.Open();
190 cmd.ExecuteNonQuery();
191 }
192 catch (Exception e)
193 {
194 m_log.DebugFormat("[ESTATE DB]: Error inserting regionID and EstateID in estate_map: {0}", e);
195 }
196 }
197 172
198 //TODO check if this is needed?? 173 DoCreate(es);
199 es.Save();
200 }
201 174
202 LoadBanList(es); 175 LoadBanList(es);
203 176
@@ -205,11 +178,40 @@ namespace OpenSim.Data.MSSQL
205 es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users"); 178 es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
206 es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups"); 179 es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
207 180
208 //Set event
209 es.OnSave += StoreEstateSettings;
210 return es; 181 return es;
211 } 182 }
212 183
184 private void DoCreate(EstateSettings es)
185 {
186 List<string> names = new List<string>(FieldList);
187
188 names.Remove("EstateID");
189
190 string sql = string.Format("insert into estate_settings ({0}) values ( @{1})", String.Join(",", names.ToArray()), String.Join(", @", names.ToArray()));
191
192 //_Log.Debug("[DB ESTATE]: SQL: " + sql);
193 using (SqlConnection conn = new SqlConnection(m_connectionString))
194 using (SqlCommand insertCommand = new SqlCommand(sql, conn))
195 {
196 insertCommand.CommandText = sql + " SET @ID = SCOPE_IDENTITY()";
197
198 foreach (string name in names)
199 {
200 insertCommand.Parameters.Add(_Database.CreateParameter("@" + name, _FieldMap[name].GetValue(es)));
201 }
202 SqlParameter idParameter = new SqlParameter("@ID", SqlDbType.Int);
203 idParameter.Direction = ParameterDirection.Output;
204 insertCommand.Parameters.Add(idParameter);
205 conn.Open();
206 insertCommand.ExecuteNonQuery();
207
208 es.EstateID = Convert.ToUInt32(idParameter.Value);
209 }
210
211 //TODO check if this is needed??
212 es.Save();
213 }
214
213 /// <summary> 215 /// <summary>
214 /// Stores the estate settings. 216 /// Stores the estate settings.
215 /// </summary> 217 /// </summary>
@@ -498,24 +500,43 @@ namespace OpenSim.Data.MSSQL
498 500
499 public bool LinkRegion(UUID regionID, int estateID) 501 public bool LinkRegion(UUID regionID, int estateID)
500 { 502 {
501 string sql = "insert into estate_map values (@RegionID, @EstateID)"; 503 string deleteSQL = "delete from estate_map where RegionID = @RegionID";
504 string insertSQL = "insert into estate_map values (@RegionID, @EstateID)";
502 using (SqlConnection conn = new SqlConnection(m_connectionString)) 505 using (SqlConnection conn = new SqlConnection(m_connectionString))
503 { 506 {
504 conn.Open(); 507 conn.Open();
508 SqlTransaction transaction = conn.BeginTransaction();
509
505 try 510 try
506 { 511 {
507 using (SqlCommand cmd = new SqlCommand(sql, conn)) 512 using (SqlCommand cmd = new SqlCommand(deleteSQL, conn))
508 { 513 {
509 cmd.Parameters.AddWithValue("@RegionID", regionID); 514 cmd.Transaction = transaction;
515 cmd.Parameters.AddWithValue("@RegionID", regionID.Guid);
516
517 cmd.ExecuteNonQuery();
518 }
519
520 using (SqlCommand cmd = new SqlCommand(insertSQL, conn))
521 {
522 cmd.Transaction = transaction;
523 cmd.Parameters.AddWithValue("@RegionID", regionID.Guid);
510 cmd.Parameters.AddWithValue("@EstateID", estateID); 524 cmd.Parameters.AddWithValue("@EstateID", estateID);
511 525
512 int ret = cmd.ExecuteNonQuery(); 526 int ret = cmd.ExecuteNonQuery();
527
528 if (ret != 0)
529 transaction.Commit();
530 else
531 transaction.Rollback();
532
513 return (ret != 0); 533 return (ret != 0);
514 } 534 }
515 } 535 }
516 catch (Exception ex) 536 catch (Exception ex)
517 { 537 {
518 m_log.Error("[REGION DB]: LinkRegion failed: " + ex.Message); 538 m_log.Error("[REGION DB]: LinkRegion failed: " + ex.Message);
539 transaction.Rollback();
519 } 540 }
520 } 541 }
521 return false; 542 return false;