Skip to content

Instantly share code, notes, and snippets.

@goldalworming
Last active August 29, 2015 14:05
Show Gist options
  • Save goldalworming/2f1d113e0819cd9113f0 to your computer and use it in GitHub Desktop.
Save goldalworming/2f1d113e0819cd9113f0 to your computer and use it in GitHub Desktop.
query yang parameternya dari text
exports.install = function(framework) {
framework.route('/querymysql/', multiplequery,['post','json']);
};
function multiplequery(){
var self = this;
var auth = self.module('authorization');
self.post.newkode = function(){
return utils.GUID([9]);
}
self.post.key = key;
self.post.completeurl = self.post.domain+'.'+host;
var arr = [
{query:"insert into id_urls set urls=?,idclient=?",param:"[req.post.completeurl,req.post.key]"},
{query:"insert into id_admindomain set kode=?,idclient=?,kode_user=?",param:"[req.post.newkode(),req.post.key,req.user.id]"}
];
self.db(function(err, connection){
var fn_async = function(item, next) {
var fn = new Function('req', "return "+item.param);
var param = fn({get:self.get,post:self.post,user:self.user,helpers:framework.helpers});
connection.query(item.query, param, function(err, rows) {
if(err !== null){self.error(err);
connection.release();self.json({success:false, err:'failed116'});
}
});
next();
};
var fn_complete = function() {
connection.release();
self.json({success:true, msg: 'success multiple query'});
};
arr.wait(fn_async, fn_complete);
});
}
exports.install = function(framework) {
framework.route('/querymysql/', multiplequerytransaction,['post','json']);
};
function multiplequerytransaction(){
var self = this;
self.post.newkode = function(){
// utils sudah bawaan dari total.js
return utils.GUID([9]);
}
self.post.key = key;
self.post.completeurl = self.post.domain+'.'+host;
var arr = [
{query:"insert into id_urls set urls=?,idclient=?",param:"[req.post.completeurl,req.post.key]"},
{query:"insert into id_admindomain set kode=?,idclient=?,kode_user=?",param:"[req.post.newkode(),req.post.key,req.user.id]"}
];
self.db(function(err, connection){
connection.beginTransaction(function(err) {
if (err) { throw err; }
var fn_async = function(item, next) {
var fn = new Function('req', "return "+item.param);
var param = fn({get:self.get,post:self.post,user:self.post.user,helpers:framework.helpers});
connection.query(item.query, param, function(err, rows) {
if(err !== null){
connection.rollback(function(){
self.error(err);
connection.release();self.json({success:false, err:'failed30'});
});
}
});
next();
};
var fn_complete = function() {
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
self.error(err);
connection.release();self.json({success:false, err:'failed42'});
});
}
connection.release();
self.json({success:true, msg: 'success multiple query'});
});
};
arr.wait(fn_async, fn_complete);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment