|
| 1 | +/** |
| 2 | + * Created by Ori Braun on 2/9/2019. |
| 3 | + */ |
| 4 | + |
| 5 | +var connectionProvider = require("./../connection/mySqlConnectionProvider.js"); |
| 6 | +var sqlHelper = require("../../helpers/sql.js"); |
| 7 | +var tableName = ""; |
| 8 | +var Model = { |
| 9 | + runSql : function(sqlStatement, callback) { |
| 10 | + var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection(); |
| 11 | + var obj = { |
| 12 | + err : 0 , |
| 13 | + errMessage : "" |
| 14 | + }; |
| 15 | + var data = []; |
| 16 | + if (connection) { |
| 17 | + connection.query(sqlStatement, function(err, rows, fields) { |
| 18 | + if (err) { |
| 19 | + obj.err = 1; |
| 20 | + obj.errMessage = err; |
| 21 | + } else { |
| 22 | + rows.forEach(function (row) { |
| 23 | + data.push(row); |
| 24 | + }); |
| 25 | + } |
| 26 | + obj.data = data; |
| 27 | + callback(obj); |
| 28 | + }); |
| 29 | + } |
| 30 | + connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection); |
| 31 | + }, |
| 32 | + getAll : function(callback) { |
| 33 | + console.log("getAll"); |
| 34 | + var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection(); |
| 35 | + var obj = { |
| 36 | + err : 0 , |
| 37 | + errMessage : "" |
| 38 | + }; |
| 39 | + var query = new sqlHelper.Sql(); |
| 40 | + query.select("*"); |
| 41 | + query.from(tableName); |
| 42 | + // query to the database and get the records |
| 43 | + if (connection) { |
| 44 | + connection.query(query.get(), function (err, rows) { |
| 45 | + |
| 46 | + if (err) { |
| 47 | + obj.err = 1; |
| 48 | + obj.errMessage = err; |
| 49 | + } |
| 50 | + |
| 51 | + // send records as a response |
| 52 | + // console.log(rows); |
| 53 | + if (rows) { |
| 54 | + obj.entities = rows; |
| 55 | + } |
| 56 | + callback(obj); |
| 57 | + }); |
| 58 | + } |
| 59 | + connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection); |
| 60 | + }, |
| 61 | + insert : function(columns, values, callback) { |
| 62 | + var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection(); |
| 63 | + var obj = { |
| 64 | + err : 0 , |
| 65 | + errMessage : "" |
| 66 | + }; |
| 67 | + var query = new sqlHelper.Sql(); |
| 68 | + query.insert(tableName,columns, values); |
| 69 | + // query to the database and get the records |
| 70 | + if (connection) { |
| 71 | + connection.query(query.get(), function (err, rows) { |
| 72 | + |
| 73 | + if (err) { |
| 74 | + obj.err = 1; |
| 75 | + obj.errMessage = err; |
| 76 | + } |
| 77 | + |
| 78 | + // send records as a response |
| 79 | + // console.log(rows); |
| 80 | + if (rows) { |
| 81 | + obj.data = rows; |
| 82 | + } |
| 83 | + callback(obj); |
| 84 | + }); |
| 85 | + } |
| 86 | + connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection); |
| 87 | + }, |
| 88 | + update : function(keys, values, condition, callback) { |
| 89 | + var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection(); |
| 90 | + var obj = { |
| 91 | + err : 0 , |
| 92 | + errMessage : "" |
| 93 | + }; |
| 94 | + var query = new sqlHelper.Sql(); |
| 95 | + query.update(tableName); |
| 96 | + query.set(keys,values); |
| 97 | + query.where(condition); |
| 98 | + // query to the database and get the records |
| 99 | + if (connection) { |
| 100 | + connection.query(query.get(), function (err, rows) { |
| 101 | + |
| 102 | + if (err) { |
| 103 | + obj.err = 1; |
| 104 | + obj.errMessage = err; |
| 105 | + } |
| 106 | + |
| 107 | + // send records as a response |
| 108 | + // console.log(rows); |
| 109 | + if (rows) { |
| 110 | + obj.data = rows; |
| 111 | + } |
| 112 | + callback(obj); |
| 113 | + }); |
| 114 | + } |
| 115 | + connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection); |
| 116 | + }, |
| 117 | + delete : function(condition,callback) { |
| 118 | + var connection = connectionProvider.mysqlConnectionProvider.getSqlConnection(); |
| 119 | + var obj = { |
| 120 | + err : 0 , |
| 121 | + errMessage : "" |
| 122 | + }; |
| 123 | + var query = new sqlHelper.Sql(); |
| 124 | + query.delete(tableName); |
| 125 | + query.where(condition); |
| 126 | + // query to the database and get the records |
| 127 | + if (connection) { |
| 128 | + connection.query(query.get(), function (err, rows) { |
| 129 | + |
| 130 | + if (err) { |
| 131 | + obj.err = 1; |
| 132 | + obj.errMessage = err; |
| 133 | + } |
| 134 | + |
| 135 | + // send records as a response |
| 136 | + // console.log(rows); |
| 137 | + if (rows) { |
| 138 | + obj.data = rows; |
| 139 | + } |
| 140 | + callback(obj); |
| 141 | + }); |
| 142 | + } |
| 143 | + connectionProvider.mysqlConnectionProvider.closeSqlConnection(connection); |
| 144 | + }, |
| 145 | + setTable: function(table) { |
| 146 | + tableName = table; |
| 147 | + }, |
| 148 | +}; |
| 149 | + |
| 150 | +exports.Model = Model; |
0 commit comments