MySQLDataStore.cs 72 KB

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