PGSQLManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. if (PGFieldType == "double precision")
  236. {
  237. return Convert.ToDouble(value);
  238. }
  239. return CreateParameterValue(value);
  240. }
  241. /// <summary>
  242. /// Create a parameter for a command
  243. /// </summary>
  244. /// <param name="parameterName">Name of the parameter.</param>
  245. /// <param name="parameterObject">parameter object.</param>
  246. /// <returns></returns>
  247. internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject)
  248. {
  249. return CreateParameter(parameterName, parameterObject, false);
  250. }
  251. /// <summary>
  252. /// Creates the parameter for a command.
  253. /// </summary>
  254. /// <param name="parameterName">Name of the parameter.</param>
  255. /// <param name="parameterObject">parameter object.</param>
  256. /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
  257. /// <returns></returns>
  258. internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
  259. {
  260. //Tweak so we dont always have to add : sign
  261. if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":","");
  262. //HACK if object is null, it is turned into a string, there are no nullable type till now
  263. if (parameterObject == null) parameterObject = "";
  264. NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
  265. if (parameterOut)
  266. {
  267. parameter.Direction = ParameterDirection.Output;
  268. }
  269. else
  270. {
  271. parameter.Direction = ParameterDirection.Input;
  272. parameter.Value = CreateParameterValue(parameterObject);
  273. }
  274. return parameter;
  275. }
  276. /// <summary>
  277. /// Create a parameter with PGSQL schema type
  278. /// </summary>
  279. /// <param name="parameterName"></param>
  280. /// <param name="parameterObject"></param>
  281. /// <param name="PGFieldType"></param>
  282. /// <returns></returns>
  283. internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, string PGFieldType)
  284. {
  285. //Tweak so we dont always have to add : sign
  286. if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", "");
  287. //HACK if object is null, it is turned into a string, there are no nullable type till now
  288. if (parameterObject == null) parameterObject = "";
  289. NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType));
  290. parameter.Direction = ParameterDirection.Input;
  291. parameter.Value = CreateParameterValue(parameterObject, PGFieldType);
  292. return parameter;
  293. }
  294. /// <summary>
  295. /// Checks if we need to do some migrations to the database
  296. /// </summary>
  297. /// <param name="migrationStore">migrationStore.</param>
  298. public void CheckMigration(string migrationStore)
  299. {
  300. using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
  301. {
  302. connection.Open();
  303. Assembly assem = GetType().Assembly;
  304. PGSQLMigration migration = new PGSQLMigration(connection, assem, migrationStore);
  305. migration.Update();
  306. }
  307. }
  308. /// <summary>
  309. /// Returns the version of this DB provider
  310. /// </summary>
  311. /// <returns>A string containing the DB provider</returns>
  312. public string getVersion()
  313. {
  314. Module module = GetType().Module;
  315. // string dllName = module.Assembly.ManifestModule.Name;
  316. Version dllVersion = module.Assembly.GetName().Version;
  317. return
  318. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  319. dllVersion.Revision);
  320. }
  321. }
  322. }