1
0

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. }
  57. /// <summary>
  58. /// Type conversion to a SQLDbType functions
  59. /// </summary>
  60. /// <param name="type"></param>
  61. /// <returns></returns>
  62. internal NpgsqlDbType DbtypeFromType(Type type)
  63. {
  64. if (type == typeof(string))
  65. {
  66. return NpgsqlDbType.Varchar;
  67. }
  68. if (type == typeof(double))
  69. {
  70. return NpgsqlDbType.Double;
  71. }
  72. if (type == typeof(float))
  73. {
  74. return NpgsqlDbType.Double;
  75. }
  76. if (type == typeof(int))
  77. {
  78. return NpgsqlDbType.Integer;
  79. }
  80. if (type == typeof(bool))
  81. {
  82. return NpgsqlDbType.Boolean;
  83. }
  84. if (type == typeof(UUID))
  85. {
  86. return NpgsqlDbType.Uuid;
  87. }
  88. if (type == typeof(byte))
  89. {
  90. return NpgsqlDbType.Smallint;
  91. }
  92. if (type == typeof(sbyte))
  93. {
  94. return NpgsqlDbType.Integer;
  95. }
  96. if (type == typeof(Byte[]))
  97. {
  98. return NpgsqlDbType.Bytea;
  99. }
  100. if (type == typeof(uint) || type == typeof(ushort))
  101. {
  102. return NpgsqlDbType.Integer;
  103. }
  104. if (type == typeof(ulong))
  105. {
  106. return NpgsqlDbType.Bigint;
  107. }
  108. if (type == typeof(DateTime))
  109. {
  110. return NpgsqlDbType.Timestamp;
  111. }
  112. return NpgsqlDbType.Varchar;
  113. }
  114. internal NpgsqlDbType DbtypeFromString(Type type, string PGFieldType)
  115. {
  116. if (PGFieldType.Length == 0)
  117. {
  118. return DbtypeFromType(type);
  119. }
  120. if (PGFieldType == "character varying")
  121. {
  122. return NpgsqlDbType.Varchar;
  123. }
  124. if (PGFieldType == "double precision")
  125. {
  126. return NpgsqlDbType.Double;
  127. }
  128. if (PGFieldType == "integer")
  129. {
  130. return NpgsqlDbType.Integer;
  131. }
  132. if (PGFieldType == "smallint")
  133. {
  134. return NpgsqlDbType.Smallint;
  135. }
  136. if (PGFieldType == "boolean")
  137. {
  138. return NpgsqlDbType.Boolean;
  139. }
  140. if (PGFieldType == "uuid")
  141. {
  142. return NpgsqlDbType.Uuid;
  143. }
  144. if (PGFieldType == "bytea")
  145. {
  146. return NpgsqlDbType.Bytea;
  147. }
  148. return DbtypeFromType(type);
  149. }
  150. /// <summary>
  151. /// Creates value for parameter.
  152. /// </summary>
  153. /// <param name="value">The value.</param>
  154. /// <returns></returns>
  155. private static object CreateParameterValue(object value)
  156. {
  157. Type valueType = value.GetType();
  158. if (valueType == typeof(UUID))
  159. {
  160. return ((UUID)value).Guid;
  161. }
  162. if (valueType == typeof(bool))
  163. {
  164. return (bool)value;
  165. }
  166. if (valueType == typeof(Byte[]))
  167. {
  168. return value;
  169. }
  170. if (valueType == typeof(int))
  171. {
  172. return value;
  173. }
  174. if (valueType == typeof(uint))
  175. {
  176. return (int)(uint)value;
  177. }
  178. if (valueType == typeof(ushort))
  179. {
  180. return (int)(ushort)value;
  181. }
  182. return value;
  183. }
  184. /// <summary>
  185. /// Create value for parameter based on PGSQL Schema
  186. /// </summary>
  187. /// <param name="value"></param>
  188. /// <param name="PGFieldType"></param>
  189. /// <returns></returns>
  190. internal static object CreateParameterValue(object value, string PGFieldType)
  191. {
  192. if (PGFieldType == "uuid")
  193. {
  194. if(value is not UUID uid)
  195. UUID.TryParse(value.ToString(), out uid);
  196. return uid.Guid;
  197. }
  198. if (PGFieldType == "smallint" || PGFieldType == "smallserial")
  199. {
  200. short.TryParse(value.ToString(), out short sintout);
  201. return sintout;
  202. }
  203. if (PGFieldType == "integer")
  204. {
  205. int.TryParse(value.ToString(), out int intout);
  206. return intout;
  207. }
  208. if (PGFieldType == "bigint")
  209. {
  210. long.TryParse(value.ToString(), out long lintout);
  211. return lintout;
  212. }
  213. if (PGFieldType == "boolean" || PGFieldType == "bit")
  214. {
  215. return (value.ToString() == "true");
  216. }
  217. if (PGFieldType == "timestamp with time zone")
  218. {
  219. return (DateTime)value;
  220. }
  221. if (PGFieldType == "timestamp without time zone")
  222. {
  223. return (DateTime)value;
  224. }
  225. if (PGFieldType == "double precision")
  226. {
  227. return Convert.ToDouble(value);
  228. }
  229. if (PGFieldType == "character" || PGFieldType == "character varying" || PGFieldType == "text")
  230. return value.ToString();
  231. return CreateParameterValue(value);
  232. }
  233. /// <summary>
  234. /// Create a parameter for a command
  235. /// </summary>
  236. /// <param name="parameterName">Name of the parameter.</param>
  237. /// <param name="parameterObject">parameter object.</param>
  238. /// <returns></returns>
  239. internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject)
  240. {
  241. return CreateParameter(parameterName, parameterObject, false);
  242. }
  243. /// <summary>
  244. /// Creates the parameter for a command.
  245. /// </summary>
  246. /// <param name="parameterName">Name of the parameter.</param>
  247. /// <param name="parameterObject">parameter object.</param>
  248. /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
  249. /// <returns></returns>
  250. internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
  251. {
  252. //Tweak so we dont always have to add : sign
  253. if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":","");
  254. //HACK if object is null, it is turned into a string, there are no nullable type till now
  255. if (parameterObject == null) parameterObject = "";
  256. NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
  257. if (parameterOut)
  258. {
  259. parameter.Direction = ParameterDirection.Output;
  260. }
  261. else
  262. {
  263. parameter.Direction = ParameterDirection.Input;
  264. parameter.Value = CreateParameterValue(parameterObject);
  265. }
  266. return parameter;
  267. }
  268. internal NpgsqlParameter CreateParameterNullBytea(string parameterName)
  269. {
  270. //Tweak so we dont always have to add : sign
  271. if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", "");
  272. //HACK if object is null, it is turned into a string, there are no nullable type till now
  273. NpgsqlParameter parameter = new NpgsqlParameter(parameterName, NpgsqlDbType.Bytea);
  274. parameter.Direction = ParameterDirection.Input;
  275. parameter.Value = DBNull.Value;
  276. return parameter;
  277. }
  278. /// <summary>
  279. /// Create a parameter with PGSQL schema type
  280. /// </summary>
  281. /// <param name="parameterName"></param>
  282. /// <param name="parameterObject"></param>
  283. /// <param name="PGFieldType"></param>
  284. /// <returns></returns>
  285. internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, string PGFieldType)
  286. {
  287. //Tweak so we dont always have to add : sign
  288. if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", "");
  289. //HACK if object is null, it is turned into a string, there are no nullable type till now
  290. if (parameterObject == null) parameterObject = "";
  291. NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType));
  292. parameter.Direction = ParameterDirection.Input;
  293. parameter.Value = CreateParameterValue(parameterObject, PGFieldType);
  294. return parameter;
  295. }
  296. /// <summary>
  297. /// Checks if we need to do some migrations to the database
  298. /// </summary>
  299. /// <param name="migrationStore">migrationStore.</param>
  300. public void CheckMigration(string migrationStore)
  301. {
  302. using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
  303. {
  304. connection.Open();
  305. Assembly assem = GetType().Assembly;
  306. PGSQLMigration migration = new PGSQLMigration(connection, assem, migrationStore);
  307. migration.Update();
  308. }
  309. }
  310. /// <summary>
  311. /// Returns the version of this DB provider
  312. /// </summary>
  313. /// <returns>A string containing the DB provider</returns>
  314. public string getVersion()
  315. {
  316. Module module = GetType().Module;
  317. // string dllName = module.Assembly.ManifestModule.Name;
  318. Version dllVersion = module.Assembly.GetName().Version;
  319. return
  320. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  321. dllVersion.Revision);
  322. }
  323. }
  324. }