clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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  
28 using System;
29 using System.Collections.Generic;
30 using System.Data;
31 using System.Data.SqlClient;
32 using System.IO;
33 using System.Reflection;
34 using log4net;
35 using OpenMetaverse;
36  
37 namespace OpenSim.Data.MSSQL
38 {
39 /// <summary>
40 /// A management class for the MS SQL Storage Engine
41 /// </summary>
42 public class MSSQLManager
43 {
44 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45  
46 /// <summary>
47 /// Connection string for ADO.net
48 /// </summary>
49 private readonly string connectionString;
50  
51 /// <summary>
52 /// Initialize the manager and set the connectionstring
53 /// </summary>
54 /// <param name="connection"></param>
55 public MSSQLManager(string connection)
56 {
57 connectionString = connection;
58 }
59  
60 /// <summary>
61 /// Type conversion to a SQLDbType functions
62 /// </summary>
63 /// <param name="type"></param>
64 /// <returns></returns>
65 internal SqlDbType DbtypeFromType(Type type)
66 {
67 if (type == typeof(string))
68 {
69 return SqlDbType.VarChar;
70 }
71 if (type == typeof(double))
72 {
73 return SqlDbType.Float;
74 }
75 if (type == typeof(Single))
76 {
77 return SqlDbType.Float;
78 }
79 if (type == typeof(int))
80 {
81 return SqlDbType.Int;
82 }
83 if (type == typeof(bool))
84 {
85 return SqlDbType.Bit;
86 }
87 if (type == typeof(UUID))
88 {
89 return SqlDbType.UniqueIdentifier;
90 }
91 if (type == typeof(sbyte))
92 {
93 return SqlDbType.Int;
94 }
95 if (type == typeof(Byte[]))
96 {
97 return SqlDbType.Image;
98 }
99 if (type == typeof(uint) || type == typeof(ushort))
100 {
101 return SqlDbType.Int;
102 }
103 if (type == typeof(ulong))
104 {
105 return SqlDbType.BigInt;
106 }
107 if (type == typeof(DateTime))
108 {
109 return SqlDbType.DateTime;
110 }
111  
112 return SqlDbType.VarChar;
113 }
114  
115 /// <summary>
116 /// Creates value for parameter.
117 /// </summary>
118 /// <param name="value">The value.</param>
119 /// <returns></returns>
120 private static object CreateParameterValue(object value)
121 {
122 Type valueType = value.GetType();
123  
124 if (valueType == typeof(UUID)) //TODO check if this works
125 {
126 return ((UUID) value).Guid;
127 }
128 if (valueType == typeof(UUID))
129 {
130 return ((UUID)value).Guid;
131 }
132 if (valueType == typeof(bool))
133 {
134 return (bool)value ? 1 : 0;
135 }
136 if (valueType == typeof(Byte[]))
137 {
138 return value;
139 }
140 if (valueType == typeof(int))
141 {
142 return value;
143 }
144 return value;
145 }
146  
147 /// <summary>
148 /// Create a parameter for a command
149 /// </summary>
150 /// <param name="parameterName">Name of the parameter.</param>
151 /// <param name="parameterObject">parameter object.</param>
152 /// <returns></returns>
153 internal SqlParameter CreateParameter(string parameterName, object parameterObject)
154 {
155 return CreateParameter(parameterName, parameterObject, false);
156 }
157  
158 /// <summary>
159 /// Creates the parameter for a command.
160 /// </summary>
161 /// <param name="parameterName">Name of the parameter.</param>
162 /// <param name="parameterObject">parameter object.</param>
163 /// <param name="parameterOut">if set to <c>true</c> parameter is a output parameter</param>
164 /// <returns></returns>
165 internal SqlParameter CreateParameter(string parameterName, object parameterObject, bool parameterOut)
166 {
167 //Tweak so we dont always have to add @ sign
168 if (!parameterName.StartsWith("@")) parameterName = "@" + parameterName;
169  
170 //HACK if object is null, it is turned into a string, there are no nullable type till now
171 if (parameterObject == null) parameterObject = "";
172  
173 SqlParameter parameter = new SqlParameter(parameterName, DbtypeFromType(parameterObject.GetType()));
174  
175 if (parameterOut)
176 {
177 parameter.Direction = ParameterDirection.Output;
178 }
179 else
180 {
181 parameter.Direction = ParameterDirection.Input;
182 parameter.Value = CreateParameterValue(parameterObject);
183 }
184  
185 return parameter;
186 }
187  
188 /// <summary>
189 /// Checks if we need to do some migrations to the database
190 /// </summary>
191 /// <param name="migrationStore">migrationStore.</param>
192 public void CheckMigration(string migrationStore)
193 {
194 using (SqlConnection connection = new SqlConnection(connectionString))
195 {
196 connection.Open();
197 Assembly assem = GetType().Assembly;
198 MSSQLMigration migration = new MSSQLMigration(connection, assem, migrationStore);
199  
200 migration.Update();
201 }
202 }
203  
204 /// <summary>
205 /// Returns the version of this DB provider
206 /// </summary>
207 /// <returns>A string containing the DB provider</returns>
208 public string getVersion()
209 {
210 Module module = GetType().Module;
211 // string dllName = module.Assembly.ManifestModule.Name;
212 Version dllVersion = module.Assembly.GetName().Version;
213  
214 return
215 string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
216 dllVersion.Revision);
217 }
218 }
219 }