Skip to content

Commit

Permalink
Merge pull request #220 from EragonJ/change-db
Browse files Browse the repository at this point in the history
Drop websql and store plain file in dataPath
  • Loading branch information
avihaymenahem committed Oct 26, 2014
2 parents 9770f7a + f831c84 commit 528aed9
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 190 deletions.
98 changes: 54 additions & 44 deletions coffee/_history.coffee
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
# Init preparation (should be improved later)
db.transaction (tx) ->
tx.executeSql(
'CREATE TABLE IF NOT EXISTS history ' +
'(artist, title, cover_url_medium, cover_url_large, last_played)'
)

class History
@clear: (success) ->
db.transaction (tx) ->
tx.executeSql 'DROP TABLE history'
success?()
db.history.remove({}, { multi: true }, (error) ->
if error
console.log("History.clear remove erorr :")
console.log(error)
success?()
else
success?()
)

@addTrack: (artist, title, cover_url_medium, cover_url_large) ->
unix_timestamp = Math.round((new Date()).getTime() / 1000)
db.transaction (tx) ->
tx.executeSql(
'CREATE TABLE IF NOT EXISTS history ' +
'(artist, title, cover_url_medium, cover_url_large, last_played)'
)

tx.executeSql(
'DELETE FROM history WHERE artist = ? and title = ?', [artist, title]
)

tx.executeSql(
'INSERT INTO history ' +
'(artist, title, cover_url_medium, cover_url_large, last_played) ' +
'VALUES (?, ?, ?, ?, ?)',
[artist, title, cover_url_medium, cover_url_large, unix_timestamp]
)
db.history.remove({
artist: artist,
title: title
}, {}, (error, numRemoved) ->
if error
console.log("History.addTrack remove erorr :")
console.log(error)
else
db.history.insert({
artist: artist,
title: title,
cover_url_medium: cover_url_medium,
cover_url_large: cover_url_large,
last_played: unix_timestamp
}, (error) ->
if error
console.log("History.addTrack insert erorr :")
console.log(error)
)
)

@removeTrack: (artist, title) ->
db.transaction (tx) ->
tx.executeSql(
'DELETE FROM history WHERE artist = ? and title = ?', [artist, title]
)
db.history.remove({
artist: artist,
title: title
}, {}, (error) ->
if error
console.log("History.removeTrack remove erorr :")
console.log(error)
)

@getTracks: (success) ->
tracks = []
db.transaction (tx) ->
tx.executeSql(
'SELECT * FROM history ORDER BY last_played DESC LIMIT 150', [],
(tx, results) ->
i = 0
while i < results.rows.length
tracks.push results.rows.item(i)
i++
success? tracks
)
db.history.find({}).sort({
last_played: -1
}).limit(150).exec((error, foundTracks) ->
if error
console.log("History.getTracks find erorr :")
console.log(error)
success?([])
else
success?(foundTracks)
)

@countTracks: (success) ->
db.transaction (tx) ->
tx.executeSql 'SELECT COUNT(*) AS cnt FROM history', [], (tx, results) ->
success? results.rows.item(0).cnt
db.history.count({}, (error, count) ->
if error
console.log("History.countTracks count erorr :")
console.log(error)
success?(0)
else
success?(count)
)

0 comments on commit 528aed9

Please sign in to comment.