Class: ItunesController::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/itunesController/database/database.rb

Constant Summary collapse

PARAM_KEY_EXPECTED_TRACKS =
1
PARAM_KEY_TRACK_COUNT =
2

Instance Method Summary collapse

Constructor Details

#initialize(controller, backend) ⇒ Database

The constructor



33
34
35
36
37
# File 'lib/itunesController/database/database.rb', line 33

def initialize(controller,backend)
    @controller = controller
    @backend = backend            
    createTables()
end

Instance Method Details

#addDeadTrack(track) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/itunesController/database/database.rb', line 39

def addDeadTrack(track)
    stmt = @backend.prepare("insert into dead_tracks(databaseId,location,name) values(?,?,?)")
    id=track.databaseId.to_i
    title=track.title.to_s
    loc = nil
    if (track.location!=nil)
        loc = track.location.to_s
    end
    @backend.executeStatement(stmt,id,loc,title)            
end

#addTrack(track) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/itunesController/database/database.rb', line 50

def addTrack(track)
    stmt = @backend.prepare("insert into tracks(databaseId,location,name) values(?,?,?)")
    id=track.databaseId.to_i
    title=track.title.to_s
    loc=track.location.to_s
    #ItunesController::ItunesControllerLogging::debug("Adding track to database with id=#{id}, title='#{title}' and location='#{loc}'")
    begin  
        @backend.executeStatement(stmt,id,loc,title)
    rescue ItunesController::DatabaseConstraintException
        stmt2 = @backend.prepare("select * from tracks where location = ?")
        rows=@backend.executeStatement(stmt2,loc) 
        if (rows.next!=nil)
            ItunesController::ItunesControllerLogging::warn("Duplicate track reference detected with databaseId #{id}, title '#{title}' and location '#{loc}'")
            stmt3 = @backend.prepare("insert into dupe_tracks(databaseId,location,name) values(?,?,?)")
            @backend.executeStatement(stmt3,id,loc,title)
        else
            ItunesController::ItunesControllerLogging::warn("Unable to add track to database with #{id}, title '#{title}' and location '#{loc}'")
        end
    end
end

#closeObject



136
137
138
# File 'lib/itunesController/database/database.rb', line 136

def close()
    @backend.close()
end

#getDeadTracksObject



116
117
118
119
120
121
122
123
124
# File 'lib/itunesController/database/database.rb', line 116

def getDeadTracks()
    result=[]
    stmt=@backend.prepare("select databaseId,location,name from dead_tracks")
    rows = @backend.executeStatement(stmt)
    while ((row = rows.next)!=nil)
        result.push(ItunesController::Track.new(row[1],row[0].to_i,row[2]))
    end
    return result
end

#getParam(key, default) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/itunesController/database/database.rb', line 95

def getParam(key,default)
    stmt=@backend.prepare("select value from params where key = ?")
    rows = @backend.executeStatement(stmt,key)
    row=rows.next
    if (row!=nil)
        return row[0]
    end

    return default
end

#getTrack(path) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/itunesController/database/database.rb', line 106

def getTrack(path)
    stmt=@backend.prepare("select databaseId,location,name from tracks where location=?")
    rows = @backend.executeStatement(stmt,path)
    row=rows.next
    if (row!=nil)
        return ItunesController::Track.new(row[1],row[0].to_i,row[2])
    end
    return nil
end

#getTrackById(id) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/itunesController/database/database.rb', line 126

def getTrackById(id)
    stmt=@backend.prepare("select databaseId,location,name from tracks where databaseId=?")
    rows = @backend.executeStatement(stmt,id)
    row=rows.next
    if (row!=nil)
        return ItunesController::Track.new(row[1],row[0].to_i,row[2])
    end
    return nil
end

#getTrackCountObject



140
141
142
# File 'lib/itunesController/database/database.rb', line 140

def getTrackCount()
    return @backend.execute("select count(*) from tracks")
end

#removeTrack(track) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/itunesController/database/database.rb', line 71

def removeTrack(track)
    ItunesController::ItunesControllerLogging::debug("Removing track from database with id=#{track.databaseId.to_i}'")
    stmt = @backend.prepare("delete from tracks where databaseId=?")
    @backend.executeStatement(stmt,track.databaseId.to_i)
    stmt = @backend.prepare("delete from dead_tracks where databaseId=?")
    @backend.executeStatement(stmt,track.databaseId.to_i)
    stmt = @backend.prepare("delete from dupe_tracks where databaseId=?")
    @backend.executeStatement(stmt,track.databaseId.to_i)
end

#removeTracksObject



81
82
83
84
85
86
# File 'lib/itunesController/database/database.rb', line 81

def removeTracks()
    ItunesController::ItunesControllerLogging::debug("Removing all tracks")
    @backend.execute("delete from tracks")
    @backend.execute("delete from dead_tracks")
    @backend.execute("delete from dupe_tracks")
end

#setParam(key, value) ⇒ Object



88
89
90
91
92
93
# File 'lib/itunesController/database/database.rb', line 88

def setParam(key,value)
    stmt = @backend.prepare("delete from params where key=?")
    @backend.executeStatement(stmt,key)
    stmt = @backend.prepare("insert into params(key,value) values(?,?)")
    @backend.executeStatement(stmt,key,value)
end