diff --git a/CHANGELOG.md b/CHANGELOG.md index be94c86..d21f9c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Change Log This change log is started in 0.3.3; +## 0.3.5 +-- Error Handling +-- Debug options + ## 0.3.4 (2016-04-19) - Execute PRAGMA statements (update/get) ```js diff --git a/README.md b/README.md index 0cfbccf..d1dd742 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,17 @@ var sqlite = require('sqlite-sync'); //requiring sqlite.connect('test/test.db'); //Creating table - you can run any command -sqlite.run("CREATE TABLE COMPANYS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL);"); +sqlite.run("CREATE TABLE COMPANYS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL);",function(res){ + if(res.error) + throw res.error; + console.log(res); +}); //Inserting - this function can be sync to, look the wiki -sqlite.insert("COMPANYS",{NAME:"My COMPANY"}, function(inserid){ - console.log(inserid); +sqlite.insert("COMPANYS",{NAME:"My COMPANY"}, function(res){ + if(res.error) + throw res.error; + console.log(res); }); //Updating - returns the number of rows modified - can be async too diff --git a/package.json b/package.json index af23079..e9842bd 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "async", "database" ], - "version": "0.3.4", + "version": "0.3.5", "author": { "name": "Jayr Alencar", "email": "jayralencarpereira@gmail.com", diff --git a/sqlite.js b/sqlite.js index cf42d6c..3049817 100644 --- a/sqlite.js +++ b/sqlite.js @@ -41,6 +41,7 @@ sqlite.prototype.buffer = null; sqlite.prototype.writer = null; sqlite.prototype.file = null; sqlite.prototype.sql = ''; +sqlite.prototype.debug = false; function sqlite () { @@ -51,39 +52,39 @@ function sqlite () { * * @param {String|Object} db - File directory+filename | buffer * @return {Object} - */ -sqlite.prototype.connect = function(db){ - if(typeof(db)=='string'){ - this.file = db; - if(fs.existsSync(this.file)){ - this.buffer = fs.readFileSync(this.file); - } - }else if(typeof(db)=="object"){ - this.buffer = db; - } - - if(this.buffer){ - try{ - this.db = new SQL.Database(this.buffer); - }catch(x){ - throw x; - } - }else{ - try{ - this.db = new SQL.Database(); - this.write(); - }catch(x){ - throw x; - } - } - - return this; -} + */ + sqlite.prototype.connect = function(db){ + if(typeof(db)=='string'){ + this.file = db; + if(fs.existsSync(this.file)){ + this.buffer = fs.readFileSync(this.file); + } + }else if(typeof(db)=="object"){ + this.buffer = db; + } + + if(this.buffer){ + try{ + this.db = new SQL.Database(this.buffer); + }catch(x){ + throw x; + } + }else{ + try{ + this.db = new SQL.Database(); + this.write(); + }catch(x){ + throw x; + } + } + + return this; + } /** * Alternative connection -*/ -sqlite.prototype.con = sqlite.prototype.connect; + */ + sqlite.prototype.con = sqlite.prototype.connect; /** * Runing queries | Sync & Async @@ -92,30 +93,30 @@ sqlite.prototype.con = sqlite.prototype.connect; * @param {Array|Function} options - Array to prepared sql | callback function * @param {Function} callback - callback function * @return {Array|Object} - */ -sqlite.prototype.run = function(sql, options, callback) { - if(typeof(options) == "function"){ - callback = options; - options = []; - } - var results; - var type = sql.substring(0,6); - type = type.toUpperCase(); - switch(type){ - case "SELECT": results = this.pvSELECT(sql, options); break; - case "INSERT": results = this.pvINSERT(sql, options); break; - case "UPDATE": results = this.pvUPDATE(sql, options); break; - case "DELETE": results = this.pvDELETE(sql, options); break; - case "PRAGMA": results = this.pvPRAGMA(sql, options); break; - default: results = this.runAll(sql) - } - if(callback){ - callback(results); - return this; - }else{ - return results; - } -}; + */ + sqlite.prototype.run = function(sql, options, callback) { + if(typeof(options) == "function"){ + callback = options; + options = []; + } + var results; + var type = sql.substring(0,6); + type = type.toUpperCase(); + switch(type){ + case "SELECT": results = this.pvSELECT(sql, options); break; + case "INSERT": results = this.pvINSERT(sql, options); break; + case "UPDATE": results = this.pvUPDATE(sql, options); break; + case "DELETE": results = this.pvDELETE(sql, options); break; + case "PRAGMA": results = this.pvPRAGMA(sql, options); break; + default: results = this.runAll(sql) + } + if(callback){ + callback(results); + return this; + }else{ + return results; + } + }; /** * Runing queries Async @@ -126,16 +127,16 @@ sqlite.prototype.run = function(sql, options, callback) { * @return {Array|Object} * @observation: This function will no longer be used soon! - */ -sqlite.prototype.runAsync = function(sql, options, callback){ - this.sql = sql; - if(typeof(options) == "function"){ - options(this.run(sql)); - }else{ - callback(this.run(sql, options)); - } - return this; -} + */ + sqlite.prototype.runAsync = function(sql, options, callback){ + this.sql = sql; + if(typeof(options) == "function"){ + options(this.run(sql)); + }else{ + callback(this.run(sql, options)); + } + return this; + } /** * PRAGMA statements @@ -143,9 +144,9 @@ sqlite.prototype.runAsync = function(sql, options, callback){ * @param {String} sql - SQL statement * @param {Array} where - Array ti prepared sql * @return {Object} -*/ -sqlite.prototype.pvPRAGMA = function(sql, where) { - if((sql.split('=')).length>1){ + */ + sqlite.prototype.pvPRAGMA = function(sql, where) { + if((sql.split('=')).length>1){ // update return this.pvUPDATE(sql, where); }else{ @@ -160,36 +161,38 @@ sqlite.prototype.pvPRAGMA = function(sql, where) { * @param {String} sql - SQL code * @param {Array} where - Array to prepared sql * @return {Object} - */ -sqlite.prototype.pvSELECT = function(sql, where){ - if(where){ - for(var i = 0 ; i < where.length; i++){ - sql = sql.replace('?',"\'"+where[i]+"\'"); - } - } - this.sql = sql; - try{ - var contents = this.db.exec(sql); - }catch(x){ - throw x - } - if(contents.length){ - var columns = contents[0].columns; - var values = contents[0].values; - var resultado = []; - for(var i = 0 ; i < values.length ; i++){ - var linha = {}; - for(var j = 0 ; j < columns.length; j++){ - linha[columns[j]] = values[i][j] - } - resultado.push(linha); - } - return resultado; - }else{ - return []; - } - -} + */ + sqlite.prototype.pvSELECT = function(sql, where){ + if(where){ + for(var i = 0 ; i < where.length; i++){ + sql = sql.replace('?',"\'"+where[i]+"\'"); + } + } + this.sql = sql; + try{ + var contents = this.db.exec(sql); + if(contents.length){ + var columns = contents[0].columns; + var values = contents[0].values; + var resultado = []; + for(var i = 0 ; i < values.length ; i++){ + var linha = {}; + for(var j = 0 ; j < columns.length; j++){ + linha[columns[j]] = values[i][j] + } + resultado.push(linha); + } + return resultado; + }else{ + return []; + } + }catch(x){ + if(this.debug){ + throw x; + } + return {error:x} + } + } /** * Runing deletes - PRIVATE @@ -197,23 +200,25 @@ sqlite.prototype.pvSELECT = function(sql, where){ * @param {String} sql - SQL code * @param {Array} where - Array to prepared sql * @return {Boo} - */ -sqlite.prototype.pvDELETE = function(sql, where){ - if(where){ - for(var i = 0 ; i < where.length; i++){ - sql = sql.replace('?',where[i]); - } - } - this.sql = sql; - try{ - this.db.exec(sql); - this.write(); - return this.db.getRowsModified(); - }catch(x){ - return false; - throw x; - } -} + */ + sqlite.prototype.pvDELETE = function(sql, where){ + if(where){ + for(var i = 0 ; i < where.length; i++){ + sql = sql.replace('?',where[i]); + } + } + this.sql = sql; + try{ + this.db.exec(sql); + this.write(); + return this.db.getRowsModified(); + }catch(x){ + if(this.debug){ + throw x; + } + return {error:x}; + } + } /** * Runing insets - PRIVATE @@ -221,20 +226,27 @@ sqlite.prototype.pvDELETE = function(sql, where){ * @param {String} sql - SQL code * @param {Array} data - Array to prepared sql * @return {Int} last insert id - */ -sqlite.prototype.pvINSERT = function(sql,data){ - if(data){ - for(var i = 0 ; i < data.length; i++){ - sql = sql.replace('?',"'"+data[i]+"'"); - } - } - this.sql = sql; - this.db.run(sql); - var last = this.pvSELECT("SELECT last_insert_rowid()"); - this.write(); - return last[0]['last_insert_rowid()']; - -} + */ + sqlite.prototype.pvINSERT = function(sql,data){ + if(data){ + for(var i = 0 ; i < data.length; i++){ + sql = sql.replace('?',"'"+data[i]+"'"); + } + } + this.sql = sql; + try{ + this.db.run(sql); + var last = this.pvSELECT("SELECT last_insert_rowid()"); + this.write(); + return last[0]['last_insert_rowid()']; + }catch(x){ + if(this.debug){ + throw x; + } + return {error:x}; + } + + } /** * Runing updates - PRIVATE @@ -242,23 +254,25 @@ sqlite.prototype.pvINSERT = function(sql,data){ * @param {String} sql - SQL code * @param {Array} data - Array to prepared sql * @return {Boo} - */ -sqlite.prototype.pvUPDATE = function(sql, data){ - if(data){ - for(var i = 0 ; i < data.length; i++){ - sql = sql.replace('?',"'"+data[i]+"'"); - } - } - this.sql = sql; - try{ - this.db.run(sql) - this.write(); - return this.db.getRowsModified(); - }catch (x){ - return false; - throw x - } -} + */ + sqlite.prototype.pvUPDATE = function(sql, data){ + if(data){ + for(var i = 0 ; i < data.length; i++){ + sql = sql.replace('?',"'"+data[i]+"'"); + } + } + this.sql = sql; + try{ + this.db.run(sql) + this.write(); + return this.db.getRowsModified(); + }catch (x){ + if(this.debug){ + throw x; + } + return {error:x}; + } + } /** * Runing INSERT - Publics @@ -267,24 +281,24 @@ sqlite.prototype.pvUPDATE = function(sql, data){ * @param {Object} data - Object to be inserted * @param {Function} callback - callback function * @return {Int|Object} - insert id | instance - */ -sqlite.prototype.insert = function(entity, data, callback){ - var keys = []; - var values = [] - for(key in data){ - keys.push(key); - values.push(data[key]); - } - - var sql = "INSERT INTO "+entity+" ("+keys.join(',')+") VALUES ('"+values.join("','")+"')"; - this.sql = sql; - if(callback){ - callback(this.run(sql)); - return this; - }else{ - return this.run(sql); - } -} + */ + sqlite.prototype.insert = function(entity, data, callback){ + var keys = []; + var values = [] + for(key in data){ + keys.push(key); + values.push(data[key]); + } + + var sql = "INSERT INTO "+entity+" ("+keys.join(',')+") VALUES ('"+values.join("','")+"')"; + this.sql = sql; + if(callback){ + callback(this.run(sql)); + return this; + }else{ + return this.run(sql); + } + } /** * Runing UPDATE - Publics @@ -294,32 +308,32 @@ sqlite.prototype.insert = function(entity, data, callback){ * @param {Object|Function} clause - Object with wheres | callback function * @param {Function} callback - callback function * @return {Boo|Object} - result | instance - */ -sqlite.prototype.update = function(entity, data, clause, callback){ - var sets = []; - var where = []; - if(typeof(clause)=="function"){ - callback = clause; - clause = {}; - } - for(key in data){ - sets.push(key+" = '"+data[key]+"'"); - } - for(key in clause){ - where.push(key+" = '"+clause[key]+"'"); - } - - var sql = "UPDATE "+entity+" SET "+sets.join(', ')+(where.length>0?" WHERE "+where.join(" AND "):""); - - this.sql = sql; - - if(callback){ - callback(this.run(sql)); - return this; - }else{ - return this.run(sql); - } -} + */ + sqlite.prototype.update = function(entity, data, clause, callback){ + var sets = []; + var where = []; + if(typeof(clause)=="function"){ + callback = clause; + clause = {}; + } + for(key in data){ + sets.push(key+" = '"+data[key]+"'"); + } + for(key in clause){ + where.push(key+" = '"+clause[key]+"'"); + } + + var sql = "UPDATE "+entity+" SET "+sets.join(', ')+(where.length>0?" WHERE "+where.join(" AND "):""); + + this.sql = sql; + + if(callback){ + callback(this.run(sql)); + return this; + }else{ + return this.run(sql); + } + } /** * Runing DELETE - Publics @@ -328,119 +342,95 @@ sqlite.prototype.update = function(entity, data, clause, callback){ * @param {Object|Function} clause - Object with wheres | callback function * @param {Function} callback - callback function * @return {Boo|Object} - result | instance - */ -sqlite.prototype.delete = function(entity, clause, callback){ - var where = []; - if(typeof(clause)=="function"){ - callback = clause; - clause = []; - } - - if(clause){ - for(key in clause){ - where.push(key+" = '"+clause[key]+"'"); - } - } - - var sql = "DELETE FROM "+entity+" WHERE "+where.join(" AND "); - - this.sql = sql; - - var result = this.pvDELETE(sql); - - if(callback){ - callback(result); - return this; - }else{ - return result; - } -} + */ + sqlite.prototype.delete = function(entity, clause, callback){ + var where = []; + if(typeof(clause)=="function"){ + callback = clause; + clause = []; + } + + if(clause){ + for(key in clause){ + where.push(key+" = '"+clause[key]+"'"); + } + } + + var sql = "DELETE FROM "+entity+" WHERE "+where.join(" AND "); + + this.sql = sql; + + var result = this.pvDELETE(sql); + + if(callback){ + callback(result); + return this; + }else{ + return result; + } + } /** * Runing All - PRIVATE * * @param {String} sql - SQL * @return {Boo} - */ -sqlite.prototype.runAll = function(sql){ - this.sql = sql; - try{ - var tes = this.db.exec(sql) - this.write(); - return tes; - }catch (x){ - return false; - throw x - } -} + */ + sqlite.prototype.runAll = function(sql){ + this.sql = sql; + try{ + var tes = this.db.exec(sql) + this.write(); + return tes; + }catch (x){ + if(this.debug){ + throw x; + } + return {error:x}; + } + } /** * Writing file or calling buffer callback * * @return {Object} - */ -sqlite.prototype.write = function(){ - var data = this.db.export(); - var buffer = new Buffer(data); - - if(this.file){ - fs.writeFileSync(this.file, buffer); - }else if(this.writer && typeof(this.writer) == 'function' ){ - this.writer(buffer); - } - return this; -} + */ + sqlite.prototype.write = function(){ + var data = this.db.export(); + var buffer = new Buffer(data); + + if(this.file){ + fs.writeFileSync(this.file, buffer); + }else if(this.writer && typeof(this.writer) == 'function' ){ + this.writer(buffer); + } + return this; + } /* * Creating functions * * @param {Function} func - the function * @return {Object} -*/ -sqlite.prototype.create_function = function(func){ - this.db.create_function(func.name, func); -} + */ + sqlite.prototype.create_function = function(func){ + this.db.create_function(func.name, func); + } /** * Closing connection -*/ -sqlite.prototype.close = function(){ - this.db.close(); -} + */ + sqlite.prototype.close = function(){ + this.db.close(); + } /** * Get current sql * @return {String} -*/ -sqlite.prototype.getSql = function(){ - return this.sql; -} - -/** - * backup - * @param {String} -*/ -sqlite.prototype.backup = function(file, name){ - var filename = path.basename(file); - name = (name)? name : (filename.split('.'))[0]; - var sql = "--\n"; - sql += "-- File generated with sqlite-sync.js on "+(new Date()).toLocaleString()+"\n"; - sql += "--\n"; - sql += "CREATE DATABASE "+name+";\n\n"; - // tables - var tables = this.run("SELECT * FROM sqlite_master"); - for(var i = 0 ; i < tables.length ; i++){ - sql+="-- Table: "+tables[i].tbl_name+"\n"; - sql+=tables[i].sql+"\n"; - - var values = this.run("SELECT * FROM "+tables[i].tbl_name); - for(var j=0;j