require 'catori' require 'dbi' class String def crearSha() Digest::SHA1.hexdigest(self.downcase.gsub(/\W/,""))[0,32] end end module Catori class Db private_class_method :new @@db=nil def self.conectar if @@db.nil? data=self.data_conexion @@db=DBI.connect('DBI:Mysql:'+data['database'], data['user'], data['password']) end @@db end def self.data_conexion if(File.exists?(Catori::CONFIG_FILE)) data={} File.open( Catori::CONFIG_FILE ) { |yf| data=YAML::load( yf ) } else raise "Install the application using catori --install" end data end def self.llenarCancion(oInfo) sSong=oInfo.title if(@@db.select_one("select COUNT(*) from song WHERE song_id=?",sSong.crearSha).to_s=="0") #puts "Agregar cancion #{sSong}" sql="INSERT INTO song (song_id,song_name) VALUES (?,?)" @@db.execute(sql,sSong.crearSha,sSong) end sAlbum=oInfo.album sArtist=oInfo.artist if(@@db.select_one("select COUNT(*) from album_song WHERE artist_id=? AND album_id=? and song_id=?",sArtist.crearSha,sAlbum.crearSha,sSong.crearSha).to_s=="0") #puts "Agregar cancion en album #{sSong}" sql="INSERT INTO album_song (artist_id,album_id,song_id,as_track) VALUES (?,?,?,?)" @@db.execute(sql, sArtist.crearSha, sAlbum.crearSha, sSong.crearSha, oInfo.tracknumber) elsif(@@db.select_one("SELECT COUNT(*) from album_song WHERE as_track!=? and artist_id=? and album_id=? and song_id=?",oInfo.tracknumber, sArtist.crearSha, sAlbum.crearSha, sSong.crearSha)) sql="UPDATE album_song SET as_track=? WHERE artist_id=? and album_id=? and song_id=?" @@db.execute(sql,oInfo.tracknumber,sArtist.crearSha, sAlbum.crearSha, sSong.crearSha) end end def self.llenarArtista(oInfo) sArtist=oInfo.artist if(@@db.select_one("select COUNT(*) from artist WHERE artist_id=?",sArtist.crearSha).to_s=="0") #puts "Agregar artista #{sArtist}" sql="INSERT INTO artist (artist_id,artist_name) VALUES (?,?)" @@db.execute(sql,sArtist.crearSha,sArtist) end end def self.llenarAlbum(oInfo) sAlbum=oInfo.album sFecha=oInfo.year sArtist=oInfo.artist if(@@db.select_one("select COUNT(*) from album WHERE artist_id=? AND album_id=?",sArtist.crearSha,sAlbum.crearSha).to_s=="0") #puts "Agregar album #{sAlbum}" sql="INSERT INTO album (artist_id,album_id,album_name,album_year) VALUES (?,?,?,?)" @@db.execute(sql,sArtist.crearSha,sAlbum.crearSha,sAlbum,sFecha) end end def self.actualizarAlbumFecha(oInfo) sAlbum=oInfo.album sFecha=oInfo.year sArtist=oInfo.artist sql="UPDATE album SET album_year=? WHERE artist_id=? and album_id=?" @@db.execute(sql,sFecha,sArtist.crearSha,sAlbum.crearSha) end def self.actualizarArchivo(oInfo) sPathId=oInfo.sFile.crearSha(); sAlbum=oInfo.album.crearSha sArtist=oInfo.artist.crearSha sSong=oInfo.title.crearSha raise "Error con el id de archivo" if (@@db.select_one("SELECT COUNT(*) from file WHERE cd_id=? AND file_id=?",oInfo.cd,sPathId).to_s=="0") sql="UPDATE file SET artist_id=?,album_id=?,song_id=? WHERE cd_id=? AND file_id=?" @@db.execute(sql,sArtist,sAlbum,sSong,oInfo.cd,sPathId) end end end