aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Storage/OpenSim.DataStore.MonoSqlite/MonoSqliteDataStore.cs
blob: 6400d29775af55db811aedc0ac62199cd580b4ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Text;

using OpenSim.Region.Environment.Scenes;
using OpenSim.Region.Environment.LandManagement;
using OpenSim.Region.Environment;
using OpenSim.Region.Interfaces;
using OpenSim.Framework.Console;
using libsecondlife;

using System.Data;
// Yes, this won't compile on MS, need to deal with that later
using Mono.Data.SqliteClient;
using Primitive = OpenSim.Region.Environment.Scenes.Primitive;

namespace OpenSim.DataStore.MonoSqliteStorage
{

    public class MonoSqliteDataStore : IRegionDataStore
    {
        private const string primSelect = "select * from prims";
        private const string shapeSelect = "select * from primshapes";

        private Dictionary<string, DbType> primDataDefs;
        private Dictionary<string, DbType> shapeDataDefs;
        
        private DataSet ds;
        private SqliteDataAdapter primDa;
        private SqliteDataAdapter shapeDa;

        public void Initialise(string dbfile, string dbname)
        {
            // for us, dbfile will be the connect string
            MainLog.Instance.Verbose("DATASTORE", "Sqlite - connecting: " + dbfile);
            SqliteConnection conn = new SqliteConnection(dbfile);

            SqliteCommand primSelectCmd = new SqliteCommand(primSelect, conn);
            primDa = new SqliteDataAdapter(primSelectCmd);
            //            SqliteCommandBuilder primCb = new SqliteCommandBuilder(primDa);
            
            SqliteCommand shapeSelectCmd = new SqliteCommand(shapeSelect, conn);
            shapeDa = new SqliteDataAdapter(shapeSelectCmd);
            // SqliteCommandBuilder shapeCb = new SqliteCommandBuilder(shapeDa);

            ds = new DataSet();

            // We fill the data set, now we've got copies in memory for the information
            // TODO: see if the linkage actually holds.
            // primDa.FillSchema(ds, SchemaType.Source, "PrimSchema");
            primDa.Fill(ds, "prims");
            ds.AcceptChanges();

            DataTable prims = ds.Tables["prims"];
            prims.PrimaryKey = new DataColumn[] { prims.Columns["UUID"] };
            setupPrimCommands(primDa, conn);
            
            // shapeDa.FillSchema(ds, SchemaType.Source, "ShapeSchema");
            shapeDa.Fill(ds, "primshapes");
            
            return;
        }

        private SqliteParameter createSqliteParameter(string name, DbType type)
        {
            SqliteParameter param = new SqliteParameter();
            param.ParameterName = ":" + name;
            param.DbType = type;
            param.SourceColumn = name;
            param.SourceVersion = DataRowVersion.Current;
            return param;
        }

        private Dictionary<string, DbType> createPrimDataDefs()
        {
            Dictionary<string, DbType> data = new Dictionary<string, DbType>();
            data.Add("UUID", DbType.String);
            data.Add("ParentID", DbType.Int32);
            data.Add("CreationDate", DbType.Int32);
            data.Add("Name", DbType.String);
            // various text fields
            data.Add("Text", DbType.String);
            data.Add("Description", DbType.String);
            data.Add("SitName", DbType.String);
            data.Add("TouchName", DbType.String);
            // permissions
            data.Add("CreatorID", DbType.String);
            data.Add("OwnerID", DbType.String);
            data.Add("GroupID", DbType.String);
            data.Add("LastOwnerID", DbType.String);
            data.Add("OwnerMask", DbType.Int32);
            data.Add("NextOwnerMask", DbType.Int32);
            data.Add("GroupMask", DbType.Int32);
            data.Add("EveryoneMask", DbType.Int32);
            data.Add("BaseMask", DbType.Int32);
            // vectors
            data.Add("PositionX", DbType.Double);
            data.Add("PositionY", DbType.Double);
            data.Add("PositionZ", DbType.Double);
            data.Add("VelocityX", DbType.Double);
            data.Add("VelocityY", DbType.Double);
            data.Add("VelocityZ", DbType.Double);
            data.Add("AngularVelocityX", DbType.Double);
            data.Add("AngularVelocityY", DbType.Double);
            data.Add("AngularVelocityZ", DbType.Double);
            data.Add("AccelerationX", DbType.Double);
            data.Add("AccelerationY", DbType.Double);
            data.Add("AccelerationZ", DbType.Double);
            // quaternions
            data.Add("RotationX", DbType.Double);
            data.Add("RotationY", DbType.Double);
            data.Add("RotationZ", DbType.Double);
            data.Add("RotationW", DbType.Double);
            return data;
        }

        private Dictionary<string, DbType> createShapeDataDefs()
        {
            Dictionary<string, DbType> data = new Dictionary<string, DbType>();
            data.Add("UUID", DbType.String);
            // shape is an enum
            data.Add("Shape", DbType.Int32);
            // vectors
            data.Add("ScaleX", DbType.Double);
            data.Add("ScaleY", DbType.Double);
            data.Add("ScaleZ", DbType.Double);
            // paths
            data.Add("PCode", DbType.Int32);
            data.Add("PathBegin", DbType.Int32);
            data.Add("PathEnd", DbType.Int32);
            data.Add("PathScaleX", DbType.Int32);
            data.Add("PathScaleY", DbType.Int32);
            data.Add("PathShearX", DbType.Int32);
            data.Add("PathShearY", DbType.Int32);
            data.Add("PathSkew", DbType.Int32);
            data.Add("PathCurve", DbType.Int32);
            data.Add("PathRadiusOffset", DbType.Int32);
            data.Add("PathRevolutions", DbType.Int32);
            data.Add("PathTaperX", DbType.Int32);
            data.Add("PathTaperY", DbType.Int32);
            data.Add("PathTwist", DbType.Int32);
            data.Add("PathTwistBegin", DbType.Int32);
            // profile
            data.Add("ProfileBegin", DbType.Int32);
            data.Add("ProfileEnd", DbType.Int32);
            data.Add("ProfileCurve", DbType.Int32);
            data.Add("ProfileHollow", DbType.Int32);
            // text TODO: this isn't right, but I'm not sure the right
            // way to specify this as a blob atm
            data.Add("Texture", DbType.Binary);
            return data;
        }

        private SqliteCommand createInsertCommand(string table, Dictionary<string, DbType> defs) 
        {
            /**
             *  This is subtle enough to deserve some commentary.
             *  Instead of doing *lots* and *lots of hardcoded strings
             *  for database definitions we'll use the fact that
             *  realistically all insert statements look like "insert
             *  into A(b, c) values(:b, :c) on the parameterized query
             *  front.  If we just have a list of b, c, etc... we can
             *  generate these strings instead of typing them out.
             */
            string[] cols = new string[defs.Keys.Count];
            defs.Keys.CopyTo(cols, 0);
            
            string sql = "insert into " + table + "(";
            sql += String.Join(", ", cols);
            // important, the first ':' needs to be here, the rest get added in the join
            sql += ") values (:";
            sql += String.Join(", :", cols);
            sql += ")";
            SqliteCommand cmd = new SqliteCommand(sql);
            
            // this provides the binding for all our parameters, so
            // much less code than it used to be
            foreach (KeyValuePair<string, DbType> kvp in defs) {
                cmd.Parameters.Add(createSqliteParameter(kvp.Key, kvp.Value));
            }
            return cmd;
        }

        private SqliteCommand createUpdateCommand(string table, string pk, Dictionary<string, DbType> defs) 
        {
            string sql = "update " + table + " set ";
            foreach (string key in defs.Keys) {
                sql += key + "= :" + key + ", ";
            }
            sql += " where " + pk;
            SqliteCommand cmd = new SqliteCommand(sql);

            // this provides the binding for all our parameters, so
            // much less code than it used to be
            foreach (KeyValuePair<string, DbType> kvp in defs) {
                cmd.Parameters.Add(createSqliteParameter(kvp.Key, kvp.Value));
            }
            return cmd;
        }

        private void setupPrimCommands(SqliteDataAdapter da, SqliteConnection conn)
        {
            Dictionary<string, DbType> primDataDefs = createPrimDataDefs();
            
            da.InsertCommand = createInsertCommand("prims", primDataDefs);
            da.InsertCommand.Connection = conn;

            da.UpdateCommand = createUpdateCommand("prims", "UUID=:UUID", primDataDefs);
            da.UpdateCommand.Connection = conn;
            
            SqliteCommand delete = new SqliteCommand("delete from prims where UUID = :UUID");
            delete.Parameters.Add(createSqliteParameter("UUID", DbType.String));
            delete.Connection = conn;
            da.DeleteCommand = delete;
        }

        private void setupShapeCommands(SqliteDataAdapter da, SqliteConnection conn)
        {
            Dictionary<string, DbType> shapeDataDefs = createShapeDataDefs();
            
            da.InsertCommand = createInsertCommand("primshapes", shapeDataDefs);
            da.InsertCommand.Connection = conn;

            da.UpdateCommand = createUpdateCommand("primshapes", "UUID=:UUID", shapeDataDefs);
            da.UpdateCommand.Connection = conn;
            
            SqliteCommand delete = new SqliteCommand("delete from primshapes where UUID = :UUID");
            delete.Parameters.Add(createSqliteParameter("UUID", DbType.String));
            delete.Connection = conn;
            da.DeleteCommand = delete;
        }

        private void fillPrimRow(DataRow row, SceneObjectPart prim) 
        {
            row["UUID"] = prim.UUID;
            row["CreationDate"] = prim.CreationDate;
            row["Name"] = prim.PartName;
            row["PositionX"] = prim.OffsetPosition.X;
            row["PositionY"] = prim.OffsetPosition.Y;
            row["PositionZ"] = prim.OffsetPosition.Z;
        }

        private void addPrim(SceneObjectPart prim)
        {
            DataTable prims = ds.Tables["prims"];
            DataTable shapes = ds.Tables["shapes"];
            
            DataRow row = prims.Rows.Find(prim.UUID);
            if (row == null) {
                row = prims.NewRow();
                // fillPrimRow(row, prim);
                prims.Rows.Add(row);
            } else {
                fillPrimRow(row, prim);
            }
        }

        public void StoreObject(SceneObjectGroup obj)
        {
            foreach (SceneObjectPart prim in obj.Children.Values)
            {
                addPrim(prim);
            }
            
            MainLog.Instance.Verbose("Attempting to do update....");
            primDa.Update(ds, "prims");
            MainLog.Instance.Verbose("Dump of prims:", ds.GetXml());
        }

        public void RemoveObject(LLUUID obj)
        {
            // TODO: remove code
        }

        public List<SceneObjectGroup> LoadObjects()
        {
            List<SceneObjectGroup> retvals = new List<SceneObjectGroup>();

            MainLog.Instance.Verbose("DATASTORE", "Sqlite - LoadObjects found " + " objects");

            return retvals;
        }
        
        public void StoreTerrain(double[,] ter)
        {

        }

        public double[,] LoadTerrain()
        {
            return null;
        }

        public void RemoveLandObject(uint id)
        {

        }

        public void StoreParcel(Land parcel)
        {

        }

        public List<Land> LoadLandObjects()
        {
            return new List<Land>();
        }

        public void Shutdown()
        {
            // TODO: DataSet commit
        }
    }
}