diff options
Diffstat (limited to 'OpenSim/Data/SQLite/SQLiteAssetData.cs')
-rw-r--r-- | OpenSim/Data/SQLite/SQLiteAssetData.cs | 301 |
1 files changed, 301 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLite/SQLiteAssetData.cs b/OpenSim/Data/SQLite/SQLiteAssetData.cs new file mode 100644 index 0000000..afa73b1 --- /dev/null +++ b/OpenSim/Data/SQLite/SQLiteAssetData.cs | |||
@@ -0,0 +1,301 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.Reflection; | ||
31 | using libsecondlife; | ||
32 | using Mono.Data.SqliteClient; | ||
33 | using OpenSim.Framework.Console; | ||
34 | |||
35 | namespace OpenSim.Framework.Data.SQLite | ||
36 | { | ||
37 | /// <summary> | ||
38 | /// A User storage interface for the DB4o database system | ||
39 | /// </summary> | ||
40 | public class SQLiteAssetData : AssetDataBase | ||
41 | { | ||
42 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
43 | |||
44 | /// <summary> | ||
45 | /// The database manager | ||
46 | /// </summary> | ||
47 | /// <summary> | ||
48 | /// Artificial constructor called upon plugin load | ||
49 | /// </summary> | ||
50 | private const string SelectAssetSQL = "select * from assets where UUID=:UUID"; | ||
51 | private const string DeleteAssetSQL = "delete from assets where UUID=:UUID"; | ||
52 | private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, InvType, Local, Temporary, Data) values(:UUID, :Name, :Description, :Type, :InvType, :Local, :Temporary, :Data)"; | ||
53 | private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, InvType=:InvType, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID"; | ||
54 | private const string assetSelect = "select * from assets"; | ||
55 | |||
56 | private SqliteConnection m_conn; | ||
57 | |||
58 | public void Initialise(string dbfile, string dbname) | ||
59 | { | ||
60 | m_conn = new SqliteConnection("URI=file:" + dbfile + ",version=3"); | ||
61 | m_conn.Open(); | ||
62 | TestTables(m_conn); | ||
63 | return; | ||
64 | } | ||
65 | |||
66 | override public AssetBase FetchAsset(LLUUID uuid) | ||
67 | { | ||
68 | |||
69 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn)) | ||
70 | { | ||
71 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid))); | ||
72 | using (IDataReader reader = cmd.ExecuteReader()) | ||
73 | { | ||
74 | if (reader.Read()) | ||
75 | { | ||
76 | AssetBase asset = buildAsset(reader); | ||
77 | reader.Close(); | ||
78 | return asset; | ||
79 | } | ||
80 | else | ||
81 | { | ||
82 | reader.Close(); | ||
83 | return null; | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | |||
89 | override public void CreateAsset(AssetBase asset) | ||
90 | { | ||
91 | m_log.Info("[SQLITE]: Creating Asset " + Util.ToRawUuidString(asset.FullID)); | ||
92 | if (ExistsAsset(asset.FullID)) | ||
93 | { | ||
94 | m_log.Info("[SQLITE]: Asset exists already, ignoring."); | ||
95 | } | ||
96 | else | ||
97 | { | ||
98 | using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn)) | ||
99 | { | ||
100 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID))); | ||
101 | cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name)); | ||
102 | cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description)); | ||
103 | cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type)); | ||
104 | cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType)); | ||
105 | cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local)); | ||
106 | cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary)); | ||
107 | cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data)); | ||
108 | |||
109 | cmd.ExecuteNonQuery(); | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | |||
114 | override public void UpdateAsset(AssetBase asset) | ||
115 | { | ||
116 | LogAssetLoad(asset); | ||
117 | |||
118 | using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn)) | ||
119 | { | ||
120 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID))); | ||
121 | cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name)); | ||
122 | cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description)); | ||
123 | cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type)); | ||
124 | cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType)); | ||
125 | cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local)); | ||
126 | cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary)); | ||
127 | cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data)); | ||
128 | |||
129 | cmd.ExecuteNonQuery(); | ||
130 | } | ||
131 | |||
132 | } | ||
133 | |||
134 | private void LogAssetLoad(AssetBase asset) | ||
135 | { | ||
136 | string temporary = asset.Temporary ? "Temporary" : "Stored"; | ||
137 | string local = asset.Local ? "Local" : "Remote"; | ||
138 | |||
139 | int assetLength = (asset.Data != null) ? asset.Data.Length : 0; | ||
140 | |||
141 | m_log.Info("[SQLITE]: " + | ||
142 | string.Format("Loaded {6} {5} Asset: [{0}][{3}/{4}] \"{1}\":{2} ({7} bytes)", | ||
143 | asset.FullID, asset.Name, asset.Description, asset.Type, | ||
144 | asset.InvType, temporary, local, assetLength)); | ||
145 | } | ||
146 | |||
147 | override public bool ExistsAsset(LLUUID uuid) | ||
148 | { | ||
149 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn)) | ||
150 | { | ||
151 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid))); | ||
152 | using (IDataReader reader = cmd.ExecuteReader()) | ||
153 | { | ||
154 | if(reader.Read()) | ||
155 | { | ||
156 | reader.Close(); | ||
157 | return true; | ||
158 | } | ||
159 | else | ||
160 | { | ||
161 | reader.Close(); | ||
162 | return false; | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | |||
168 | public void DeleteAsset(LLUUID uuid) | ||
169 | { | ||
170 | using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn)) | ||
171 | { | ||
172 | cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid))); | ||
173 | |||
174 | cmd.ExecuteNonQuery(); | ||
175 | } | ||
176 | } | ||
177 | |||
178 | override public void CommitAssets() // force a sync to the database | ||
179 | { | ||
180 | m_log.Info("[SQLITE]: Attempting commit"); | ||
181 | // lock (ds) | ||
182 | // { | ||
183 | // da.Update(ds, "assets"); | ||
184 | // ds.AcceptChanges(); | ||
185 | // } | ||
186 | } | ||
187 | |||
188 | /*********************************************************************** | ||
189 | * | ||
190 | * Database Definition Functions | ||
191 | * | ||
192 | * This should be db agnostic as we define them in ADO.NET terms | ||
193 | * | ||
194 | **********************************************************************/ | ||
195 | |||
196 | private DataTable createAssetsTable() | ||
197 | { | ||
198 | DataTable assets = new DataTable("assets"); | ||
199 | |||
200 | SQLiteUtil.createCol(assets, "UUID", typeof (String)); | ||
201 | SQLiteUtil.createCol(assets, "Name", typeof (String)); | ||
202 | SQLiteUtil.createCol(assets, "Description", typeof (String)); | ||
203 | SQLiteUtil.createCol(assets, "Type", typeof (Int32)); | ||
204 | SQLiteUtil.createCol(assets, "InvType", typeof (Int32)); | ||
205 | SQLiteUtil.createCol(assets, "Local", typeof (Boolean)); | ||
206 | SQLiteUtil.createCol(assets, "Temporary", typeof (Boolean)); | ||
207 | SQLiteUtil.createCol(assets, "Data", typeof (Byte[])); | ||
208 | // Add in contraints | ||
209 | assets.PrimaryKey = new DataColumn[] {assets.Columns["UUID"]}; | ||
210 | return assets; | ||
211 | } | ||
212 | |||
213 | /*********************************************************************** | ||
214 | * | ||
215 | * Convert between ADO.NET <=> OpenSim Objects | ||
216 | * | ||
217 | * These should be database independant | ||
218 | * | ||
219 | **********************************************************************/ | ||
220 | |||
221 | private AssetBase buildAsset(IDataReader row) | ||
222 | { | ||
223 | // TODO: this doesn't work yet because something more | ||
224 | // interesting has to be done to actually get these values | ||
225 | // back out. Not enough time to figure it out yet. | ||
226 | AssetBase asset = new AssetBase(); | ||
227 | |||
228 | asset.FullID = new LLUUID((String) row["UUID"]); | ||
229 | asset.Name = (String) row["Name"]; | ||
230 | asset.Description = (String) row["Description"]; | ||
231 | asset.Type = Convert.ToSByte(row["Type"]); | ||
232 | asset.InvType = Convert.ToSByte(row["InvType"]); | ||
233 | asset.Local = Convert.ToBoolean(row["Local"]); | ||
234 | asset.Temporary = Convert.ToBoolean(row["Temporary"]); | ||
235 | asset.Data = (byte[]) row["Data"]; | ||
236 | return asset; | ||
237 | } | ||
238 | |||
239 | |||
240 | /*********************************************************************** | ||
241 | * | ||
242 | * Database Binding functions | ||
243 | * | ||
244 | * These will be db specific due to typing, and minor differences | ||
245 | * in databases. | ||
246 | * | ||
247 | **********************************************************************/ | ||
248 | |||
249 | private void InitDB(SqliteConnection conn) | ||
250 | { | ||
251 | string createAssets = SQLiteUtil.defineTable(createAssetsTable()); | ||
252 | SqliteCommand pcmd = new SqliteCommand(createAssets, conn); | ||
253 | pcmd.ExecuteNonQuery(); | ||
254 | } | ||
255 | |||
256 | private bool TestTables(SqliteConnection conn) | ||
257 | { | ||
258 | SqliteCommand cmd = new SqliteCommand(assetSelect, conn); | ||
259 | SqliteDataAdapter pDa = new SqliteDataAdapter(cmd); | ||
260 | DataSet tmpDS = new DataSet(); | ||
261 | try | ||
262 | { | ||
263 | pDa.Fill(tmpDS, "assets"); | ||
264 | } | ||
265 | catch (SqliteSyntaxException) | ||
266 | { | ||
267 | m_log.Info("[SQLITE]: SQLite Database doesn't exist... creating"); | ||
268 | InitDB(conn); | ||
269 | } | ||
270 | return true; | ||
271 | } | ||
272 | |||
273 | #region IPlugin interface | ||
274 | |||
275 | override public string Version | ||
276 | { | ||
277 | get | ||
278 | { | ||
279 | Module module = GetType().Module; | ||
280 | string dllName = module.Assembly.ManifestModule.Name; | ||
281 | Version dllVersion = module.Assembly.GetName().Version; | ||
282 | |||
283 | return | ||
284 | string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, | ||
285 | dllVersion.Revision); | ||
286 | } | ||
287 | } | ||
288 | |||
289 | override public void Initialise() | ||
290 | { | ||
291 | Initialise("AssetStorage.db", ""); | ||
292 | } | ||
293 | |||
294 | override public string Name | ||
295 | { | ||
296 | get { return "SQLite Asset storage engine"; } | ||
297 | } | ||
298 | |||
299 | #endregion | ||
300 | } | ||
301 | } | ||