diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dao.js | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -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; |