1
0

PGSQLManager.cs 12 KB

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