示例#1
0
        public static string RetrieveNameById(int id)
        {
            using (SqliteConnection db = OnelyDB.Open())
            {
                SqliteCommand command = new SqliteCommand
                {
                    Connection  = db,
                    CommandText = "SELECT name FROM playlists WHERE id=@ID"
                };
                command.Parameters.AddWithValue("@ID", id);
                var res = command.ExecuteReader();

                if (!res.HasRows)
                {
                    OnelyDB.Close(db);
                    return(null);
                }

                var name = String.Empty;
                while (res.Read())
                {
                    name = res.GetString(0);
                }
                return(name);
            }
        }
示例#2
0
        public static int Save(Playlist playlist, string name)
        {
            using (SqliteConnection db = OnelyDB.Open())
            {
                SqliteCommand command;
                if (playlist.Name == null)
                {
                    // Is name taken?
                    command = new SqliteCommand
                    {
                        Connection  = db,
                        CommandText = "SELECT id FROM playlists WHERE name=@Name"
                    };
                    command.Parameters.AddWithValue("@Name", name);
                    var res = OnelyDB.ExecuteReader(command);
                    if (res.HasRows)
                    {
                        return(-1);
                    }

                    command = new SqliteCommand
                    {
                        Connection  = db,
                        CommandText = "INSERT INTO playlists(name) VALUES (@Name)"
                    };
                    command.Parameters.AddWithValue("@Name", name);
                    OnelyDB.ExecuteReader(command);

                    playlist.Id   = OnelyDB.GetLastInsertId(db);
                    playlist.Name = name;
                }
                else
                {
                    if (name != playlist.Name)
                    {
                        command = new SqliteCommand
                        {
                            Connection  = db,
                            CommandText = "UPDATE playlists SET name=@Name WHERE id=@ID"
                        };
                        command.Parameters.AddWithValue("@Name", name);
                        command.Parameters.AddWithValue("@ID", playlist.Id);
                        OnelyDB.ExecuteReader(command);
                        playlist.Name = name;
                    }

                    PlaylistItemStatic.DeleteBasedOnPlaylistId(playlist.Id, db);
                }
                int count = 0;
                foreach (var item in playlist.Items)
                {
                    PlaylistItemStatic.Save(item, playlist.Id, count, db);
                    count++;
                }
                OnelyDB.Close(db);
                return(playlist.Id);
            }
        }
示例#3
0
        public static async Task <List <PlaylistItem> > RetrievePlaylistItemsByPlaylistId(int id)
        {
            using (SqliteConnection db = OnelyDB.Open())
            {
                SqliteCommand command = new SqliteCommand
                {
                    Connection  = db,
                    CommandText = "SELECT audio_path, cover_path FROM playlist_items WHERE playlist_id=@ID ORDER BY sort_order ASC"
                };
                command.Parameters.AddWithValue("@ID", id);
                var res = OnelyDB.ExecuteReader(command);

                if (!res.HasRows)
                {
                    return(null);
                }

                List <PlaylistItem> items  = new List <PlaylistItem>();
                List <AlbumCover>   covers = new List <AlbumCover>();

                while (res.Read())
                {
                    var item = await LoadFromPath(res.GetString(0));

                    if (item != null)
                    {
                        items.Add(item);
                        var coverPath = res.GetString(1);
                        if ((item.Cover == null) && (!String.IsNullOrEmpty(coverPath)))
                        {
                            AlbumCover cover = GetExistingAlbumCover(coverPath, covers);
                            if (cover == null)
                            {
                                cover = await AlbumCover.FromPath(coverPath);

                                if (cover != null)
                                {
                                    covers.Add(cover);
                                }
                            }
                            if (cover != null)
                            {
                                item.MainCover = cover;
                            }
                        }
                    }
                }
                covers.Clear();
                OnelyDB.Close(db);
                return(items);
            }
        }
示例#4
0
        public static void DeletePlaylistById(int id)
        {
            using (SqliteConnection db = OnelyDB.Open())
            {
                PlaylistItemStatic.DeleteBasedOnPlaylistId(id, db);
                SqliteCommand command = new SqliteCommand
                {
                    Connection  = db,
                    CommandText = "DELETE FROM playlists WHERE id=@ID"
                };
                command.Parameters.AddWithValue("@ID", id);
                OnelyDB.ExecuteReader(command);

                OnelyDB.Close(db);
            }
        }
示例#5
0
 public static int GetDefaultPlaylistId()
 {
     using (SqliteConnection db = OnelyDB.Open())
     {
         int           id      = -1;
         SqliteCommand command = new SqliteCommand
         {
             Connection  = db,
             CommandText = "SELECT id FROM playlists WHERE name=@Default"
         };
         command.Parameters.AddWithValue("@Default", DefaultDBName);
         var res = OnelyDB.ExecuteReader(command);
         while (res.Read())
         {
             id = res.GetInt32(0);
         }
         return(id);
     }
 }
示例#6
0
        public static PlaylistReferenceCollection <PlaylistReference> GetSavedPlaylists()
        {
            using (SqliteConnection db = OnelyDB.Open())
            {
                PlaylistReferenceCollection <PlaylistReference> playlists = new PlaylistReferenceCollection <PlaylistReference>();
                SqliteCommand command = new SqliteCommand
                {
                    Connection  = db,
                    CommandText = "SELECT id, name FROM playlists WHERE name != @Default ORDER BY name"
                };
                command.Parameters.AddWithValue("@Default", DefaultDBName);
                var res = OnelyDB.ExecuteReader(command);

                while (res.Read())
                {
                    playlists.Add(new PlaylistReference(res.GetInt32(0), res.GetString(1)));
                }

                db.Close();

                return(playlists);
            }
        }