29 using System.Collections.Generic;
32 using System.Reflection;
33 using OpenSim.Framework;
39 namespace OpenSim.Data.PGSQL
51 private readonly
string connectionString;
59 connectionString = connection;
60 InitializeMonoSecurity();
65 if (!Util.IsPlatformMono)
67 if (AppDomain.CurrentDomain.GetData(
"MonoSecurityPostgresAdded") == null)
69 AppDomain.CurrentDomain.SetData(
"MonoSecurityPostgresAdded",
"true");
71 AppDomain currentDomain = AppDomain.CurrentDomain;
72 currentDomain.AssemblyResolve +=
new ResolveEventHandler(ResolveEventHandlerMonoSec);
77 private System.Reflection.Assembly ResolveEventHandlerMonoSec(
object sender, ResolveEventArgs args)
79 Assembly MyAssembly = null;
81 if (args.Name.Substring(0, args.Name.IndexOf(
",")) ==
"Mono.Security")
83 MyAssembly = Assembly.LoadFrom(
"lib/NET/Mono.Security.dll");
95 internal NpgsqlDbType DbtypeFromType(Type type)
97 if (type == typeof(
string))
99 return NpgsqlDbType.Varchar;
101 if (type == typeof(
double))
103 return NpgsqlDbType.Double;
105 if (type == typeof(Single))
107 return NpgsqlDbType.Double;
109 if (type == typeof(
int))
111 return NpgsqlDbType.Integer;
113 if (type == typeof(
bool))
115 return NpgsqlDbType.Boolean;
117 if (type == typeof(UUID))
119 return NpgsqlDbType.Uuid;
121 if (type == typeof(byte))
123 return NpgsqlDbType.Smallint;
125 if (type == typeof(sbyte))
127 return NpgsqlDbType.Integer;
129 if (type == typeof(Byte[]))
131 return NpgsqlDbType.Bytea;
133 if (type == typeof(uint) || type == typeof(ushort))
135 return NpgsqlDbType.Integer;
137 if (type == typeof(ulong))
139 return NpgsqlDbType.Bigint;
141 if (type == typeof(DateTime))
143 return NpgsqlDbType.Timestamp;
146 return NpgsqlDbType.Varchar;
149 internal NpgsqlDbType DbtypeFromString(Type type,
string PGFieldType)
151 if (PGFieldType ==
"")
153 return DbtypeFromType(type);
156 if (PGFieldType ==
"character varying")
158 return NpgsqlDbType.Varchar;
160 if (PGFieldType ==
"double precision")
162 return NpgsqlDbType.Double;
164 if (PGFieldType ==
"integer")
166 return NpgsqlDbType.Integer;
168 if (PGFieldType ==
"smallint")
170 return NpgsqlDbType.Smallint;
172 if (PGFieldType ==
"boolean")
174 return NpgsqlDbType.Boolean;
176 if (PGFieldType ==
"uuid")
178 return NpgsqlDbType.Uuid;
180 if (PGFieldType ==
"bytea")
182 return NpgsqlDbType.Bytea;
185 return DbtypeFromType(type);
193 private static object CreateParameterValue(
object value)
195 Type valueType = value.GetType();
197 if (valueType == typeof(UUID))
199 return ((UUID) value).Guid;
201 if (valueType == typeof(UUID))
203 return ((UUID)value).Guid;
205 if (valueType == typeof(
bool))
209 if (valueType == typeof(Byte[]))
213 if (valueType == typeof(
int))
226 internal static object CreateParameterValue(
object value,
string PGFieldType)
228 if (PGFieldType ==
"uuid")
231 UUID.TryParse(value.ToString(), out uidout);
234 if (PGFieldType ==
"integer")
237 int.TryParse(value.ToString(), out intout);
240 if (PGFieldType ==
"boolean")
242 return (value.ToString() ==
"true");
244 if (PGFieldType ==
"timestamp with time zone")
246 return (DateTime)value;
248 if (PGFieldType ==
"timestamp without time zone")
250 return (DateTime)value;
252 if (PGFieldType ==
"double precision")
254 return (Double)value;
256 return CreateParameterValue(value);
265 internal NpgsqlParameter CreateParameter(
string parameterName,
object parameterObject)
267 return CreateParameter(parameterName, parameterObject,
false);
277 internal NpgsqlParameter CreateParameter(
string parameterName,
object parameterObject,
bool parameterOut)
280 if (parameterName.StartsWith(
":")) parameterName = parameterName.Replace(
":",
"");
283 if (parameterObject == null) parameterObject =
"";
285 NpgsqlParameter parameter =
new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
289 parameter.Direction = ParameterDirection.Output;
293 parameter.Direction = ParameterDirection.Input;
294 parameter.Value = CreateParameterValue(parameterObject);
307 internal NpgsqlParameter CreateParameter(
string parameterName,
object parameterObject,
string PGFieldType)
310 if (parameterName.StartsWith(
":")) parameterName = parameterName.Replace(
":",
"");
313 if (parameterObject == null) parameterObject =
"";
315 NpgsqlParameter parameter =
new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType));
317 parameter.Direction = ParameterDirection.Input;
318 parameter.Value = CreateParameterValue(parameterObject, PGFieldType);
329 using (NpgsqlConnection connection =
new NpgsqlConnection(connectionString))
332 Assembly assem = GetType().Assembly;
345 Module module = GetType().Module;
347 Version dllVersion = module.Assembly.GetName().Version;
350 string.Format(
"{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
351 dllVersion.Revision);
void InitializeMonoSecurity()
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
A management class for the MS SQL Storage Engine