OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
PGSQLManager.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Data;
31 using System.IO;
32 using System.Reflection;
33 using OpenSim.Framework;
34 using log4net;
35 using OpenMetaverse;
36 using Npgsql;
37 using NpgsqlTypes;
38 
39 namespace OpenSim.Data.PGSQL
40 {
44  public class PGSQLManager
45  {
46 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 
51  private readonly string connectionString;
52 
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 
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 
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 
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 
265  internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject)
266  {
267  return CreateParameter(parameterName, parameterObject, false);
268  }
269 
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 
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 
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 
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 }
void CheckMigration(string migrationStore)
Checks if we need to do some migrations to the database
string getVersion()
Returns the version of this DB provider
PGSQLManager(string connection)
Initialize the manager and set the connectionstring
Definition: PGSQLManager.cs:57
A management class for the MS SQL Storage Engine
Definition: PGSQLManager.cs:44