From 47b4b51573f240763ee8b761bfe9f37ffce8e0fb Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 14 Jul 2012 01:02:35 +0200 Subject: o Adding support for inserting employees. --- lib/dao.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib') diff --git a/lib/dao.js b/lib/dao.js index 5ebb38e..8fd6efd 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -7,6 +7,7 @@ function EmployeeDao(con) { this.getEmployeesByName = getEmployeesByName; this.getEmployee = getEmployee; this.deleteEmployee = deleteEmployee; + this.insertEmployee = insertEmployee; } function getDepartments(pager, cb) { @@ -124,4 +125,30 @@ function deleteEmployee(emp_no, cb) { }); } +function insertEmployee(birth_date, first_name, last_name, gender, hire_date, cb) { + var con = this.con; + var insert_employee = { + text: 'INSERT INTO employees(birth_date, first_name, last_name, gender, hire_date) VALUES($1, $2, $3, $4, $5)', + values: [] + }; + var employee_currval = { + text: 'SELECT currval(\'employees_seq\') as emp_no', + values: [] + }; + insert_employee.values.push(birth_date); + insert_employee.values.push(first_name); + insert_employee.values.push(last_name); + insert_employee.values.push(gender); + insert_employee.values.push(hire_date); + con.query('begin', function(err, rs) { if(err) return cb(err); + con.query(insert_employee, function(err, rs) { if(err) return cb(err); + con.query(employee_currval, function(err, rs2) { if(err) return cb(err); + con.query('commit', function(err) { if(err) return cb(err); + cb(undefined, rs2.rows[0].emp_no); + }); + }); + }); + }); +} + module.exports = EmployeeDao; -- cgit v1.2.3