Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Get started on SQLite adapter

  • Loading branch information...
commit fa57fa46a2145c123becdc9175e767c6b024e4fb 1 parent 35e6734
mde authored
View
41 lib/adapters/sql/base.js
@@ -18,6 +18,47 @@ utils.mixin(Adapter.prototype, new (function () {
return tableName;
};
+ this._createInsertStatement = function (item, props, useAutoIncrementId) {
+ var sql = ''
+ , modelName = item.type
+ , cols = []
+ , vals = [];
+
+ // If using string UUID ids
+ if (!useAutoIncrementId) {
+ item.id = utils.string.uuid();
+ cols.push(this._columnizePropertyName('id'));
+ vals.push(datatypes.string.serialize(item.id, {
+ escape: true
+ , useQuotes: true
+ }));
+ }
+ else {
+ cols.push(this._columnizePropertyName('id'));
+ vals.push('DEFAULT');
+ }
+
+ for (var p in props) {
+ def = props[p];
+ prop = item[p];
+ // Use the same definition of NULL as for updates
+ prop = this.transformValue(prop, def.datatype);
+ if (prop != 'NULL') {
+ cols.push(this._columnizePropertyName(p, {
+ useQuotes: true
+ }));
+ vals.push(prop);
+ }
+ }
+ sql += 'INSERT INTO ' + this._tableizeModelName(modelName) + ' ';
+ sql += '(' + cols.join(', ') + ')';
+ sql += ' VALUES ';
+ sql += '(' + vals.join(', ') + ')';
+ sql += ';';
+
+ return sql;
+ };
+
this._columnizePropertyName = function (name, options) {
var opts = options || {}
, columnName = utils.string.snakeize(name)
View
38 lib/adapters/sql/postgres.js
@@ -180,41 +180,9 @@ utils.mixin(Adapter.prototype, new (function () {
, sql = '';
items.forEach(function (item) {
- var cols = []
- , vals = [];
-
- // If using string UUID ids
- if (!ctor.autoIncrementId) {
- item.id = utils.string.uuid();
- cols.push(self._columnizePropertyName('id'));
- vals.push(datatypes.string.serialize(item.id, {
- escape: true
- , useQuotes: true
- }));
- }
- else {
- cols.push(self._columnizePropertyName('id'));
- vals.push('DEFAULT');
- }
-
- for (var p in props) {
- def = props[p];
- prop = item[p];
- // Use the same definition of NULL as for updates
- prop = self.transformValue(prop, def.datatype);
- if (prop != 'NULL') {
- cols.push(self._columnizePropertyName(p, {
- useQuotes: true
- }));
- vals.push(prop);
- }
- }
- sql += 'INSERT INTO ' + self._tableizeModelName(modelName) + ' ';
- sql += '(' + cols.join(', ') + ')';
- sql += ' VALUES ';
- sql += '(' + vals.join(', ') + ')';
- sql += ' RETURNING id'
- sql += ';\n';
+ var statement = self._createInsertStatement(item, props, ctor.autoIncrementId);
+ statement = statement.replace(/;$/, ' RETURNING id;');
+ sql += statement;
});
this.exec(sql, function (err, res) {
View
64 lib/adapters/sql/sqlite.js
@@ -0,0 +1,64 @@
+var file = require('utilities').file
+ , sqlite = file.requireLocal('node-sqlite')
+ , generator = require('../../../lib/generators/sql')
+ , utils = require('utilities')
+ , model = require('../../../lib')
+ , Query = require('../../../lib/query/query').Query
+ , BaseAdapter = require('./base').Adapter
+ , Adapter
+ , _baseConfig;
+
+_baseConfig = {
+ database: process.env.USER
+};
+
+Adapter = function (options) {
+ var opts = options || {}
+ , config;
+
+ this.name = 'sqlite';
+ this.config = _baseConfig;
+ this.client = null;
+
+ utils.mixin(this.config, opts);
+
+ this.init.apply(this, arguments);
+};
+
+Adapter.prototype = new BaseAdapter();
+Adapter.prototype.constructor = Adapter;
+
+utils.mixin(Adapter.prototype, new (function () {
+
+ this.init = function () {
+ this.client = sqlite.openDatabaseSync(this.config.database);
+ };
+
+ this.connect = function () {};
+
+ this.disconnect = function () {};
+
+ this.exec = function (query, callback) {
+ this.client.query(query, callback);
+ };
+
+ this.load = function (query, callback) {
+ };
+
+ this.update = function (data, query, callback) {
+ };
+
+ this.remove = function (query, callback) {
+ };
+
+ this.insert = function (data, opts, callback) {
+ };
+
+
+ this.createTable = function (names, callback) {
+ };
+
+})());
+
+module.exports.Adapter = Adapter;
+
Please sign in to comment.
Something went wrong with that request. Please try again.