aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/PGSQL/PGSQLManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/PGSQL/PGSQLManager.cs')
-rw-r--r--OpenSim/Data/PGSQL/PGSQLManager.cs354
1 files changed, 354 insertions, 0 deletions
diff --git a/OpenSim/Data/PGSQL/PGSQLManager.cs b/OpenSim/Data/PGSQL/PGSQLManager.cs
new file mode 100644
index 0000000..46f835a
--- /dev/null
+++ b/OpenSim/Data/PGSQL/PGSQLManager.cs
@@ -0,0 +1,354 @@
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 OpenSimulator 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.Data;
31using System.IO;
32using System.Reflection;
33using OpenSim.Framework;
34using log4net;
35using OpenMetaverse;
36using Npgsql;
37using NpgsqlTypes;
38
39namespace OpenSim.Data.PGSQL
40{
41 /// <summary>
42 /// A management class for the MS SQL Storage Engine
43 /// </summary>
44 public class PGSQLManager
45 {
46// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47
48 /// <summary>
49 /// Connection string for ADO.net
50 /// </summary>
51 private readonly string connectionString;
52
53 /// <summary>
54 /// Initialize the manager and set the connectionstring
55 /// </summary>
56 /// <param name="connection"></param>
57 public PGSQLManager(string connection)
58 {
59 connectionString = connection;
60 InitializeMonoSecurity();
61 }
62
63 public void InitializeMonoSecurity()
64 {
65 if (!Util.IsPlatformMono)
66 {
67 if (AppDomain.CurrentDomain.GetData("MonoSecurityPostgresAdded") == null)
68 {
69 AppDomain.CurrentDomain.SetData("MonoSecurityPostgresAdded", "true");
70
71 AppDomain currentDomain = AppDomain.CurrentDomain;
72 currentDomain.AssemblyResolve += new ResolveEventHandler(ResolveEventHandlerMonoSec);
73 }
74 }
75 }
76
77 private System.Reflection.Assembly ResolveEventHandlerMonoSec(object sender, ResolveEventArgs args)
78 {
79 Assembly MyAssembly = null;
80
81 if (args.Name.Substring(0, args.Name.IndexOf(",")) == "Mono.Security")
82 {
83 MyAssembly = Assembly.LoadFrom("lib/NET/Mono.Security.dll");
84 }
85
86 //Return the loaded assembly.
87 return MyAssembly;
88 }
89
90 /// <summary>
91 /// Type conversion to a SQLDbType functions
92 /// </summary>
93 /// <param name="type"></param>
94 /// <returns></returns>
95 internal NpgsqlDbType DbtypeFromType(Type type)
96 {
97 if (type == typeof(string))
98 {
99 return NpgsqlDbType.Varchar;
100 }
101 if (type == typeof(double))
102 {
103 return NpgsqlDbType.Double;
104 }
105 if (type == typeof(Single))
106 {
107 return NpgsqlDbType.Double;
108 }
109 if (type == typeof(int))
110 {
111 return NpgsqlDbType.Integer;
112 }
113 if (type == typeof(bool))
114 {
115 return NpgsqlDbType.Boolean;
116 }
117 if (type == typeof(UUID))
118 {
119 return NpgsqlDbType.Uuid;
120 }
121 if (type == typeof(byte))
122 {
123 return NpgsqlDbType.Smallint;
124 }
125 if (type == typeof(sbyte))
126 {
127 return NpgsqlDbType.Integer;
128 }
129 if (type == typeof(Byte[]))
130 {
131 return NpgsqlDbType.Bytea;
132 }
133 if (type == typeof(uint) || type == typeof(ushort))
134 {
135 return NpgsqlDbType.Integer;
136 }
137 if (type == typeof(ulong))
138 {
139 return NpgsqlDbType.Bigint;
140 }
141 if (type == typeof(DateTime))
142 {
143 return NpgsqlDbType.Timestamp;
144 }
145
146 return NpgsqlDbType.Varchar;
147 }
148
149 internal NpgsqlDbType DbtypeFromString(Type type, string PGFieldType)
150 {
151 if (PGFieldType == "")
152 {
153 return DbtypeFromType(type);
154 }
155
156 if (PGFieldType == "character varying")
157 {
158 return NpgsqlDbType.Varchar;
159 }
160 if (PGFieldType == "double precision")
161 {
162 return NpgsqlDbType.Double;
163 }
164 if (PGFieldType == "integer")
165 {
166 return NpgsqlDbType.Integer;
167 }
168 if (PGFieldType == "smallint")
169 {
170 return NpgsqlDbType.Smallint;
171 }
172 if (PGFieldType == "boolean")
173 {
174 return NpgsqlDbType.Boolean;
175 }
176 if (PGFieldType == "uuid")
177 {
178 return NpgsqlDbType.Uuid;
179 }
180 if (PGFieldType == "bytea")
181 {
182 return NpgsqlDbType.Bytea;
183 }
184
185 return DbtypeFromType(type);
186 }
187
188 /// <summary>
189 /// Creates value for parameter.
190 /// </summary>
191 /// <param name="value">The value.</param>
192 /// <returns></returns>
193 private static object CreateParameterValue(object value)
194 {
195 Type valueType = value.GetType();
196
197 if (valueType == typeof(UUID)) //TODO check if this works
198 {
199 return ((UUID) value).Guid;
200 }
201 if (valueType == typeof(UUID))
202 {
203 return ((UUID)value).Guid;
204 }
205 if (valueType == typeof(bool))
206 {
207 return (bool)value;
208 }
209 if (valueType == typeof(Byte[]))
210 {
211 return value;
212 }
213 if (valueType == typeof(int))
214 {
215 return value;
216 }
217 return value;
218 }
219
220 /// <summary>
221 /// Create value for parameter based on PGSQL Schema
222 /// </summary>
223 /// <param name="value"></param>
224 /// <param name="PGFieldType"></param>
225 /// <returns></returns>
226 internal static object CreateParameterValue(object value, string PGFieldType)
227 {
228 if (PGFieldType == "uuid")
229 {
230 UUID uidout;
231 UUID.TryParse(value.ToString(), out uidout);
232 return uidout;
233 }
234 if (PGFieldType == "integer")
235 {
236 int intout;
237 int.TryParse(value.ToString(), out intout);
238 return intout;
239 }
240 if (PGFieldType == "boolean")
241 {
242 return (value.ToString() == "true");
243 }
244 if (PGFieldType == "timestamp with time zone")
245 {
246 return (DateTime)value;
247 }
248 if (PGFieldType == "timestamp without time zone")
249 {
250 return (DateTime)value;
251 }
252 if (PGFieldType == "double precision")
253 {
254 return (Double)value;
255 }
256 return CreateParameterValue(value);
257 }
258
259 /// <summary>
260 /// Create a parameter for a command
261 /// </summary>
262 /// <param name="parameterName">Name of the parameter.</param>
263 /// <param name="parameterObject">parameter object.</param>
264 /// <returns></returns>
265 internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject)
266 {
267 return CreateParameter(parameterName, parameterObject, false);
268 }
269
270 /// <summary>
271 /// Creates the parameter for a command.
272 /// </summary>
273 /// <param name="parameterName">Name of the parameter.</param>
274 /// <param name="parameterObject">parameter object.</param>
275 /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
276 /// <returns></returns>
277 internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
278 {
279 //Tweak so we dont always have to add : sign
280 if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":","");
281
282 //HACK if object is null, it is turned into a string, there are no nullable type till now
283 if (parameterObject == null) parameterObject = "";
284
285 NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
286
287 if (parameterOut)
288 {
289 parameter.Direction = ParameterDirection.Output;
290 }
291 else
292 {
293 parameter.Direction = ParameterDirection.Input;
294 parameter.Value = CreateParameterValue(parameterObject);
295 }
296
297 return parameter;
298 }
299
300 /// <summary>
301 /// Create a parameter with PGSQL schema type
302 /// </summary>
303 /// <param name="parameterName"></param>
304 /// <param name="parameterObject"></param>
305 /// <param name="PGFieldType"></param>
306 /// <returns></returns>
307 internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, string PGFieldType)
308 {
309 //Tweak so we dont always have to add : sign
310 if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", "");
311
312 //HACK if object is null, it is turned into a string, there are no nullable type till now
313 if (parameterObject == null) parameterObject = "";
314
315 NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType));
316
317 parameter.Direction = ParameterDirection.Input;
318 parameter.Value = CreateParameterValue(parameterObject, PGFieldType);
319
320 return parameter;
321 }
322
323 /// <summary>
324 /// Checks if we need to do some migrations to the database
325 /// </summary>
326 /// <param name="migrationStore">migrationStore.</param>
327 public void CheckMigration(string migrationStore)
328 {
329 using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
330 {
331 connection.Open();
332 Assembly assem = GetType().Assembly;
333 PGSQLMigration migration = new PGSQLMigration(connection, assem, migrationStore);
334
335 migration.Update();
336 }
337 }
338
339 /// <summary>
340 /// Returns the version of this DB provider
341 /// </summary>
342 /// <returns>A string containing the DB provider</returns>
343 public string getVersion()
344 {
345 Module module = GetType().Module;
346 // string dllName = module.Assembly.ManifestModule.Name;
347 Version dllVersion = module.Assembly.GetName().Version;
348
349 return
350 string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
351 dllVersion.Revision);
352 }
353 }
354}