Class: Sh::Database
Instance Method Summary collapse
- #albums(params = {}) ⇒ Object
- #artists(params = {}) ⇒ Object
- #clean ⇒ Object
- #contains?(path) ⇒ Boolean
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #save_song(song) ⇒ Object
- #songs(params = {}) ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sh_database.rb', line 9 def initialize @db = Sequel.sqlite(Sh::Global::PATHS[:db_file]) @db.create_table :artists do primary_key :id String :mbid String :name String :image_path String :info end unless @db.table_exists? :artists @db.create_table :albums do primary_key :id String :mbid String :title String :image_path String :info String :date end unless @db.table_exists? :albums @db.create_table :songs do primary_key :id String :mbid String :path String :title String :lyrics String :image_path String :track_num foreign_key :album_id, :albums foreign_key :artist_id, :artists end unless @db.table_exists? :songs end |
Instance Method Details
#albums(params = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/sh_database.rb', line 136 def albums(params={}) albums = [] params ||= {} rows = @db[:albums] rows = rows.filter(params) unless params == {} rows.each do |hash| albums << hash_to_album(hash) end return albums end |
#artists(params = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/sh_database.rb', line 125 def artists(params={}) artists = [] params ||= {} rows = @db[:artists] rows = rows.filter(params) unless params == {} rows.each do |hash| artists << hash_to_artist(hash) end return artists end |
#clean ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/sh_database.rb', line 162 def clean @db[:songs].each do |hash| path = hash[:path] unless File.exists? path Sh::Log.debug "Removing #{path} from database" @db[:songs].filter(:path => path).delete end end end |
#contains?(path) ⇒ Boolean
158 159 160 |
# File 'lib/sh_database.rb', line 158 def contains? path @db[:songs][:path => path] end |
#save_song(song) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sh_database.rb', line 42 def save_song song begin # Get artist info artist = song.artist artist_hash = { :mbid => artist.mbid, :name => artist.name, :image_path => artist.image_path, :info => artist.info } # Insert/update artist in database artist_id = nil row_artist = @db[:artists][:name => artist.name] if row_artist artist_hash.each do |k, v| if row_artist[k] != v @db[:artists].filter(:name => artist.name).update(k => v) end end artist_id = row_artist[:id] else artist_id = @db[:artists].insert(artist_hash) end artist.db_id = artist_id # Get album info album = song.album album_hash = { :mbid => album.mbid, :title => album.title, :image_path => album.image_path, :date => album.date, :info => album.info, } # Insert/update artist in database album_id = nil row_album = @db[:albums][:title => album.title] if row_album album_hash.each do |k, v| if row_album[k] != v @db[:albums].filter(:title => album.title).update(k => v) end end album_id = row_album[:id] else album_id = @db[:albums].insert(album_hash) end album.db_id = album_id song_hash = { :path => song.path, :mbid => song.mbid, :title => song.title, :lyrics => song.lyrics, :track_num => song.track_num, :album_id => album_id, :artist_id => artist_id } song_id = nil row_song = @db[:songs][:path => song.path] if row_song song_hash.each do |k, v| if row_song[k] != v @db[:songs].filter(:path => song.path).update(k => v) end end song_id = row_song[:id] else song_id = @db[:songs].insert(song_hash) row_song = @db[:songs][:id => song_id] end song.db_id = song_id return row_song rescue Sh::Log.warning "Couldn't save #{song.path} to database", $! return end end |
#songs(params = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sh_database.rb', line 147 def songs(params={}) songs = [] params ||= {} rows = @db[:songs] rows = rows.filter(params) unless params == {} rows.each do |hash| songs << hash_to_song(hash) end return songs end |