aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenGridServices-Source/OpenGrid.Framework.Data.SQLite/SQLiteManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenGridServices-Source/OpenGrid.Framework.Data.SQLite/SQLiteManager.cs')
-rw-r--r--OpenGridServices-Source/OpenGrid.Framework.Data.SQLite/SQLiteManager.cs172
1 files changed, 172 insertions, 0 deletions
diff --git a/OpenGridServices-Source/OpenGrid.Framework.Data.SQLite/SQLiteManager.cs b/OpenGridServices-Source/OpenGrid.Framework.Data.SQLite/SQLiteManager.cs
new file mode 100644
index 0000000..408a582
--- /dev/null
+++ b/OpenGridServices-Source/OpenGrid.Framework.Data.SQLite/SQLiteManager.cs
@@ -0,0 +1,172 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5
6using System.Data.SQLite;
7
8using OpenGrid.Framework.Data;
9
10namespace OpenGrid.Framework.Data.SQLite
11{
12 class SQLiteManager
13 {
14 IDbConnection dbcon;
15
16 /// <summary>
17 /// Initialises and creates a new SQLite connection and maintains it.
18 /// </summary>
19 /// <param name="hostname">The SQLite server being connected to</param>
20 /// <param name="database">The name of the SQLite database being used</param>
21 /// <param name="username">The username logging into the database</param>
22 /// <param name="password">The password for the user logging in</param>
23 /// <param name="cpooling">Whether to use connection pooling or not, can be one of the following: 'yes', 'true', 'no' or 'false', if unsure use 'false'.</param>
24 public SQLiteManager(string hostname, string database, string username, string password, string cpooling)
25 {
26 try
27 {
28 string connectionString = "URI=file:GridServerSqlite.db;";
29 dbcon = new SQLiteConnection(connectionString);
30
31 dbcon.Open();
32 }
33 catch (Exception e)
34 {
35 throw new Exception("Error initialising SQLite Database: " + e.ToString());
36 }
37 }
38
39 /// <summary>
40 /// Shuts down the database connection
41 /// </summary>
42 public void Close()
43 {
44 dbcon.Close();
45 dbcon = null;
46 }
47
48 /// <summary>
49 /// Runs a query with protection against SQL Injection by using parameterised input.
50 /// </summary>
51 /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
52 /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
53 /// <returns>A SQLite DB Command</returns>
54 public IDbCommand Query(string sql, Dictionary<string, string> parameters)
55 {
56 SQLiteCommand dbcommand = (SQLiteCommand)dbcon.CreateCommand();
57 dbcommand.CommandText = sql;
58 foreach (KeyValuePair<string, string> param in parameters)
59 {
60 SQLiteParameter paramx = new SQLiteParameter(param.Key,param.Value);
61 dbcommand.Parameters.Add(paramx);
62 }
63
64 return (IDbCommand)dbcommand;
65 }
66
67 public SimProfileData getRow(IDataReader reader)
68 {
69 SimProfileData retval = new SimProfileData();
70
71 if (reader.Read())
72 {
73 // Region Main
74 retval.regionHandle = (ulong)reader["regionHandle"];
75 retval.regionName = (string)reader["regionName"];
76 retval.UUID = new libsecondlife.LLUUID((string)reader["uuid"]);
77
78 // Secrets
79 retval.regionRecvKey = (string)reader["regionRecvKey"];
80 retval.regionSecret = (string)reader["regionSecret"];
81 retval.regionSendKey = (string)reader["regionSendKey"];
82
83 // Region Server
84 retval.regionDataURI = (string)reader["regionDataURI"];
85 retval.regionOnline = false; // Needs to be pinged before this can be set.
86 retval.serverIP = (string)reader["serverIP"];
87 retval.serverPort = (uint)reader["serverPort"];
88 retval.serverURI = (string)reader["serverURI"];
89
90 // Location
91 retval.regionLocX = (uint)((int)reader["locX"]);
92 retval.regionLocY = (uint)((int)reader["locY"]);
93 retval.regionLocZ = (uint)((int)reader["locZ"]);
94
95 // Neighbours - 0 = No Override
96 retval.regionEastOverrideHandle = (ulong)reader["eastOverrideHandle"];
97 retval.regionWestOverrideHandle = (ulong)reader["westOverrideHandle"];
98 retval.regionSouthOverrideHandle = (ulong)reader["southOverrideHandle"];
99 retval.regionNorthOverrideHandle = (ulong)reader["northOverrideHandle"];
100
101 // Assets
102 retval.regionAssetURI = (string)reader["regionAssetURI"];
103 retval.regionAssetRecvKey = (string)reader["regionAssetRecvKey"];
104 retval.regionAssetSendKey = (string)reader["regionAssetSendKey"];
105
106 // Userserver
107 retval.regionUserURI = (string)reader["regionUserURI"];
108 retval.regionUserRecvKey = (string)reader["regionUserRecvKey"];
109 retval.regionUserSendKey = (string)reader["regionUserSendKey"];
110 }
111 else
112 {
113 throw new Exception("No rows to return");
114 }
115 return retval;
116 }
117
118 public bool insertRow(SimProfileData profile)
119 {
120 string sql = "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
121 sql += "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
122 sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES ";
123
124 sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
125 sql += "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, ";
126 sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);";
127
128 Dictionary<string, string> parameters = new Dictionary<string, string>();
129
130 parameters["regionHandle"] = profile.regionHandle.ToString();
131 parameters["regionName"] = profile.regionName;
132 parameters["uuid"] = profile.UUID.ToString();
133 parameters["regionRecvKey"] = profile.regionRecvKey;
134 parameters["regionSendKey"] = profile.regionSendKey;
135 parameters["regionDataURI"] = profile.regionDataURI;
136 parameters["serverIP"] = profile.serverIP;
137 parameters["serverPort"] = profile.serverPort.ToString();
138 parameters["serverURI"] = profile.serverURI;
139 parameters["locX"] = profile.regionLocX.ToString();
140 parameters["locY"] = profile.regionLocY.ToString();
141 parameters["locZ"] = profile.regionLocZ.ToString();
142 parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString();
143 parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString();
144 parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString();
145 parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString();
146 parameters["regionAssetURI"] = profile.regionAssetURI;
147 parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey;
148 parameters["regionAssetSendKey"] = profile.regionAssetSendKey;
149 parameters["regionUserURI"] = profile.regionUserURI;
150 parameters["regionUserRecvKey"] = profile.regionUserRecvKey;
151 parameters["regionUserSendKey"] = profile.regionUserSendKey;
152
153 bool returnval = false;
154
155 try
156 {
157 IDbCommand result = Query(sql, parameters);
158
159 if (result.ExecuteNonQuery() == 1)
160 returnval = true;
161
162 result.Dispose();
163 }
164 catch (Exception e)
165 {
166 return false;
167 }
168
169 return returnval;
170 }
171 }
172}