diff options
author | Adam Frisby | 2007-07-11 08:10:25 +0000 |
---|---|---|
committer | Adam Frisby | 2007-07-11 08:10:25 +0000 |
commit | e2ff441e31328e60c8bb1d4bb32fa4ac64f91978 (patch) | |
tree | 8405b6cef57b66a58f31a24c859846085d0b81f7 /OpenSim/Framework/Data.SQLite/SQLiteManager.cs | |
parent | * Wiping trunk in prep for Sugilite (diff) | |
parent | * Applying dalien's patches from bug#177 and #179 (diff) | |
download | opensim-SC-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.zip opensim-SC-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.tar.gz opensim-SC-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.tar.bz2 opensim-SC-e2ff441e31328e60c8bb1d4bb32fa4ac64f91978.tar.xz |
* Bringing Sugilite in to trunk
Diffstat (limited to 'OpenSim/Framework/Data.SQLite/SQLiteManager.cs')
-rw-r--r-- | OpenSim/Framework/Data.SQLite/SQLiteManager.cs | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/OpenSim/Framework/Data.SQLite/SQLiteManager.cs b/OpenSim/Framework/Data.SQLite/SQLiteManager.cs new file mode 100644 index 0000000..c9931ab --- /dev/null +++ b/OpenSim/Framework/Data.SQLite/SQLiteManager.cs | |||
@@ -0,0 +1,206 @@ | |||
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 | */ | ||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data; | ||
31 | using System.Data.SQLite; | ||
32 | using libsecondlife; | ||
33 | |||
34 | namespace OpenSim.Framework.Data.SQLite | ||
35 | { | ||
36 | class SQLiteManager | ||
37 | { | ||
38 | IDbConnection dbcon; | ||
39 | |||
40 | /// <summary> | ||
41 | /// Initialises and creates a new SQLite connection and maintains it. | ||
42 | /// </summary> | ||
43 | /// <param name="hostname">The SQLite server being connected to</param> | ||
44 | /// <param name="database">The name of the SQLite database being used</param> | ||
45 | /// <param name="username">The username logging into the database</param> | ||
46 | /// <param name="password">The password for the user logging in</param> | ||
47 | /// <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> | ||
48 | public SQLiteManager(string hostname, string database, string username, string password, string cpooling) | ||
49 | { | ||
50 | try | ||
51 | { | ||
52 | string connectionString = "URI=file:GridServerSqlite.db;"; | ||
53 | dbcon = new SQLiteConnection(connectionString); | ||
54 | |||
55 | dbcon.Open(); | ||
56 | } | ||
57 | catch (Exception e) | ||
58 | { | ||
59 | throw new Exception("Error initialising SQLite Database: " + e.ToString()); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | /// <summary> | ||
64 | /// Shuts down the database connection | ||
65 | /// </summary> | ||
66 | public void Close() | ||
67 | { | ||
68 | dbcon.Close(); | ||
69 | dbcon = null; | ||
70 | } | ||
71 | |||
72 | /// <summary> | ||
73 | /// Runs a query with protection against SQL Injection by using parameterised input. | ||
74 | /// </summary> | ||
75 | /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param> | ||
76 | /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param> | ||
77 | /// <returns>A SQLite DB Command</returns> | ||
78 | public IDbCommand Query(string sql, Dictionary<string, string> parameters) | ||
79 | { | ||
80 | SQLiteCommand dbcommand = (SQLiteCommand)dbcon.CreateCommand(); | ||
81 | dbcommand.CommandText = sql; | ||
82 | foreach (KeyValuePair<string, string> param in parameters) | ||
83 | { | ||
84 | SQLiteParameter paramx = new SQLiteParameter(param.Key,param.Value); | ||
85 | dbcommand.Parameters.Add(paramx); | ||
86 | } | ||
87 | |||
88 | return (IDbCommand)dbcommand; | ||
89 | } | ||
90 | |||
91 | /// <summary> | ||
92 | /// Reads a region row from a database reader | ||
93 | /// </summary> | ||
94 | /// <param name="reader">An active database reader</param> | ||
95 | /// <returns>A region profile</returns> | ||
96 | public SimProfileData getRow(IDataReader reader) | ||
97 | { | ||
98 | SimProfileData retval = new SimProfileData(); | ||
99 | |||
100 | if (reader.Read()) | ||
101 | { | ||
102 | // Region Main | ||
103 | retval.regionHandle = (ulong)reader["regionHandle"]; | ||
104 | retval.regionName = (string)reader["regionName"]; | ||
105 | retval.UUID = new LLUUID((string)reader["uuid"]); | ||
106 | |||
107 | // Secrets | ||
108 | retval.regionRecvKey = (string)reader["regionRecvKey"]; | ||
109 | retval.regionSecret = (string)reader["regionSecret"]; | ||
110 | retval.regionSendKey = (string)reader["regionSendKey"]; | ||
111 | |||
112 | // Region Server | ||
113 | retval.regionDataURI = (string)reader["regionDataURI"]; | ||
114 | retval.regionOnline = false; // Needs to be pinged before this can be set. | ||
115 | retval.serverIP = (string)reader["serverIP"]; | ||
116 | retval.serverPort = (uint)reader["serverPort"]; | ||
117 | retval.serverURI = (string)reader["serverURI"]; | ||
118 | |||
119 | // Location | ||
120 | retval.regionLocX = (uint)((int)reader["locX"]); | ||
121 | retval.regionLocY = (uint)((int)reader["locY"]); | ||
122 | retval.regionLocZ = (uint)((int)reader["locZ"]); | ||
123 | |||
124 | // Neighbours - 0 = No Override | ||
125 | retval.regionEastOverrideHandle = (ulong)reader["eastOverrideHandle"]; | ||
126 | retval.regionWestOverrideHandle = (ulong)reader["westOverrideHandle"]; | ||
127 | retval.regionSouthOverrideHandle = (ulong)reader["southOverrideHandle"]; | ||
128 | retval.regionNorthOverrideHandle = (ulong)reader["northOverrideHandle"]; | ||
129 | |||
130 | // Assets | ||
131 | retval.regionAssetURI = (string)reader["regionAssetURI"]; | ||
132 | retval.regionAssetRecvKey = (string)reader["regionAssetRecvKey"]; | ||
133 | retval.regionAssetSendKey = (string)reader["regionAssetSendKey"]; | ||
134 | |||
135 | // Userserver | ||
136 | retval.regionUserURI = (string)reader["regionUserURI"]; | ||
137 | retval.regionUserRecvKey = (string)reader["regionUserRecvKey"]; | ||
138 | retval.regionUserSendKey = (string)reader["regionUserSendKey"]; | ||
139 | } | ||
140 | else | ||
141 | { | ||
142 | throw new Exception("No rows to return"); | ||
143 | } | ||
144 | return retval; | ||
145 | } | ||
146 | |||
147 | /// <summary> | ||
148 | /// Inserts a new region into the database | ||
149 | /// </summary> | ||
150 | /// <param name="profile">The region to insert</param> | ||
151 | /// <returns>Success?</returns> | ||
152 | public bool insertRow(SimProfileData profile) | ||
153 | { | ||
154 | string sql = "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, "; | ||
155 | sql += "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, "; | ||
156 | sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES "; | ||
157 | |||
158 | sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, "; | ||
159 | sql += "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, "; | ||
160 | sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);"; | ||
161 | |||
162 | Dictionary<string, string> parameters = new Dictionary<string, string>(); | ||
163 | |||
164 | parameters["regionHandle"] = profile.regionHandle.ToString(); | ||
165 | parameters["regionName"] = profile.regionName; | ||
166 | parameters["uuid"] = profile.UUID.ToString(); | ||
167 | parameters["regionRecvKey"] = profile.regionRecvKey; | ||
168 | parameters["regionSendKey"] = profile.regionSendKey; | ||
169 | parameters["regionDataURI"] = profile.regionDataURI; | ||
170 | parameters["serverIP"] = profile.serverIP; | ||
171 | parameters["serverPort"] = profile.serverPort.ToString(); | ||
172 | parameters["serverURI"] = profile.serverURI; | ||
173 | parameters["locX"] = profile.regionLocX.ToString(); | ||
174 | parameters["locY"] = profile.regionLocY.ToString(); | ||
175 | parameters["locZ"] = profile.regionLocZ.ToString(); | ||
176 | parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString(); | ||
177 | parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString(); | ||
178 | parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString(); | ||
179 | parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString(); | ||
180 | parameters["regionAssetURI"] = profile.regionAssetURI; | ||
181 | parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey; | ||
182 | parameters["regionAssetSendKey"] = profile.regionAssetSendKey; | ||
183 | parameters["regionUserURI"] = profile.regionUserURI; | ||
184 | parameters["regionUserRecvKey"] = profile.regionUserRecvKey; | ||
185 | parameters["regionUserSendKey"] = profile.regionUserSendKey; | ||
186 | |||
187 | bool returnval = false; | ||
188 | |||
189 | try | ||
190 | { | ||
191 | IDbCommand result = Query(sql, parameters); | ||
192 | |||
193 | if (result.ExecuteNonQuery() == 1) | ||
194 | returnval = true; | ||
195 | |||
196 | result.Dispose(); | ||
197 | } | ||
198 | catch (Exception) | ||
199 | { | ||
200 | return false; | ||
201 | } | ||
202 | |||
203 | return returnval; | ||
204 | } | ||
205 | } | ||
206 | } | ||