SQLiteRegionData.cs 91 KB

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