Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
Allow using the meta "schemaName" property to override schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McCallum authored and keeganmccallum committed Jul 22, 2015
1 parent b420eee commit 5587101
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
66 changes: 41 additions & 25 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ module.exports = (function () {

// Return attributes
describe: function (connection, collection, cb) {
// Add in logic here to describe a collection (e.g. DESCRIBE TABLE logic)
var tableName = collection;
var statement = "SELECT c.name AS ColumnName,TYPE_NAME(c.user_type_id) AS TypeName,c.is_nullable AS Nullable,c.is_identity AS AutoIncrement,ISNULL((SELECT is_unique FROM sys.indexes i LEFT OUTER JOIN sys.index_columns ic ON i.index_id=ic.index_id WHERE i.object_id=t.object_id AND ic.object_id=t.object_id AND ic.column_id=c.column_id),0) AS [Unique],ISNULL((SELECT is_primary_key FROM sys.indexes i LEFT OUTER JOIN sys.index_columns ic ON i.index_id=ic.index_id WHERE i.object_id=t.object_id AND ic.object_id=t.object_id AND ic.column_id=c.column_id),0) AS PrimaryKey,ISNULL((SELECT COUNT(*) FROM sys.indexes i LEFT OUTER JOIN sys.index_columns ic ON i.index_id=ic.index_id WHERE i.object_id=t.object_id AND ic.object_id=t.object_id AND ic.column_id=c.column_id),0) AS Indexed FROM sys.tables t INNER JOIN sys.columns c ON c.object_id=t.object_id LEFT OUTER JOIN sys.index_columns ic ON ic.object_id=t.object_id WHERE t.name='" + tableName + "'";
// Add in logic here to describe a collection (e.g. DESCRIBE TABLE logic)
var schemaName = getSchemaName(connection, collection);
var statement = "SELECT c.name AS ColumnName,TYPE_NAME(c.user_type_id) AS TypeName,c.is_nullable AS Nullable,c.is_identity AS AutoIncrement,ISNULL((SELECT is_unique FROM sys.indexes i LEFT OUTER JOIN sys.index_columns ic ON i.index_id=ic.index_id WHERE i.object_id=t.object_id AND ic.object_id=t.object_id AND ic.column_id=c.column_id),0) AS [Unique],ISNULL((SELECT is_primary_key FROM sys.indexes i LEFT OUTER JOIN sys.index_columns ic ON i.index_id=ic.index_id WHERE i.object_id=t.object_id AND ic.object_id=t.object_id AND ic.column_id=c.column_id),0) AS PrimaryKey,ISNULL((SELECT COUNT(*) FROM sys.indexes i LEFT OUTER JOIN sys.index_columns ic ON i.index_id=ic.index_id WHERE i.object_id=t.object_id AND ic.object_id=t.object_id AND ic.column_id=c.column_id),0) AS Indexed FROM sys.tables t INNER JOIN sys.columns c ON c.object_id=t.object_id LEFT OUTER JOIN sys.index_columns ic ON ic.object_id=t.object_id WHERE t.name='" + collection + "' AND OBJECT_SCHEMA_NAME(t.object_id) = '" + schemaName + "'";
adapter.connectConnection(connection, function __DESCRIBE__(err, uniqId) {
if (err) {
console.error(err);
Expand Down Expand Up @@ -223,14 +223,16 @@ module.exports = (function () {
*
*/
define: function (connection, collection, definition, cb) {
// Add in logic here to create a collection (e.g. CREATE TABLE logic)
// Add in logic here to create a collection (e.g. CREATE TABLE logic)
adapter.connectConnection(connection, function __DEFINE__(err, uniqId) {
if (err) {
console.error(err);
return cb(err);
}
var schema = sql.schema(collection, definition);
var statement = 'CREATE TABLE [' + collection + '] (' + schema + ')';
var schemaName = getSchemaName(connection, collection)
var tableName = '[' + schemaName + ']' + '.[' + collection + ']';
var statement = 'CREATE TABLE ' + tableName + ' (' + schema + ')';

uniqId = uniqId || false;
var mssqlConnect;
Expand Down Expand Up @@ -261,7 +263,9 @@ module.exports = (function () {
*/
drop: function (connection, collection, relations, cb) {
// Add in logic here to delete a collection (e.g. DROP TABLE logic)
var statement = 'IF OBJECT_ID(\'dbo.'+ collection + '\', \'U\') IS NOT NULL DROP TABLE [' + collection + ']';
var schemaName = getSchemaName(connection, collection);
var tableName = '[' + schemaName + ']' + '.[' + collection + ']';
var statement = 'IF OBJECT_ID(\''+ tableName + '\', \'U\') IS NOT NULL DROP TABLE '+tableName ;
adapter.connectConnection(connection, function __DROP__(err, uniqId) {
if (err) {
console.error(err);
Expand Down Expand Up @@ -299,15 +303,17 @@ module.exports = (function () {
*
*/
find: function (connection, collection, options, cb) {
// Check if this is an aggregate query and that there is something to return
// Check if this is an aggregate query and that there is something to return
if (options.groupBy || options.sum || options.average || options.min || options.max) {
if (!options.sum && !options.average && !options.min && !options.max) {
return cb(new Error('Cannot groupBy without a calculation'));
}
}

options.__primaryKey__ = adapter.getPrimaryKey(connection, collection);
var statement = sql.selectQuery(collection, options);
var schemaName = getSchemaName(connection, collection);
var tableName = '[' + schemaName + ']' + '.[' + collection + ']';
var statement = sql.selectQuery(tableName, options);
adapter.connectConnection(connection, function __FIND__(err, uniqId) {
if (err) {
console.error(err);
Expand Down Expand Up @@ -380,11 +386,13 @@ module.exports = (function () {
//console.log(pk, '==', key);
}
});
var statement = sql.insertQuery(collection, values);
var schemaName = getSchemaName(connection, collection);
var tableName = '[' + schemaName + ']' + '.[' + collection + ']';
var statement = sql.insertQuery(tableName, values);
if (identityInsert) {
statement = 'SET IDENTITY_INSERT '+ collection + ' ON; '+
statement = 'SET IDENTITY_INSERT '+ tableName + ' ON; '+
statement +
'SET IDENTITY_INSERT '+ collection + ' OFF;';
'SET IDENTITY_INSERT '+ tableName + ' OFF;';
}

//console.log('create statement:', statement);
Expand Down Expand Up @@ -440,12 +448,14 @@ module.exports = (function () {
},

update: function (connection, collection, options, values, cb) {
var tableName = collection;
var schemaName = getSchemaName(connection, collection);
var tableName = '[' + schemaName + ']' + '.[' + collection + ']';

var criteria = sql.serializeOptions(collection, options);

var pk = adapter.getPrimaryKey(connection, collection);

var statement = 'SELECT [' + pk + '] FROM [' + tableName + '] ' + criteria;
var statement = 'SELECT [' + pk + '] FROM' + tableName + ' ' + criteria;
adapter.connectConnection(connection, function __UPDATE__(err, uniqId) {
if (err) {
console.error(err);
Expand Down Expand Up @@ -480,7 +490,7 @@ module.exports = (function () {

delete values[pk];

statement = 'UPDATE [' + tableName + '] SET ' + sql.updateCriteria(collection, values) + ' ';
statement = 'UPDATE ' + tableName + ' SET ' + sql.updateCriteria(collection, values) + ' ';
statement += sql.serializeOptions(collection, options);

request.query(statement, function(err, _recordset) {
Expand All @@ -506,8 +516,9 @@ module.exports = (function () {
},

destroy: function (connection, collection, options, cb) {
var tableName = collection;
var statement = 'DELETE FROM [' + tableName + '] ';
var schemaName = getSchemaName(connection, collection);
var tableName = '[' + schemaName + ']' + '.[' + collection + ']';
var statement = 'DELETE FROM '+tableName;
statement += sql.serializeOptions(collection, options);
adapter.connectConnection(connection, function __DELETE__(err, uniqId) {
if (err) {
Expand Down Expand Up @@ -565,18 +576,23 @@ module.exports = (function () {

};

function marshalConfig(_config) {
var config = _.defaults(_config, {
server: _config.host,
pool: {
max: _config.pool.max,
min: _config.pool.min,
idleTimeoutMillis: _config.pool.idleTimeout * 1000
}
function marshalConfig(_config) {
var config = _.defaults(_config, {
server: _config.host,
pool: {
max: _config.pool.max,
min: _config.pool.min,
idleTimeoutMillis: _config.pool.idleTimeout * 1000
}
});

return config;
}
}

function getSchemaName (connection, collection) {
var collectionObject = connections[connection].collections[collection];
return collectionObject.meta && collectionObject.meta.schemaName ? collectionObject.meta.schemaName : 'dbo';
}

return adapter;
})();
6 changes: 3 additions & 3 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ var sql = {
addColumn: function (collectionName, attrName, attrDef) {
var tableName = collectionName;
var columnDefinition = sql._schema(collectionName, attrDef, attrName);
return 'ALTER TABLE [' + tableName + '] ADD ' + columnDefinition;
return 'ALTER TABLE ' + tableName + ' ADD ' + columnDefinition;
},

// @returns ALTER query for dropping a column
removeColumn: function (collectionName, attrName) {
var tableName = collectionName;
attrName = attrName;
return 'ALTER TABLE [' + tableName + '] DROP COLUMN ' + attrName;
return 'ALTER TABLE ' + tableName + ' DROP COLUMN ' + attrName;
},

selectQuery: function (collectionName, options) {
Expand All @@ -85,7 +85,7 @@ var sql = {

insertQuery: function (collectionName, data) {
var tableName = collectionName;
return 'INSERT INTO [' + tableName + '] ' + '(' + sql.attributes(collectionName, data) + ')' + ' VALUES (' + sql.values(collectionName, data) + '); SELECT @@IDENTITY AS [id]';
return 'INSERT INTO ' + tableName + ' (' + sql.attributes(collectionName, data) + ')' + ' VALUES (' + sql.values(collectionName, data) + '); SELECT @@IDENTITY AS [id]';
},

// Create a schema csv for a DDL query
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ utils.buildSelectStatement = function(criteria, table) {
query = query.slice(0, -2) + ' ';

// Add FROM clause
return query += 'FROM [' + table + '] ';
return query += 'FROM '+table;
}


Expand All @@ -155,5 +155,5 @@ utils.buildSelectStatement = function(criteria, table) {
query += 'TOP ' + criteria.limit + ' ';
}

return query += '* FROM [' + table + '] ';
return query += '* FROM '+table ;
};

0 comments on commit 5587101

Please sign in to comment.