banshee r2999 - in trunk/banshee: . src/Core/Banshee.Services/Banshee.Collection.Database src/Core/Hyena/Hyena.Data.Sqlite



Author: scottp
Date: Sun Jan 20 06:02:09 2008
New Revision: 2999
URL: http://svn.gnome.org/viewvc/banshee?rev=2999&view=rev

Log:
	* src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs: 
Cleaned up
	  CheckVersion and CheckTable, beautified

	* src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs: Dealt with 
new
  	  type names and cleaned up.

	* src/Core/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs: 
Reworked
	  TableExitst and added new convenience methods IndexExitsts,
	  GetSchema, and QueryString. Also added more overloads to 
QueryInt32

	* src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelCache.cs: Made 
IndexOf to
	  use QueryInt32.

	* src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumnAttribute.cs: 
Renamed
	  DatabaseColumnBaseAttribute to AbstractDatabaseColumnAttribute 
and
	  renamed DatabaseVirtualColumnAttribute to
	  VirtualDatabaseColumnAttribute.

	* 
src/Core/Banshee.Services/Banshee.Collection.Database/LibraryAlbumInfo.cs,
	* 
src/Core/Banshee.Services/Banshee.Collection.Database/LibraryTrackInfo.cs:
	  Dealt with new type names


Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryAlbumInfo.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryTrackInfo.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumnAttribute.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelCache.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryAlbumInfo.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryAlbumInfo.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryAlbumInfo.cs	Sun Jan 20 06:02:09 2008
@@ -111,7 +111,7 @@
             set { base.Title = value; }
         }
 
-        [DatabaseVirtualColumn("Name", "CoreArtists", "ArtistID", "ArtistID")]
+        [VirtualDatabaseColumn("Name", "CoreArtists", "ArtistID", "ArtistID")]
         public override string ArtistName {
             get { return base.ArtistName; }
             set { base.ArtistName = value; }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryTrackInfo.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryTrackInfo.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/LibraryTrackInfo.cs	Sun Jan 20 06:02:09 2008
@@ -89,13 +89,13 @@
             set { album_id = value; }
         }
 
-        [DatabaseVirtualColumn("Name", "CoreArtists", "ArtistID", "ArtistID")]
+        [VirtualDatabaseColumn("Name", "CoreArtists", "ArtistID", "ArtistID")]
         public override string ArtistName {
             get { return base.ArtistName; }
             set { base.ArtistName = value; }
         }
         
-        [DatabaseVirtualColumn("Title", "CoreAlbums", "AlbumID", "AlbumID")]
+        [VirtualDatabaseColumn("Title", "CoreAlbums", "AlbumID", "AlbumID")]
         public override string AlbumTitle {
             get { return base.AlbumTitle; }
             set { base.AlbumTitle = value; }

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs	Sun Jan 20 06:02:09 2008
@@ -27,7 +27,6 @@
 //
 
 using System;
-using System.Collections.Generic;
 using System.Data;
 using System.Reflection;
 using System.Text;
@@ -36,42 +35,46 @@
 {
     internal abstract class AbstractDatabaseColumn
     {
-        private readonly DatabaseColumnBaseAttribute attribute;
+        private readonly AbstractDatabaseColumnAttribute attribute;
         private readonly FieldInfo field_info;
         private readonly PropertyInfo property_info;
         private readonly Type type;
         private readonly string column_type;
         private readonly string name;
         
-        public AbstractDatabaseColumn (FieldInfo field_info, DatabaseColumnBaseAttribute attribute)
+        public AbstractDatabaseColumn (FieldInfo field_info, AbstractDatabaseColumnAttribute attribute)
             : this (attribute, field_info, field_info.FieldType)
         {
             this.field_info = field_info;
         }
         
-        public AbstractDatabaseColumn (PropertyInfo property_info, DatabaseColumnBaseAttribute attribute) :
+        public AbstractDatabaseColumn (PropertyInfo property_info, AbstractDatabaseColumnAttribute attribute) :
             this (attribute, property_info, property_info.PropertyType)
         {
             if (!property_info.CanRead || !property_info.CanWrite) {
-                throw new Exception (String.Format ("{0}: The property {1} must have both a get and a set " +
-                                                  "block in order to be bound to a database column.",
-                                                  property_info.DeclaringType,
-                                                  property_info.Name));
+                throw new Exception (String.Format (
+                    "{0}: The property {1} must have both a get and a set " +
+                    "block in order to be bound to a database column.",
+                    property_info.DeclaringType,
+                    property_info.Name)
+                );
             }
             this.property_info = property_info;
         }
         
-        private AbstractDatabaseColumn (DatabaseColumnBaseAttribute attribute, MemberInfo member_info, Type type)
+        private AbstractDatabaseColumn (AbstractDatabaseColumnAttribute attribute, MemberInfo member_info, Type type)
         {
-            if (type.Equals (typeof (string))) {
+            if (type == typeof (string)) {
                 column_type = "TEXT";
-            } else if (type.Equals (typeof (int)) || type.Equals (typeof (long))) {
+            } else if (type == typeof (int) || type == typeof (long)) {
                 column_type = "INTEGER";
             } else {
-                throw new Exception (String.Format ("{0}.{1}: The type {2} cannot be bound to a database column.",
-                                                  member_info.DeclaringType,
-                                                  member_info.Name,
-                                                  type.FullName));
+                throw new Exception (String.Format (
+                    "{0}.{1}: The type {2} cannot be bound to a database column.",
+                    member_info.DeclaringType,
+                    member_info.Name,
+                    type.Name)
+                );
             }
             this.attribute = attribute;
             this.name = attribute.ColumnName ?? member_info.Name;
@@ -80,18 +83,9 @@
         
         public object GetValue (object target)
         {
-            object result;
-            if (field_info != null) {
-                result = field_info.GetValue (target);
-            } else {
-                result = property_info.GetGetMethod (true).Invoke (target, null);
-            }
-            return GetValue (type, result);
-        }
-        
-        protected virtual object GetValue (Type type, object value)
-        {
-            return value;
+            return field_info != null
+                ? field_info.GetValue (target)
+                : property_info.GetGetMethod (true).Invoke (target, null);
         }
         
         public void SetValue (object target, IDataReader reader, int column)
@@ -103,15 +97,15 @@
             }
         }
         
-        protected virtual object SetValue (Type type, IDataReader reader, int column)
+        private static object SetValue (Type type, IDataReader reader, int column)
         {
             // FIXME should we insist on nullable types?
             object result;
-            if (type.Equals (typeof (string))) {
+            if (type == typeof (string)) {
                 result = !reader.IsDBNull (column)
                     ? String.Intern (reader.GetString (column))
                     : null;
-            } else if (type.Equals (typeof (int))) {
+            } else if (type == typeof (int)) {
                 result = !reader.IsDBNull (column)
                     ? reader.GetInt32 (column)
                     : 0;
@@ -132,7 +126,7 @@
         }
     }
     
-    internal class DatabaseColumn : AbstractDatabaseColumn
+    internal sealed class DatabaseColumn : AbstractDatabaseColumn
     {
         private DatabaseColumnAttribute attribute;
         
@@ -195,17 +189,17 @@
         }
     }
     
-    internal class VirtualDatabaseColumn : AbstractDatabaseColumn
+    internal sealed class VirtualDatabaseColumn : AbstractDatabaseColumn
     {
-        private DatabaseVirtualColumnAttribute attribute;
+        private VirtualDatabaseColumnAttribute attribute;
         
-        public VirtualDatabaseColumn (FieldInfo field_info, DatabaseVirtualColumnAttribute attribute)
+        public VirtualDatabaseColumn (FieldInfo field_info, VirtualDatabaseColumnAttribute attribute)
             : base (field_info, attribute)
         {
             this.attribute = attribute;
         }
         
-        public VirtualDatabaseColumn (PropertyInfo property_info, DatabaseVirtualColumnAttribute attribute)
+        public VirtualDatabaseColumn (PropertyInfo property_info, VirtualDatabaseColumnAttribute attribute)
             : base (property_info, attribute)
         {
             this.attribute = attribute;

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumnAttribute.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumnAttribute.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumnAttribute.cs	Sun Jan 20 06:02:09 2008
@@ -42,15 +42,15 @@
         Unique = 4
     }
     
-    public abstract class DatabaseColumnBaseAttribute : Attribute
+    public abstract class AbstractDatabaseColumnAttribute : Attribute
     {
         private string column_name;
         
-        public DatabaseColumnBaseAttribute ()
+        public AbstractDatabaseColumnAttribute ()
         {
         }
         
-        public DatabaseColumnBaseAttribute (string column_name)
+        public AbstractDatabaseColumnAttribute (string column_name)
         {
             this.column_name = column_name;
         }
@@ -61,7 +61,7 @@
     }
     
     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
-    public sealed class DatabaseColumnAttribute : DatabaseColumnBaseAttribute
+    public sealed class DatabaseColumnAttribute : AbstractDatabaseColumnAttribute
     {
         private DatabaseColumnConstraints contraints;
         private string default_value;
@@ -92,14 +92,14 @@
     }
     
     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
-    public sealed class DatabaseVirtualColumnAttribute : DatabaseColumnBaseAttribute
+    public sealed class VirtualDatabaseColumnAttribute : AbstractDatabaseColumnAttribute
     {
         private string target_table;
         private string target_column;
         private string local_key;
         private string foreign_key;
         
-        public DatabaseVirtualColumnAttribute (string column_name, string target_table, string local_key, string foreign_key)
+        public VirtualDatabaseColumnAttribute (string column_name, string target_table, string local_key, string foreign_key)
             : base (column_name)
         {
             this.target_table = target_table;

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs	Sun Jan 20 06:02:09 2008
@@ -28,6 +28,7 @@
 
 using System;
 using System.IO;
+using System.Collections.Generic;
 using System.Data;
 using Mono.Data.Sqlite;
 
@@ -85,24 +86,43 @@
 
         public bool TableExists (string tableName)
         {
-            IDbCommand command = connection.CreateCommand ();
-            command.CommandText = @"
-                SELECT COUNT(*)
-                    FROM sqlite_master
-                    WHERE Type='table' AND Name=:table_name";
-            
-            IDbDataParameter table_param = command.CreateParameter ();
-            table_param.ParameterName = "table_name";
-            table_param.Value = tableName;
-            
-            command.Parameters.Add (table_param);
-            
-            try {
-                return Convert.ToInt32 (command.ExecuteScalar ()) > 0;
-            } catch (Exception e) {
-                Console.WriteLine ("Caught exception trying to execute {0}", command.CommandText);
-                throw e;
+            return Exists ("table", tableName);
+        }
+        
+        public bool IndexExists (string indexName)
+        {
+            return Exists ("index", indexName);
+        }
+        
+        private bool Exists (string type, string name)
+        {
+            return
+                QueryInt32 (
+                    String.Format (@"
+                        SELECT COUNT(*)
+                            FROM sqlite_master
+                            WHERE Type='{0}' AND Name='{1}'",
+                        type, name)
+                ) > 0;
+        }
+        
+        private static readonly char [] ws_chars = new char [] { ' ', '\t', '\n', '\r' };
+        public Dictionary<string, string> GetSchema (string name)
+        {
+            string sql = QueryString (String.Format (
+                "SELECT sql FROM sqlite_master WHERE Name='{0}'", name));
+            if (String.IsNullOrEmpty (sql)) {
+                throw new Exception (String.Format (
+                    "Cannot get schema for {0} because it does not exist", name));
+            }
+            Dictionary<string, string> schema = new Dictionary<string, string> ();
+            sql = sql.Substring (sql.IndexOf ('(') + 1);
+            foreach (string column_def in sql.Split (',')) {
+                string column_def_t = column_def.Trim ();
+                int ws_index = column_def_t.IndexOfAny (ws_chars);
+                schema.Add (column_def_t.Substring (0, ws_index), null);
             }
+            return schema;
         }
 
         public IDataReader ExecuteReader (SqliteCommand command)
@@ -149,10 +169,35 @@
         {
             return ExecuteScalar (new SqliteCommand (command.ToString ()));
         }
+        
+        public Int32 QueryInt32 (SqliteCommand command)
+        {
+            return Convert.ToInt32 (ExecuteScalar (command));
+        }
+        
+        public Int32 QueryInt32 (HyenaSqliteCommand command)
+        {
+            return QueryInt32 (command.Command);
+        }
 
         public Int32 QueryInt32 (object command)
         {
-            return Convert.ToInt32 (ExecuteScalar (command));
+            return QueryInt32 (new SqliteCommand (command.ToString ()));
+        }
+        
+        public string QueryString (SqliteCommand command)
+        {
+            return Convert.ToString (ExecuteScalar (command));
+        }
+        
+        public string QueryString (HyenaSqliteCommand command)
+        {
+            return QueryString (command.Command);
+        }
+        
+        public string QueryString (object command)
+        {
+            return QueryString (new SqliteCommand (command.ToString ()));
         }
 
         public int Execute (SqliteCommand command)

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelCache.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelCache.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelCache.cs	Sun Jan 20 06:02:09 2008
@@ -45,7 +45,7 @@
         private string reload_sql;
         private int uid;
         private int rows;
-        private bool warm = false;
+        private bool warm;
         private int first_order_id;
 
         public SqliteModelCache (HyenaSqliteConnection connection,
@@ -135,18 +135,14 @@
                 return -1;
             }
             select_single_command.ApplyValues (item_id);
-            using (IDataReader target_reader = connection.ExecuteReader (select_single_command)) {
-                if (!target_reader.Read ()) {
-                    return -1;
-                }
-                if (first_order_id == -1) {
-                    using (IDataReader reader = connection.ExecuteReader (select_first_command)) {
-                        reader.Read ();
-                        first_order_id = reader.GetInt32 (0);
-                    }
-                }
-                return target_reader.GetInt32 (0) - first_order_id;
+            int target_id = connection.QueryInt32 (select_single_command);
+            if (target_id == 0) {
+                return -1;
+            }
+            if (first_order_id == -1) {
+                first_order_id = connection.QueryInt32 (select_first_command);
             }
+            return target_id - first_order_id;
         }
 
         public override int Reload ()
@@ -205,7 +201,7 @@
 
         private void CheckCacheTable ()
         {
-            if (!connection.TableExists(CacheTableName)) {
+            if (!connection.TableExists (CacheTableName)) {
                 connection.Execute (String.Format (@"
                     CREATE TABLE {0} (
                         OrderID INTEGER PRIMARY KEY,
@@ -214,7 +210,7 @@
                 ));
             }
 
-            if (!connection.TableExists(CacheModelsTableName)) {
+            if (!connection.TableExists (CacheModelsTableName)) {
                 connection.Execute (String.Format (
                     "CREATE TABLE {0} (CacheID INTEGER PRIMARY KEY, ModelID TEXT UNIQUE)",
                     CacheModelsTableName

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs	Sun Jan 20 06:02:09 2008
@@ -99,95 +99,85 @@
             CheckTable ();
         }
         
-        private void CheckTable ()
+        protected virtual void CheckVersion ()
         {
-            Console.WriteLine ("In {0} checking for table {1}", this, TableName);
-            using (IDataReader reader = connection.ExecuteReader (GetSchemaSql (TableName))) {
-                if (reader.Read ()) {
-                    Dictionary<string, string> schema = GetSchema (reader);
-                    foreach (DatabaseColumn column in columns) {
-                        if (!schema.ContainsKey (column.Name)) {
-                            connection.Execute (String.Format (
-                                "ALTER TABLE {0} ADD {1}", TableName, column.Schema));
-                        }
-                        if (column.Index != null) {
-                            using (IDataReader index_reader = connection.ExecuteReader (GetSchemaSql (column.Index))) {
-                                if (!index_reader.Read ()) {
-                                    connection.Execute (String.Format (
-                                        "CREATE INDEX {0} ON {1}({2})", column.Index, TableName, column.Name));
-                                }
-                            }
+            if (connection.TableExists (HyenaTableName)) {
+                using (IDataReader reader = connection.ExecuteReader (SelectVersionSql (TableName))) {
+                    if (reader.Read ()) {
+                        int table_version = reader.GetInt32 (0);
+                        if (table_version < ModelVersion) {
+                            MigrateTable (table_version);
+                            UpdateVersion (TableName, ModelVersion);
                         }
+                    } else {
+                        InsertVersion (TableName, ModelVersion);
                     }
-                } else {
-                    CreateTable ();
+                }
+                int db_version = connection.QueryInt32 (SelectVersionSql (HYENA_DATABASE_NAME));
+                if (db_version < DatabaseVersion) {
+                    MigrateDatabase (db_version);
+                    UpdateVersion (HYENA_DATABASE_NAME, DatabaseVersion);
                 }
             }
+            else {
+                connection.Execute (String.Format (
+                    @"CREATE TABLE {0} (
+                        id INTEGER PRIMARY KEY,
+                        name TEXT UNIQUE,
+                        version INTEGER)",
+                    HyenaTableName)
+                );
+                
+                InsertVersion (HYENA_DATABASE_NAME, DatabaseVersion);
+                InsertVersion (TableName, ModelVersion);
+            }
         }
         
-        private static Dictionary<string, string> GetSchema (IDataReader reader)
+        private string SelectVersionSql (string name)
         {
-            Dictionary<string, string> schema = new Dictionary<string, string> ();
-            string sql = reader.GetString (0);
-            sql = sql.Substring (sql.IndexOf ('(') + 1);
-            foreach (string column_def in sql.Split (',')) {
-                string column_def_t = column_def.Trim ();
-                int ws_index = column_def_t.IndexOfAny (new char [] { ' ', '\t', '\n', '\r' });
-                schema.Add (column_def_t.Substring (0, ws_index), null);
-            }
-            return schema;
+            return String.Format (
+                "SELECT version FROM {0} WHERE name='{1}'",
+                HyenaTableName, name);
         }
         
-        protected virtual void CheckVersion ()
+        private void UpdateVersion (string name, int version)
         {
-            using (IDataReader reader = connection.ExecuteReader (GetSchemaSql (HyenaTableName))) {
-                if (reader.Read ()) {
-                    using (IDataReader table_reader = connection.ExecuteReader (String.Format (
-                        "SELECT version FROM {0} WHERE name = '{1}'", HyenaTableName, TableName))) {
-                        if (table_reader.Read ()) {
-                            int version = table_reader.GetInt32 (0);
-                            if (version < ModelVersion) {
-                                MigrateTable (version);
-                                connection.Execute (String.Format( 
-                                    "UPDATE {0} SET version = {1} WHERE name = '{3}'",
-                                    HyenaTableName, ModelVersion, TableName));
-                            }
-                        } else {
-                            connection.Execute (String.Format (
-                                "INSERT INTO {0} (name, version) VALUES ('{1}', {2})",
-                                HyenaTableName, TableName, ModelVersion));
-                        }
+            connection.Execute (String.Format (
+                "UPDATE {0} SET version={1} WHERE name='{2}'",
+                HyenaTableName, version, name));
+        }
+        
+        private void InsertVersion (string name, int version)
+        {
+            connection.Execute (String.Format (
+                "INSERT INTO {0} (name, version) VALUES ('{1}', {2})",
+                HyenaTableName, name, version));
+        }
+        
+        private void CheckTable ()
+        {
+            //Console.WriteLine ("In {0} checking for table {1}", this, TableName);
+            Dictionary<string, string> schema = connection.GetSchema (TableName);
+            if (schema.Count > 0) {
+                foreach (DatabaseColumn column in columns) {
+                    if (!schema.ContainsKey (column.Name)) {
+                        connection.Execute (String.Format (
+                            "ALTER TABLE {0} ADD {1}",
+                            TableName, column.Schema)
+                        );
                     }
-                    using (IDataReader db_reader = connection.ExecuteReader (String.Format (
-                        "SELECT version FROM {0} WHERE name = '{1}'", HyenaTableName, HYENA_DATABASE_NAME))) {
-                        db_reader.Read ();
-                        int version = db_reader.GetInt32 (0);
-                        if (version < DatabaseVersion) {
-                            MigrateDatabase (version);
-                            connection.Execute (String.Format (
-                                "UPDATE {0} SET version = {1} WHERE name = '{2}'",
-                                HyenaTableName, ModelVersion, HYENA_DATABASE_NAME));
-                        }
+                    if (column.Index != null && !connection.IndexExists (column.Index)) {
+                        connection.Execute (String.Format (
+                            "CREATE INDEX {0} ON {1}({2})",
+                            column.Index, TableName, column.Name)
+                        );
                     }
                 }
-                else {
-                    connection.Execute (String.Format (
-                        "CREATE TABLE {0} (id INTEGER PRIMARY KEY, name TEXT UNIQUE, version INTEGER)", HyenaTableName));
-                    connection.Execute (String.Format (
-                        "INSERT INTO {0} (name, version) VALUES ('{1}', {2})",
-                        HyenaTableName, HYENA_DATABASE_NAME, DatabaseVersion));
-                    connection.Execute (String.Format (
-                        "INSERT INTO {0} (name, version) VALUES ('{1}', {2})",
-                        HyenaTableName, TableName, ModelVersion));
-                }
+            } else {
+                CreateTable ();
             }
         }
         
-        protected static string GetSchemaSql (string TableName)
-        {
-            return String.Format ("SELECT sql FROM sqlite_master WHERE name = '{0}'", TableName);
-        }
-        
         private void AddColumn (MemberInfo member, Attribute attribute)
         {
             DatabaseColumnAttribute column = attribute as DatabaseColumnAttribute;
@@ -198,10 +188,16 @@
                 
                 foreach (DatabaseColumn col in columns) {
                     if (col.Name == c.Name) {
-                        throw new Exception (String.Format ("{0} has multiple columns named {1}", TableName, c.Name));
+                        throw new Exception (String.Format (
+                            "{0} has multiple columns named {1}",
+                             TableName, c.Name)
+                        );
                     }
                     if (col.Index != null && col.Index == c.Index) {
-                        throw new Exception (String.Format ("{0} has multiple indecies named {1}", TableName, c.Name));
+                        throw new Exception (String.Format (
+                            "{0} has multiple indecies named {1}",
+                            TableName, c.Name)
+                        );
                     }
                 }
                 
@@ -209,12 +205,14 @@
                 
                 if ((c.Constraints & DatabaseColumnConstraints.PrimaryKey) > 0) {
                     if (key != null) {
-                        throw new Exception (String.Format ("Multiple primary keys in the {0} table", TableName));
+                        throw new Exception (String.Format (
+                            "Multiple primary keys in the {0} table", TableName)
+                        );
                     }
                     key = c;
                 }
             }
-            DatabaseVirtualColumnAttribute virtual_column = attribute as DatabaseVirtualColumnAttribute;
+            VirtualDatabaseColumnAttribute virtual_column = attribute as VirtualDatabaseColumnAttribute;
             if (virtual_column != null) {
                 if (member is FieldInfo) {
                     virtual_columns.Add (new VirtualDatabaseColumn ((FieldInfo) member, virtual_column));
@@ -230,7 +228,9 @@
             foreach (DatabaseColumn column in columns) {
                 if (column.Index != null) {
                     connection.Execute (String.Format (
-                        "CREATE INDEX {0} ON {1}({2})", column.Index, TableName, column.Name));
+                        "CREATE INDEX {0} ON {1}({2})",
+                        column.Index, TableName, column.Name)
+                    );
                 }
             }
         }
@@ -372,8 +372,9 @@
                     }
 
                     insert_command = new HyenaSqliteCommand (String.Format (
-                            "INSERT INTO {0} ({1}) VALUES ({2})",
-                            TableName, cols.ToString (), vals.ToString ()), count);
+                        "INSERT INTO {0} ({1}) VALUES ({2})",
+                        TableName, cols.ToString (), vals.ToString ()), count
+                    );
                 }
                 return insert_command;
             }
@@ -411,9 +412,14 @@
         protected virtual HyenaSqliteCommand SelectCommand {
             get {
                 if (select_command == null) {
-                    select_command = new HyenaSqliteCommand (Where.Length > 0
-                        ? String.Format ("SELECT {0} FROM {1} WHERE {2}", Select, From, Where)
-                        : String.Format ("SELECT {0} FROM {1}", Select, From));
+                    select_command = new HyenaSqliteCommand (
+                        String.Format (
+                            "SELECT {0} FROM {1}{2}{3}",
+                            Select, From,
+                            (String.IsNullOrEmpty (Where) ? String.Empty : " WHERE "),
+                            Where
+                        )
+                    );
                 }
                 return select_command;
             }
@@ -422,9 +428,14 @@
         protected virtual HyenaSqliteCommand SelectRangeCommand {
             get {
                 if (select_range_command == null) {
-                    select_range_command = new HyenaSqliteCommand (Where.Length > 0
-                        ? String.Format ("SELECT {0} FROM {1} WHERE {2} LIMIT ?, ?", Select, From, Where)
-                        : String.Format ("SELECT {0} FROM {1} LIMIT ?, ?", Select, From), 2);
+                    select_command = new HyenaSqliteCommand (
+                        String.Format (
+                            "SELECT {0} FROM {1}{2}{3} LIMIT ?, ?",
+                            Select, From,
+                            (String.IsNullOrEmpty (Where) ? String.Empty : " WHERE "),
+                            Where
+                        ), 2
+                    );
                 }
                 return select_range_command;
             }
@@ -433,9 +444,14 @@
         protected virtual HyenaSqliteCommand SelectSingleCommand {
             get {
                 if (select_single_command == null) {
-                    select_single_command = new HyenaSqliteCommand (Where.Length > 0
-                        ? String.Format ("SELECT {0} FROM {1} WHERE {2} AND {3} = ?", Select, From, Where, PrimaryKey)
-                        : String.Format ("SELECT {0} FROM {1} WHERE {2} = ?", Select, From, PrimaryKey), 1);
+                    select_single_command = new HyenaSqliteCommand (
+                        String.Format (
+                            "SELECT {0} FROM {1} WHERE {2}{3}{4} = ?",
+                            Select, From, Where,
+                            (String.IsNullOrEmpty (Where) ? String.Empty : " AND "),
+                            PrimaryKey
+                        ), 1
+                    );
                 }
                 return select_single_command;
             }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]