aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
diff options
context:
space:
mode:
authorSean Dague2007-09-07 13:29:08 +0000
committerSean Dague2007-09-07 13:29:08 +0000
commit294572d7bbd2609e8cf14d9c85c9d7e38921dea6 (patch)
tree607d57e4497d68aabffb21291727e30cfaf22c8c /OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
parentremove build files from svn (diff)
downloadopensim-SC-294572d7bbd2609e8cf14d9c85c9d7e38921dea6.zip
opensim-SC-294572d7bbd2609e8cf14d9c85c9d7e38921dea6.tar.gz
opensim-SC-294572d7bbd2609e8cf14d9c85c9d7e38921dea6.tar.bz2
opensim-SC-294572d7bbd2609e8cf14d9c85c9d7e38921dea6.tar.xz
added SQLiteAssetData class first pass. This should work,
but it will current store local and temporary assets as well. Will fix that later.
Diffstat (limited to 'OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs')
-rw-r--r--OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs237
1 files changed, 237 insertions, 0 deletions
diff --git a/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs b/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
new file mode 100644
index 0000000..9138673
--- /dev/null
+++ b/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs
@@ -0,0 +1,237 @@
1/*
2* Copyright (c) Contributors, http://www.openmetaverse.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*/
28using System;
29using System.IO;
30using libsecondlife;
31using OpenSim.Framework.Utilities;
32using System.Data;
33using System.Data.SqlTypes;
34using Mono.Data.SqliteClient;
35using OpenSim.Framework.Console;
36using OpenSim.Framework.Types;
37
38namespace OpenSim.Framework.Data.SQLite
39{
40 /// <summary>
41 /// A User storage interface for the DB4o database system
42 /// </summary>
43 public class SQLiteAssetData : SQLiteBase
44 {
45 /// <summary>
46 /// The database manager
47 /// </summary>
48
49 /// <summary>
50 /// Artificial constructor called upon plugin load
51 /// </summary>
52 private const string assetSelect = "select * from assets";
53 private DataSet ds;
54 private SqliteDataAdapter da;
55
56 public void Initialise(string dbfile, string dbname)
57 {
58 SqliteConnection conn = new SqliteConnection("URI=file:" + dbfile + ",version=3");
59 TestTables(conn);
60
61 ds = new DataSet();
62 da = new SqliteDataAdapter(new SqliteCommand(assetSelect, conn));
63
64 ds.Tables.Add(createAssetsTable());
65
66 setupAssetCommands(da, conn);
67 da.Fill(ds.Tables["assets"]);
68
69 return;
70 }
71
72 public AssetBase FetchAsset(LLUUID uuid)
73 {
74 AssetBase asset = new AssetBase();
75 DataRow row = ds.Tables["assets"].Rows.Find(uuid);
76 if (row != null)
77 {
78 return buildAsset(row);
79 }
80 else
81 {
82 return null;
83 }
84 }
85
86 public void CreateAsset(AssetBase asset)
87 {
88 // no difference for now
89 UpdateAsset(asset);
90 }
91
92 public void UpdateAsset(AssetBase asset)
93 {
94 DataTable assets = ds.Tables["assets"];
95 DataRow row = assets.Rows.Find(asset.FullID);
96 if (row == null)
97 {
98 row = assets.NewRow();
99 fillAssetRow(row, asset);
100 assets.Rows.Add(row);
101 }
102 else
103 {
104 fillAssetRow(row, asset);
105 }
106 }
107
108 public bool ExistsAsset(LLUUID uuid)
109 {
110 DataRow row = ds.Tables["assets"].Rows.Find(uuid);
111 return (row != null);
112 }
113
114 public void CommitAssets() // force a sync to the database
115 {
116 da.Update(ds, "assets");
117 }
118
119 /***********************************************************************
120 *
121 * Database Definition Functions
122 *
123 * This should be db agnostic as we define them in ADO.NET terms
124 *
125 **********************************************************************/
126
127 private DataTable createAssetsTable()
128 {
129 DataTable assets = new DataTable("assets");
130
131 createCol(assets, "UUID", typeof(System.String));
132 createCol(assets, "Name", typeof(System.String));
133 createCol(assets, "Description", typeof(System.String));
134 createCol(assets, "Type", typeof(System.Int32));
135 createCol(assets, "InvType", typeof(System.Int32));
136 createCol(assets, "Local", typeof(System.Boolean));
137 createCol(assets, "Temporary", typeof(System.Boolean));
138 createCol(assets, "Data", typeof(System.Byte[]));
139 // Add in contraints
140 assets.PrimaryKey = new DataColumn[] { assets.Columns["UUID"] };
141 return assets;
142 }
143
144 /***********************************************************************
145 *
146 * Convert between ADO.NET <=> OpenSim Objects
147 *
148 * These should be database independant
149 *
150 **********************************************************************/
151
152 private AssetBase buildAsset(DataRow row)
153 {
154 // TODO: this doesn't work yet because something more
155 // interesting has to be done to actually get these values
156 // back out. Not enough time to figure it out yet.
157 AssetBase asset = new AssetBase();
158
159 asset.FullID = new LLUUID((String)row["UUID"]);
160 asset.Name = (String)row["Name"];
161 asset.Description = (String)row["Description"];
162 asset.Type = Convert.ToSByte(row["Type"]);
163 asset.InvType = Convert.ToSByte(row["InvType"]);
164 asset.Local = Convert.ToBoolean(row["Local"]);
165 asset.Temporary = Convert.ToBoolean(row["Temporary"]);
166 asset.Data = (byte[])row["Data"];
167 return asset;
168 }
169
170
171 private void fillAssetRow(DataRow row, AssetBase asset)
172 {
173 row["UUID"] = asset.FullID;
174 row["Name"] = asset.Name;
175 row["Description"] = asset.Description;
176 row["Type"] = asset.Type;
177 row["InvType"] = asset.InvType;
178 row["Local"] = asset.Local;
179 row["Temporary"] = asset.Temporary;
180 row["Data"] = asset.Data;
181
182 // ADO.NET doesn't handle NULL very well
183 foreach (DataColumn col in ds.Tables["assets"].Columns) {
184 if (row[col] == null) {
185 row[col] = "";
186 }
187 }
188 }
189
190 /***********************************************************************
191 *
192 * Database Binding functions
193 *
194 * These will be db specific due to typing, and minor differences
195 * in databases.
196 *
197 **********************************************************************/
198
199 private void setupAssetCommands(SqliteDataAdapter da, SqliteConnection conn)
200 {
201 da.InsertCommand = createInsertCommand("assets", ds.Tables["assets"]);
202 da.InsertCommand.Connection = conn;
203
204 da.UpdateCommand = createUpdateCommand("assets", "UUID=:UUID", ds.Tables["assets"]);
205 da.UpdateCommand.Connection = conn;
206
207 SqliteCommand delete = new SqliteCommand("delete from assets where UUID = :UUID");
208 delete.Parameters.Add(createSqliteParameter("UUID", typeof(System.String)));
209 delete.Connection = conn;
210 da.DeleteCommand = delete;
211 }
212
213 private void InitDB(SqliteConnection conn)
214 {
215 string createAssets = defineTable(createAssetsTable());
216 SqliteCommand pcmd = new SqliteCommand(createAssets, conn);
217 conn.Open();
218 pcmd.ExecuteNonQuery();
219 conn.Close();
220 }
221
222 private bool TestTables(SqliteConnection conn)
223 {
224 SqliteCommand cmd = new SqliteCommand(assetSelect, conn);
225 SqliteDataAdapter pDa = new SqliteDataAdapter(cmd);
226 DataSet tmpDS = new DataSet();
227 try {
228 pDa.Fill(tmpDS, "assets");
229 } catch (Mono.Data.SqliteClient.SqliteSyntaxException) {
230 MainLog.Instance.Verbose("DATASTORE", "SQLite Database doesn't exist... creating");
231 InitDB(conn);
232 }
233 return true;
234 }
235
236 }
237}