diff options
author | Sean Dague | 2008-04-02 15:24:31 +0000 |
---|---|---|
committer | Sean Dague | 2008-04-02 15:24:31 +0000 |
commit | c52c68f314c67c76c7181a6d0828f476290fbd66 (patch) | |
tree | 66ab347502892902a096fa985f31b25738eb1381 /OpenSim/Data/SQLite/SQLiteRegionData.cs | |
parent | reorganizing namespaces to put all the Data stuff into it's own namespace (diff) | |
download | opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.zip opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.gz opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.bz2 opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.xz |
whole lot more moving
Diffstat (limited to 'OpenSim/Data/SQLite/SQLiteRegionData.cs')
-rw-r--r-- | OpenSim/Data/SQLite/SQLiteRegionData.cs | 1741 |
1 files changed, 1741 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLite/SQLiteRegionData.cs b/OpenSim/Data/SQLite/SQLiteRegionData.cs new file mode 100644 index 0000000..77161a4 --- /dev/null +++ b/OpenSim/Data/SQLite/SQLiteRegionData.cs | |||
@@ -0,0 +1,1741 @@ | |||
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 OpenSim 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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data; | ||
31 | using System.IO; | ||
32 | using libsecondlife; | ||
33 | using Mono.Data.SqliteClient; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Framework.Console; | ||
36 | using OpenSim.Region.Environment.Interfaces; | ||
37 | using OpenSim.Region.Environment.Scenes; | ||
38 | |||
39 | namespace OpenSim.Framework.Data.SQLite | ||
40 | { | ||
41 | public class SQLiteRegionData : IRegionDataStore | ||
42 | { | ||
43 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | |||
45 | private const string primSelect = "select * from prims"; | ||
46 | private const string shapeSelect = "select * from primshapes"; | ||
47 | private const string itemsSelect = "select * from primitems"; | ||
48 | private const string terrainSelect = "select * from terrain limit 1"; | ||
49 | private const string landSelect = "select * from land"; | ||
50 | private const string landAccessListSelect = "select distinct * from landaccesslist"; | ||
51 | |||
52 | private DataSet ds; | ||
53 | private SqliteDataAdapter primDa; | ||
54 | private SqliteDataAdapter shapeDa; | ||
55 | private SqliteDataAdapter itemsDa; | ||
56 | private SqliteDataAdapter terrainDa; | ||
57 | private SqliteDataAdapter landDa; | ||
58 | private SqliteDataAdapter landAccessListDa; | ||
59 | |||
60 | private SqliteConnection m_conn; | ||
61 | |||
62 | private String m_connectionString; | ||
63 | |||
64 | // Temporary attribute while this is experimental | ||
65 | private bool persistPrimInventories; | ||
66 | |||
67 | /*********************************************************************** | ||
68 | * | ||
69 | * Public Interface Functions | ||
70 | * | ||
71 | **********************************************************************/ | ||
72 | |||
73 | // see IRegionDataStore | ||
74 | public void Initialise(string connectionString, bool persistPrimInventories) | ||
75 | { | ||
76 | m_connectionString = connectionString; | ||
77 | this.persistPrimInventories = persistPrimInventories; | ||
78 | |||
79 | ds = new DataSet(); | ||
80 | |||
81 | m_log.Info("[DATASTORE]: Sqlite - connecting: " + connectionString); | ||
82 | m_conn = new SqliteConnection(m_connectionString); | ||
83 | m_conn.Open(); | ||
84 | |||
85 | SqliteCommand primSelectCmd = new SqliteCommand(primSelect, m_conn); | ||
86 | primDa = new SqliteDataAdapter(primSelectCmd); | ||
87 | // SqliteCommandBuilder primCb = new SqliteCommandBuilder(primDa); | ||
88 | |||
89 | SqliteCommand shapeSelectCmd = new SqliteCommand(shapeSelect, m_conn); | ||
90 | shapeDa = new SqliteDataAdapter(shapeSelectCmd); | ||
91 | // SqliteCommandBuilder shapeCb = new SqliteCommandBuilder(shapeDa); | ||
92 | |||
93 | SqliteCommand itemsSelectCmd = new SqliteCommand(itemsSelect, m_conn); | ||
94 | itemsDa = new SqliteDataAdapter(itemsSelectCmd); | ||
95 | |||
96 | SqliteCommand terrainSelectCmd = new SqliteCommand(terrainSelect, m_conn); | ||
97 | terrainDa = new SqliteDataAdapter(terrainSelectCmd); | ||
98 | |||
99 | SqliteCommand landSelectCmd = new SqliteCommand(landSelect, m_conn); | ||
100 | landDa = new SqliteDataAdapter(landSelectCmd); | ||
101 | |||
102 | SqliteCommand landAccessListSelectCmd = new SqliteCommand(landAccessListSelect, m_conn); | ||
103 | landAccessListDa = new SqliteDataAdapter(landAccessListSelectCmd); | ||
104 | |||
105 | // We fill the data set, now we've got copies in memory for the information | ||
106 | // TODO: see if the linkage actually holds. | ||
107 | // primDa.FillSchema(ds, SchemaType.Source, "PrimSchema"); | ||
108 | TestTables(m_conn); | ||
109 | |||
110 | lock (ds) | ||
111 | { | ||
112 | ds.Tables.Add(createPrimTable()); | ||
113 | setupPrimCommands(primDa, m_conn); | ||
114 | primDa.Fill(ds.Tables["prims"]); | ||
115 | |||
116 | ds.Tables.Add(createShapeTable()); | ||
117 | setupShapeCommands(shapeDa, m_conn); | ||
118 | |||
119 | if (persistPrimInventories) | ||
120 | { | ||
121 | ds.Tables.Add(createItemsTable()); | ||
122 | setupItemsCommands(itemsDa, m_conn); | ||
123 | itemsDa.Fill(ds.Tables["primitems"]); | ||
124 | } | ||
125 | |||
126 | ds.Tables.Add(createTerrainTable()); | ||
127 | setupTerrainCommands(terrainDa, m_conn); | ||
128 | |||
129 | ds.Tables.Add(createLandTable()); | ||
130 | setupLandCommands(landDa, m_conn); | ||
131 | |||
132 | ds.Tables.Add(createLandAccessListTable()); | ||
133 | setupLandAccessCommands(landAccessListDa, m_conn); | ||
134 | |||
135 | // WORKAROUND: This is a work around for sqlite on | ||
136 | // windows, which gets really unhappy with blob columns | ||
137 | // that have no sample data in them. At some point we | ||
138 | // need to actually find a proper way to handle this. | ||
139 | try | ||
140 | { | ||
141 | shapeDa.Fill(ds.Tables["primshapes"]); | ||
142 | } | ||
143 | catch (Exception) | ||
144 | { | ||
145 | m_log.Info("[DATASTORE]: Caught fill error on primshapes table"); | ||
146 | } | ||
147 | |||
148 | try | ||
149 | { | ||
150 | terrainDa.Fill(ds.Tables["terrain"]); | ||
151 | } | ||
152 | catch (Exception) | ||
153 | { | ||
154 | m_log.Info("[DATASTORE]: Caught fill error on terrain table"); | ||
155 | } | ||
156 | |||
157 | try | ||
158 | { | ||
159 | landDa.Fill(ds.Tables["land"]); | ||
160 | } | ||
161 | catch (Exception) | ||
162 | { | ||
163 | m_log.Info("[DATASTORE]: Caught fill error on land table"); | ||
164 | } | ||
165 | |||
166 | try | ||
167 | { | ||
168 | landAccessListDa.Fill(ds.Tables["landaccesslist"]); | ||
169 | } | ||
170 | catch (Exception) | ||
171 | { | ||
172 | m_log.Info("[DATASTORE]: Caught fill error on landaccesslist table"); | ||
173 | } | ||
174 | return; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | public void StoreObject(SceneObjectGroup obj, LLUUID regionUUID) | ||
179 | { | ||
180 | lock (ds) | ||
181 | { | ||
182 | foreach (SceneObjectPart prim in obj.Children.Values) | ||
183 | { | ||
184 | if ((prim.ObjectFlags & (uint) LLObject.ObjectFlags.Physics) == 0) | ||
185 | { | ||
186 | m_log.Info("[DATASTORE]: Adding obj: " + obj.UUID + " to region: " + regionUUID); | ||
187 | addPrim(prim, Util.ToRawUuidString(obj.UUID), Util.ToRawUuidString(regionUUID)); | ||
188 | } | ||
189 | else if (prim.Stopped) | ||
190 | { | ||
191 | //m_log.Info("[DATASTORE]: " + | ||
192 | //"Adding stopped obj: " + obj.UUID + " to region: " + regionUUID); | ||
193 | //addPrim(prim, Util.ToRawUuidString(obj.UUID), Util.ToRawUuidString(regionUUID)); | ||
194 | } | ||
195 | else | ||
196 | { | ||
197 | // m_log.Info("[DATASTORE]: Ignoring Physical obj: " + obj.UUID + " in region: " + regionUUID); | ||
198 | } | ||
199 | } | ||
200 | } | ||
201 | |||
202 | Commit(); | ||
203 | // m_log.Info("[Dump of prims]: " + ds.GetXml()); | ||
204 | } | ||
205 | |||
206 | public void RemoveObject(LLUUID obj, LLUUID regionUUID) | ||
207 | { | ||
208 | m_log.InfoFormat("[DATASTORE]: Removing obj: {0} from region: {1}", obj.UUID, regionUUID); | ||
209 | |||
210 | DataTable prims = ds.Tables["prims"]; | ||
211 | DataTable shapes = ds.Tables["primshapes"]; | ||
212 | |||
213 | string selectExp = "SceneGroupID = '" + Util.ToRawUuidString(obj) + "'"; | ||
214 | lock (ds) | ||
215 | { | ||
216 | DataRow[] primRows = prims.Select(selectExp); | ||
217 | foreach (DataRow row in primRows) | ||
218 | { | ||
219 | // Remove shape rows | ||
220 | LLUUID uuid = new LLUUID((string) row["UUID"]); | ||
221 | DataRow shapeRow = shapes.Rows.Find(Util.ToRawUuidString(uuid)); | ||
222 | if (shapeRow != null) | ||
223 | { | ||
224 | shapeRow.Delete(); | ||
225 | } | ||
226 | |||
227 | if (persistPrimInventories) | ||
228 | { | ||
229 | RemoveItems(uuid); | ||
230 | } | ||
231 | |||
232 | // Remove prim row | ||
233 | row.Delete(); | ||
234 | } | ||
235 | } | ||
236 | |||
237 | Commit(); | ||
238 | } | ||
239 | |||
240 | /// <summary> | ||
241 | /// Remove all persisted items of the given prim. | ||
242 | /// The caller must acquire the necessrary synchronization locks and commit or rollback changes. | ||
243 | /// </summary> | ||
244 | private void RemoveItems(LLUUID uuid) | ||
245 | { | ||
246 | DataTable items = ds.Tables["primitems"]; | ||
247 | |||
248 | String sql = String.Format("primID = '{0}'", uuid); | ||
249 | DataRow[] itemRows = items.Select(sql); | ||
250 | |||
251 | foreach (DataRow itemRow in itemRows) | ||
252 | { | ||
253 | itemRow.Delete(); | ||
254 | } | ||
255 | } | ||
256 | |||
257 | /// <summary> | ||
258 | /// Load persisted objects from region storage. | ||
259 | /// </summary> | ||
260 | /// <param name="regionUUID"></param> | ||
261 | /// <returns>List of loaded groups</returns> | ||
262 | public List<SceneObjectGroup> LoadObjects(LLUUID regionUUID) | ||
263 | { | ||
264 | Dictionary<LLUUID, SceneObjectGroup> createdObjects = new Dictionary<LLUUID, SceneObjectGroup>(); | ||
265 | |||
266 | List<SceneObjectGroup> retvals = new List<SceneObjectGroup>(); | ||
267 | |||
268 | DataTable prims = ds.Tables["prims"]; | ||
269 | DataTable shapes = ds.Tables["primshapes"]; | ||
270 | |||
271 | string byRegion = "RegionUUID = '" + Util.ToRawUuidString(regionUUID) + "'"; | ||
272 | string orderByParent = "ParentID ASC"; | ||
273 | |||
274 | lock (ds) | ||
275 | { | ||
276 | DataRow[] primsForRegion = prims.Select(byRegion, orderByParent); | ||
277 | m_log.Info("[DATASTORE]: " + | ||
278 | "Loaded " + primsForRegion.Length + " prims for region: " + regionUUID); | ||
279 | |||
280 | foreach (DataRow primRow in primsForRegion) | ||
281 | { | ||
282 | try | ||
283 | { | ||
284 | SceneObjectPart prim = null; | ||
285 | |||
286 | string uuid = (string) primRow["UUID"]; | ||
287 | string objID = (string) primRow["SceneGroupID"]; | ||
288 | if (uuid == objID) //is new SceneObjectGroup ? | ||
289 | { | ||
290 | SceneObjectGroup group = new SceneObjectGroup(); | ||
291 | prim = buildPrim(primRow); | ||
292 | DataRow shapeRow = shapes.Rows.Find(Util.ToRawUuidString(prim.UUID)); | ||
293 | if (shapeRow != null) | ||
294 | { | ||
295 | prim.Shape = buildShape(shapeRow); | ||
296 | } | ||
297 | else | ||
298 | { | ||
299 | m_log.Info( | ||
300 | "No shape found for prim in storage, so setting default box shape"); | ||
301 | prim.Shape = PrimitiveBaseShape.Default; | ||
302 | } | ||
303 | group.AddPart(prim); | ||
304 | group.RootPart = prim; | ||
305 | |||
306 | createdObjects.Add(Util.ToRawUuidString(group.UUID), group); | ||
307 | retvals.Add(group); | ||
308 | } | ||
309 | else | ||
310 | { | ||
311 | prim = buildPrim(primRow); | ||
312 | DataRow shapeRow = shapes.Rows.Find(Util.ToRawUuidString(prim.UUID)); | ||
313 | if (shapeRow != null) | ||
314 | { | ||
315 | prim.Shape = buildShape(shapeRow); | ||
316 | } | ||
317 | else | ||
318 | { | ||
319 | m_log.Info( | ||
320 | "No shape found for prim in storage, so setting default box shape"); | ||
321 | prim.Shape = PrimitiveBaseShape.Default; | ||
322 | } | ||
323 | createdObjects[new LLUUID(objID)].AddPart(prim); | ||
324 | } | ||
325 | |||
326 | if (persistPrimInventories) | ||
327 | { | ||
328 | LoadItems(prim); | ||
329 | } | ||
330 | } | ||
331 | catch (Exception e) | ||
332 | { | ||
333 | m_log.Error("[DATASTORE]: Failed create prim object, exception and data follows"); | ||
334 | m_log.Info("[DATASTORE]: " + e.ToString()); | ||
335 | foreach (DataColumn col in prims.Columns) | ||
336 | { | ||
337 | m_log.Info("[DATASTORE]: Col: " + col.ColumnName + " => " + primRow[col]); | ||
338 | } | ||
339 | } | ||
340 | } | ||
341 | } | ||
342 | return retvals; | ||
343 | } | ||
344 | |||
345 | /// <summary> | ||
346 | /// Load in a prim's persisted inventory. | ||
347 | /// </summary> | ||
348 | /// <param name="prim"></param> | ||
349 | private void LoadItems(SceneObjectPart prim) | ||
350 | { | ||
351 | //m_log.DebugFormat("[DATASTORE]: Loading inventory for {0}, {1}", prim.Name, prim.UUID); | ||
352 | |||
353 | DataTable dbItems = ds.Tables["primitems"]; | ||
354 | |||
355 | String sql = String.Format("primID = '{0}'", prim.UUID.ToString()); | ||
356 | DataRow[] dbItemRows = dbItems.Select(sql); | ||
357 | |||
358 | IList<TaskInventoryItem> inventory = new List<TaskInventoryItem>(); | ||
359 | |||
360 | foreach (DataRow row in dbItemRows) | ||
361 | { | ||
362 | TaskInventoryItem item = buildItem(row); | ||
363 | inventory.Add(item); | ||
364 | |||
365 | //m_log.DebugFormat("[DATASTORE]: Restored item {0}, {1}", item.Name, item.ItemID); | ||
366 | } | ||
367 | |||
368 | prim.RestoreInventoryItems(inventory); | ||
369 | |||
370 | // XXX A nasty little hack to recover the folder id for the prim (which is currently stored in | ||
371 | // every item). This data should really be stored in the prim table itself. | ||
372 | if (dbItemRows.Length > 0) | ||
373 | { | ||
374 | prim.FolderID = inventory[0].ParentID; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | public void StoreTerrain(double[,] ter, LLUUID regionID) | ||
379 | { | ||
380 | lock (ds) | ||
381 | { | ||
382 | int revision = Util.UnixTimeSinceEpoch(); | ||
383 | |||
384 | // the following is an work around for .NET. The perf | ||
385 | // issues associated with it aren't as bad as you think. | ||
386 | m_log.Info("[DATASTORE]: Storing terrain revision r" + revision.ToString()); | ||
387 | String sql = "insert into terrain(RegionUUID, Revision, Heightfield)" + | ||
388 | " values(:RegionUUID, :Revision, :Heightfield)"; | ||
389 | |||
390 | using (SqliteCommand cmd = new SqliteCommand(sql, m_conn)) | ||
391 | { | ||
392 | cmd.Parameters.Add(new SqliteParameter(":RegionUUID", Util.ToRawUuidString(regionID))); | ||
393 | cmd.Parameters.Add(new SqliteParameter(":Revision", revision)); | ||
394 | cmd.Parameters.Add(new SqliteParameter(":Heightfield", serializeTerrain(ter))); | ||
395 | cmd.ExecuteNonQuery(); | ||
396 | } | ||
397 | |||
398 | // This is added to get rid of the infinitely growing | ||
399 | // terrain databases which negatively impact on SQLite | ||
400 | // over time. Before reenabling this feature there | ||
401 | // needs to be a limitter put on the number of | ||
402 | // revisions in the database, as this old | ||
403 | // implementation is a DOS attack waiting to happen. | ||
404 | |||
405 | using ( | ||
406 | SqliteCommand cmd = | ||
407 | new SqliteCommand("delete from terrain where RegionUUID=:RegionUUID and Revision < :Revision", | ||
408 | m_conn)) | ||
409 | { | ||
410 | cmd.Parameters.Add(new SqliteParameter(":RegionUUID", Util.ToRawUuidString(regionID))); | ||
411 | cmd.Parameters.Add(new SqliteParameter(":Revision", revision)); | ||
412 | cmd.ExecuteNonQuery(); | ||
413 | } | ||
414 | } | ||
415 | } | ||
416 | |||
417 | public double[,] LoadTerrain(LLUUID regionID) | ||
418 | { | ||
419 | lock (ds) | ||
420 | { | ||
421 | double[,] terret = new double[256,256]; | ||
422 | terret.Initialize(); | ||
423 | |||
424 | String sql = "select RegionUUID, Revision, Heightfield from terrain" + | ||
425 | " where RegionUUID=:RegionUUID order by Revision desc"; | ||
426 | |||
427 | using (SqliteCommand cmd = new SqliteCommand(sql, m_conn)) | ||
428 | { | ||
429 | cmd.Parameters.Add(new SqliteParameter(":RegionUUID", Util.ToRawUuidString(regionID))); | ||
430 | |||
431 | using (IDataReader row = cmd.ExecuteReader()) | ||
432 | { | ||
433 | int rev = 0; | ||
434 | if (row.Read()) | ||
435 | { | ||
436 | // TODO: put this into a function | ||
437 | MemoryStream str = new MemoryStream((byte[]) row["Heightfield"]); | ||
438 | BinaryReader br = new BinaryReader(str); | ||
439 | for (int x = 0; x < 256; x++) | ||
440 | { | ||
441 | for (int y = 0; y < 256; y++) | ||
442 | { | ||
443 | terret[x, y] = br.ReadDouble(); | ||
444 | } | ||
445 | } | ||
446 | rev = (int) row["Revision"]; | ||
447 | } | ||
448 | else | ||
449 | { | ||
450 | m_log.Info("[DATASTORE]: No terrain found for region"); | ||
451 | return null; | ||
452 | } | ||
453 | |||
454 | m_log.Info("[DATASTORE]: Loaded terrain revision r" + rev.ToString()); | ||
455 | } | ||
456 | } | ||
457 | return terret; | ||
458 | } | ||
459 | } | ||
460 | |||
461 | public void RemoveLandObject(LLUUID globalID) | ||
462 | { | ||
463 | lock (ds) | ||
464 | { | ||
465 | using (SqliteCommand cmd = new SqliteCommand("delete from land where UUID=:UUID", m_conn)) | ||
466 | { | ||
467 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(globalID))); | ||
468 | cmd.ExecuteNonQuery(); | ||
469 | } | ||
470 | |||
471 | using (SqliteCommand cmd = new SqliteCommand("delete from landaccesslist where LandUUID=:UUID", m_conn)) | ||
472 | { | ||
473 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(globalID))); | ||
474 | cmd.ExecuteNonQuery(); | ||
475 | } | ||
476 | } | ||
477 | } | ||
478 | |||
479 | public void StoreLandObject(ILandObject parcel) | ||
480 | { | ||
481 | lock (ds) | ||
482 | { | ||
483 | DataTable land = ds.Tables["land"]; | ||
484 | DataTable landaccesslist = ds.Tables["landaccesslist"]; | ||
485 | |||
486 | DataRow landRow = land.Rows.Find(Util.ToRawUuidString(parcel.landData.globalID)); | ||
487 | if (landRow == null) | ||
488 | { | ||
489 | landRow = land.NewRow(); | ||
490 | fillLandRow(landRow, parcel.landData, parcel.regionUUID); | ||
491 | land.Rows.Add(landRow); | ||
492 | } | ||
493 | else | ||
494 | { | ||
495 | fillLandRow(landRow, parcel.landData, parcel.regionUUID); | ||
496 | } | ||
497 | |||
498 | // I know this caused someone issues before, but OpenSim is unusable if we leave this stuff around | ||
499 | using (SqliteCommand cmd = new SqliteCommand("delete from landaccesslist where LandUUID=:LandUUID", m_conn)) | ||
500 | { | ||
501 | cmd.Parameters.Add(new SqliteParameter(":LandUUID", Util.ToRawUuidString(parcel.landData.globalID))); | ||
502 | cmd.ExecuteNonQuery(); | ||
503 | } | ||
504 | |||
505 | foreach (ParcelManager.ParcelAccessEntry entry in parcel.landData.parcelAccessList) | ||
506 | { | ||
507 | DataRow newAccessRow = landaccesslist.NewRow(); | ||
508 | fillLandAccessRow(newAccessRow, entry, parcel.landData.globalID); | ||
509 | landaccesslist.Rows.Add(newAccessRow); | ||
510 | } | ||
511 | } | ||
512 | |||
513 | Commit(); | ||
514 | } | ||
515 | |||
516 | public List<LandData> LoadLandObjects(LLUUID regionUUID) | ||
517 | { | ||
518 | List<LandData> landDataForRegion = new List<LandData>(); | ||
519 | lock (ds) | ||
520 | { | ||
521 | DataTable land = ds.Tables["land"]; | ||
522 | DataTable landaccesslist = ds.Tables["landaccesslist"]; | ||
523 | string searchExp = "RegionUUID = '" + Util.ToRawUuidString(regionUUID) + "'"; | ||
524 | DataRow[] rawDataForRegion = land.Select(searchExp); | ||
525 | foreach (DataRow rawDataLand in rawDataForRegion) | ||
526 | { | ||
527 | LandData newLand = buildLandData(rawDataLand); | ||
528 | string accessListSearchExp = "LandUUID = '" + Util.ToRawUuidString(newLand.globalID) + "'"; | ||
529 | DataRow[] rawDataForLandAccessList = landaccesslist.Select(accessListSearchExp); | ||
530 | foreach (DataRow rawDataLandAccess in rawDataForLandAccessList) | ||
531 | { | ||
532 | newLand.parcelAccessList.Add(buildLandAccessData(rawDataLandAccess)); | ||
533 | } | ||
534 | |||
535 | landDataForRegion.Add(newLand); | ||
536 | } | ||
537 | } | ||
538 | return landDataForRegion; | ||
539 | } | ||
540 | |||
541 | public void Commit() | ||
542 | { | ||
543 | lock (ds) | ||
544 | { | ||
545 | primDa.Update(ds, "prims"); | ||
546 | shapeDa.Update(ds, "primshapes"); | ||
547 | |||
548 | if (persistPrimInventories) | ||
549 | { | ||
550 | itemsDa.Update(ds, "primitems"); | ||
551 | } | ||
552 | |||
553 | terrainDa.Update(ds, "terrain"); | ||
554 | landDa.Update(ds, "land"); | ||
555 | landAccessListDa.Update(ds, "landaccesslist"); | ||
556 | ds.AcceptChanges(); | ||
557 | } | ||
558 | } | ||
559 | |||
560 | public void Shutdown() | ||
561 | { | ||
562 | Commit(); | ||
563 | } | ||
564 | |||
565 | /*********************************************************************** | ||
566 | * | ||
567 | * Database Definition Functions | ||
568 | * | ||
569 | * This should be db agnostic as we define them in ADO.NET terms | ||
570 | * | ||
571 | **********************************************************************/ | ||
572 | |||
573 | private void createCol(DataTable dt, string name, Type type) | ||
574 | { | ||
575 | DataColumn col = new DataColumn(name, type); | ||
576 | dt.Columns.Add(col); | ||
577 | } | ||
578 | |||
579 | private DataTable createTerrainTable() | ||
580 | { | ||
581 | DataTable terrain = new DataTable("terrain"); | ||
582 | |||
583 | createCol(terrain, "RegionUUID", typeof (String)); | ||
584 | createCol(terrain, "Revision", typeof (Int32)); | ||
585 | createCol(terrain, "Heightfield", typeof (Byte[])); | ||
586 | |||
587 | return terrain; | ||
588 | } | ||
589 | |||
590 | private DataTable createPrimTable() | ||
591 | { | ||
592 | DataTable prims = new DataTable("prims"); | ||
593 | |||
594 | createCol(prims, "UUID", typeof (String)); | ||
595 | createCol(prims, "RegionUUID", typeof (String)); | ||
596 | createCol(prims, "ParentID", typeof (Int32)); | ||
597 | createCol(prims, "CreationDate", typeof (Int32)); | ||
598 | createCol(prims, "Name", typeof (String)); | ||
599 | createCol(prims, "SceneGroupID", typeof (String)); | ||
600 | // various text fields | ||
601 | createCol(prims, "Text", typeof (String)); | ||
602 | createCol(prims, "Description", typeof (String)); | ||
603 | createCol(prims, "SitName", typeof (String)); | ||
604 | createCol(prims, "TouchName", typeof (String)); | ||
605 | // permissions | ||
606 | createCol(prims, "ObjectFlags", typeof (Int32)); | ||
607 | createCol(prims, "CreatorID", typeof (String)); | ||
608 | createCol(prims, "OwnerID", typeof (String)); | ||
609 | createCol(prims, "GroupID", typeof (String)); | ||
610 | createCol(prims, "LastOwnerID", typeof (String)); | ||
611 | createCol(prims, "OwnerMask", typeof (Int32)); | ||
612 | createCol(prims, "NextOwnerMask", typeof (Int32)); | ||
613 | createCol(prims, "GroupMask", typeof (Int32)); | ||
614 | createCol(prims, "EveryoneMask", typeof (Int32)); | ||
615 | createCol(prims, "BaseMask", typeof (Int32)); | ||
616 | // vectors | ||
617 | createCol(prims, "PositionX", typeof (Double)); | ||
618 | createCol(prims, "PositionY", typeof (Double)); | ||
619 | createCol(prims, "PositionZ", typeof (Double)); | ||
620 | createCol(prims, "GroupPositionX", typeof (Double)); | ||
621 | createCol(prims, "GroupPositionY", typeof (Double)); | ||
622 | createCol(prims, "GroupPositionZ", typeof (Double)); | ||
623 | createCol(prims, "VelocityX", typeof (Double)); | ||
624 | createCol(prims, "VelocityY", typeof (Double)); | ||
625 | createCol(prims, "VelocityZ", typeof (Double)); | ||
626 | createCol(prims, "AngularVelocityX", typeof (Double)); | ||
627 | createCol(prims, "AngularVelocityY", typeof (Double)); | ||
628 | createCol(prims, "AngularVelocityZ", typeof (Double)); | ||
629 | createCol(prims, "AccelerationX", typeof (Double)); | ||
630 | createCol(prims, "AccelerationY", typeof (Double)); | ||
631 | createCol(prims, "AccelerationZ", typeof (Double)); | ||
632 | // quaternions | ||
633 | createCol(prims, "RotationX", typeof (Double)); | ||
634 | createCol(prims, "RotationY", typeof (Double)); | ||
635 | createCol(prims, "RotationZ", typeof (Double)); | ||
636 | createCol(prims, "RotationW", typeof (Double)); | ||
637 | |||
638 | // sit target | ||
639 | createCol(prims, "SitTargetOffsetX", typeof (Double)); | ||
640 | createCol(prims, "SitTargetOffsetY", typeof (Double)); | ||
641 | createCol(prims, "SitTargetOffsetZ", typeof (Double)); | ||
642 | |||
643 | createCol(prims, "SitTargetOrientW", typeof (Double)); | ||
644 | createCol(prims, "SitTargetOrientX", typeof (Double)); | ||
645 | createCol(prims, "SitTargetOrientY", typeof (Double)); | ||
646 | createCol(prims, "SitTargetOrientZ", typeof (Double)); | ||
647 | |||
648 | // Add in contraints | ||
649 | prims.PrimaryKey = new DataColumn[] {prims.Columns["UUID"]}; | ||
650 | |||
651 | return prims; | ||
652 | } | ||
653 | |||
654 | private DataTable createShapeTable() | ||
655 | { | ||
656 | DataTable shapes = new DataTable("primshapes"); | ||
657 | createCol(shapes, "UUID", typeof (String)); | ||
658 | // shape is an enum | ||
659 | createCol(shapes, "Shape", typeof (Int32)); | ||
660 | // vectors | ||
661 | createCol(shapes, "ScaleX", typeof (Double)); | ||
662 | createCol(shapes, "ScaleY", typeof (Double)); | ||
663 | createCol(shapes, "ScaleZ", typeof (Double)); | ||
664 | // paths | ||
665 | createCol(shapes, "PCode", typeof (Int32)); | ||
666 | createCol(shapes, "PathBegin", typeof (Int32)); | ||
667 | createCol(shapes, "PathEnd", typeof (Int32)); | ||
668 | createCol(shapes, "PathScaleX", typeof (Int32)); | ||
669 | createCol(shapes, "PathScaleY", typeof (Int32)); | ||
670 | createCol(shapes, "PathShearX", typeof (Int32)); | ||
671 | createCol(shapes, "PathShearY", typeof (Int32)); | ||
672 | createCol(shapes, "PathSkew", typeof (Int32)); | ||
673 | createCol(shapes, "PathCurve", typeof (Int32)); | ||
674 | createCol(shapes, "PathRadiusOffset", typeof (Int32)); | ||
675 | createCol(shapes, "PathRevolutions", typeof (Int32)); | ||
676 | createCol(shapes, "PathTaperX", typeof (Int32)); | ||
677 | createCol(shapes, "PathTaperY", typeof (Int32)); | ||
678 | createCol(shapes, "PathTwist", typeof (Int32)); | ||
679 | createCol(shapes, "PathTwistBegin", typeof (Int32)); | ||
680 | // profile | ||
681 | createCol(shapes, "ProfileBegin", typeof (Int32)); | ||
682 | createCol(shapes, "ProfileEnd", typeof (Int32)); | ||
683 | createCol(shapes, "ProfileCurve", typeof (Int32)); | ||
684 | createCol(shapes, "ProfileHollow", typeof (Int32)); | ||
685 | createCol(shapes, "State", typeof(Int32)); | ||
686 | // text TODO: this isn't right, but I'm not sure the right | ||
687 | // way to specify this as a blob atm | ||
688 | createCol(shapes, "Texture", typeof (Byte[])); | ||
689 | createCol(shapes, "ExtraParams", typeof (Byte[])); | ||
690 | |||
691 | shapes.PrimaryKey = new DataColumn[] {shapes.Columns["UUID"]}; | ||
692 | |||
693 | return shapes; | ||
694 | } | ||
695 | |||
696 | private DataTable createItemsTable() | ||
697 | { | ||
698 | DataTable items = new DataTable("primitems"); | ||
699 | |||
700 | createCol(items, "itemID", typeof (String)); | ||
701 | createCol(items, "primID", typeof (String)); | ||
702 | createCol(items, "assetID", typeof (String)); | ||
703 | createCol(items, "parentFolderID", typeof (String)); | ||
704 | |||
705 | createCol(items, "invType", typeof (Int32)); | ||
706 | createCol(items, "assetType", typeof (Int32)); | ||
707 | |||
708 | createCol(items, "name", typeof (String)); | ||
709 | createCol(items, "description", typeof (String)); | ||
710 | |||
711 | createCol(items, "creationDate", typeof (Int64)); | ||
712 | createCol(items, "creatorID", typeof (String)); | ||
713 | createCol(items, "ownerID", typeof (String)); | ||
714 | createCol(items, "lastOwnerID", typeof (String)); | ||
715 | createCol(items, "groupID", typeof (String)); | ||
716 | |||
717 | createCol(items, "nextPermissions", typeof (UInt32)); | ||
718 | createCol(items, "currentPermissions", typeof (UInt32)); | ||
719 | createCol(items, "basePermissions", typeof (UInt32)); | ||
720 | createCol(items, "everyonePermissions", typeof (UInt32)); | ||
721 | createCol(items, "groupPermissions", typeof (UInt32)); | ||
722 | |||
723 | items.PrimaryKey = new DataColumn[] {items.Columns["itemID"]}; | ||
724 | |||
725 | return items; | ||
726 | } | ||
727 | |||
728 | private DataTable createLandTable() | ||
729 | { | ||
730 | DataTable land = new DataTable("land"); | ||
731 | createCol(land, "UUID", typeof (String)); | ||
732 | createCol(land, "RegionUUID", typeof (String)); | ||
733 | createCol(land, "LocalLandID", typeof (UInt32)); | ||
734 | |||
735 | // Bitmap is a byte[512] | ||
736 | createCol(land, "Bitmap", typeof (Byte[])); | ||
737 | |||
738 | createCol(land, "Name", typeof (String)); | ||
739 | createCol(land, "Desc", typeof (String)); | ||
740 | createCol(land, "OwnerUUID", typeof (String)); | ||
741 | createCol(land, "IsGroupOwned", typeof (Boolean)); | ||
742 | createCol(land, "Area", typeof (Int32)); | ||
743 | createCol(land, "AuctionID", typeof (Int32)); //Unemplemented | ||
744 | createCol(land, "Category", typeof (Int32)); //Enum libsecondlife.Parcel.ParcelCategory | ||
745 | createCol(land, "ClaimDate", typeof (Int32)); | ||
746 | createCol(land, "ClaimPrice", typeof (Int32)); | ||
747 | createCol(land, "GroupUUID", typeof (string)); | ||
748 | createCol(land, "SalePrice", typeof (Int32)); | ||
749 | createCol(land, "LandStatus", typeof (Int32)); //Enum. libsecondlife.Parcel.ParcelStatus | ||
750 | createCol(land, "LandFlags", typeof (UInt32)); | ||
751 | createCol(land, "LandingType", typeof (Byte)); | ||
752 | createCol(land, "MediaAutoScale", typeof (Byte)); | ||
753 | createCol(land, "MediaTextureUUID", typeof (String)); | ||
754 | createCol(land, "MediaURL", typeof (String)); | ||
755 | createCol(land, "MusicURL", typeof (String)); | ||
756 | createCol(land, "PassHours", typeof (Double)); | ||
757 | createCol(land, "PassPrice", typeof (UInt32)); | ||
758 | createCol(land, "SnapshotUUID", typeof (String)); | ||
759 | createCol(land, "UserLocationX", typeof (Double)); | ||
760 | createCol(land, "UserLocationY", typeof (Double)); | ||
761 | createCol(land, "UserLocationZ", typeof (Double)); | ||
762 | createCol(land, "UserLookAtX", typeof (Double)); | ||
763 | createCol(land, "UserLookAtY", typeof (Double)); | ||
764 | createCol(land, "UserLookAtZ", typeof (Double)); | ||
765 | |||
766 | land.PrimaryKey = new DataColumn[] {land.Columns["UUID"]}; | ||
767 | |||
768 | return land; | ||
769 | } | ||
770 | |||
771 | private DataTable createLandAccessListTable() | ||
772 | { | ||
773 | DataTable landaccess = new DataTable("landaccesslist"); | ||
774 | createCol(landaccess, "LandUUID", typeof (String)); | ||
775 | createCol(landaccess, "AccessUUID", typeof (String)); | ||
776 | createCol(landaccess, "Flags", typeof (UInt32)); | ||
777 | |||
778 | return landaccess; | ||
779 | } | ||
780 | |||
781 | /*********************************************************************** | ||
782 | * | ||
783 | * Convert between ADO.NET <=> OpenSim Objects | ||
784 | * | ||
785 | * These should be database independant | ||
786 | * | ||
787 | **********************************************************************/ | ||
788 | |||
789 | private SceneObjectPart buildPrim(DataRow row) | ||
790 | { | ||
791 | // TODO: this doesn't work yet because something more | ||
792 | // interesting has to be done to actually get these values | ||
793 | // back out. Not enough time to figure it out yet. | ||
794 | SceneObjectPart prim = new SceneObjectPart(); | ||
795 | prim.UUID = new LLUUID((String) row["UUID"]); | ||
796 | // explicit conversion of integers is required, which sort | ||
797 | // of sucks. No idea if there is a shortcut here or not. | ||
798 | prim.ParentID = Convert.ToUInt32(row["ParentID"]); | ||
799 | prim.CreationDate = Convert.ToInt32(row["CreationDate"]); | ||
800 | prim.Name = (String) row["Name"]; | ||
801 | // various text fields | ||
802 | prim.Text = (String) row["Text"]; | ||
803 | prim.Description = (String) row["Description"]; | ||
804 | prim.SitName = (String) row["SitName"]; | ||
805 | prim.TouchName = (String) row["TouchName"]; | ||
806 | // permissions | ||
807 | prim.ObjectFlags = Convert.ToUInt32(row["ObjectFlags"]); | ||
808 | prim.CreatorID = new LLUUID((String) row["CreatorID"]); | ||
809 | prim.OwnerID = new LLUUID((String) row["OwnerID"]); | ||
810 | prim.GroupID = new LLUUID((String) row["GroupID"]); | ||
811 | prim.LastOwnerID = new LLUUID((String) row["LastOwnerID"]); | ||
812 | prim.OwnerMask = Convert.ToUInt32(row["OwnerMask"]); | ||
813 | prim.NextOwnerMask = Convert.ToUInt32(row["NextOwnerMask"]); | ||
814 | prim.GroupMask = Convert.ToUInt32(row["GroupMask"]); | ||
815 | prim.EveryoneMask = Convert.ToUInt32(row["EveryoneMask"]); | ||
816 | prim.BaseMask = Convert.ToUInt32(row["BaseMask"]); | ||
817 | // vectors | ||
818 | prim.OffsetPosition = new LLVector3( | ||
819 | Convert.ToSingle(row["PositionX"]), | ||
820 | Convert.ToSingle(row["PositionY"]), | ||
821 | Convert.ToSingle(row["PositionZ"]) | ||
822 | ); | ||
823 | prim.GroupPosition = new LLVector3( | ||
824 | Convert.ToSingle(row["GroupPositionX"]), | ||
825 | Convert.ToSingle(row["GroupPositionY"]), | ||
826 | Convert.ToSingle(row["GroupPositionZ"]) | ||
827 | ); | ||
828 | prim.Velocity = new LLVector3( | ||
829 | Convert.ToSingle(row["VelocityX"]), | ||
830 | Convert.ToSingle(row["VelocityY"]), | ||
831 | Convert.ToSingle(row["VelocityZ"]) | ||
832 | ); | ||
833 | prim.AngularVelocity = new LLVector3( | ||
834 | Convert.ToSingle(row["AngularVelocityX"]), | ||
835 | Convert.ToSingle(row["AngularVelocityY"]), | ||
836 | Convert.ToSingle(row["AngularVelocityZ"]) | ||
837 | ); | ||
838 | prim.Acceleration = new LLVector3( | ||
839 | Convert.ToSingle(row["AccelerationX"]), | ||
840 | Convert.ToSingle(row["AccelerationY"]), | ||
841 | Convert.ToSingle(row["AccelerationZ"]) | ||
842 | ); | ||
843 | // quaternions | ||
844 | prim.RotationOffset = new LLQuaternion( | ||
845 | Convert.ToSingle(row["RotationX"]), | ||
846 | Convert.ToSingle(row["RotationY"]), | ||
847 | Convert.ToSingle(row["RotationZ"]), | ||
848 | Convert.ToSingle(row["RotationW"]) | ||
849 | ); | ||
850 | |||
851 | try | ||
852 | { | ||
853 | prim.SetSitTargetLL(new LLVector3( | ||
854 | Convert.ToSingle(row["SitTargetOffsetX"]), | ||
855 | Convert.ToSingle(row["SitTargetOffsetY"]), | ||
856 | Convert.ToSingle(row["SitTargetOffsetZ"])), new LLQuaternion( | ||
857 | Convert.ToSingle( | ||
858 | row["SitTargetOrientX"]), | ||
859 | Convert.ToSingle( | ||
860 | row["SitTargetOrientY"]), | ||
861 | Convert.ToSingle( | ||
862 | row["SitTargetOrientZ"]), | ||
863 | Convert.ToSingle( | ||
864 | row["SitTargetOrientW"]))); | ||
865 | } | ||
866 | catch (InvalidCastException) | ||
867 | { | ||
868 | // Database table was created before we got here and now has null values :P | ||
869 | m_conn.Open(); | ||
870 | SqliteCommand cmd = | ||
871 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOffsetX float NOT NULL default 0;", m_conn); | ||
872 | cmd.ExecuteNonQuery(); | ||
873 | cmd = | ||
874 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOffsetY float NOT NULL default 0;", m_conn); | ||
875 | cmd.ExecuteNonQuery(); | ||
876 | cmd = | ||
877 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOffsetZ float NOT NULL default 0;", m_conn); | ||
878 | cmd.ExecuteNonQuery(); | ||
879 | cmd = | ||
880 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOrientW float NOT NULL default 0;", m_conn); | ||
881 | cmd.ExecuteNonQuery(); | ||
882 | cmd = | ||
883 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOrientX float NOT NULL default 0;", m_conn); | ||
884 | cmd.ExecuteNonQuery(); | ||
885 | cmd = | ||
886 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOrientY float NOT NULL default 0;", m_conn); | ||
887 | cmd.ExecuteNonQuery(); | ||
888 | cmd = | ||
889 | new SqliteCommand("ALTER TABLE prims ADD COLUMN SitTargetOrientZ float NOT NULL default 0;", m_conn); | ||
890 | cmd.ExecuteNonQuery(); | ||
891 | } | ||
892 | |||
893 | return prim; | ||
894 | } | ||
895 | |||
896 | /// <summary> | ||
897 | /// Build a prim inventory item from the persisted data. | ||
898 | /// </summary> | ||
899 | /// <param name="row"></param> | ||
900 | /// <returns></returns> | ||
901 | private TaskInventoryItem buildItem(DataRow row) | ||
902 | { | ||
903 | TaskInventoryItem taskItem = new TaskInventoryItem(); | ||
904 | |||
905 | taskItem.ItemID = new LLUUID((String)row["itemID"]); | ||
906 | taskItem.ParentPartID = new LLUUID((String)row["primID"]); | ||
907 | taskItem.AssetID = new LLUUID((String)row["assetID"]); | ||
908 | taskItem.ParentID = new LLUUID((String)row["parentFolderID"]); | ||
909 | |||
910 | taskItem.InvType = Convert.ToInt32(row["invType"]); | ||
911 | taskItem.Type = Convert.ToInt32(row["assetType"]); | ||
912 | |||
913 | taskItem.Name = (String)row["name"]; | ||
914 | taskItem.Description = (String)row["description"]; | ||
915 | taskItem.CreationDate = Convert.ToUInt32(row["creationDate"]); | ||
916 | taskItem.CreatorID = new LLUUID((String)row["creatorID"]); | ||
917 | taskItem.OwnerID = new LLUUID((String)row["ownerID"]); | ||
918 | taskItem.LastOwnerID = new LLUUID((String)row["lastOwnerID"]); | ||
919 | taskItem.GroupID = new LLUUID((String)row["groupID"]); | ||
920 | |||
921 | taskItem.NextOwnerMask = Convert.ToUInt32(row["nextPermissions"]); | ||
922 | taskItem.OwnerMask = Convert.ToUInt32(row["currentPermissions"]); | ||
923 | taskItem.BaseMask = Convert.ToUInt32(row["basePermissions"]); | ||
924 | taskItem.EveryoneMask = Convert.ToUInt32(row["everyonePermissions"]); | ||
925 | taskItem.GroupMask = Convert.ToUInt32(row["groupPermissions"]); | ||
926 | |||
927 | return taskItem; | ||
928 | } | ||
929 | |||
930 | private LandData buildLandData(DataRow row) | ||
931 | { | ||
932 | LandData newData = new LandData(); | ||
933 | |||
934 | newData.globalID = new LLUUID((String) row["UUID"]); | ||
935 | newData.localID = Convert.ToInt32(row["LocalLandID"]); | ||
936 | |||
937 | // Bitmap is a byte[512] | ||
938 | newData.landBitmapByteArray = (Byte[]) row["Bitmap"]; | ||
939 | |||
940 | newData.landName = (String) row["Name"]; | ||
941 | newData.landDesc = (String) row["Desc"]; | ||
942 | newData.ownerID = (String) row["OwnerUUID"]; | ||
943 | newData.isGroupOwned = (Boolean) row["IsGroupOwned"]; | ||
944 | newData.area = Convert.ToInt32(row["Area"]); | ||
945 | newData.auctionID = Convert.ToUInt32(row["AuctionID"]); //Unemplemented | ||
946 | newData.category = (Parcel.ParcelCategory) Convert.ToInt32(row["Category"]); | ||
947 | //Enum libsecondlife.Parcel.ParcelCategory | ||
948 | newData.claimDate = Convert.ToInt32(row["ClaimDate"]); | ||
949 | newData.claimPrice = Convert.ToInt32(row["ClaimPrice"]); | ||
950 | newData.groupID = new LLUUID((String) row["GroupUUID"]); | ||
951 | newData.salePrice = Convert.ToInt32(row["SalePrice"]); | ||
952 | newData.landStatus = (Parcel.ParcelStatus) Convert.ToInt32(row["LandStatus"]); | ||
953 | //Enum. libsecondlife.Parcel.ParcelStatus | ||
954 | newData.landFlags = Convert.ToUInt32(row["LandFlags"]); | ||
955 | newData.landingType = (Byte) row["LandingType"]; | ||
956 | newData.mediaAutoScale = (Byte) row["MediaAutoScale"]; | ||
957 | newData.mediaID = new LLUUID((String) row["MediaTextureUUID"]); | ||
958 | newData.mediaURL = (String) row["MediaURL"]; | ||
959 | newData.musicURL = (String) row["MusicURL"]; | ||
960 | newData.passHours = Convert.ToSingle(row["PassHours"]); | ||
961 | newData.passPrice = Convert.ToInt32(row["PassPrice"]); | ||
962 | newData.snapshotID = (String) row["SnapshotUUID"]; | ||
963 | |||
964 | newData.userLocation = | ||
965 | new LLVector3(Convert.ToSingle(row["UserLocationX"]), Convert.ToSingle(row["UserLocationY"]), | ||
966 | Convert.ToSingle(row["UserLocationZ"])); | ||
967 | newData.userLookAt = | ||
968 | new LLVector3(Convert.ToSingle(row["UserLookAtX"]), Convert.ToSingle(row["UserLookAtY"]), | ||
969 | Convert.ToSingle(row["UserLookAtZ"])); | ||
970 | newData.parcelAccessList = new List<ParcelManager.ParcelAccessEntry>(); | ||
971 | |||
972 | return newData; | ||
973 | } | ||
974 | |||
975 | private ParcelManager.ParcelAccessEntry buildLandAccessData(DataRow row) | ||
976 | { | ||
977 | ParcelManager.ParcelAccessEntry entry = new ParcelManager.ParcelAccessEntry(); | ||
978 | entry.AgentID = new LLUUID((string) row["AccessUUID"]); | ||
979 | entry.Flags = (ParcelManager.AccessList) row["Flags"]; | ||
980 | entry.Time = new DateTime(); | ||
981 | return entry; | ||
982 | } | ||
983 | |||
984 | private Array serializeTerrain(double[,] val) | ||
985 | { | ||
986 | MemoryStream str = new MemoryStream(65536*sizeof (double)); | ||
987 | BinaryWriter bw = new BinaryWriter(str); | ||
988 | |||
989 | // TODO: COMPATIBILITY - Add byte-order conversions | ||
990 | for (int x = 0; x < 256; x++) | ||
991 | for (int y = 0; y < 256; y++) | ||
992 | bw.Write(val[x, y]); | ||
993 | |||
994 | return str.ToArray(); | ||
995 | } | ||
996 | |||
997 | // private void fillTerrainRow(DataRow row, LLUUID regionUUID, int rev, double[,] val) | ||
998 | // { | ||
999 | // row["RegionUUID"] = regionUUID; | ||
1000 | // row["Revision"] = rev; | ||
1001 | |||
1002 | // MemoryStream str = new MemoryStream(65536*sizeof (double)); | ||
1003 | // BinaryWriter bw = new BinaryWriter(str); | ||
1004 | |||
1005 | // // TODO: COMPATIBILITY - Add byte-order conversions | ||
1006 | // for (int x = 0; x < 256; x++) | ||
1007 | // for (int y = 0; y < 256; y++) | ||
1008 | // bw.Write(val[x, y]); | ||
1009 | |||
1010 | // row["Heightfield"] = str.ToArray(); | ||
1011 | // } | ||
1012 | |||
1013 | private void fillPrimRow(DataRow row, SceneObjectPart prim, LLUUID sceneGroupID, LLUUID regionUUID) | ||
1014 | { | ||
1015 | row["UUID"] = Util.ToRawUuidString(prim.UUID); | ||
1016 | row["RegionUUID"] = Util.ToRawUuidString(regionUUID); | ||
1017 | row["ParentID"] = prim.ParentID; | ||
1018 | row["CreationDate"] = prim.CreationDate; | ||
1019 | row["Name"] = prim.Name; | ||
1020 | row["SceneGroupID"] = Util.ToRawUuidString(sceneGroupID); | ||
1021 | // the UUID of the root part for this SceneObjectGroup | ||
1022 | // various text fields | ||
1023 | row["Text"] = prim.Text; | ||
1024 | row["Description"] = prim.Description; | ||
1025 | row["SitName"] = prim.SitName; | ||
1026 | row["TouchName"] = prim.TouchName; | ||
1027 | // permissions | ||
1028 | row["ObjectFlags"] = prim.ObjectFlags; | ||
1029 | row["CreatorID"] = Util.ToRawUuidString(prim.CreatorID); | ||
1030 | row["OwnerID"] = Util.ToRawUuidString(prim.OwnerID); | ||
1031 | row["GroupID"] = Util.ToRawUuidString(prim.GroupID); | ||
1032 | row["LastOwnerID"] = Util.ToRawUuidString(prim.LastOwnerID); | ||
1033 | row["OwnerMask"] = prim.OwnerMask; | ||
1034 | row["NextOwnerMask"] = prim.NextOwnerMask; | ||
1035 | row["GroupMask"] = prim.GroupMask; | ||
1036 | row["EveryoneMask"] = prim.EveryoneMask; | ||
1037 | row["BaseMask"] = prim.BaseMask; | ||
1038 | // vectors | ||
1039 | row["PositionX"] = prim.OffsetPosition.X; | ||
1040 | row["PositionY"] = prim.OffsetPosition.Y; | ||
1041 | row["PositionZ"] = prim.OffsetPosition.Z; | ||
1042 | row["GroupPositionX"] = prim.GroupPosition.X; | ||
1043 | row["GroupPositionY"] = prim.GroupPosition.Y; | ||
1044 | row["GroupPositionZ"] = prim.GroupPosition.Z; | ||
1045 | row["VelocityX"] = prim.Velocity.X; | ||
1046 | row["VelocityY"] = prim.Velocity.Y; | ||
1047 | row["VelocityZ"] = prim.Velocity.Z; | ||
1048 | row["AngularVelocityX"] = prim.AngularVelocity.X; | ||
1049 | row["AngularVelocityY"] = prim.AngularVelocity.Y; | ||
1050 | row["AngularVelocityZ"] = prim.AngularVelocity.Z; | ||
1051 | row["AccelerationX"] = prim.Acceleration.X; | ||
1052 | row["AccelerationY"] = prim.Acceleration.Y; | ||
1053 | row["AccelerationZ"] = prim.Acceleration.Z; | ||
1054 | // quaternions | ||
1055 | row["RotationX"] = prim.RotationOffset.X; | ||
1056 | row["RotationY"] = prim.RotationOffset.Y; | ||
1057 | row["RotationZ"] = prim.RotationOffset.Z; | ||
1058 | row["RotationW"] = prim.RotationOffset.W; | ||
1059 | |||
1060 | // Sit target | ||
1061 | LLVector3 sitTargetPos = prim.GetSitTargetPositionLL(); | ||
1062 | row["SitTargetOffsetX"] = sitTargetPos.X; | ||
1063 | row["SitTargetOffsetY"] = sitTargetPos.Y; | ||
1064 | row["SitTargetOffsetZ"] = sitTargetPos.Z; | ||
1065 | |||
1066 | LLQuaternion sitTargetOrient = prim.GetSitTargetOrientationLL(); | ||
1067 | row["SitTargetOrientW"] = sitTargetOrient.W; | ||
1068 | row["SitTargetOrientX"] = sitTargetOrient.X; | ||
1069 | row["SitTargetOrientY"] = sitTargetOrient.Y; | ||
1070 | row["SitTargetOrientZ"] = sitTargetOrient.Z; | ||
1071 | } | ||
1072 | |||
1073 | private void fillItemRow(DataRow row, TaskInventoryItem taskItem) | ||
1074 | { | ||
1075 | row["itemID"] = taskItem.ItemID; | ||
1076 | row["primID"] = taskItem.ParentPartID; | ||
1077 | row["assetID"] = taskItem.AssetID; | ||
1078 | row["parentFolderID"] = taskItem.ParentID; | ||
1079 | |||
1080 | row["invType"] = taskItem.InvType; | ||
1081 | row["assetType"] = taskItem.Type; | ||
1082 | |||
1083 | row["name"] = taskItem.Name; | ||
1084 | row["description"] = taskItem.Description; | ||
1085 | row["creationDate"] = taskItem.CreationDate; | ||
1086 | row["creatorID"] = taskItem.CreatorID; | ||
1087 | row["ownerID"] = taskItem.OwnerID; | ||
1088 | row["lastOwnerID"] = taskItem.LastOwnerID; | ||
1089 | row["groupID"] = taskItem.GroupID; | ||
1090 | row["nextPermissions"] = taskItem.NextOwnerMask; | ||
1091 | row["currentPermissions"] = taskItem.OwnerMask; | ||
1092 | row["basePermissions"] = taskItem.BaseMask; | ||
1093 | row["everyonePermissions"] = taskItem.EveryoneMask; | ||
1094 | row["groupPermissions"] = taskItem.GroupMask; | ||
1095 | } | ||
1096 | |||
1097 | private void fillLandRow(DataRow row, LandData land, LLUUID regionUUID) | ||
1098 | { | ||
1099 | row["UUID"] = Util.ToRawUuidString(land.globalID); | ||
1100 | row["RegionUUID"] = Util.ToRawUuidString(regionUUID); | ||
1101 | row["LocalLandID"] = land.localID; | ||
1102 | |||
1103 | // Bitmap is a byte[512] | ||
1104 | row["Bitmap"] = land.landBitmapByteArray; | ||
1105 | |||
1106 | row["Name"] = land.landName; | ||
1107 | row["Desc"] = land.landDesc; | ||
1108 | row["OwnerUUID"] = Util.ToRawUuidString(land.ownerID); | ||
1109 | row["IsGroupOwned"] = land.isGroupOwned; | ||
1110 | row["Area"] = land.area; | ||
1111 | row["AuctionID"] = land.auctionID; //Unemplemented | ||
1112 | row["Category"] = land.category; //Enum libsecondlife.Parcel.ParcelCategory | ||
1113 | row["ClaimDate"] = land.claimDate; | ||
1114 | row["ClaimPrice"] = land.claimPrice; | ||
1115 | row["GroupUUID"] = Util.ToRawUuidString(land.groupID); | ||
1116 | row["SalePrice"] = land.salePrice; | ||
1117 | row["LandStatus"] = land.landStatus; //Enum. libsecondlife.Parcel.ParcelStatus | ||
1118 | row["LandFlags"] = land.landFlags; | ||
1119 | row["LandingType"] = land.landingType; | ||
1120 | row["MediaAutoScale"] = land.mediaAutoScale; | ||
1121 | row["MediaTextureUUID"] = Util.ToRawUuidString(land.mediaID); | ||
1122 | row["MediaURL"] = land.mediaURL; | ||
1123 | row["MusicURL"] = land.musicURL; | ||
1124 | row["PassHours"] = land.passHours; | ||
1125 | row["PassPrice"] = land.passPrice; | ||
1126 | row["SnapshotUUID"] = Util.ToRawUuidString(land.snapshotID); | ||
1127 | row["UserLocationX"] = land.userLocation.X; | ||
1128 | row["UserLocationY"] = land.userLocation.Y; | ||
1129 | row["UserLocationZ"] = land.userLocation.Z; | ||
1130 | row["UserLookAtX"] = land.userLookAt.X; | ||
1131 | row["UserLookAtY"] = land.userLookAt.Y; | ||
1132 | row["UserLookAtZ"] = land.userLookAt.Z; | ||
1133 | } | ||
1134 | |||
1135 | private void fillLandAccessRow(DataRow row, ParcelManager.ParcelAccessEntry entry, LLUUID parcelID) | ||
1136 | { | ||
1137 | row["LandUUID"] = Util.ToRawUuidString(parcelID); | ||
1138 | row["AccessUUID"] = Util.ToRawUuidString(entry.AgentID); | ||
1139 | row["Flags"] = entry.Flags; | ||
1140 | } | ||
1141 | |||
1142 | private PrimitiveBaseShape buildShape(DataRow row) | ||
1143 | { | ||
1144 | PrimitiveBaseShape s = new PrimitiveBaseShape(); | ||
1145 | s.Scale = new LLVector3( | ||
1146 | Convert.ToSingle(row["ScaleX"]), | ||
1147 | Convert.ToSingle(row["ScaleY"]), | ||
1148 | Convert.ToSingle(row["ScaleZ"]) | ||
1149 | ); | ||
1150 | // paths | ||
1151 | s.PCode = Convert.ToByte(row["PCode"]); | ||
1152 | s.PathBegin = Convert.ToUInt16(row["PathBegin"]); | ||
1153 | s.PathEnd = Convert.ToUInt16(row["PathEnd"]); | ||
1154 | s.PathScaleX = Convert.ToByte(row["PathScaleX"]); | ||
1155 | s.PathScaleY = Convert.ToByte(row["PathScaleY"]); | ||
1156 | s.PathShearX = Convert.ToByte(row["PathShearX"]); | ||
1157 | s.PathShearY = Convert.ToByte(row["PathShearY"]); | ||
1158 | s.PathSkew = Convert.ToSByte(row["PathSkew"]); | ||
1159 | s.PathCurve = Convert.ToByte(row["PathCurve"]); | ||
1160 | s.PathRadiusOffset = Convert.ToSByte(row["PathRadiusOffset"]); | ||
1161 | s.PathRevolutions = Convert.ToByte(row["PathRevolutions"]); | ||
1162 | s.PathTaperX = Convert.ToSByte(row["PathTaperX"]); | ||
1163 | s.PathTaperY = Convert.ToSByte(row["PathTaperY"]); | ||
1164 | s.PathTwist = Convert.ToSByte(row["PathTwist"]); | ||
1165 | s.PathTwistBegin = Convert.ToSByte(row["PathTwistBegin"]); | ||
1166 | // profile | ||
1167 | s.ProfileBegin = Convert.ToUInt16(row["ProfileBegin"]); | ||
1168 | s.ProfileEnd = Convert.ToUInt16(row["ProfileEnd"]); | ||
1169 | s.ProfileCurve = Convert.ToByte(row["ProfileCurve"]); | ||
1170 | s.ProfileHollow = Convert.ToUInt16(row["ProfileHollow"]); | ||
1171 | try | ||
1172 | { | ||
1173 | s.State = Convert.ToByte(row["State"]); | ||
1174 | } | ||
1175 | catch (InvalidCastException) | ||
1176 | { | ||
1177 | m_conn.Open(); | ||
1178 | SqliteCommand cmd = | ||
1179 | new SqliteCommand("ALTER TABLE primshapes ADD COLUMN State Integer NOT NULL default 0;", m_conn); | ||
1180 | cmd.ExecuteNonQuery(); | ||
1181 | } | ||
1182 | // text TODO: this isn't right] = but I'm not sure the right | ||
1183 | // way to specify this as a blob atm | ||
1184 | |||
1185 | byte[] textureEntry = (byte[]) row["Texture"]; | ||
1186 | s.TextureEntry = textureEntry; | ||
1187 | |||
1188 | s.ExtraParams = (byte[]) row["ExtraParams"]; | ||
1189 | // System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); | ||
1190 | // string texture = encoding.GetString((Byte[])row["Texture"]); | ||
1191 | // if (!texture.StartsWith("<")) | ||
1192 | // { | ||
1193 | // //here so that we can still work with old format database files (ie from before I added xml serialization) | ||
1194 | // LLObject.TextureEntry textureEntry = null; | ||
1195 | // textureEntry = new LLObject.TextureEntry(new LLUUID(texture)); | ||
1196 | // s.TextureEntry = textureEntry.ToBytes(); | ||
1197 | // } | ||
1198 | // else | ||
1199 | // { | ||
1200 | // TextureBlock textureEntry = TextureBlock.FromXmlString(texture); | ||
1201 | // s.TextureEntry = textureEntry.TextureData; | ||
1202 | // s.ExtraParams = textureEntry.ExtraParams; | ||
1203 | // } | ||
1204 | |||
1205 | return s; | ||
1206 | } | ||
1207 | |||
1208 | private void fillShapeRow(DataRow row, SceneObjectPart prim) | ||
1209 | { | ||
1210 | PrimitiveBaseShape s = prim.Shape; | ||
1211 | row["UUID"] = Util.ToRawUuidString(prim.UUID); | ||
1212 | // shape is an enum | ||
1213 | row["Shape"] = 0; | ||
1214 | // vectors | ||
1215 | row["ScaleX"] = s.Scale.X; | ||
1216 | row["ScaleY"] = s.Scale.Y; | ||
1217 | row["ScaleZ"] = s.Scale.Z; | ||
1218 | // paths | ||
1219 | row["PCode"] = s.PCode; | ||
1220 | row["PathBegin"] = s.PathBegin; | ||
1221 | row["PathEnd"] = s.PathEnd; | ||
1222 | row["PathScaleX"] = s.PathScaleX; | ||
1223 | row["PathScaleY"] = s.PathScaleY; | ||
1224 | row["PathShearX"] = s.PathShearX; | ||
1225 | row["PathShearY"] = s.PathShearY; | ||
1226 | row["PathSkew"] = s.PathSkew; | ||
1227 | row["PathCurve"] = s.PathCurve; | ||
1228 | row["PathRadiusOffset"] = s.PathRadiusOffset; | ||
1229 | row["PathRevolutions"] = s.PathRevolutions; | ||
1230 | row["PathTaperX"] = s.PathTaperX; | ||
1231 | row["PathTaperY"] = s.PathTaperY; | ||
1232 | row["PathTwist"] = s.PathTwist; | ||
1233 | row["PathTwistBegin"] = s.PathTwistBegin; | ||
1234 | // profile | ||
1235 | row["ProfileBegin"] = s.ProfileBegin; | ||
1236 | row["ProfileEnd"] = s.ProfileEnd; | ||
1237 | row["ProfileCurve"] = s.ProfileCurve; | ||
1238 | row["ProfileHollow"] = s.ProfileHollow; | ||
1239 | row["State"] = s.State; | ||
1240 | |||
1241 | row["Texture"] = s.TextureEntry; | ||
1242 | row["ExtraParams"] = s.ExtraParams; | ||
1243 | } | ||
1244 | |||
1245 | private void addPrim(SceneObjectPart prim, LLUUID sceneGroupID, LLUUID regionUUID) | ||
1246 | { | ||
1247 | DataTable prims = ds.Tables["prims"]; | ||
1248 | DataTable shapes = ds.Tables["primshapes"]; | ||
1249 | |||
1250 | DataRow primRow = prims.Rows.Find(Util.ToRawUuidString(prim.UUID)); | ||
1251 | if (primRow == null) | ||
1252 | { | ||
1253 | primRow = prims.NewRow(); | ||
1254 | fillPrimRow(primRow, prim, sceneGroupID, regionUUID); | ||
1255 | prims.Rows.Add(primRow); | ||
1256 | } | ||
1257 | else | ||
1258 | { | ||
1259 | fillPrimRow(primRow, prim, sceneGroupID, regionUUID); | ||
1260 | } | ||
1261 | |||
1262 | DataRow shapeRow = shapes.Rows.Find(Util.ToRawUuidString(prim.UUID)); | ||
1263 | if (shapeRow == null) | ||
1264 | { | ||
1265 | shapeRow = shapes.NewRow(); | ||
1266 | fillShapeRow(shapeRow, prim); | ||
1267 | shapes.Rows.Add(shapeRow); | ||
1268 | } | ||
1269 | else | ||
1270 | { | ||
1271 | fillShapeRow(shapeRow, prim); | ||
1272 | } | ||
1273 | } | ||
1274 | |||
1275 | // see IRegionDatastore | ||
1276 | public void StorePrimInventory(LLUUID primID, ICollection<TaskInventoryItem> items) | ||
1277 | { | ||
1278 | if (!persistPrimInventories) | ||
1279 | return; | ||
1280 | |||
1281 | m_log.InfoFormat("[DATASTORE]: Entered StorePrimInventory with prim ID {0}", primID); | ||
1282 | |||
1283 | DataTable dbItems = ds.Tables["primitems"]; | ||
1284 | |||
1285 | // For now, we're just going to crudely remove all the previous inventory items | ||
1286 | // no matter whether they have changed or not, and replace them with the current set. | ||
1287 | lock (ds) | ||
1288 | { | ||
1289 | RemoveItems(primID); | ||
1290 | |||
1291 | // repalce with current inventory details | ||
1292 | foreach (TaskInventoryItem newItem in items) | ||
1293 | { | ||
1294 | // m_log.InfoFormat( | ||
1295 | // "[DATASTORE]: ", | ||
1296 | // "Adding item {0}, {1} to prim ID {2}", | ||
1297 | // newItem.Name, newItem.ItemID, newItem.ParentPartID); | ||
1298 | |||
1299 | DataRow newItemRow = dbItems.NewRow(); | ||
1300 | fillItemRow(newItemRow, newItem); | ||
1301 | dbItems.Rows.Add(newItemRow); | ||
1302 | } | ||
1303 | } | ||
1304 | |||
1305 | Commit(); | ||
1306 | } | ||
1307 | |||
1308 | /*********************************************************************** | ||
1309 | * | ||
1310 | * SQL Statement Creation Functions | ||
1311 | * | ||
1312 | * These functions create SQL statements for update, insert, and create. | ||
1313 | * They can probably be factored later to have a db independant | ||
1314 | * portion and a db specific portion | ||
1315 | * | ||
1316 | **********************************************************************/ | ||
1317 | |||
1318 | private SqliteCommand createInsertCommand(string table, DataTable dt) | ||
1319 | { | ||
1320 | /** | ||
1321 | * This is subtle enough to deserve some commentary. | ||
1322 | * Instead of doing *lots* and *lots of hardcoded strings | ||
1323 | * for database definitions we'll use the fact that | ||
1324 | * realistically all insert statements look like "insert | ||
1325 | * into A(b, c) values(:b, :c) on the parameterized query | ||
1326 | * front. If we just have a list of b, c, etc... we can | ||
1327 | * generate these strings instead of typing them out. | ||
1328 | */ | ||
1329 | string[] cols = new string[dt.Columns.Count]; | ||
1330 | for (int i = 0; i < dt.Columns.Count; i++) | ||
1331 | { | ||
1332 | DataColumn col = dt.Columns[i]; | ||
1333 | cols[i] = col.ColumnName; | ||
1334 | } | ||
1335 | |||
1336 | string sql = "insert into " + table + "("; | ||
1337 | sql += String.Join(", ", cols); | ||
1338 | // important, the first ':' needs to be here, the rest get added in the join | ||
1339 | sql += ") values (:"; | ||
1340 | sql += String.Join(", :", cols); | ||
1341 | sql += ")"; | ||
1342 | SqliteCommand cmd = new SqliteCommand(sql); | ||
1343 | |||
1344 | // this provides the binding for all our parameters, so | ||
1345 | // much less code than it used to be | ||
1346 | foreach (DataColumn col in dt.Columns) | ||
1347 | { | ||
1348 | cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType)); | ||
1349 | } | ||
1350 | return cmd; | ||
1351 | } | ||
1352 | |||
1353 | private SqliteCommand createUpdateCommand(string table, string pk, DataTable dt) | ||
1354 | { | ||
1355 | string sql = "update " + table + " set "; | ||
1356 | string subsql = String.Empty; | ||
1357 | foreach (DataColumn col in dt.Columns) | ||
1358 | { | ||
1359 | if (subsql.Length > 0) | ||
1360 | { | ||
1361 | // a map function would rock so much here | ||
1362 | subsql += ", "; | ||
1363 | } | ||
1364 | subsql += col.ColumnName + "= :" + col.ColumnName; | ||
1365 | } | ||
1366 | sql += subsql; | ||
1367 | sql += " where " + pk; | ||
1368 | SqliteCommand cmd = new SqliteCommand(sql); | ||
1369 | |||
1370 | // this provides the binding for all our parameters, so | ||
1371 | // much less code than it used to be | ||
1372 | |||
1373 | foreach (DataColumn col in dt.Columns) | ||
1374 | { | ||
1375 | cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType)); | ||
1376 | } | ||
1377 | return cmd; | ||
1378 | } | ||
1379 | |||
1380 | |||
1381 | private string defineTable(DataTable dt) | ||
1382 | { | ||
1383 | string sql = "create table " + dt.TableName + "("; | ||
1384 | string subsql = String.Empty; | ||
1385 | foreach (DataColumn col in dt.Columns) | ||
1386 | { | ||
1387 | if (subsql.Length > 0) | ||
1388 | { | ||
1389 | // a map function would rock so much here | ||
1390 | subsql += ",\n"; | ||
1391 | } | ||
1392 | subsql += col.ColumnName + " " + sqliteType(col.DataType); | ||
1393 | if (dt.PrimaryKey.Length > 0 && col == dt.PrimaryKey[0]) | ||
1394 | { | ||
1395 | subsql += " primary key"; | ||
1396 | } | ||
1397 | } | ||
1398 | sql += subsql; | ||
1399 | sql += ")"; | ||
1400 | return sql; | ||
1401 | } | ||
1402 | |||
1403 | /*********************************************************************** | ||
1404 | * | ||
1405 | * Database Binding functions | ||
1406 | * | ||
1407 | * These will be db specific due to typing, and minor differences | ||
1408 | * in databases. | ||
1409 | * | ||
1410 | **********************************************************************/ | ||
1411 | |||
1412 | ///<summary> | ||
1413 | /// This is a convenience function that collapses 5 repetitive | ||
1414 | /// lines for defining SqliteParameters to 2 parameters: | ||
1415 | /// column name and database type. | ||
1416 | /// | ||
1417 | /// It assumes certain conventions like :param as the param | ||
1418 | /// name to replace in parametrized queries, and that source | ||
1419 | /// version is always current version, both of which are fine | ||
1420 | /// for us. | ||
1421 | ///</summary> | ||
1422 | ///<returns>a built sqlite parameter</returns> | ||
1423 | private SqliteParameter createSqliteParameter(string name, Type type) | ||
1424 | { | ||
1425 | SqliteParameter param = new SqliteParameter(); | ||
1426 | param.ParameterName = ":" + name; | ||
1427 | param.DbType = dbtypeFromType(type); | ||
1428 | param.SourceColumn = name; | ||
1429 | param.SourceVersion = DataRowVersion.Current; | ||
1430 | return param; | ||
1431 | } | ||
1432 | |||
1433 | private void setupPrimCommands(SqliteDataAdapter da, SqliteConnection conn) | ||
1434 | { | ||
1435 | da.InsertCommand = createInsertCommand("prims", ds.Tables["prims"]); | ||
1436 | da.InsertCommand.Connection = conn; | ||
1437 | |||
1438 | da.UpdateCommand = createUpdateCommand("prims", "UUID=:UUID", ds.Tables["prims"]); | ||
1439 | da.UpdateCommand.Connection = conn; | ||
1440 | |||
1441 | SqliteCommand delete = new SqliteCommand("delete from prims where UUID = :UUID"); | ||
1442 | delete.Parameters.Add(createSqliteParameter("UUID", typeof (String))); | ||
1443 | delete.Connection = conn; | ||
1444 | da.DeleteCommand = delete; | ||
1445 | } | ||
1446 | |||
1447 | private void setupItemsCommands(SqliteDataAdapter da, SqliteConnection conn) | ||
1448 | { | ||
1449 | da.InsertCommand = createInsertCommand("primitems", ds.Tables["primitems"]); | ||
1450 | da.InsertCommand.Connection = conn; | ||
1451 | |||
1452 | da.UpdateCommand = createUpdateCommand("primitems", "itemID = :itemID", ds.Tables["primitems"]); | ||
1453 | da.UpdateCommand.Connection = conn; | ||
1454 | |||
1455 | SqliteCommand delete = new SqliteCommand("delete from primitems where itemID = :itemID"); | ||
1456 | delete.Parameters.Add(createSqliteParameter("itemID", typeof (String))); | ||
1457 | delete.Connection = conn; | ||
1458 | da.DeleteCommand = delete; | ||
1459 | } | ||
1460 | |||
1461 | private void setupTerrainCommands(SqliteDataAdapter da, SqliteConnection conn) | ||
1462 | { | ||
1463 | da.InsertCommand = createInsertCommand("terrain", ds.Tables["terrain"]); | ||
1464 | da.InsertCommand.Connection = conn; | ||
1465 | } | ||
1466 | |||
1467 | private void setupLandCommands(SqliteDataAdapter da, SqliteConnection conn) | ||
1468 | { | ||
1469 | da.InsertCommand = createInsertCommand("land", ds.Tables["land"]); | ||
1470 | da.InsertCommand.Connection = conn; | ||
1471 | |||
1472 | da.UpdateCommand = createUpdateCommand("land", "UUID=:UUID", ds.Tables["land"]); | ||
1473 | da.UpdateCommand.Connection = conn; | ||
1474 | } | ||
1475 | |||
1476 | private void setupLandAccessCommands(SqliteDataAdapter da, SqliteConnection conn) | ||
1477 | { | ||
1478 | da.InsertCommand = createInsertCommand("landaccesslist", ds.Tables["landaccesslist"]); | ||
1479 | da.InsertCommand.Connection = conn; | ||
1480 | } | ||
1481 | |||
1482 | private void setupShapeCommands(SqliteDataAdapter da, SqliteConnection conn) | ||
1483 | { | ||
1484 | da.InsertCommand = createInsertCommand("primshapes", ds.Tables["primshapes"]); | ||
1485 | da.InsertCommand.Connection = conn; | ||
1486 | |||
1487 | da.UpdateCommand = createUpdateCommand("primshapes", "UUID=:UUID", ds.Tables["primshapes"]); | ||
1488 | da.UpdateCommand.Connection = conn; | ||
1489 | |||
1490 | SqliteCommand delete = new SqliteCommand("delete from primshapes where UUID = :UUID"); | ||
1491 | delete.Parameters.Add(createSqliteParameter("UUID", typeof (String))); | ||
1492 | delete.Connection = conn; | ||
1493 | da.DeleteCommand = delete; | ||
1494 | } | ||
1495 | |||
1496 | /// <summary> | ||
1497 | /// Create the necessary database tables. | ||
1498 | /// </summary> | ||
1499 | /// <param name="conn"></param> | ||
1500 | private void InitDB(SqliteConnection conn) | ||
1501 | { | ||
1502 | string createPrims = defineTable(createPrimTable()); | ||
1503 | string createShapes = defineTable(createShapeTable()); | ||
1504 | string createItems = defineTable(createItemsTable()); | ||
1505 | string createTerrain = defineTable(createTerrainTable()); | ||
1506 | string createLand = defineTable(createLandTable()); | ||
1507 | string createLandAccessList = defineTable(createLandAccessListTable()); | ||
1508 | |||
1509 | SqliteCommand pcmd = new SqliteCommand(createPrims, conn); | ||
1510 | SqliteCommand scmd = new SqliteCommand(createShapes, conn); | ||
1511 | SqliteCommand icmd = new SqliteCommand(createItems, conn); | ||
1512 | SqliteCommand tcmd = new SqliteCommand(createTerrain, conn); | ||
1513 | SqliteCommand lcmd = new SqliteCommand(createLand, conn); | ||
1514 | SqliteCommand lalcmd = new SqliteCommand(createLandAccessList, conn); | ||
1515 | |||
1516 | try | ||
1517 | { | ||
1518 | pcmd.ExecuteNonQuery(); | ||
1519 | } | ||
1520 | catch (SqliteSyntaxException) | ||
1521 | { | ||
1522 | m_log.Warn("[SQLITE]: Primitives Table Already Exists"); | ||
1523 | } | ||
1524 | |||
1525 | try | ||
1526 | { | ||
1527 | scmd.ExecuteNonQuery(); | ||
1528 | } | ||
1529 | catch (SqliteSyntaxException) | ||
1530 | { | ||
1531 | m_log.Warn("[SQLITE]: Shapes Table Already Exists"); | ||
1532 | } | ||
1533 | |||
1534 | if (persistPrimInventories) | ||
1535 | { | ||
1536 | try | ||
1537 | { | ||
1538 | icmd.ExecuteNonQuery(); | ||
1539 | } | ||
1540 | catch (SqliteSyntaxException) | ||
1541 | { | ||
1542 | m_log.Warn("[SQLITE]: Primitives Inventory Table Already Exists"); | ||
1543 | } | ||
1544 | } | ||
1545 | |||
1546 | try | ||
1547 | { | ||
1548 | tcmd.ExecuteNonQuery(); | ||
1549 | } | ||
1550 | catch (SqliteSyntaxException) | ||
1551 | { | ||
1552 | m_log.Warn("[SQLITE]: Terrain Table Already Exists"); | ||
1553 | } | ||
1554 | |||
1555 | try | ||
1556 | { | ||
1557 | lcmd.ExecuteNonQuery(); | ||
1558 | } | ||
1559 | catch (SqliteSyntaxException) | ||
1560 | { | ||
1561 | m_log.Warn("[SQLITE]: Land Table Already Exists"); | ||
1562 | } | ||
1563 | |||
1564 | try | ||
1565 | { | ||
1566 | lalcmd.ExecuteNonQuery(); | ||
1567 | } | ||
1568 | catch (SqliteSyntaxException) | ||
1569 | { | ||
1570 | m_log.Warn("[SQLITE]: LandAccessList Table Already Exists"); | ||
1571 | } | ||
1572 | } | ||
1573 | |||
1574 | private bool TestTables(SqliteConnection conn) | ||
1575 | { | ||
1576 | SqliteCommand primSelectCmd = new SqliteCommand(primSelect, conn); | ||
1577 | SqliteDataAdapter pDa = new SqliteDataAdapter(primSelectCmd); | ||
1578 | |||
1579 | SqliteCommand shapeSelectCmd = new SqliteCommand(shapeSelect, conn); | ||
1580 | SqliteDataAdapter sDa = new SqliteDataAdapter(shapeSelectCmd); | ||
1581 | |||
1582 | SqliteCommand itemsSelectCmd = new SqliteCommand(itemsSelect, conn); | ||
1583 | SqliteDataAdapter iDa = new SqliteDataAdapter(itemsSelectCmd); | ||
1584 | |||
1585 | SqliteCommand terrainSelectCmd = new SqliteCommand(terrainSelect, conn); | ||
1586 | SqliteDataAdapter tDa = new SqliteDataAdapter(terrainSelectCmd); | ||
1587 | |||
1588 | SqliteCommand landSelectCmd = new SqliteCommand(landSelect, conn); | ||
1589 | SqliteDataAdapter lDa = new SqliteDataAdapter(landSelectCmd); | ||
1590 | |||
1591 | SqliteCommand landAccessListSelectCmd = new SqliteCommand(landAccessListSelect, conn); | ||
1592 | SqliteDataAdapter lalDa = new SqliteDataAdapter(landAccessListSelectCmd); | ||
1593 | |||
1594 | DataSet tmpDS = new DataSet(); | ||
1595 | try | ||
1596 | { | ||
1597 | pDa.Fill(tmpDS, "prims"); | ||
1598 | sDa.Fill(tmpDS, "primshapes"); | ||
1599 | |||
1600 | if (persistPrimInventories) | ||
1601 | iDa.Fill(tmpDS, "primitems"); | ||
1602 | |||
1603 | tDa.Fill(tmpDS, "terrain"); | ||
1604 | lDa.Fill(tmpDS, "land"); | ||
1605 | lalDa.Fill(tmpDS, "landaccesslist"); | ||
1606 | } | ||
1607 | catch (SqliteSyntaxException) | ||
1608 | { | ||
1609 | m_log.Info("[DATASTORE]: SQLite Database doesn't exist... creating"); | ||
1610 | InitDB(conn); | ||
1611 | } | ||
1612 | |||
1613 | pDa.Fill(tmpDS, "prims"); | ||
1614 | sDa.Fill(tmpDS, "primshapes"); | ||
1615 | |||
1616 | if (persistPrimInventories) | ||
1617 | iDa.Fill(tmpDS, "primitems"); | ||
1618 | |||
1619 | tDa.Fill(tmpDS, "terrain"); | ||
1620 | lDa.Fill(tmpDS, "land"); | ||
1621 | lalDa.Fill(tmpDS, "landaccesslist"); | ||
1622 | |||
1623 | foreach (DataColumn col in createPrimTable().Columns) | ||
1624 | { | ||
1625 | if (!tmpDS.Tables["prims"].Columns.Contains(col.ColumnName)) | ||
1626 | { | ||
1627 | m_log.Info("[DATASTORE]: Missing required column:" + col.ColumnName); | ||
1628 | return false; | ||
1629 | } | ||
1630 | } | ||
1631 | |||
1632 | foreach (DataColumn col in createShapeTable().Columns) | ||
1633 | { | ||
1634 | if (!tmpDS.Tables["primshapes"].Columns.Contains(col.ColumnName)) | ||
1635 | { | ||
1636 | m_log.Info("[DATASTORE]: Missing required column:" + col.ColumnName); | ||
1637 | return false; | ||
1638 | } | ||
1639 | } | ||
1640 | |||
1641 | // XXX primitems should probably go here eventually | ||
1642 | |||
1643 | foreach (DataColumn col in createTerrainTable().Columns) | ||
1644 | { | ||
1645 | if (!tmpDS.Tables["terrain"].Columns.Contains(col.ColumnName)) | ||
1646 | { | ||
1647 | m_log.Info("[DATASTORE]: Missing require column:" + col.ColumnName); | ||
1648 | return false; | ||
1649 | } | ||
1650 | } | ||
1651 | |||
1652 | foreach (DataColumn col in createLandTable().Columns) | ||
1653 | { | ||
1654 | if (!tmpDS.Tables["land"].Columns.Contains(col.ColumnName)) | ||
1655 | { | ||
1656 | m_log.Info("[DATASTORE]: Missing require column:" + col.ColumnName); | ||
1657 | return false; | ||
1658 | } | ||
1659 | } | ||
1660 | |||
1661 | foreach (DataColumn col in createLandAccessListTable().Columns) | ||
1662 | { | ||
1663 | if (!tmpDS.Tables["landaccesslist"].Columns.Contains(col.ColumnName)) | ||
1664 | { | ||
1665 | m_log.Info("[DATASTORE]: Missing require column:" + col.ColumnName); | ||
1666 | return false; | ||
1667 | } | ||
1668 | } | ||
1669 | |||
1670 | return true; | ||
1671 | } | ||
1672 | |||
1673 | /*********************************************************************** | ||
1674 | * | ||
1675 | * Type conversion functions | ||
1676 | * | ||
1677 | **********************************************************************/ | ||
1678 | |||
1679 | private DbType dbtypeFromType(Type type) | ||
1680 | { | ||
1681 | if (type == typeof (String)) | ||
1682 | { | ||
1683 | return DbType.String; | ||
1684 | } | ||
1685 | else if (type == typeof (Int32)) | ||
1686 | { | ||
1687 | return DbType.Int32; | ||
1688 | } | ||
1689 | else if (type == typeof (Double)) | ||
1690 | { | ||
1691 | return DbType.Double; | ||
1692 | } | ||
1693 | else if (type == typeof (Byte)) | ||
1694 | { | ||
1695 | return DbType.Byte; | ||
1696 | } | ||
1697 | else if (type == typeof (Double)) | ||
1698 | { | ||
1699 | return DbType.Double; | ||
1700 | } | ||
1701 | else if (type == typeof (Byte[])) | ||
1702 | { | ||
1703 | return DbType.Binary; | ||
1704 | } | ||
1705 | else | ||
1706 | { | ||
1707 | return DbType.String; | ||
1708 | } | ||
1709 | } | ||
1710 | |||
1711 | // this is something we'll need to implement for each db | ||
1712 | // slightly differently. | ||
1713 | private string sqliteType(Type type) | ||
1714 | { | ||
1715 | if (type == typeof (String)) | ||
1716 | { | ||
1717 | return "varchar(255)"; | ||
1718 | } | ||
1719 | else if (type == typeof (Int32)) | ||
1720 | { | ||
1721 | return "integer"; | ||
1722 | } | ||
1723 | else if (type == typeof (Int64)) | ||
1724 | { | ||
1725 | return "integer"; | ||
1726 | } | ||
1727 | else if (type == typeof (Double)) | ||
1728 | { | ||
1729 | return "float"; | ||
1730 | } | ||
1731 | else if (type == typeof (Byte[])) | ||
1732 | { | ||
1733 | return "blob"; | ||
1734 | } | ||
1735 | else | ||
1736 | { | ||
1737 | return "string"; | ||
1738 | } | ||
1739 | } | ||
1740 | } | ||
1741 | } | ||