Skip to content

Commit

Permalink
Changed the store to use the hdb driver (See #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
marianr committed Jan 15, 2014
1 parent c1c1f17 commit 4ddcb48
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 95 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ seneca-hana-store
=================

seneca-hana-store is a [SAP HANA][sapcom] database plugin for the [Seneca][seneca] MVP toolkit. The plugin is using the
[node-hana-odbc][hanaodbc] driver.
[hdb][nodehdb] driver.

Usage:

Expand All @@ -11,10 +11,11 @@ Usage:

var config = {}
var storeopts = {
dsn:'HANA',
username:'user',
password:'password'
schema: 'schema'
host: '127.0.0.1',
port: 30015,
user: 'user',
password: 'password',
schema: 'schema'
};

...
Expand All @@ -29,4 +30,4 @@ Usage:

[sapcom]: http://www54.sap.com/pc/tech/in-memory-computing-hana/software/overview/index.html
[seneca]: http://senecajs.org/
[hanaodbc]: https://github.com/rksm/node-hana-odbc.git
[nodehdb]: https://github.com/SAP/node-hdb.git
137 changes: 74 additions & 63 deletions lib/hana-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
var _ = require('underscore'),
util = require('util'),
uuid = require('node-uuid'),
hana = require('hana-odbc'),
relationalstore = require('./relational-util');
hana = require('hdb'),
relationalstore = require('./relational-util');


var MIN_WAIT = 16;
Expand All @@ -19,13 +19,13 @@ function tblmeta(data) {
tbl.columns = {};
data.forEach(function (meta) {
if (meta.COLUMN_NAME) {
tbl.schemaName = meta.TABLE_SCHEM;
tbl.schemaName = meta.SCHEMA_NAME;
tbl.tableName = meta.TABLE_NAME;
tbl.columns[meta.COLUMN_NAME] = {
columnName: meta.COLUMN_NAME,
dataType: meta.DATA_TYPE,
dataTypeName: meta.TYPE_NAME,
length: meta.COLUMN_SIZE,
dataType: meta.DATA_TYPE_ID,
dataTypeName: meta.DATA_TYPE_NAME,
length: meta.LENGTH,
isNullable: meta.NULLABLE === 0 ? false : true
};
} else {
Expand Down Expand Up @@ -77,7 +77,8 @@ function wherestm(entp, q, columns) {
if(_.isString(qok[p]) || _.isNumber(qok[p]) || _.isBoolean(qok[p])) {
if (qok[p] && columns[p] && columns[p].dataTypeName && relationalstore.isAllowed(columns[p].dataTypeName)) {
var val = relationalstore.getMapper(columns[p].dataTypeName).toSQL(qok[p]);
wargs.push(quoteprop(p) + ' = ' + quoteval(val.value));
// wargs.push(quoteprop(p) + ' = ' + quoteval(val.value));
wargs.push(util.format('"%s" = \'%s\'', p, val.value));
}
}
}
Expand Down Expand Up @@ -111,54 +112,61 @@ function metastm(q) {
return ' ' + params.join(' ');
}

function tname(schema, table) {
return schema ? util.format('"%s"."%s"', schema, table) : util.format("%s", table);
}

function savestm(ent, tblspec) {

var table = tblspec.tableName,
entp = relationalstore.makeentp(ent, tblspec),
fields = _.keys(entp),
cols = [],
schema = tblspec.schemaName,
entp = relationalstore.makeentp(ent, tblspec),
fields = _.keys(entp),
cols = [],
vals = [];

fields.forEach(function (field) {
cols.push(quoteprop(field));
vals.push(quoteval(entp[field]));
});

return 'INSERT INTO ' + quoteprop(table) + ' (' + cols + ') VALUES (' + vals + ')';
return 'INSERT INTO ' + tname(schema, table) + ' (' + cols + ') VALUES (' + vals + ')';
}


function updatestm(ent, tblspec) {
var table = tblspec.tableName,
fields = ent.fields$(),
entp = relationalstore.makeentp(ent, tblspec),
params = [];
schema = tblspec.schemaName,
fields = ent.fields$(),
entp = relationalstore.makeentp(ent, tblspec),
params = [];

fields.forEach(function (field) {
if (!(_.isUndefined(ent[field]) || _.isNull(ent[field]))) {
params.push(quoteprop(field) + ' = ' + quoteval(entp[field]));
}
});

return 'UPDATE ' + quoteprop(table) + ' SET ' + params + ' WHERE "id" = ' + quoteval(ent.id);
return 'UPDATE ' + tname(schema, table) + ' SET ' + params + ' WHERE "id" = ' + quoteval(ent.id);
}


function deletestm(qent, q, tblspec) {

var table = tblspec.tableName,
entp = relationalstore.makeentp(qent, tblspec);
schema = tblspec.schemaName,
entp = relationalstore.makeentp(qent, tblspec);

return 'DELETE FROM ' + quoteprop(table) + wherestm(entp, q, tblspec.columns);
return 'DELETE FROM ' + tname(schema, table) + wherestm(entp, q, tblspec.columns);
}


function selectstm(qent, q, tblspec) {
var table = tblspec.tableName,
entp = relationalstore.makeentp(qent, tblspec);
schema = tblspec.schemaName,
entp = relationalstore.makeentp(qent, tblspec);

return 'SELECT * FROM ' + quoteprop(table) + wherestm(entp, q, tblspec.columns) + metastm(q);
return 'SELECT * FROM ' + tname(schema, table) + wherestm(entp, q, tblspec.columns) + metastm(q);
}


Expand All @@ -181,7 +189,9 @@ module.exports = function (opts) {
table = (canon.base ? canon.base + '_' : '') + canon.name;

if (!tablesmap[table]) {
dbinst.db.describe({database: schema, schema: schema, table: table}, function (err, data) {
var stm = util.format('select * from SYS.TABLE_COLUMNS where SCHEMA_NAME=\'%s\' and TABLE_NAME=\'%s\'', schema, table);
dbinst.exec(stm, function (err, data) {
// dbinst.db.describe({database: schema, schema: schema, table: table}, function (err, data) {
if (err) {
return cb(err);
}
Expand All @@ -204,24 +214,24 @@ module.exports = function (opts) {
return cb(null, ent.id$);
}

if (opts.idtype && 'sequence' === opts.idtype) {
dbinst.query(quoteprop(tblspec.schemaName), idvalstm(tblspec.tableName), function (err, data) {
if (err) {
return cb(err);
}
var hasData = data && data.rows && data.rows[0];
if (hasData) {
for (var p in data.rows[0]) {
var id = data.rows[0][p];
}
return cb(null, id);
} else {
return cb(null, null);
}
});
} else {
return cb(null, uuid.v4());
}
// if (opts.idtype && 'sequence' === opts.idtype) {
// dbinst.query(quoteprop(tblspec.schemaName), idvalstm(tblspec.tableName), function (err, data) {
// if (err) {
// return cb(err);
// }
// var hasData = data && data.rows && data.rows[0];
// if (hasData) {
// for (var p in data.rows[0]) {
// var id = data.rows[0][p];
// }
// return cb(null, id);
// } else {
// return cb(null, null);
// }
// });
// } else {
return cb(null, uuid.v4());
// }
}

function configure(spec, cb) {
Expand All @@ -230,19 +240,17 @@ module.exports = function (opts) {
var conf;
if (!conf) {
conf = {};
conf.dsn = spec.dsn;
conf.username = spec.username;
conf.host = spec.host;
conf.port = spec.port;
conf.user = spec.user;
conf.password = spec.password;
conf.db = spec.db;
conf.schema = spec.schema || '';
}

var dsn = util.format('DSN=' + conf.dsn + ';UID=%s;PWD=%s', conf.username, conf.password);

dbinst = hana.getSession({dsn: dsn});
dbinst = hana.createClient(conf);

dbinst.connect(function (err) {
if (err) {
if (error(spec, err, cb)) {
return cb(err);
}
return cb();
Expand Down Expand Up @@ -288,7 +296,7 @@ module.exports = function (opts) {

close: function (cb) {
if (dbinst) {
dbinst.close();
dbinst.disconnect(cb);
}
},

Expand All @@ -303,7 +311,8 @@ module.exports = function (opts) {
}

if (update) {
dbinst.query(quoteprop(schema), updatestm(ent, tblspec), function (err, res) {
var stm = updatestm(ent, tblspec);
dbinst.exec(stm, function (err, rows) {
if (!error(args, err, cb)) {
seneca.log(args.tag$, 'update', ent);
ent.load$(ent.data$(),function (err, dbent) {
Expand All @@ -320,7 +329,8 @@ module.exports = function (opts) {
return cb(new Error('Error getting "id" value'));
}
ent.id = id;
dbinst.query(quoteprop(schema), savestm(ent, tblspec), function (err, res) {
var stm = savestm(ent, tblspec);
dbinst.exec(stm, function (err, rows) {
if (!error(args, err, cb)) {
seneca.log(args.tag$, 'save/insert', ent);
ent.load$(ent.data$(),function (err, dbent) {
Expand All @@ -346,11 +356,11 @@ module.exports = function (opts) {
if (err) {
return cb(err);
}

dbinst.query(quoteprop(schema), selectstm(qent, q, tblspec), function (err, res) {
var stm = selectstm(qent, q, tblspec);
dbinst.exec(stm, function (err, rows) {
if (!error(args, err, cb)) {
var dbent = relationalstore.makeent(qent, res.rows[0], tblspec);
seneca.log(args.tag$, 'load', res.rows[0]);
var dbent = relationalstore.makeent(qent, rows[0], tblspec);
seneca.log(args.tag$, 'load', rows[0]);
return cb(null, dbent);
}
});
Expand All @@ -368,10 +378,10 @@ module.exports = function (opts) {
return cb(err);
}

var query = selectstm(qent, q, tblspec);
dbinst.query(quoteprop(schema), query, function (err, res) {
var stm = selectstm(qent, q, tblspec);
dbinst.exec(stm, function (err, rows) {
if (!error(args, err, cb)) {
res.rows.forEach(function (row) {
rows.forEach(function (row) {
var ent = relationalstore.makeent(qent, row, tblspec);
list.push(ent);
});
Expand All @@ -393,20 +403,20 @@ module.exports = function (opts) {
}

if (q.all$) {
var query = deletestm(qent, q, tblspec);
dbinst.query(quoteprop(schema), query, function (err, res) {
var stm = deletestm(qent, q, tblspec);
dbinst.exec(stm, function (err, rows) {
if (!error(args, err, cb)) {
seneca.log(args.tag$, 'remove', qent);
return cb();
}
});
} else {
var select = selectstm(qent, q, tblspec);
dbinst.query(quoteprop(schema), select, function (err, res) {
var selstm = selectstm(qent, q, tblspec);
dbinst.exec(selstm, function (err, rows) {
if (!error(args, err, cb)) {
var entp = res.rows[0];
var query = deletestm(qent, entp, tblspec);
dbinst.query(quoteprop(schema), query, function (err, res) {
var entp = rows[0];
var delstm = deletestm(qent, entp, tblspec);
dbinst.exec(delstm, function (err, rows) {
if (!error(args, err, cb)) {
seneca.log(args.tag$, 'remove', qent);
return cb();
Expand All @@ -420,7 +430,8 @@ module.exports = function (opts) {


native: function (args, cb) {
cb(null, dbinst);
// cb(null, dbinst);
cb()
}

};
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "seneca-hana-store",
"version": "0.1.2",
"description": "Seneca data store plugin for SAP HANA ODBC",
"version": "0.1.3",
"description": "Seneca data store plugin for SAP HANA",
"main": "lib/hana-store.js",
"author": "Marian Radulescu",
"license": "MIT",
Expand All @@ -20,7 +20,7 @@
"scripts": {},
"dependencies": {
"seneca": "0.5.11",
"hana-odbc": "0.1.3",
"hdb": "0.2.0",
"underscore": "1.4.x",
"node-uuid": "1.4.x",
"moment": "2.2.x"
Expand Down
12 changes: 3 additions & 9 deletions scripts/mktestschema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ SCHEMA=hana_test
function create_sql_file {
echo "CREATE SCHEMA \"$SCHEMA\"" > $SQL_FILE
echo "DROP TABLE \"$SCHEMA\".\"foo\" CASCADE" >> $SQL_FILE
echo "CREATE ROW TABLE \"$SCHEMA\".\"foo\"( \"id\" INTEGER not null, \"p1\" NVARCHAR (100) null, \"p2\" NVARCHAR (100) null, primary key (\"id\"))" >> $SQL_FILE
echo "DROP SEQUENCE \"$SCHEMA\".\"moon_bar_id\"" >> $SQL_FILE
echo "CREATE SEQUENCE \"$SCHEMA\".\"moon_bar_id\" START WITH 1" >> $SQL_FILE
echo "CREATE ROW TABLE \"$SCHEMA\".\"foo\"( \"id\" NVARCHAR(36) not null, \"p1\" NVARCHAR (100) null, \"p2\" NVARCHAR (100) null, primary key (\"id\"))" >> $SQL_FILE

echo "DROP TABLE \"$SCHEMA\".\"moon_bar\" CASCADE" >> $SQL_FILE
echo "CREATE ROW TABLE \"$SCHEMA\".\"moon_bar\" (\"id\" INTEGER, \"str\" NVARCHAR(100), \"int\" INTEGER, \"dec\" DOUBLE, \"bol\" NVARCHAR(5), \"wen\" NVARCHAR(100), \"arr\" NVARCHAR(1000), \"obj\" NVARCHAR(1000), \"mark\" NVARCHAR(100), \"seneca\" NVARCHAR(1000))" >> $SQL_FILE
echo "DROP SEQUENCE \"$SCHEMA\".\"foo_id\"" >> $SQL_FILE
echo "CREATE SEQUENCE \"$SCHEMA\".\"foo_id\" START WITH 1" >> $SQL_FILE
echo "CREATE ROW TABLE \"$SCHEMA\".\"moon_bar\" (\"id\" NVARCHAR(36), \"str\" NVARCHAR(100), \"int\" INTEGER, \"dec\" DOUBLE, \"bol\" NVARCHAR(5), \"wen\" NVARCHAR(100), \"arr\" NVARCHAR(1000), \"obj\" NVARCHAR(1000), \"mark\" NVARCHAR(100), \"seneca\" NVARCHAR(1000))" >> $SQL_FILE

echo "DROP TABLE \"$SCHEMA\".\"mapping_test\" CASCADE" >> $SQL_FILE
echo "CREATE ROW TABLE \"$SCHEMA\".\"mapping_test\" (\"id\" INTEGER, \"ctimestamp\" TIMESTAMP, \"cseconddate\" SECONDDATE, \"cdate\" DATE, \"ctime\" TIME, \"cdouble\" DOUBLE, \"creal\" REAL, \"cdecimal\" DECIMAL, \"csmalldecimal\" SMALLDECIMAL, \"cbigint\" BIGINT, \"cinteger\" INTEGER, \"csmallint\" SMALLINT, \"ctinyint\" TINYINT, \"cnclob\" NCLOB, \"cnvarchar\" NVARCHAR, \"cclob\" CLOB, \"cvarchar\" VARCHAR, \"cblob\" BLOB, \"cvarbinary\" VARBINARY, \"seneca\" NVARCHAROH(1000))" >> $SQL_FILE
echo "DROP SEQUENCE \"$SCHEMA\".\"mapping_test_id\"" >> $SQL_FILE
echo "CREATE SEQUENCE \"$SCHEMA\".\"mapping_test_id\" START WITH 1" >> $SQL_FILE
echo "CREATE ROW TABLE \"$SCHEMA\".\"mapping_test\" (\"id\" NVARCHAR(36), \"ctimestamp\" TIMESTAMP, \"cseconddate\" SECONDDATE, \"cdate\" DATE, \"ctime\" TIME, \"cdouble\" DOUBLE, \"creal\" REAL, \"cdecimal\" DECIMAL, \"csmalldecimal\" SMALLDECIMAL, \"cbigint\" BIGINT, \"cinteger\" INTEGER, \"csmallint\" SMALLINT, \"ctinyint\" TINYINT, \"cnclob\" NCLOB, \"cnvarchar\" NVARCHAR, \"cclob\" CLOB, \"cvarchar\" VARCHAR, \"cblob\" BLOB, \"cvarbinary\" VARBINARY, \"seneca\" NVARCHAROH(1000))" >> $SQL_FILE

}

Expand Down
Loading

0 comments on commit 4ddcb48

Please sign in to comment.