OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SQLiteUtils.cs
Go to the documentation of this file.
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.Data;
30 #if CSharpSqlite
31  using Community.CsharpSqlite.Sqlite;
32 #else
33  using Mono.Data.Sqlite;
34 #endif
35 
36 namespace OpenSim.Data.SQLite
37 {
41  public class SQLiteUtil
42  {
43  /***********************************************************************
44  *
45  * Database Definition Helper Functions
46  *
47  * This should be db agnostic as we define them in ADO.NET terms
48  *
49  **********************************************************************/
50 
57  public static void createCol(DataTable dt, string name, Type type)
58  {
59  DataColumn col = new DataColumn(name, type);
60  dt.Columns.Add(col);
61  }
62 
63  /***********************************************************************
64  *
65  * SQL Statement Creation Functions
66  *
67  * These functions create SQL statements for update, insert, and create.
68  * They can probably be factored later to have a db independant
69  * portion and a db specific portion
70  *
71  **********************************************************************/
72 
88  public static SqliteCommand createInsertCommand(string table, DataTable dt)
89  {
90 
91  string[] cols = new string[dt.Columns.Count];
92  for (int i = 0; i < dt.Columns.Count; i++)
93  {
94  DataColumn col = dt.Columns[i];
95  cols[i] = col.ColumnName;
96  }
97 
98  string sql = "insert into " + table + "(";
99  sql += String.Join(", ", cols);
100  // important, the first ':' needs to be here, the rest get added in the join
101  sql += ") values (:";
102  sql += String.Join(", :", cols);
103  sql += ")";
104  SqliteCommand cmd = new SqliteCommand(sql);
105 
106  // this provides the binding for all our parameters, so
107  // much less code than it used to be
108  foreach (DataColumn col in dt.Columns)
109  {
110  cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType));
111  }
112  return cmd;
113  }
114 
122  public static SqliteCommand createUpdateCommand(string table, string pk, DataTable dt)
123  {
124  string sql = "update " + table + " set ";
125  string subsql = String.Empty;
126  foreach (DataColumn col in dt.Columns)
127  {
128  if (subsql.Length > 0)
129  {
130  // a map function would rock so much here
131  subsql += ", ";
132  }
133  subsql += col.ColumnName + "= :" + col.ColumnName;
134  }
135  sql += subsql;
136  sql += " where " + pk;
137  SqliteCommand cmd = new SqliteCommand(sql);
138 
139  // this provides the binding for all our parameters, so
140  // much less code than it used to be
141 
142  foreach (DataColumn col in dt.Columns)
143  {
144  cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType));
145  }
146  return cmd;
147  }
148 
154  public static string defineTable(DataTable dt)
155  {
156  string sql = "create table " + dt.TableName + "(";
157  string subsql = String.Empty;
158  foreach (DataColumn col in dt.Columns)
159  {
160  if (subsql.Length > 0)
161  {
162  // a map function would rock so much here
163  subsql += ",\n";
164  }
165  subsql += col.ColumnName + " " + sqliteType(col.DataType);
166  if (dt.PrimaryKey.Length > 0)
167  {
168  if (col == dt.PrimaryKey[0])
169  {
170  subsql += " primary key";
171  }
172  }
173  }
174  sql += subsql;
175  sql += ")";
176  return sql;
177  }
178 
179  /***********************************************************************
180  *
181  * Database Binding functions
182  *
183  * These will be db specific due to typing, and minor differences
184  * in databases.
185  *
186  **********************************************************************/
187 
205  public static SqliteParameter createSqliteParameter(string name, Type type)
206  {
207  SqliteParameter param = new SqliteParameter();
208  param.ParameterName = ":" + name;
209  param.DbType = dbtypeFromType(type);
210  param.SourceColumn = name;
211  param.SourceVersion = DataRowVersion.Current;
212  return param;
213  }
214 
215  /***********************************************************************
216  *
217  * Type conversion functions
218  *
219  **********************************************************************/
220 
226  public static DbType dbtypeFromType(Type type)
227  {
228  if (type == typeof (String))
229  {
230  return DbType.String;
231  }
232  else if (type == typeof (Int32))
233  {
234  return DbType.Int32;
235  }
236  else if (type == typeof (UInt32))
237  {
238  return DbType.UInt32;
239  }
240  else if (type == typeof (Int64))
241  {
242  return DbType.Int64;
243  }
244  else if (type == typeof (UInt64))
245  {
246  return DbType.UInt64;
247  }
248  else if (type == typeof (Double))
249  {
250  return DbType.Double;
251  }
252  else if (type == typeof (Boolean))
253  {
254  return DbType.Boolean;
255  }
256  else if (type == typeof (Byte[]))
257  {
258  return DbType.Binary;
259  }
260  else
261  {
262  return DbType.String;
263  }
264  }
265 
271  public static string sqliteType(Type type)
272  {
273  if (type == typeof (String))
274  {
275  return "varchar(255)";
276  }
277  else if (type == typeof (Int32))
278  {
279  return "integer";
280  }
281  else if (type == typeof (UInt32))
282  {
283  return "integer";
284  }
285  else if (type == typeof (Int64))
286  {
287  return "varchar(255)";
288  }
289  else if (type == typeof (UInt64))
290  {
291  return "varchar(255)";
292  }
293  else if (type == typeof (Double))
294  {
295  return "float";
296  }
297  else if (type == typeof (Boolean))
298  {
299  return "integer";
300  }
301  else if (type == typeof (Byte[]))
302  {
303  return "blob";
304  }
305  else
306  {
307  return "string";
308  }
309  }
310  }
311 }
static SqliteCommand createInsertCommand(string table, DataTable dt)
Create an insert command
Definition: SQLiteUtils.cs:88
static SqliteCommand createUpdateCommand(string table, string pk, DataTable dt)
create an update command
Definition: SQLiteUtils.cs:122
A base class for methods needed by all SQLite database classes
Definition: SQLiteUtils.cs:41
static string sqliteType(Type type)
Definition: SQLiteUtils.cs:271
static string defineTable(DataTable dt)
Definition: SQLiteUtils.cs:154
static SqliteParameter createSqliteParameter(string name, Type type)
Definition: SQLiteUtils.cs:205
static void createCol(DataTable dt, string name, Type type)
Definition: SQLiteUtils.cs:57
static DbType dbtypeFromType(Type type)
Type conversion function
Definition: SQLiteUtils.cs:226