aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-14 01:02:35 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-14 01:02:35 +0200
commit47b4b51573f240763ee8b761bfe9f37ffce8e0fb (patch)
tree08cb4a77db6c84e7d9a560b8a1b4240b1c9dce26 /lib
parent27fdccc050bbfe09beca29d8d50ca25e18c5102a (diff)
downloadexample-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.tar.gz
example-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.tar.bz2
example-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.tar.xz
example-collection-json-db-47b4b51573f240763ee8b761bfe9f37ffce8e0fb.zip
o Adding support for inserting employees.
Diffstat (limited to 'lib')
-rw-r--r--lib/dao.js27
1 files changed, 27 insertions, 0 deletions
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;