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