Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Adding support for multiple tables with same name across different databases #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)
--

Copyright © 2015 Cody Stoltman.
Copyright © 2014 Cody Stoltman.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
Waterline-Sequel [![Build Status](https://travis-ci.org/balderdashy/waterline-sequel.svg?branch=master)](https://travis-ci.org/balderdashy/waterline-sequel)
Waterline-Sequel
====================

A helper library for generating SQL queries from the Waterline Query Language.

### Running the tests
Simply run `npm test`.
55 changes: 44 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "waterline-sequel",
"description": "A helper library for generating SQL queries from the Waterline Query Language.",
"version": "0.2.1",
"author": "Cody Stoltman <particlebanana@gmail.com>",
"version": "0.1.1",
"author": {
"name": "Cody Stoltman",
"email": "particlebanana@gmail.com"
},
"url": "http://github.com/balderdashy/waterline-sequel",
"keywords": [],
"repository": {
@@ -12,18 +15,48 @@
"dependencies": {
"lodash": "~2.4.1"
},
"devDependencies": {
"chai": "^2.1.1",
"mocha": "^2.2.1",
"should": "^5.2.0"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha -b"
},
"devDependencies": {},
"scripts": {},
"main": "sequel/index",
"directories": {
"sequel": "./sequel"
},
"license": "MIT",
"bugs": "https://github.com/balderdashy/waterline-sequel/issues"
"bugs": {
"url": "https://github.com/balderdashy/waterline-sequel/issues"
},
"gitHead": "4dae54b8817491f54a092f147654224ffedebaff",
"homepage": "https://github.com/balderdashy/waterline-sequel",
"_id": "waterline-sequel@0.1.1",
"_shasum": "37ff379142eac50f3e01ebe2f2ee28f4680ee5ee",
"_from": "waterline-sequel@~0.1.1",
"_npmVersion": "1.4.28",
"_npmUser": {
"name": "particlebanana",
"email": "particlebanana@gmail.com"
},
"maintainers": [
{
"name": "sgress454",
"email": "scott@balderdash.co"
},
{
"name": "balderdashy",
"email": "mike@balderdash.co"
},
{
"name": "particlebanana",
"email": "particlebanana@gmail.com"
},
{
"name": "tjwebb",
"email": "me@traviswebb.com"
}
],
"dist": {
"shasum": "37ff379142eac50f3e01ebe2f2ee28f4680ee5ee",
"tarball": "http://registry.npmjs.org/waterline-sequel/-/waterline-sequel-0.1.1.tgz"
},
"_resolved": "https://registry.npmjs.org/waterline-sequel/-/waterline-sequel-0.1.1.tgz",
"readme": "ERROR: No README data found!"
}
46 changes: 5 additions & 41 deletions sequel/index.js
Original file line number Diff line number Diff line change
@@ -108,48 +108,10 @@ Sequel.prototype.find = function find(currentTable, queryObject) {

};

/**
* Build a SQL Count Query using the defined schema.
*/

Sequel.prototype.count = function count(currentTable, queryObject) {

// Step 1:
// Build out the Count statements
// TO-DO: limit this to a certain column, e.g. id, for performance gains
this.queries = ['SELECT COUNT(*) FROM ' + currentTable];

var whereObject;
var childQueries;
var query;
var values;

/**
* Step 2 - Build out the parent query.
*/

whereObject = this.simpleWhere(currentTable, queryObject);

this.queries[0] += ' ' + whereObject.query;
this.values[0] = whereObject.values;

/**
* Step 3 - Build out the child query templates.
*/

childQueries = this.complexWhere(currentTable, queryObject);
this.queries = this.queries.concat(childQueries);

return {
query: this.queries,
values: this.values
};

};


/**
* Build a SQL Create Query.
*
*/

Sequel.prototype.create = function create(currentTable, data) {
@@ -177,6 +139,7 @@ Sequel.prototype.create = function create(currentTable, data) {

/**
* Build a SQL Update Query.
*
*/

Sequel.prototype.update = function update(currentTable, queryObject, data) {
@@ -188,9 +151,9 @@ Sequel.prototype.update = function update(currentTable, queryObject, data) {
};

// Get the attribute identity (as opposed to the table name)
var identity = _.find(_.values(this.schema), {tableName: currentTable}).identity;
//var identity = _.find(_.values(this.schema), {tableName: currentTable}).identity;
// Create the query with the tablename aliased as the identity (in case they are different)
var query = 'UPDATE ' + utils.escapeName(currentTable, this.escapeCharacter) + ' AS ' + utils.escapeName(identity, this.escapeCharacter) + ' ';
var query = 'UPDATE ' + utils.escapeName(currentTable, this.escapeCharacter) + ' AS ' + utils.escapeName(currentTable, this.escapeCharacter) + ' ';

// Transform the Data object into arrays used in a parameterized query
var attributes = utils.mapAttributes(data, options);
@@ -231,6 +194,7 @@ Sequel.prototype.update = function update(currentTable, queryObject, data) {

/**
* Build Delete SQL query.
*
*/

Sequel.prototype.destroy = function destroy(currentTable, queryObject) {
15 changes: 8 additions & 7 deletions sequel/select.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ var hop = utils.object.hasOwnProperty;
var SelectBuilder = module.exports = function(schema, currentTable, queryObject, options) {

this.schema = schema;
this.tableName = currentTable;
this.currentTable = _.find(_.values(schema), {tableName: currentTable}).identity;
this.escapeCharacter = '"';
this.cast = false;
@@ -56,21 +57,21 @@ SelectBuilder.prototype.buildSimpleSelect = function buildSimpleSelect(queryObje
}

// Escape table name
var tableName = utils.escapeName(self.schema[self.currentTable].tableName, self.escapeCharacter);
var tableName = utils.escapeName(self.tableName, self.escapeCharacter);

var selectKeys = [];
var query = 'SELECT ';

var attributes = queryObject.select || Object.keys(this.schema[this.currentTable].attributes);
var attributes = queryObject.select || Object.keys(this.schema[this.tableName].attributes);
delete queryObject.select;

attributes.forEach(function(key) {
// Default schema to {} in case a raw DB column name is sent. This shouldn't happen
// after https://github.com/balderdashy/waterline/commit/687c869ad54f499018ab0b038d3de4435c96d1dd
// but leaving here as a failsafe.
var schema = self.schema[self.currentTable].attributes[key] || {};
var schema = self.schema[self.tableName].attributes[key] || {};
if(hop(schema, 'collection')) return;
selectKeys.push({ table: self.currentTable, key: schema.columnName || key });
selectKeys.push({ table: self.tableName, key: schema.columnName || key });
});

// Add any hasFK strategy joins to the main query
@@ -105,7 +106,7 @@ SelectBuilder.prototype.buildSimpleSelect = function buildSimpleSelect(queryObje
});

// Remove the last comma
query = query.slice(0, -2) + ' FROM ' + tableName + ' AS ' + utils.escapeName(self.currentTable, self.escapeCharacter) + ' ';
query = query.slice(0, -2) + ' FROM ' + tableName + ' AS ' + utils.escapeName(self.tableName, self.escapeCharacter) + ' ';

return query;
};
@@ -131,7 +132,7 @@ SelectBuilder.prototype.processAggregates = function processAggregates(criteria)


var query = 'SELECT ';
var tableName = utils.escapeName(this.currentTable, this.escapeCharacter);
var tableName = utils.escapeName(this.tableName, this.escapeCharacter);

// Append groupBy columns to select statement
if(criteria.groupBy) {
@@ -215,6 +216,6 @@ SelectBuilder.prototype.processAggregates = function processAggregates(criteria)
query = query.slice(0, -2) + ' ';

// Add FROM clause
query += 'FROM ' + utils.escapeName(self.schema[self.currentTable].tableName, self.escapeCharacter) + ' AS ' + tableName + ' ';
query += 'FROM ' + utils.escapeName(self.tableName, self.escapeCharacter) + ' AS ' + tableName + ' ';
return query;
};
7 changes: 4 additions & 3 deletions sequel/where.js
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ var hop = utils.object.hasOwnProperty;
var WhereBuilder = module.exports = function WhereBuilder(schema, currentTable, options) {

this.schema = schema;
this.tableName = currentTable;
this.currentTable = _.find(_.values(schema), {tableName: currentTable}).identity;

this.wlNext = {};
@@ -115,8 +116,8 @@ WhereBuilder.prototype.single = function single(queryObject, options) {
if(!hop(queryObject, 'sort')) {
var childPK;

_.keys(this.schema[this.currentTable].attributes).forEach(function(attr) {
var expandedAttr = self.schema[self.currentTable].attributes[attr];
_.keys(this.schema[this.tableName].attributes).forEach(function(attr) {
var expandedAttr = self.schema[self.tableName].attributes[attr];
if(!hop(expandedAttr, 'primaryKey')) return;
childPK = expandedAttr.columnName || attr;
});
@@ -142,7 +143,7 @@ WhereBuilder.prototype.single = function single(queryObject, options) {
wlNext: this.wlNext
}, options);

this.criteriaParser = new CriteriaParser(this.currentTable, this.schema, _options);
this.criteriaParser = new CriteriaParser(this.tableName, this.schema, _options);
parsedCriteria = this.criteriaParser.read(tmpCriteria);
queryString += parsedCriteria.query;