aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/PGSQL/PGSQLManager.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/PGSQL/PGSQLManager.cs350
1 files changed, 350 insertions, 0 deletions
diff --git a/OpenSim/Data/PGSQL/PGSQLManager.cs b/OpenSim/Data/PGSQL/PGSQLManager.cs
new file mode 100644
index 0000000..97f40b2
--- /dev/null
+++ b/OpenSim/Data/PGSQL/PGSQLManager.cs
@@ -0,0 +1,350 @@
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 return CreateParameterValue(value);
253 }
254
255 /// <summary>
256 /// Create a parameter for a command
257 /// </summary>
258 /// <param name="parameterName">Name of the parameter.</param>
259 /// <param name="parameterObject">parameter object.</param>
260 /// <returns></returns>
261 internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject)
262 {
263 return CreateParameter(parameterName, parameterObject, false);
264 }
265
266 /// <summary>
267 /// Creates the parameter for a command.
268 /// </summary>
269 /// <param name="parameterName">Name of the parameter.</param>
270 /// <param name="parameterObject">parameter object.</param>
271 /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
272 /// <returns></returns>
273 internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
274 {
275 //Tweak so we dont always have to add : sign
276 if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":","");
277
278 //HACK if object is null, it is turned into a string, there are no nullable type till now
279 if (parameterObject == null) parameterObject = "";
280
281 NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
282
283 if (parameterOut)
284 {
285 parameter.Direction = ParameterDirection.Output;
286 }
287 else
288 {
289 parameter.Direction = ParameterDirection.Input;
290 parameter.Value = CreateParameterValue(parameterObject);
291 }
292
293 return parameter;
294 }
295
296 /// <summary>
297 /// Create a parameter with PGSQL schema type
298 /// </summary>
299 /// <param name="parameterName"></param>
300 /// <param name="parameterObject"></param>
301 /// <param name="PGFieldType"></param>
302 /// <returns></returns>
303 internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, string PGFieldType)
304 {
305 //Tweak so we dont always have to add : sign
306 if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", "");
307
308 //HACK if object is null, it is turned into a string, there are no nullable type till now
309 if (parameterObject == null) parameterObject = "";
310
311 NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType));
312
313 parameter.Direction = ParameterDirection.Input;
314 parameter.Value = CreateParameterValue(parameterObject, PGFieldType);
315
316 return parameter;
317 }
318
319 /// <summary>
320 /// Checks if we need to do some migrations to the database
321 /// </summary>
322 /// <param name="migrationStore">migrationStore.</param>
323 public void CheckMigration(string migrationStore)
324 {
325 using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
326 {
327 connection.Open();
328 Assembly assem = GetType().Assembly;
329 PGSQLMigration migration = new PGSQLMigration(connection, assem, migrationStore);
330
331 migration.Update();
332 }
333 }
334
335 /// <summary>
336 /// Returns the version of this DB provider
337 /// </summary>
338 /// <returns>A string containing the DB provider</returns>
339 public string getVersion()
340 {
341 Module module = GetType().Module;
342 // string dllName = module.Assembly.ManifestModule.Name;
343 Version dllVersion = module.Assembly.GetName().Version;
344
345 return
346 string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
347 dllVersion.Revision);
348 }
349 }
350}