diff options
author | David Walter Seikel | 2016-11-03 21:44:39 +1000 |
---|---|---|
committer | David Walter Seikel | 2016-11-03 21:44:39 +1000 |
commit | 134f86e8d5c414409631b25b8c6f0ee45fbd8631 (patch) | |
tree | 216b89d3fb89acfb81be1e440c25c41ab09fa96d /OpenSim/Data/MSSQL/MSSQLManager.cs | |
parent | More changing to production grid. Double oops. (diff) | |
download | opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.zip opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.gz opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.bz2 opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.xz |
Initial update to OpenSim 0.8.2.1 source code.
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/PGSQL/PGSQLManager.cs (renamed from OpenSim/Data/MSSQL/MSSQLManager.cs) | 185 |
1 files changed, 160 insertions, 25 deletions
diff --git a/OpenSim/Data/MSSQL/MSSQLManager.cs b/OpenSim/Data/PGSQL/PGSQLManager.cs index 9a0015c..46f835a 100644 --- a/OpenSim/Data/MSSQL/MSSQLManager.cs +++ b/OpenSim/Data/PGSQL/PGSQLManager.cs | |||
@@ -28,18 +28,20 @@ | |||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Data; | 30 | using System.Data; |
31 | using System.Data.SqlClient; | ||
32 | using System.IO; | 31 | using System.IO; |
33 | using System.Reflection; | 32 | using System.Reflection; |
33 | using OpenSim.Framework; | ||
34 | using log4net; | 34 | using log4net; |
35 | using OpenMetaverse; | 35 | using OpenMetaverse; |
36 | using Npgsql; | ||
37 | using NpgsqlTypes; | ||
36 | 38 | ||
37 | namespace OpenSim.Data.MSSQL | 39 | namespace OpenSim.Data.PGSQL |
38 | { | 40 | { |
39 | /// <summary> | 41 | /// <summary> |
40 | /// A management class for the MS SQL Storage Engine | 42 | /// A management class for the MS SQL Storage Engine |
41 | /// </summary> | 43 | /// </summary> |
42 | public class MSSQLManager | 44 | public class PGSQLManager |
43 | { | 45 | { |
44 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 46 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
45 | 47 | ||
@@ -52,9 +54,37 @@ namespace OpenSim.Data.MSSQL | |||
52 | /// Initialize the manager and set the connectionstring | 54 | /// Initialize the manager and set the connectionstring |
53 | /// </summary> | 55 | /// </summary> |
54 | /// <param name="connection"></param> | 56 | /// <param name="connection"></param> |
55 | public MSSQLManager(string connection) | 57 | public PGSQLManager(string connection) |
56 | { | 58 | { |
57 | connectionString = connection; | 59 | connectionString = connection; |
60 | InitializeMonoSecurity(); | ||
61 | } | ||
62 | |||
63 | public void InitializeMonoSecurity() | ||
64 | { | ||
65 | if (!Util.IsPlatformMono) | ||
66 | { | ||
67 | if (AppDomain.CurrentDomain.GetData("MonoSecurityPostgresAdded") == null) | ||
68 | { | ||
69 | AppDomain.CurrentDomain.SetData("MonoSecurityPostgresAdded", "true"); | ||
70 | |||
71 | AppDomain currentDomain = AppDomain.CurrentDomain; | ||
72 | currentDomain.AssemblyResolve += new ResolveEventHandler(ResolveEventHandlerMonoSec); | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | private System.Reflection.Assembly ResolveEventHandlerMonoSec(object sender, ResolveEventArgs args) | ||
78 | { | ||
79 | Assembly MyAssembly = null; | ||
80 | |||
81 | if (args.Name.Substring(0, args.Name.IndexOf(",")) == "Mono.Security") | ||
82 | { | ||
83 | MyAssembly = Assembly.LoadFrom("lib/NET/Mono.Security.dll"); | ||
84 | } | ||
85 | |||
86 | //Return the loaded assembly. | ||
87 | return MyAssembly; | ||
58 | } | 88 | } |
59 | 89 | ||
60 | /// <summary> | 90 | /// <summary> |
@@ -62,54 +92,97 @@ namespace OpenSim.Data.MSSQL | |||
62 | /// </summary> | 92 | /// </summary> |
63 | /// <param name="type"></param> | 93 | /// <param name="type"></param> |
64 | /// <returns></returns> | 94 | /// <returns></returns> |
65 | internal SqlDbType DbtypeFromType(Type type) | 95 | internal NpgsqlDbType DbtypeFromType(Type type) |
66 | { | 96 | { |
67 | if (type == typeof(string)) | 97 | if (type == typeof(string)) |
68 | { | 98 | { |
69 | return SqlDbType.VarChar; | 99 | return NpgsqlDbType.Varchar; |
70 | } | 100 | } |
71 | if (type == typeof(double)) | 101 | if (type == typeof(double)) |
72 | { | 102 | { |
73 | return SqlDbType.Float; | 103 | return NpgsqlDbType.Double; |
74 | } | 104 | } |
75 | if (type == typeof(Single)) | 105 | if (type == typeof(Single)) |
76 | { | 106 | { |
77 | return SqlDbType.Float; | 107 | return NpgsqlDbType.Double; |
78 | } | 108 | } |
79 | if (type == typeof(int)) | 109 | if (type == typeof(int)) |
80 | { | 110 | { |
81 | return SqlDbType.Int; | 111 | return NpgsqlDbType.Integer; |
82 | } | 112 | } |
83 | if (type == typeof(bool)) | 113 | if (type == typeof(bool)) |
84 | { | 114 | { |
85 | return SqlDbType.Bit; | 115 | return NpgsqlDbType.Boolean; |
86 | } | 116 | } |
87 | if (type == typeof(UUID)) | 117 | if (type == typeof(UUID)) |
88 | { | 118 | { |
89 | return SqlDbType.UniqueIdentifier; | 119 | return NpgsqlDbType.Uuid; |
120 | } | ||
121 | if (type == typeof(byte)) | ||
122 | { | ||
123 | return NpgsqlDbType.Smallint; | ||
90 | } | 124 | } |
91 | if (type == typeof(sbyte)) | 125 | if (type == typeof(sbyte)) |
92 | { | 126 | { |
93 | return SqlDbType.Int; | 127 | return NpgsqlDbType.Integer; |
94 | } | 128 | } |
95 | if (type == typeof(Byte[])) | 129 | if (type == typeof(Byte[])) |
96 | { | 130 | { |
97 | return SqlDbType.Image; | 131 | return NpgsqlDbType.Bytea; |
98 | } | 132 | } |
99 | if (type == typeof(uint) || type == typeof(ushort)) | 133 | if (type == typeof(uint) || type == typeof(ushort)) |
100 | { | 134 | { |
101 | return SqlDbType.Int; | 135 | return NpgsqlDbType.Integer; |
102 | } | 136 | } |
103 | if (type == typeof(ulong)) | 137 | if (type == typeof(ulong)) |
104 | { | 138 | { |
105 | return SqlDbType.BigInt; | 139 | return NpgsqlDbType.Bigint; |
106 | } | 140 | } |
107 | if (type == typeof(DateTime)) | 141 | if (type == typeof(DateTime)) |
108 | { | 142 | { |
109 | return SqlDbType.DateTime; | 143 | return NpgsqlDbType.Timestamp; |
110 | } | 144 | } |
111 | 145 | ||
112 | return SqlDbType.VarChar; | 146 | return NpgsqlDbType.Varchar; |
147 | } | ||
148 | |||
149 | internal NpgsqlDbType DbtypeFromString(Type type, string PGFieldType) | ||
150 | { | ||
151 | if (PGFieldType == "") | ||
152 | { | ||
153 | return DbtypeFromType(type); | ||
154 | } | ||
155 | |||
156 | if (PGFieldType == "character varying") | ||
157 | { | ||
158 | return NpgsqlDbType.Varchar; | ||
159 | } | ||
160 | if (PGFieldType == "double precision") | ||
161 | { | ||
162 | return NpgsqlDbType.Double; | ||
163 | } | ||
164 | if (PGFieldType == "integer") | ||
165 | { | ||
166 | return NpgsqlDbType.Integer; | ||
167 | } | ||
168 | if (PGFieldType == "smallint") | ||
169 | { | ||
170 | return NpgsqlDbType.Smallint; | ||
171 | } | ||
172 | if (PGFieldType == "boolean") | ||
173 | { | ||
174 | return NpgsqlDbType.Boolean; | ||
175 | } | ||
176 | if (PGFieldType == "uuid") | ||
177 | { | ||
178 | return NpgsqlDbType.Uuid; | ||
179 | } | ||
180 | if (PGFieldType == "bytea") | ||
181 | { | ||
182 | return NpgsqlDbType.Bytea; | ||
183 | } | ||
184 | |||
185 | return DbtypeFromType(type); | ||
113 | } | 186 | } |
114 | 187 | ||
115 | /// <summary> | 188 | /// <summary> |
@@ -131,7 +204,7 @@ namespace OpenSim.Data.MSSQL | |||
131 | } | 204 | } |
132 | if (valueType == typeof(bool)) | 205 | if (valueType == typeof(bool)) |
133 | { | 206 | { |
134 | return (bool)value ? 1 : 0; | 207 | return (bool)value; |
135 | } | 208 | } |
136 | if (valueType == typeof(Byte[])) | 209 | if (valueType == typeof(Byte[])) |
137 | { | 210 | { |
@@ -145,12 +218,51 @@ namespace OpenSim.Data.MSSQL | |||
145 | } | 218 | } |
146 | 219 | ||
147 | /// <summary> | 220 | /// <summary> |
221 | /// Create value for parameter based on PGSQL Schema | ||
222 | /// </summary> | ||
223 | /// <param name="value"></param> | ||
224 | /// <param name="PGFieldType"></param> | ||
225 | /// <returns></returns> | ||
226 | internal static object CreateParameterValue(object value, string PGFieldType) | ||
227 | { | ||
228 | if (PGFieldType == "uuid") | ||
229 | { | ||
230 | UUID uidout; | ||
231 | UUID.TryParse(value.ToString(), out uidout); | ||
232 | return uidout; | ||
233 | } | ||
234 | if (PGFieldType == "integer") | ||
235 | { | ||
236 | int intout; | ||
237 | int.TryParse(value.ToString(), out intout); | ||
238 | return intout; | ||
239 | } | ||
240 | if (PGFieldType == "boolean") | ||
241 | { | ||
242 | return (value.ToString() == "true"); | ||
243 | } | ||
244 | if (PGFieldType == "timestamp with time zone") | ||
245 | { | ||
246 | return (DateTime)value; | ||
247 | } | ||
248 | if (PGFieldType == "timestamp without time zone") | ||
249 | { | ||
250 | return (DateTime)value; | ||
251 | } | ||
252 | if (PGFieldType == "double precision") | ||
253 | { | ||
254 | return (Double)value; | ||
255 | } | ||
256 | return CreateParameterValue(value); | ||
257 | } | ||
258 | |||
259 | /// <summary> | ||
148 | /// Create a parameter for a command | 260 | /// Create a parameter for a command |
149 | /// </summary> | 261 | /// </summary> |
150 | /// <param name="parameterName">Name of the parameter.</param> | 262 | /// <param name="parameterName">Name of the parameter.</param> |
151 | /// <param name="parameterObject">parameter object.</param> | 263 | /// <param name="parameterObject">parameter object.</param> |
152 | /// <returns></returns> | 264 | /// <returns></returns> |
153 | internal SqlParameter CreateParameter(string parameterName, object parameterObject) | 265 | internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject) |
154 | { | 266 | { |
155 | return CreateParameter(parameterName, parameterObject, false); | 267 | return CreateParameter(parameterName, parameterObject, false); |
156 | } | 268 | } |
@@ -162,15 +274,15 @@ namespace OpenSim.Data.MSSQL | |||
162 | /// <param name="parameterObject">parameter object.</param> | 274 | /// <param name="parameterObject">parameter object.</param> |
163 | /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param> | 275 | /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param> |
164 | /// <returns></returns> | 276 | /// <returns></returns> |
165 | internal SqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut) | 277 | internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut) |
166 | { | 278 | { |
167 | //Tweak so we dont always have to add @ sign | 279 | //Tweak so we dont always have to add : sign |
168 | if (!parameterName.StartsWith("@")) parameterName = "@" + parameterName; | 280 | if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":",""); |
169 | 281 | ||
170 | //HACK if object is null, it is turned into a string, there are no nullable type till now | 282 | //HACK if object is null, it is turned into a string, there are no nullable type till now |
171 | if (parameterObject == null) parameterObject = ""; | 283 | if (parameterObject == null) parameterObject = ""; |
172 | 284 | ||
173 | SqlParameter parameter = new SqlParameter(parameterName, DbtypeFromType(parameterObject.GetType())); | 285 | NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromType(parameterObject.GetType())); |
174 | 286 | ||
175 | if (parameterOut) | 287 | if (parameterOut) |
176 | { | 288 | { |
@@ -186,16 +298,39 @@ namespace OpenSim.Data.MSSQL | |||
186 | } | 298 | } |
187 | 299 | ||
188 | /// <summary> | 300 | /// <summary> |
301 | /// Create a parameter with PGSQL schema type | ||
302 | /// </summary> | ||
303 | /// <param name="parameterName"></param> | ||
304 | /// <param name="parameterObject"></param> | ||
305 | /// <param name="PGFieldType"></param> | ||
306 | /// <returns></returns> | ||
307 | internal NpgsqlParameter CreateParameter(string parameterName, object parameterObject, string PGFieldType) | ||
308 | { | ||
309 | //Tweak so we dont always have to add : sign | ||
310 | if (parameterName.StartsWith(":")) parameterName = parameterName.Replace(":", ""); | ||
311 | |||
312 | //HACK if object is null, it is turned into a string, there are no nullable type till now | ||
313 | if (parameterObject == null) parameterObject = ""; | ||
314 | |||
315 | NpgsqlParameter parameter = new NpgsqlParameter(parameterName, DbtypeFromString(parameterObject.GetType(), PGFieldType)); | ||
316 | |||
317 | parameter.Direction = ParameterDirection.Input; | ||
318 | parameter.Value = CreateParameterValue(parameterObject, PGFieldType); | ||
319 | |||
320 | return parameter; | ||
321 | } | ||
322 | |||
323 | /// <summary> | ||
189 | /// Checks if we need to do some migrations to the database | 324 | /// Checks if we need to do some migrations to the database |
190 | /// </summary> | 325 | /// </summary> |
191 | /// <param name="migrationStore">migrationStore.</param> | 326 | /// <param name="migrationStore">migrationStore.</param> |
192 | public void CheckMigration(string migrationStore) | 327 | public void CheckMigration(string migrationStore) |
193 | { | 328 | { |
194 | using (SqlConnection connection = new SqlConnection(connectionString)) | 329 | using (NpgsqlConnection connection = new NpgsqlConnection(connectionString)) |
195 | { | 330 | { |
196 | connection.Open(); | 331 | connection.Open(); |
197 | Assembly assem = GetType().Assembly; | 332 | Assembly assem = GetType().Assembly; |
198 | MSSQLMigration migration = new MSSQLMigration(connection, assem, migrationStore); | 333 | PGSQLMigration migration = new PGSQLMigration(connection, assem, migrationStore); |
199 | 334 | ||
200 | migration.Update(); | 335 | migration.Update(); |
201 | } | 336 | } |