123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Reflection;
- using OpenSim.Framework;
- using log4net;
- using OpenMetaverse;
- using Npgsql;
- using NpgsqlTypes;
- namespace OpenSim.Data.PGSQL
- {
-
-
-
- public class PGSQLManager
- {
-
-
-
- private readonly string connectionString;
-
-
-
-
- public PGSQLManager(string connection)
- {
- connectionString = connection;
- InitializeMonoSecurity();
- }
- public void InitializeMonoSecurity()
- {
- if (!Util.IsPlatformMono)
- {
- if (AppDomain.CurrentDomain.GetData("MonoSecurityPostgresAdded") == null)
- {
- AppDomain.CurrentDomain.SetData("MonoSecurityPostgresAdded", "true");
- AppDomain currentDomain = AppDomain.CurrentDomain;
- currentDomain.AssemblyResolve += new ResolveEventHandler(ResolveEventHandlerMonoSec);
- }
- }
- }
- private System.Reflection.Assembly ResolveEventHandlerMonoSec(object sender, ResolveEventArgs args)
- {
- Assembly MyAssembly = null;
- if (args.Name.Substring(0, args.Name.IndexOf(",")) == "Mono.Security")
- {
- MyAssembly = Assembly.LoadFrom("lib/NET/Mono.Security.dll");
- }
-
- return MyAssembly;
- }
-
-
-
-
-
- internal NpgsqlDbType DbtypeFromType(Type type)
- {
- if (type == typeof(string))
- {
- return NpgsqlDbType.Varchar;
- }
- if (type == typeof(double))
- {
- return NpgsqlDbType.Double;
- }
- if (type == typeof(Single))
- {
- return NpgsqlDbType.Double;
- }
- if (type == typeof(int))
- {
- return NpgsqlDbType.Integer;
- }
- if (type == typeof(bool))
- {
- return NpgsqlDbType.Boolean;
- }
- if (type == typeof(UUID))
- {
- return NpgsqlDbType.Uuid;
- }
- if (type == typeof(byte))
- {
- return NpgsqlDbType.Smallint;
- }
- if (type == typeof(sbyte))
- {
- return NpgsqlDbType.Integer;
- }
- if (type == typeof(Byte[]))
- {
- return NpgsqlDbType.Bytea;
- }
- if (type == typeof(uint) || type == typeof(ushort))
- {
- return NpgsqlDbType.Integer;
- }
- if (type == typeof(ulong))
- {
- return NpgsqlDbType.Bigint;
- }
- if (type == typeof(DateTime))
- {
- return NpgsqlDbType.Timestamp;
- }
- return NpgsqlDbType.Varchar;
- }
- internal NpgsqlDbType DbtypeFromString(Type type, string PGFieldType)
- {
- if (PGFieldType == "")
- {
- return DbtypeFromType(type);
- }
- if (PGFieldType == "character varying")
- {
- return NpgsqlDbType.Varchar;
- }
- if (PGFieldType == "double precision")
- {
- return NpgsqlDbType.Double;
- }
- if (PGFieldType == "integer")
- {
- return NpgsqlDbType.Integer;
- }
- if (PGFieldType == "smallint")
- {
- return NpgsqlDbType.Smallint;
- }
- if (PGFieldType == "boolean")
- {
- return NpgsqlDbType.Boolean;
- }
- if (PGFieldType == "uuid")
- {
- return NpgsqlDbType.Uuid;
- }
- if (PGFieldType == "bytea")
- {
- return NpgsqlDbType.Bytea;
- }
- return DbtypeFromType(type);
- }
-
-
-
-
-
- private static object CreateParameterValue(object value)
- {
- Type valueType = value.GetType();
- if (valueType == typeof(UUID))
- {
- return ((UUID) value).Guid;
- }
- if (valueType == typeof(UUID))
- {
- return ((UUID)value).Guid;
- }
- if (valueType == typeof(bool))
- {
- return (bool)value;
- }
- if (valueType == typeof(Byte[]))
- {
- return value;
- }
- if (valueType == typeof(int))
- {
- return value;
- }
- return value;
- }
-
-
-
-
-
-
- internal static object CreateParameterValue(object value, string PGFieldType)
- {
- if (PGFieldType == "uuid")
- {
- UUID uidout;
- UUID.TryParse(value.ToString(), out uidout);
- return uidout;
- }
- if (PGFieldType == "integer")
- {
- int intout;
- int.TryParse(value.ToString(), out intout);
- return intout;
- }
- if (PGFieldType == "boolean")
- {
- return (value.ToString() == "true");
- }
- if (PGFieldType == "timestamp with time zone")
- {
- return (DateTime)value;
- }
- if (PGFieldType == "timestamp without time zone")
- {
- return (DateTime)value;
- }
- return CreateParameterValue(value);
- }
-
-
-
-
-
-
- internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject)
- {
- return CreateParameter(parameterName, parameterObject, false);
- }
-
-
-
-
-
-
-
- internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
- {
-
- if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":","");
-
- if (parameterObject == null) parameterObject = "";
- NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
- if (parameterOut)
- {
- parameter.Direction = ParameterDirection.Output;
- }
- else
- {
- parameter.Direction = ParameterDirection.Input;
- parameter.Value = CreateParameterValue(parameterObject);
- }
- return parameter;
- }
-
-
-
-
-
-
-
- internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, string PGFieldType)
- {
-
- if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", "");
-
- if (parameterObject == null) parameterObject = "";
- NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType));
- parameter.Direction = ParameterDirection.Input;
- parameter.Value = CreateParameterValue(parameterObject, PGFieldType);
- return parameter;
- }
-
-
-
-
- public void CheckMigration(string migrationStore)
- {
- using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
- {
- connection.Open();
- Assembly assem = GetType().Assembly;
- PGSQLMigration migration = new PGSQLMigration(connection, assem, migrationStore);
- migration.Update();
- }
- }
-
-
-
-
- public string getVersion()
- {
- Module module = GetType().Module;
-
- Version dllVersion = module.Assembly.GetName().Version;
- return
- string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
- dllVersion.Revision);
- }
- }
- }
|