Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Database abstraction and Timer class implementation
- Loading branch information
Showing
5 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
Ti.include("../helpers/utils.js"); | ||
|
||
// Database constructor | ||
function Db(){ | ||
this.db = Titanium.Database.open("timersdb"); | ||
this.db.execute('CREATE TABLE IF NOT EXISTS timers (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, intial_date DATE, final_date DATE)'); | ||
} | ||
|
||
// Basic CRUD operations | ||
Db.prototype.save = function(timer){ | ||
var sql = "INSERT INTO timers(name, initial_date, final_date) VALUES('{timer.name}', '{timer.initialDate}', '{timer.finalDate}')".supplant(timer); | ||
this.db.execute(sql); | ||
} | ||
|
||
Db.prototype.remove = function(id){ | ||
var sql = "DELETE FROM timers WHERE id = {id}".supplant({'id': id}); | ||
this.db.execute(sql); | ||
} | ||
|
||
Db.prototype.update = function(timer){ | ||
var sql = "UPDATE timers SET name = '{name}', initial_date = '{initialDate}', final_date = '{finalDate}' where id = {id}".supplant(timer); | ||
this.db.execute(sql); | ||
} | ||
|
||
Db.prototype.list = function(){ | ||
var sql = 'SELECT * FROM timers'; | ||
var cursor = this.db.execute(sql); | ||
var timers = []; | ||
while(cursor.isValidRow()){ | ||
var timer = populateTimerObject(cursor); | ||
} | ||
cursor.close(); | ||
return timers; | ||
} | ||
|
||
Db.prototype.get = function(id){ | ||
var sql = "SELECT * FROM timers WHERE id = {id}".supplant({'id':id}); | ||
var cursor = this.db.execute(sql); | ||
var timer = null; | ||
if(cursor.isValidRow()){ | ||
timer = populateTimerObject(cursor); | ||
} | ||
cursor.close(); | ||
return timer; | ||
} | ||
|
||
// Helper function to populate a new Timer object from a database cursor | ||
function populateTimerObject(cursor){ | ||
var timer = new Timer(cursor.fieldByName('name'), cursor.fieldByName('initial_date'), cursor.fielByName('final_date')); | ||
timer.id = cursor.field(0); | ||
return timer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var isDate = function(obj){ | ||
return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear); | ||
} | ||
|
||
Date.prototype.strftime = function(){ | ||
function _zero(num){return (num < 10 ) ? "0"+num : ""+num }; | ||
return [this.getFullYear()+"", _zero(this.getMonth()+1), _zero(this.getDate()) ].join('-') + " " + | ||
[this.getHours(), this.getMinutes(), this.getSeconds()].join(':') | ||
} | ||
|
||
if (!String.prototype.supplant) { | ||
String.prototype.supplant = function (o) { | ||
return this.replace(/{([^{}]*)}/g, | ||
function (a, b) { | ||
var r = o[b]; | ||
return isDate(o[b]) ? o[b].strftime(): r.toString(); | ||
} | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Timer Object | ||
|
||
// Constructor | ||
function Timer(name, initialDate, finalDate){ | ||
this.name = name; | ||
this.initialDate = initialDate; | ||
this.finalDate = finalDate; | ||
this.id = null; | ||
} | ||
|
||
// Check if a current date falls in the range | ||
// of initial and final date | ||
Timer.prototype.isValidDate = function(date){ | ||
// TODO: Validate that 'date' object is Date | ||
return this.initialDate <= date <= this.finalDate | ||
} | ||
|
||
Timer.prototype.toString = function(){ | ||
return 'Timer['+this.name+'] = <'+this.initialDate+'><'+this.finalDate+'>' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters