SQLiteUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.Data;
  29. #if CSharpSqlite
  30. using Community.CsharpSqlite.Sqlite;
  31. #else
  32. using Mono.Data.Sqlite;
  33. #endif
  34. namespace OpenSim.Data.SQLite
  35. {
  36. /// <summary>
  37. /// A base class for methods needed by all SQLite database classes
  38. /// </summary>
  39. public class SQLiteUtil
  40. {
  41. /***********************************************************************
  42. *
  43. * Database Definition Helper Functions
  44. *
  45. * This should be db agnostic as we define them in ADO.NET terms
  46. *
  47. **********************************************************************/
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="dt"></param>
  52. /// <param name="name"></param>
  53. /// <param name="type"></param>
  54. public static void createCol(DataTable dt, string name, Type type)
  55. {
  56. DataColumn col = new DataColumn(name, type);
  57. dt.Columns.Add(col);
  58. }
  59. /***********************************************************************
  60. *
  61. * SQL Statement Creation Functions
  62. *
  63. * These functions create SQL statements for update, insert, and create.
  64. * They can probably be factored later to have a db independant
  65. * portion and a db specific portion
  66. *
  67. **********************************************************************/
  68. /// <summary>
  69. /// Create an insert command
  70. /// </summary>
  71. /// <param name="table">table name</param>
  72. /// <param name="dt">data table</param>
  73. /// <returns>the created command</returns>
  74. /// <remarks>
  75. /// This is subtle enough to deserve some commentary.
  76. /// Instead of doing *lots* and *lots of hardcoded strings
  77. /// for database definitions we'll use the fact that
  78. /// realistically all insert statements look like "insert
  79. /// into A(b, c) values(:b, :c) on the parameterized query
  80. /// front. If we just have a list of b, c, etc... we can
  81. /// generate these strings instead of typing them out.
  82. /// </remarks>
  83. public static SqliteCommand createInsertCommand(string table, DataTable dt)
  84. {
  85. string[] cols = new string[dt.Columns.Count];
  86. for (int i = 0; i < dt.Columns.Count; i++)
  87. {
  88. DataColumn col = dt.Columns[i];
  89. cols[i] = col.ColumnName;
  90. }
  91. string sql = "insert into " + table + "(";
  92. sql += String.Join(", ", cols);
  93. // important, the first ':' needs to be here, the rest get added in the join
  94. sql += ") values (:";
  95. sql += String.Join(", :", cols);
  96. sql += ")";
  97. SqliteCommand cmd = new SqliteCommand(sql);
  98. // this provides the binding for all our parameters, so
  99. // much less code than it used to be
  100. foreach (DataColumn col in dt.Columns)
  101. {
  102. cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType));
  103. }
  104. return cmd;
  105. }
  106. /// <summary>
  107. /// create an update command
  108. /// </summary>
  109. /// <param name="table">table name</param>
  110. /// <param name="pk"></param>
  111. /// <param name="dt"></param>
  112. /// <returns>the created command</returns>
  113. public static SqliteCommand createUpdateCommand(string table, string pk, DataTable dt)
  114. {
  115. string sql = "update " + table + " set ";
  116. string subsql = String.Empty;
  117. foreach (DataColumn col in dt.Columns)
  118. {
  119. if (subsql.Length > 0)
  120. {
  121. // a map function would rock so much here
  122. subsql += ", ";
  123. }
  124. subsql += col.ColumnName + "= :" + col.ColumnName;
  125. }
  126. sql += subsql;
  127. sql += " where " + pk;
  128. SqliteCommand cmd = new SqliteCommand(sql);
  129. // this provides the binding for all our parameters, so
  130. // much less code than it used to be
  131. foreach (DataColumn col in dt.Columns)
  132. {
  133. cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType));
  134. }
  135. return cmd;
  136. }
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. /// <param name="dt">Data Table</param>
  141. /// <returns></returns>
  142. public static string defineTable(DataTable dt)
  143. {
  144. string sql = "create table " + dt.TableName + "(";
  145. string subsql = String.Empty;
  146. foreach (DataColumn col in dt.Columns)
  147. {
  148. if (subsql.Length > 0)
  149. {
  150. // a map function would rock so much here
  151. subsql += ",\n";
  152. }
  153. subsql += col.ColumnName + " " + sqliteType(col.DataType);
  154. if (dt.PrimaryKey.Length > 0)
  155. {
  156. if (col == dt.PrimaryKey[0])
  157. {
  158. subsql += " primary key";
  159. }
  160. }
  161. }
  162. sql += subsql;
  163. sql += ")";
  164. return sql;
  165. }
  166. /***********************************************************************
  167. *
  168. * Database Binding functions
  169. *
  170. * These will be db specific due to typing, and minor differences
  171. * in databases.
  172. *
  173. **********************************************************************/
  174. ///<summary>
  175. /// <para>
  176. /// This is a convenience function that collapses 5 repetitive
  177. /// lines for defining SqliteParameters to 2 parameters:
  178. /// column name and database type.
  179. /// </para>
  180. ///
  181. /// <para>
  182. /// It assumes certain conventions like :param as the param
  183. /// name to replace in parametrized queries, and that source
  184. /// version is always current version, both of which are fine
  185. /// for us.
  186. /// </para>
  187. ///</summary>
  188. /// <param name="name"></param>
  189. /// <param name="type"></param>
  190. ///<returns>a built sqlite parameter</returns>
  191. public static SqliteParameter createSqliteParameter(string name, Type type)
  192. {
  193. SqliteParameter param = new SqliteParameter();
  194. param.ParameterName = ":" + name;
  195. param.DbType = dbtypeFromType(type);
  196. param.SourceColumn = name;
  197. param.SourceVersion = DataRowVersion.Current;
  198. return param;
  199. }
  200. /***********************************************************************
  201. *
  202. * Type conversion functions
  203. *
  204. **********************************************************************/
  205. /// <summary>
  206. /// Type conversion function
  207. /// </summary>
  208. /// <param name="type">a type</param>
  209. /// <returns>a DbType</returns>
  210. public static DbType dbtypeFromType(Type type)
  211. {
  212. if (type == typeof (String))
  213. {
  214. return DbType.String;
  215. }
  216. else if (type == typeof (Int32))
  217. {
  218. return DbType.Int32;
  219. }
  220. else if (type == typeof (UInt32))
  221. {
  222. return DbType.UInt32;
  223. }
  224. else if (type == typeof (Int64))
  225. {
  226. return DbType.Int64;
  227. }
  228. else if (type == typeof (UInt64))
  229. {
  230. return DbType.UInt64;
  231. }
  232. else if (type == typeof (Double))
  233. {
  234. return DbType.Double;
  235. }
  236. else if (type == typeof (Boolean))
  237. {
  238. return DbType.Boolean;
  239. }
  240. else if (type == typeof (Byte[]))
  241. {
  242. return DbType.Binary;
  243. }
  244. else
  245. {
  246. return DbType.String;
  247. }
  248. }
  249. /// <summary>
  250. /// </summary>
  251. /// <param name="type">a Type</param>
  252. /// <returns>a string</returns>
  253. /// <remarks>this is something we'll need to implement for each db slightly differently.</remarks>
  254. public static string sqliteType(Type type)
  255. {
  256. if (type == typeof (String))
  257. {
  258. return "varchar(255)";
  259. }
  260. else if (type == typeof (Int32))
  261. {
  262. return "integer";
  263. }
  264. else if (type == typeof (UInt32))
  265. {
  266. return "integer";
  267. }
  268. else if (type == typeof (Int64))
  269. {
  270. return "varchar(255)";
  271. }
  272. else if (type == typeof (UInt64))
  273. {
  274. return "varchar(255)";
  275. }
  276. else if (type == typeof (Double))
  277. {
  278. return "float";
  279. }
  280. else if (type == typeof (Boolean))
  281. {
  282. return "integer";
  283. }
  284. else if (type == typeof (Byte[]))
  285. {
  286. return "blob";
  287. }
  288. else
  289. {
  290. return "string";
  291. }
  292. }
  293. }
  294. }