From 6a45a1ce9c7be613ebc7f16372acf27c991a245e Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 7 Sep 2007 12:37:05 +0000 Subject: factor out common methods to SQLiteBase --- OpenSim/Framework/Data.SQLite/SQLiteUserData.cs | 170 +----------------------- 1 file changed, 1 insertion(+), 169 deletions(-) (limited to 'OpenSim/Framework/Data.SQLite/SQLiteUserData.cs') diff --git a/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs b/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs index b09354d..26c832d 100644 --- a/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs +++ b/OpenSim/Framework/Data.SQLite/SQLiteUserData.cs @@ -39,7 +39,7 @@ namespace OpenSim.Framework.Data.SQLite /// /// A User storage interface for the DB4o database system /// - public class SQLiteUserData : IUserData + public class SQLiteUserData : SQLiteBase, IUserData { /// /// The database manager @@ -284,12 +284,6 @@ namespace OpenSim.Framework.Data.SQLite * **********************************************************************/ - private void createCol(DataTable dt, string name, System.Type type) - { - DataColumn col = new DataColumn(name, type); - dt.Columns.Add(col); - } - private DataTable createUsersTable() { DataTable users = new DataTable("users"); @@ -477,98 +471,6 @@ namespace OpenSim.Framework.Data.SQLite /*********************************************************************** * - * SQL Statement Creation Functions - * - * These functions create SQL statements for update, insert, and create. - * They can probably be factored later to have a db independant - * portion and a db specific portion - * - **********************************************************************/ - - private SqliteCommand createInsertCommand(string table, DataTable dt) - { - /** - * This is subtle enough to deserve some commentary. - * Instead of doing *lots* and *lots of hardcoded strings - * for database definitions we'll use the fact that - * realistically all insert statements look like "insert - * into A(b, c) values(:b, :c) on the parameterized query - * front. If we just have a list of b, c, etc... we can - * generate these strings instead of typing them out. - */ - string[] cols = new string[dt.Columns.Count]; - for (int i = 0; i < dt.Columns.Count; i++) { - DataColumn col = dt.Columns[i]; - cols[i] = col.ColumnName; - } - - string sql = "insert into " + table + "("; - sql += String.Join(", ", cols); - // important, the first ':' needs to be here, the rest get added in the join - sql += ") values (:"; - sql += String.Join(", :", cols); - sql += ")"; - SqliteCommand cmd = new SqliteCommand(sql); - - // this provides the binding for all our parameters, so - // much less code than it used to be - foreach (DataColumn col in dt.Columns) - { - cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType)); - } - return cmd; - } - - private SqliteCommand createUpdateCommand(string table, string pk, DataTable dt) - { - string sql = "update " + table + " set "; - string subsql = ""; - foreach (DataColumn col in dt.Columns) - { - if (subsql.Length > 0) - { // a map function would rock so much here - subsql += ", "; - } - subsql += col.ColumnName + "= :" + col.ColumnName; - } - sql += subsql; - sql += " where " + pk; - SqliteCommand cmd = new SqliteCommand(sql); - - // this provides the binding for all our parameters, so - // much less code than it used to be - - foreach (DataColumn col in dt.Columns) - { - cmd.Parameters.Add(createSqliteParameter(col.ColumnName, col.DataType)); - } - return cmd; - } - - - private string defineTable(DataTable dt) - { - string sql = "create table " + dt.TableName + "("; - string subsql = ""; - foreach (DataColumn col in dt.Columns) - { - if (subsql.Length > 0) - { // a map function would rock so much here - subsql += ",\n"; - } - subsql += col.ColumnName + " " + sqliteType(col.DataType); - if(col == dt.PrimaryKey[0]) - { - subsql += " primary key"; - } - } - sql += subsql; - sql += ")"; - return sql; - } - - /*********************************************************************** - * * Database Binding functions * * These will be db specific due to typing, and minor differences @@ -576,27 +478,6 @@ namespace OpenSim.Framework.Data.SQLite * **********************************************************************/ - /// - /// This is a convenience function that collapses 5 repetitive - /// lines for defining SqliteParameters to 2 parameters: - /// column name and database type. - /// - /// It assumes certain conventions like :param as the param - /// name to replace in parametrized queries, and that source - /// version is always current version, both of which are fine - /// for us. - /// - ///a built sqlite parameter - private SqliteParameter createSqliteParameter(string name, System.Type type) - { - SqliteParameter param = new SqliteParameter(); - param.ParameterName = ":" + name; - param.DbType = dbtypeFromType(type); - param.SourceColumn = name; - param.SourceVersion = DataRowVersion.Current; - return param; - } - private void setupUserCommands(SqliteDataAdapter da, SqliteConnection conn) { da.InsertCommand = createInsertCommand("users", ds.Tables["users"]); @@ -634,54 +515,5 @@ namespace OpenSim.Framework.Data.SQLite return true; } - /*********************************************************************** - * - * Type conversion functions - * - **********************************************************************/ - - private DbType dbtypeFromType(Type type) - { - if (type == typeof(System.String)) { - return DbType.String; - } else if (type == typeof(System.Int32)) { - return DbType.Int32; - } else if (type == typeof(System.UInt32)) { - return DbType.UInt32; - } else if (type == typeof(System.Int64)) { - return DbType.Int64; - } else if (type == typeof(System.UInt64)) { - return DbType.UInt64; - } else if (type == typeof(System.Double)) { - return DbType.Double; - } else if (type == typeof(System.Byte[])) { - return DbType.Binary; - } else { - return DbType.String; - } - } - - // this is something we'll need to implement for each db - // slightly differently. - private string sqliteType(Type type) - { - if (type == typeof(System.String)) { - return "varchar(255)"; - } else if (type == typeof(System.Int32)) { - return "integer"; - } else if (type == typeof(System.UInt32)) { - return "integer"; - } else if (type == typeof(System.Int64)) { - return "varchar(255)"; - } else if (type == typeof(System.UInt64)) { - return "varchar(255)"; - } else if (type == typeof(System.Double)) { - return "float"; - } else if (type == typeof(System.Byte[])) { - return "blob"; - } else { - return "string"; - } - } } } -- cgit v1.1