diff options
author | Sean Dague | 2008-04-02 15:24:31 +0000 |
---|---|---|
committer | Sean Dague | 2008-04-02 15:24:31 +0000 |
commit | c52c68f314c67c76c7181a6d0828f476290fbd66 (patch) | |
tree | 66ab347502892902a096fa985f31b25738eb1381 /OpenSim/Data/SQLite/SQLiteManager.cs | |
parent | reorganizing namespaces to put all the Data stuff into it's own namespace (diff) | |
download | opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.zip opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.gz opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.bz2 opensim-SC-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.xz |
whole lot more moving
Diffstat (limited to 'OpenSim/Data/SQLite/SQLiteManager.cs')
-rw-r--r-- | OpenSim/Data/SQLite/SQLiteManager.cs | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLite/SQLiteManager.cs b/OpenSim/Data/SQLite/SQLiteManager.cs new file mode 100644 index 0000000..b383b0d --- /dev/null +++ b/OpenSim/Data/SQLite/SQLiteManager.cs | |||
@@ -0,0 +1,282 @@ | |||
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.Collections.Generic; | ||
30 | using System.Data; | ||
31 | using System.Data.SQLite; | ||
32 | using libsecondlife; | ||
33 | using Mono.Data.SqliteClient; | ||
34 | using OpenSim.Framework.Console; | ||
35 | |||
36 | namespace OpenSim.Framework.Data.SQLite | ||
37 | { | ||
38 | internal class SQLiteManager : SQLiteUtil | ||
39 | { | ||
40 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
41 | |||
42 | private IDbConnection dbcon; | ||
43 | |||
44 | /// <summary> | ||
45 | /// Initialises and creates a new SQLite connection and maintains it. | ||
46 | /// </summary> | ||
47 | /// <param name="hostname">The SQLite server being connected to</param> | ||
48 | /// <param name="database">The name of the SQLite database being used</param> | ||
49 | /// <param name="username">The username logging into the database</param> | ||
50 | /// <param name="password">The password for the user logging in</param> | ||
51 | /// <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> | ||
52 | public SQLiteManager(string hostname, string database, string username, string password, string cpooling) | ||
53 | { | ||
54 | try | ||
55 | { | ||
56 | string connectionString = "URI=file:GridServerSqlite.db;"; | ||
57 | dbcon = new SQLiteConnection(connectionString); | ||
58 | |||
59 | dbcon.Open(); | ||
60 | } | ||
61 | catch (Exception e) | ||
62 | { | ||
63 | throw new Exception("Error initialising SQLite Database: " + e.ToString()); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | /// <summary> | ||
68 | /// Shuts down the database connection | ||
69 | /// </summary> | ||
70 | public void Close() | ||
71 | { | ||
72 | dbcon.Close(); | ||
73 | dbcon = null; | ||
74 | } | ||
75 | |||
76 | /// <summary> | ||
77 | /// Runs a query with protection against SQL Injection by using parameterised input. | ||
78 | /// </summary> | ||
79 | /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param> | ||
80 | /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param> | ||
81 | /// <returns>A SQLite DB Command</returns> | ||
82 | public IDbCommand Query(string sql, Dictionary<string, string> parameters) | ||
83 | { | ||
84 | SQLiteCommand dbcommand = (SQLiteCommand) dbcon.CreateCommand(); | ||
85 | dbcommand.CommandText = sql; | ||
86 | foreach (KeyValuePair<string, string> param in parameters) | ||
87 | { | ||
88 | SQLiteParameter paramx = new SQLiteParameter(param.Key, param.Value); | ||
89 | dbcommand.Parameters.Add(paramx); | ||
90 | } | ||
91 | |||
92 | return (IDbCommand) dbcommand; | ||
93 | } | ||
94 | |||
95 | // TODO: unused | ||
96 | // private bool TestTables(SQLiteConnection conn) | ||
97 | // { | ||
98 | // SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM regions", conn); | ||
99 | // SQLiteDataAdapter pDa = new SQLiteDataAdapter(cmd); | ||
100 | // DataSet tmpDS = new DataSet(); | ||
101 | // try | ||
102 | // { | ||
103 | // pDa.Fill(tmpDS, "regions"); | ||
104 | // } | ||
105 | // catch (SqliteSyntaxException) | ||
106 | // { | ||
107 | // m_log.Info("[DATASTORE]: SQLite Database doesn't exist... creating"); | ||
108 | // InitDB(conn); | ||
109 | // } | ||
110 | // return true; | ||
111 | // } | ||
112 | |||
113 | // TODO: unused | ||
114 | // private DataTable createRegionsTable() | ||
115 | // { | ||
116 | // DataTable regions = new DataTable("regions"); | ||
117 | |||
118 | // createCol(regions, "regionHandle", typeof (ulong)); | ||
119 | // createCol(regions, "regionName", typeof (String)); | ||
120 | // createCol(regions, "uuid", typeof (String)); | ||
121 | |||
122 | // createCol(regions, "regionRecvKey", typeof (String)); | ||
123 | // createCol(regions, "regionSecret", typeof (String)); | ||
124 | // createCol(regions, "regionSendKey", typeof (String)); | ||
125 | |||
126 | // createCol(regions, "regionDataURI", typeof (String)); | ||
127 | // createCol(regions, "serverIP", typeof (String)); | ||
128 | // createCol(regions, "serverPort", typeof (String)); | ||
129 | // createCol(regions, "serverURI", typeof (String)); | ||
130 | |||
131 | |||
132 | // createCol(regions, "locX", typeof (uint)); | ||
133 | // createCol(regions, "locY", typeof (uint)); | ||
134 | // createCol(regions, "locZ", typeof (uint)); | ||
135 | |||
136 | // createCol(regions, "eastOverrideHandle", typeof (ulong)); | ||
137 | // createCol(regions, "westOverrideHandle", typeof (ulong)); | ||
138 | // createCol(regions, "southOverrideHandle", typeof (ulong)); | ||
139 | // createCol(regions, "northOverrideHandle", typeof (ulong)); | ||
140 | |||
141 | // createCol(regions, "regionAssetURI", typeof (String)); | ||
142 | // createCol(regions, "regionAssetRecvKey", typeof (String)); | ||
143 | // createCol(regions, "regionAssetSendKey", typeof (String)); | ||
144 | |||
145 | // createCol(regions, "regionUserURI", typeof (String)); | ||
146 | // createCol(regions, "regionUserRecvKey", typeof (String)); | ||
147 | // createCol(regions, "regionUserSendKey", typeof (String)); | ||
148 | |||
149 | // // Add in contraints | ||
150 | // regions.PrimaryKey = new DataColumn[] {regions.Columns["UUID"]}; | ||
151 | // return regions; | ||
152 | // } | ||
153 | |||
154 | // TODO: unused | ||
155 | // private void InitDB(SQLiteConnection conn) | ||
156 | // { | ||
157 | // string createUsers = defineTable(createRegionsTable()); | ||
158 | // SQLiteCommand pcmd = new SQLiteCommand(createUsers, conn); | ||
159 | // conn.Open(); | ||
160 | // pcmd.ExecuteNonQuery(); | ||
161 | // conn.Close(); | ||
162 | // } | ||
163 | |||
164 | /// <summary> | ||
165 | /// Reads a region row from a database reader | ||
166 | /// </summary> | ||
167 | /// <param name="reader">An active database reader</param> | ||
168 | /// <returns>A region profile</returns> | ||
169 | public RegionProfileData getRow(IDataReader reader) | ||
170 | { | ||
171 | RegionProfileData retval = new RegionProfileData(); | ||
172 | |||
173 | if (reader.Read()) | ||
174 | { | ||
175 | // Region Main | ||
176 | retval.regionHandle = (ulong) reader["regionHandle"]; | ||
177 | retval.regionName = (string) reader["regionName"]; | ||
178 | retval.UUID = new LLUUID((string) reader["uuid"]); | ||
179 | |||
180 | // Secrets | ||
181 | retval.regionRecvKey = (string) reader["regionRecvKey"]; | ||
182 | retval.regionSecret = (string) reader["regionSecret"]; | ||
183 | retval.regionSendKey = (string) reader["regionSendKey"]; | ||
184 | |||
185 | // Region Server | ||
186 | retval.regionDataURI = (string) reader["regionDataURI"]; | ||
187 | retval.regionOnline = false; // Needs to be pinged before this can be set. | ||
188 | retval.serverIP = (string) reader["serverIP"]; | ||
189 | retval.serverPort = (uint) reader["serverPort"]; | ||
190 | retval.serverURI = (string) reader["serverURI"]; | ||
191 | |||
192 | // Location | ||
193 | retval.regionLocX = (uint) ((int) reader["locX"]); | ||
194 | retval.regionLocY = (uint) ((int) reader["locY"]); | ||
195 | retval.regionLocZ = (uint) ((int) reader["locZ"]); | ||
196 | |||
197 | // Neighbours - 0 = No Override | ||
198 | retval.regionEastOverrideHandle = (ulong) reader["eastOverrideHandle"]; | ||
199 | retval.regionWestOverrideHandle = (ulong) reader["westOverrideHandle"]; | ||
200 | retval.regionSouthOverrideHandle = (ulong) reader["southOverrideHandle"]; | ||
201 | retval.regionNorthOverrideHandle = (ulong) reader["northOverrideHandle"]; | ||
202 | |||
203 | // Assets | ||
204 | retval.regionAssetURI = (string) reader["regionAssetURI"]; | ||
205 | retval.regionAssetRecvKey = (string) reader["regionAssetRecvKey"]; | ||
206 | retval.regionAssetSendKey = (string) reader["regionAssetSendKey"]; | ||
207 | |||
208 | // Userserver | ||
209 | retval.regionUserURI = (string) reader["regionUserURI"]; | ||
210 | retval.regionUserRecvKey = (string) reader["regionUserRecvKey"]; | ||
211 | retval.regionUserSendKey = (string) reader["regionUserSendKey"]; | ||
212 | } | ||
213 | else | ||
214 | { | ||
215 | throw new Exception("No rows to return"); | ||
216 | } | ||
217 | return retval; | ||
218 | } | ||
219 | |||
220 | /// <summary> | ||
221 | /// Inserts a new region into the database | ||
222 | /// </summary> | ||
223 | /// <param name="profile">The region to insert</param> | ||
224 | /// <returns>Success?</returns> | ||
225 | public bool insertRow(RegionProfileData profile) | ||
226 | { | ||
227 | string sql = | ||
228 | "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, "; | ||
229 | sql += | ||
230 | "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, "; | ||
231 | sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES "; | ||
232 | |||
233 | sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, "; | ||
234 | sql += | ||
235 | "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, "; | ||
236 | sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);"; | ||
237 | |||
238 | Dictionary<string, string> parameters = new Dictionary<string, string>(); | ||
239 | |||
240 | parameters["regionHandle"] = profile.regionHandle.ToString(); | ||
241 | parameters["regionName"] = profile.regionName; | ||
242 | parameters["uuid"] = profile.UUID.ToString(); | ||
243 | parameters["regionRecvKey"] = profile.regionRecvKey; | ||
244 | parameters["regionSendKey"] = profile.regionSendKey; | ||
245 | parameters["regionDataURI"] = profile.regionDataURI; | ||
246 | parameters["serverIP"] = profile.serverIP; | ||
247 | parameters["serverPort"] = profile.serverPort.ToString(); | ||
248 | parameters["serverURI"] = profile.serverURI; | ||
249 | parameters["locX"] = profile.regionLocX.ToString(); | ||
250 | parameters["locY"] = profile.regionLocY.ToString(); | ||
251 | parameters["locZ"] = profile.regionLocZ.ToString(); | ||
252 | parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString(); | ||
253 | parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString(); | ||
254 | parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString(); | ||
255 | parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString(); | ||
256 | parameters["regionAssetURI"] = profile.regionAssetURI; | ||
257 | parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey; | ||
258 | parameters["regionAssetSendKey"] = profile.regionAssetSendKey; | ||
259 | parameters["regionUserURI"] = profile.regionUserURI; | ||
260 | parameters["regionUserRecvKey"] = profile.regionUserRecvKey; | ||
261 | parameters["regionUserSendKey"] = profile.regionUserSendKey; | ||
262 | |||
263 | bool returnval = false; | ||
264 | |||
265 | try | ||
266 | { | ||
267 | IDbCommand result = Query(sql, parameters); | ||
268 | |||
269 | if (result.ExecuteNonQuery() == 1) | ||
270 | returnval = true; | ||
271 | |||
272 | result.Dispose(); | ||
273 | } | ||
274 | catch (Exception) | ||
275 | { | ||
276 | return false; | ||
277 | } | ||
278 | |||
279 | return returnval; | ||
280 | } | ||
281 | } | ||
282 | } | ||