aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Data.SQLite
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Data.SQLite')
-rw-r--r--OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs169
1 files changed, 66 insertions, 103 deletions
diff --git a/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs b/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
index 462c433..7b02e99 100644
--- a/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
+++ b/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
@@ -45,76 +45,86 @@ namespace OpenSim.Framework.Data.SQLite
45 /// <summary> 45 /// <summary>
46 /// Artificial constructor called upon plugin load 46 /// Artificial constructor called upon plugin load
47 /// </summary> 47 /// </summary>
48 private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
49 private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
50 private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, InvType, Local, Temporary, Data) values(:UUID, :Name, :Description, :Type, :InvType, :Local, :Temporary, :Data)";
51 private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, InvType=:InvType, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID";
48 private const string assetSelect = "select * from assets"; 52 private const string assetSelect = "select * from assets";
49 53
50 private DataSet ds; 54 private SqliteConnection m_conn;
51 private SqliteDataAdapter da;
52 55
53 public void Initialise(string dbfile, string dbname) 56 public void Initialise(string dbfile, string dbname)
54 { 57 {
55 SqliteConnection conn = new SqliteConnection("URI=file:" + dbfile + ",version=3"); 58 m_conn = new SqliteConnection("URI=file:" + dbfile + ",version=3");
56 TestTables(conn); 59 m_conn.Open();
57 60
58 ds = new DataSet(); 61 TestTables(m_conn);
59 da = new SqliteDataAdapter(new SqliteCommand(assetSelect, conn)); 62 return;
63 }
60 64
61 lock (ds) 65 public AssetBase FetchAsset(LLUUID uuid)
66 {
67
68 using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
62 { 69 {
63 ds.Tables.Add(createAssetsTable()); 70 cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.UUID.ToString()));
64 71 using (IDataReader reader = cmd.ExecuteReader())
65 setupAssetCommands(da, conn);
66 try
67 {
68 da.Fill(ds.Tables["assets"]);
69 }
70 catch (Exception e)
71 { 72 {
72 MainLog.Instance.Verbose("SQLITE", e.ToString()); 73 reader.Read();
74 if (reader != null)
75 {
76 return buildAsset(reader);
77 }
78 else
79 {
80 return null;
81 }
73 } 82 }
74 } 83 }
75
76 return;
77 } 84 }
78 85
79 public AssetBase FetchAsset(LLUUID uuid) 86 public void CreateAsset(AssetBase asset)
80 { 87 {
81 AssetBase asset = new AssetBase(); 88 if (ExistsAsset(asset.FullID))
82 DataRow row = ds.Tables["assets"].Rows.Find(Util.ToRawUuidString(uuid));
83 if (row != null)
84 { 89 {
85 return buildAsset(row); 90 UpdateAsset(asset);
86 } 91 }
87 else 92 else
88 { 93 {
89 return null; 94 using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
95 {
96 cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.UUID.ToString()));
97 cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
98 cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
99 cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
100 cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType));
101 cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
102 cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
103 cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
104
105 cmd.ExecuteNonQuery();
106 }
90 } 107 }
91 } 108 }
92 109
93 public void CreateAsset(AssetBase asset)
94 {
95 // no difference for now
96 UpdateAsset(asset);
97 }
98
99 public void UpdateAsset(AssetBase asset) 110 public void UpdateAsset(AssetBase asset)
100 { 111 {
101 LogAssetLoad(asset); 112 LogAssetLoad(asset);
102 113
103 DataTable assets = ds.Tables["assets"]; 114 using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
104 lock (ds)
105 { 115 {
106 DataRow row = assets.Rows.Find(Util.ToRawUuidString(asset.FullID)); 116 cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.UUID.ToString()));
107 if (row == null) 117 cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
108 { 118 cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
109 row = assets.NewRow(); 119 cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
110 fillAssetRow(row, asset); 120 cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType));
111 assets.Rows.Add(row); 121 cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
112 } 122 cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
113 else 123 cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
114 { 124
115 fillAssetRow(row, asset); 125 cmd.ExecuteNonQuery();
116 }
117 } 126 }
127
118 } 128 }
119 129
120 private void LogAssetLoad(AssetBase asset) 130 private void LogAssetLoad(AssetBase asset)
@@ -130,30 +140,27 @@ namespace OpenSim.Framework.Data.SQLite
130 140
131 public bool ExistsAsset(LLUUID uuid) 141 public bool ExistsAsset(LLUUID uuid)
132 { 142 {
133 DataRow row = ds.Tables["assets"].Rows.Find(Util.ToRawUuidString(uuid)); 143 return (FetchAsset(uuid) != null);
134 return (row != null);
135 } 144 }
136 145
137 public void DeleteAsset(LLUUID uuid) 146 public void DeleteAsset(LLUUID uuid)
138 { 147 {
139 lock (ds) 148 using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
140 { 149 {
141 DataRow row = ds.Tables["assets"].Rows.Find(Util.ToRawUuidString(uuid)); 150 cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.UUID.ToString()));
142 if (row != null) 151
143 { 152 cmd.ExecuteNonQuery();
144 row.Delete();
145 }
146 } 153 }
147 } 154 }
148 155
149 public void CommitAssets() // force a sync to the database 156 public void CommitAssets() // force a sync to the database
150 { 157 {
151 MainLog.Instance.Verbose("SQLITE", "Attempting commit"); 158 MainLog.Instance.Verbose("SQLITE", "Attempting commit");
152 lock (ds) 159 // lock (ds)
153 { 160 // {
154 da.Update(ds, "assets"); 161 // da.Update(ds, "assets");
155 ds.AcceptChanges(); 162 // ds.AcceptChanges();
156 } 163 // }
157 } 164 }
158 165
159 /*********************************************************************** 166 /***********************************************************************
@@ -189,7 +196,7 @@ namespace OpenSim.Framework.Data.SQLite
189 * 196 *
190 **********************************************************************/ 197 **********************************************************************/
191 198
192 private AssetBase buildAsset(DataRow row) 199 private AssetBase buildAsset(IDataReader row)
193 { 200 {
194 // TODO: this doesn't work yet because something more 201 // TODO: this doesn't work yet because something more
195 // interesting has to be done to actually get these values 202 // interesting has to be done to actually get these values
@@ -208,34 +215,6 @@ namespace OpenSim.Framework.Data.SQLite
208 } 215 }
209 216
210 217
211 private void fillAssetRow(DataRow row, AssetBase asset)
212 {
213 row["UUID"] = Util.ToRawUuidString(asset.FullID);
214 row["Name"] = asset.Name;
215 if (asset.Description != null)
216 {
217 row["Description"] = asset.Description;
218 }
219 else
220 {
221 row["Description"] = " ";
222 }
223 row["Type"] = asset.Type;
224 row["InvType"] = asset.InvType;
225 row["Local"] = asset.Local;
226 row["Temporary"] = asset.Temporary;
227 row["Data"] = asset.Data;
228
229 // ADO.NET doesn't handle NULL very well
230 foreach (DataColumn col in ds.Tables["assets"].Columns)
231 {
232 if (row[col] == null)
233 {
234 row[col] = "";
235 }
236 }
237 }
238
239 /*********************************************************************** 218 /***********************************************************************
240 * 219 *
241 * Database Binding functions 220 * Database Binding functions
@@ -245,27 +224,11 @@ namespace OpenSim.Framework.Data.SQLite
245 * 224 *
246 **********************************************************************/ 225 **********************************************************************/
247 226
248 private void setupAssetCommands(SqliteDataAdapter da, SqliteConnection conn)
249 {
250 da.InsertCommand = createInsertCommand("assets", ds.Tables["assets"]);
251 da.InsertCommand.Connection = conn;
252
253 da.UpdateCommand = createUpdateCommand("assets", "UUID=:UUID", ds.Tables["assets"]);
254 da.UpdateCommand.Connection = conn;
255
256 SqliteCommand delete = new SqliteCommand("delete from assets where UUID = :UUID");
257 delete.Parameters.Add(createSqliteParameter("UUID", typeof (String)));
258 delete.Connection = conn;
259 da.DeleteCommand = delete;
260 }
261
262 private void InitDB(SqliteConnection conn) 227 private void InitDB(SqliteConnection conn)
263 { 228 {
264 string createAssets = defineTable(createAssetsTable()); 229 string createAssets = defineTable(createAssetsTable());
265 SqliteCommand pcmd = new SqliteCommand(createAssets, conn); 230 SqliteCommand pcmd = new SqliteCommand(createAssets, conn);
266 conn.Open();
267 pcmd.ExecuteNonQuery(); 231 pcmd.ExecuteNonQuery();
268 conn.Close();
269 } 232 }
270 233
271 private bool TestTables(SqliteConnection conn) 234 private bool TestTables(SqliteConnection conn)