banshee r3700 - in trunk/banshee: . src/Core/Banshee.Services/Banshee.Collection.Database src/Core/Banshee.Services/Banshee.Library src/Libraries/Hyena/Hyena.Data.Sqlite tests/Hyena



Author: gburt
Date: Mon Apr  7 03:55:06 2008
New Revision: 3700
URL: http://svn.gnome.org/viewvc/banshee?rev=3700&view=rev

Log:
2008-04-06  Gabriel Burt  <gabriel burt gmail com>

	* src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseAlbumInfo.cs:
	* src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseArtistInfo.cs:
	Make sure to load all columns in FindOrCreate methods.

	* src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseImportManager.cs:
	* src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs:
	Change PrimarySourceIds to a int [].

	* src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackInfo.cs:
	Fix bug in ContainsUri method.

	* src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs: Add support
	for Arrays as query values (for use in "IN (?)" conditions).

	* tests/Hyena/SqliteCommandTests.cs: Test array support.


Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseAlbumInfo.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseArtistInfo.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseImportManager.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackInfo.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs
   trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
   trunk/banshee/tests/Hyena/SqliteCommandTests.cs

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseAlbumInfo.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseAlbumInfo.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseAlbumInfo.cs	Mon Apr  7 03:55:06 2008
@@ -48,9 +48,11 @@
             get { return provider; }
         }
 
-        private static HyenaSqliteCommand select_command = new HyenaSqliteCommand (
-            "SELECT AlbumID, Title FROM CoreAlbums WHERE ArtistID = ? AND Title = ?"
-        );
+        private static HyenaSqliteCommand select_command = new HyenaSqliteCommand (String.Format (
+            "SELECT {0} FROM {1} WHERE {2} AND CoreAlbums.ArtistID = ? AND CoreAlbums.Title = ?",
+            provider.Select, provider.From,
+            (String.IsNullOrEmpty (provider.Where) ? "1=1" : provider.Where)
+        ));
 
         private enum Column : int {
             AlbumID,
@@ -81,7 +83,7 @@
 
             using (IDataReader reader = ServiceManager.DbConnection.Query (select_command, artist.DbId, album.Title)) {
                 if (reader.Read ()) {
-                    last_album = new DatabaseAlbumInfo (reader);
+                    last_album = provider.Load (reader, 0);
                     last_album.ArtistId = artist.DbId;
                     last_album.ArtistName = artist.Name;
                 } else {
@@ -114,17 +116,6 @@
         {
         }
 
-        protected DatabaseAlbumInfo (IDataReader reader) : base (null)
-        {
-            LoadFromReader (reader);
-        }
-
-        private void LoadFromReader (IDataReader reader)
-        {
-            dbid = Convert.ToInt32 (reader[(int) Column.AlbumID]);
-            Title = reader[(int) Column.Title] as string;
-        }
-
         public void Save ()
         {
             Provider.Save (this);

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseArtistInfo.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseArtistInfo.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseArtistInfo.cs	Mon Apr  7 03:55:06 2008
@@ -48,9 +48,11 @@
             get { return provider; }
         }
 
-        private static HyenaSqliteCommand select_command = new HyenaSqliteCommand (
-            "SELECT ArtistID, Name FROM CoreArtists WHERE Name = ?"
-        );
+        private static HyenaSqliteCommand select_command = new HyenaSqliteCommand (String.Format (
+            "SELECT {0} FROM {1} WHERE {2} AND CoreArtists.Name = ?",
+            provider.Select, provider.From,
+            (String.IsNullOrEmpty (provider.Where) ? "1=1" : provider.Where)
+        ));
 
         private enum Column : int {
             ArtistID,
@@ -79,7 +81,7 @@
 
             using (IDataReader reader = ServiceManager.DbConnection.Query (select_command, artist.Name)) {
                 if (reader.Read ()) {
-                    last_artist = new DatabaseArtistInfo (reader);
+                    last_artist = provider.Load (reader, 0);
                 } else {
                     last_artist = artist;
                     last_artist.Save ();
@@ -106,22 +108,11 @@
         {
         }
 
-        protected DatabaseArtistInfo (IDataReader reader) : base (null)
-        {
-            LoadFromReader (reader);
-        }
-
         public void Save ()
         {
             Provider.Save (this);
         }
 
-        private void LoadFromReader (IDataReader reader)
-        {
-            dbid = Convert.ToInt32 (reader[(int)Column.ArtistID]);
-            Name = reader[(int)Column.Name] as string;
-        }
-
         [DatabaseColumn("ArtistID", Constraints = DatabaseColumnConstraints.PrimaryKey)]
         private int dbid;
         public int DbId {

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseImportManager.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseImportManager.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseImportManager.cs	Mon Apr  7 03:55:06 2008
@@ -93,15 +93,15 @@
         protected bool can_copy_to_library = false;
         private Dictionary<int, int> counts;
         private ErrorSource error_source;
-        protected string primary_source_ids;
+        protected int [] primary_source_ids;
         protected string base_directory;
     
         public DatabaseImportManager (PrimarySource psource) :
-            this (psource.ErrorSource, delegate { return psource; }, psource.DbId.ToString (), psource.BaseDirectory)
+            this (psource.ErrorSource, delegate { return psource; }, new int [] {psource.DbId}, psource.BaseDirectory)
         {
         }
 
-        public DatabaseImportManager (ErrorSource error_source, TrackPrimarySourceChooser chooser, string primarySourceIds, string baseDirectory) : this (chooser)
+        public DatabaseImportManager (ErrorSource error_source, TrackPrimarySourceChooser chooser, int [] primarySourceIds, string baseDirectory) : this (chooser)
         {
             this.error_source = error_source;
             primary_source_ids = primarySourceIds;
@@ -118,7 +118,7 @@
             get { return error_source; }
         }
 
-        protected virtual string PrimarySourceIds {
+        protected virtual int [] PrimarySourceIds {
             get { return primary_source_ids; }
         }
 

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackInfo.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackInfo.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackInfo.cs	Mon Apr  7 03:55:06 2008
@@ -448,7 +448,7 @@
             "SELECT COUNT(*) FROM CoreTracks WHERE PrimarySourceId IN (?) AND (Uri = ? OR Uri = ?)"
         );
         
-        public static bool ContainsUri (SafeUri uri, string relative_path, string primary_sources)
+        public static bool ContainsUri (SafeUri uri, string relative_path, int [] primary_sources)
         {
             return ServiceManager.DbConnection.Query<int> (check_command.ApplyValues (primary_sources, relative_path, uri.AbsoluteUri)) > 0;
         }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs	Mon Apr  7 03:55:06 2008
@@ -56,10 +56,12 @@
             get { return ServiceManager.SourceManager.MusicLibrary.ErrorSource; }
         }
 
-        protected override string PrimarySourceIds {
+        protected override int [] PrimarySourceIds {
             get {
                 if (primary_source_ids == null) {
-                    primary_source_ids = String.Format ("{0}, {1}", ServiceManager.SourceManager.VideoLibrary.DbId, ServiceManager.SourceManager.MusicLibrary.DbId);
+                    primary_source_ids = new int [] {
+                        ServiceManager.SourceManager.VideoLibrary.DbId, ServiceManager.SourceManager.MusicLibrary.DbId
+                    };
                 }
                 return primary_source_ids;
             }

Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs	Mon Apr  7 03:55:06 2008
@@ -156,13 +156,7 @@
 
             // Transform values as necessary - not needed for numerical types
             for (int i = 0; i < parameter_count; i++) {
-                if (param_values[i] is string) {
-                    param_values[i] = String.Format ("'{0}'", (param_values[i] as string).Replace ("'", "''"));
-                } else if (param_values[i] is DateTime) {
-                    param_values[i] = DateTimeUtil.FromDateTime ((DateTime) param_values[i]);
-                } else if (param_values[i] == null) {
-                    param_values[i] = "NULL";
-                }
+                param_values[i] = SqlifyObject (param_values[i]);
             }
 
             current_values = param_values;
@@ -170,6 +164,31 @@
             return this;
         }
 
+        protected static object SqlifyObject (object o)
+        {
+            if (o is string) {
+                return String.Format ("'{0}'", (o as string).Replace ("'", "''"));
+            } else if (o is DateTime) {
+                return DateTimeUtil.FromDateTime ((DateTime) o);
+            } else if (o == null) {
+                return "NULL";
+            } else if (o is Array) {
+                StringBuilder sb = new StringBuilder ();
+                bool first = true;
+                foreach (object i in (o as Array)) {
+                    if (!first)
+                        sb.Append (",");
+                    else
+                        first = false;
+
+                    sb.Append (SqlifyObject (i));
+                }
+                return sb.ToString ();
+            } else {
+                return o;
+            }
+        }
+
         private string CurrentSqlText {
             get {
                 if (command_format == null) {

Modified: trunk/banshee/tests/Hyena/SqliteCommandTests.cs
==============================================================================
--- trunk/banshee/tests/Hyena/SqliteCommandTests.cs	(original)
+++ trunk/banshee/tests/Hyena/SqliteCommandTests.cs	Mon Apr  7 03:55:06 2008
@@ -54,6 +54,10 @@
 
         HyenaSqliteCommand cmd2 = new HyenaSqliteCommand ("select foo from bar where baz = ?, bar = ?, boo = ?");
         Assert.AreEqual ("select foo from bar where baz = NULL, bar = NULL, boo = 22", GetGeneratedSql (cmd2.ApplyValues (null, null, 22)));
+
+        HyenaSqliteCommand cmd3 = new HyenaSqliteCommand ("select foo from bar where id in (?) and foo not in (?)");
+        Assert.AreEqual ("select foo from bar where id in (1,2,4) and foo not in ('foo','baz')",
+                GetGeneratedSql (cmd3.ApplyValues (new int [] {1, 2, 4}, new string [] {"foo", "baz"})));
     }
 
     static PropertyInfo tf = typeof(HyenaSqliteCommand).GetProperty ("CurrentSqlText", BindingFlags.Instance | BindingFlags.NonPublic);



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