mirror of
https://github.com/knex/knex.git
synced 2025-12-25 05:59:00 +00:00
Adapter for FoundationDB SQL Layer
This commit is contained in:
parent
8c8fcb5b29
commit
2d6d2f28f7
@ -20,13 +20,14 @@ By default, Knex runs tests against MySQL (using [mysql](https://github.com/feli
|
||||
No setup is required for SQLite.
|
||||
|
||||
### Specifying Databases
|
||||
You can optionally specify which dialects to test using the `KNEX_TEST_INTEGRATION_DIALECTS` environment variable. Values shoud be space separated and can include:
|
||||
You can optionally specify which dialects to test using the `KNEX_TEST_INTEGRATION_DIALECTS` environment variable. Values should be space separated and can include:
|
||||
* mysql
|
||||
* mysql2
|
||||
* postgres
|
||||
* sqlite3
|
||||
* maria
|
||||
* oracle
|
||||
* fdbsql
|
||||
|
||||
```bash
|
||||
$ KNEX_TEST_INTEGRATION_DIALECTS='postgres mysql' npm test
|
||||
|
||||
@ -17,7 +17,8 @@ var excluded = {
|
||||
mysql: ['mysql'],
|
||||
mysql2: ['mysql2'],
|
||||
pg: ['pg', 'pg.js', 'pg-query-stream'],
|
||||
websql: ['sqlite3']
|
||||
websql: ['sqlite3'],
|
||||
fdbsql: ['fdbsql']
|
||||
};
|
||||
|
||||
var bases = {
|
||||
@ -28,7 +29,8 @@ var bases = {
|
||||
mysql2: './lib/dialects/mysql2',
|
||||
pg: './lib/dialects/postgres',
|
||||
sqlite3: './lib/dialects/sqlite3',
|
||||
websql: './lib/dialects/websql'
|
||||
websql: './lib/dialects/websql',
|
||||
fdbsql: './lib/dialects/fdbsql'
|
||||
};
|
||||
|
||||
var alwaysExcluded = ['./lib/migrate/index.js', './lib/seed/index.js'];
|
||||
@ -56,7 +58,7 @@ function build(targets) {
|
||||
}
|
||||
|
||||
function buildKnex() {
|
||||
var b = build(['mysql', 'mysql2', 'mariasql', 'pg', 'sqlite3', 'websql', 'oracle']);
|
||||
var b = build(['mysql', 'mysql2', 'mariasql', 'pg', 'sqlite3', 'websql', 'oracle', 'fdbsql']);
|
||||
var outStream = fs.createWriteStream('./browser/knex.js');
|
||||
b.bundle().pipe(outStream);
|
||||
return outStream;
|
||||
|
||||
4
knex.js
4
knex.js
@ -33,6 +33,7 @@ var pg = function() { return require('./lib/dialects/postgres'); };
|
||||
var sqlite3 = function() { return require('./lib/dialects/sqlite3'); };
|
||||
var strong_oracle = function() { return require('./lib/dialects/strong-oracle'); };
|
||||
var websql = function() { return require('./lib/dialects/websql'); };
|
||||
var fdbsql = function() { return require('./lib/dialects/fdbsql'); };
|
||||
|
||||
// The client names we'll allow in the `{name: lib}` pairing.
|
||||
var Clients = Knex.Clients = {
|
||||
@ -48,7 +49,8 @@ var Clients = Knex.Clients = {
|
||||
'sqlite' : sqlite3,
|
||||
'sqlite3' : sqlite3,
|
||||
'strong-oracle': strong_oracle,
|
||||
'websql' : websql
|
||||
'websql' : websql,
|
||||
'fdbsql' : fdbsql
|
||||
};
|
||||
|
||||
// Each of the methods which may be statically chained from knex.
|
||||
|
||||
63
lib/dialects/fdbsql/formatter.js
Normal file
63
lib/dialects/fdbsql/formatter.js
Normal file
@ -0,0 +1,63 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer Formatter
|
||||
// This file was adapted from the PostgreSQL Formatter
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Formatter = require('../../formatter');
|
||||
var inherits = require('inherits');
|
||||
|
||||
// The "formatter" is used to ensure all output is properly
|
||||
// escaped & parameterized.
|
||||
function Formatter_FDB() {
|
||||
this.client = client;
|
||||
this.paramCount = 0;
|
||||
Formatter.apply(this, arguments);
|
||||
}
|
||||
inherits(Formatter_FDB, Formatter);
|
||||
|
||||
Formatter_FDB.prototype.operators = [
|
||||
'=', '<', '>', '<=', '>=', '<>', '!=',
|
||||
'like', 'not like', 'between', 'ilike',
|
||||
'&', '|', '#', '<<', '>>', '&&', '^', '||'
|
||||
];
|
||||
|
||||
// Wraps a value (column, tableName) with the correct ticks.
|
||||
Formatter_FDB.prototype.wrapValue = function(value) {
|
||||
if (value === '*') return value;
|
||||
return this.escapeIdentifier(value);
|
||||
};
|
||||
|
||||
// Properly escapes identifier for query.
|
||||
Formatter_FDB.prototype.escapeIdentifier = function(str) {
|
||||
var escaped = '"';
|
||||
for(var i = 0; i < str.length; i++) {
|
||||
var c = str[i];
|
||||
if(c === '"') {
|
||||
escaped += c + c;
|
||||
} else {
|
||||
escaped += c;
|
||||
}
|
||||
}
|
||||
escaped += '"';
|
||||
return escaped;
|
||||
};
|
||||
|
||||
// Memoize the calls to "wrap" for a little extra perf.
|
||||
var wrapperMemo = (function(){
|
||||
var memo = Object.create(null);
|
||||
return function(key) {
|
||||
if (memo[key] === void 0) {
|
||||
memo[key] = this._wrapString(key);
|
||||
}
|
||||
return memo[key];
|
||||
};
|
||||
}());
|
||||
|
||||
Formatter_FDB.prototype._wrap = wrapperMemo;
|
||||
|
||||
// Assign the formatter to the the client.
|
||||
client.Formatter = Formatter_FDB;
|
||||
|
||||
};
|
||||
16
lib/dialects/fdbsql/functionhelper.js
Normal file
16
lib/dialects/fdbsql/functionhelper.js
Normal file
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer Function Helper
|
||||
// This file was adapted from the PostgreSQL Function Helper
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var FunctionHelper = require('../../functionhelper');
|
||||
|
||||
var FunctionHelper_FDB = Object.create(FunctionHelper);
|
||||
FunctionHelper_FDB.client = client;
|
||||
|
||||
// Assign the newly extended `FunctionHelper` constructor to the client object.
|
||||
client.FunctionHelper = FunctionHelper_FDB;
|
||||
|
||||
};
|
||||
138
lib/dialects/fdbsql/index.js
Normal file
138
lib/dialects/fdbsql/index.js
Normal file
@ -0,0 +1,138 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer index
|
||||
// This file was adapted from the PostgreSQL index
|
||||
|
||||
var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
|
||||
var Client = require('../../client');
|
||||
var Promise = require('../../promise');
|
||||
|
||||
var pg;
|
||||
|
||||
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
||||
// objects, which extend the base 'lib/query/builder' and
|
||||
// 'lib/query/compiler', respectively.
|
||||
function Client_FDB(config) {
|
||||
Client.apply(this, arguments);
|
||||
if (config.returning) this.defaultReturning = config.returning;
|
||||
if (config.debug) this.isDebugging = true;
|
||||
if (config.connection) {
|
||||
// If no port has been defined,
|
||||
if (!config.connection.port)
|
||||
// set the port to the SQL Layer's default port, 15432.
|
||||
config.connection.port = 15432;
|
||||
this.initDriver();
|
||||
this.initRunner();
|
||||
this.connectionSettings = config.connection;
|
||||
this.initPool();
|
||||
this.pool = new this.Pool(config.pool);
|
||||
}
|
||||
}
|
||||
inherits(Client_FDB, Client);
|
||||
|
||||
Client_FDB.prototype.dialect = 'fdbsql';
|
||||
|
||||
// Lazy load the pg dependency, since we might just be using
|
||||
// the client to generate SQL strings.
|
||||
Client_FDB.prototype.initDriver = function() {
|
||||
pg = pg || (function() {
|
||||
try {
|
||||
return require('pg');
|
||||
} catch (e) {
|
||||
return require('pg.js');
|
||||
}
|
||||
})();
|
||||
|
||||
};
|
||||
|
||||
// Attach a `Formatter` constructor to the client object.
|
||||
Client_FDB.prototype.initFormatter = function() {
|
||||
require('./formatter')(this);
|
||||
};
|
||||
|
||||
// Attaches the `Raw` constructor to the client object.
|
||||
Client_FDB.prototype.initRaw = function() {
|
||||
require('./raw')(this);
|
||||
};
|
||||
|
||||
// Attaches the `FunctionHelper` constructor to the client object.
|
||||
Client_FDB.prototype.initFunctionHelper = function() {
|
||||
require('./functionhelper')(this);
|
||||
};
|
||||
|
||||
// Attaches the `Transaction` constructor to the client object.
|
||||
Client_FDB.prototype.initTransaction = function() {
|
||||
require('./transaction')(this);
|
||||
};
|
||||
|
||||
// Attaches `QueryBuilder` and `QueryCompiler` constructors
|
||||
// to the client object.
|
||||
Client_FDB.prototype.initQuery = function() {
|
||||
require('./query')(this);
|
||||
};
|
||||
|
||||
// Initializes a new pool instance for the current client.
|
||||
Client_FDB.prototype.initPool = function() {
|
||||
require('./pool')(this);
|
||||
};
|
||||
|
||||
// Initialize the query "runner"
|
||||
Client_FDB.prototype.initRunner = function() {
|
||||
require('./runner')(this);
|
||||
};
|
||||
|
||||
// Lazy-load the schema dependencies; we may not need to use them.
|
||||
Client_FDB.prototype.initSchema = function() {
|
||||
require('./schema')(this);
|
||||
};
|
||||
|
||||
// Lazy-load the migration dependency
|
||||
Client_FDB.prototype.initMigrator = function() {
|
||||
require('./migrator')(this);
|
||||
};
|
||||
|
||||
// Lazy-load the seeding dependency
|
||||
Client_FDB.prototype.initSeeder = function() {
|
||||
require('./seeder')(this);
|
||||
};
|
||||
|
||||
var utils;
|
||||
|
||||
// Prep the bindings as needed by fdb SQL layer.
|
||||
Client_FDB.prototype.prepBindings = function(bindings, tz) {
|
||||
/*jshint unused: false*/
|
||||
utils = utils || require('./utils');
|
||||
return _.map(bindings, utils.prepareValue);
|
||||
};
|
||||
|
||||
// Get a raw connection, called by the `pool` whenever a new
|
||||
// connection needs to be added to the pool.
|
||||
Client_FDB.prototype.acquireRawConnection = Promise.method(function(callback) {
|
||||
/*jshint unused: false*/
|
||||
// TODO: use callback or remove callback
|
||||
var connection = new pg.Client(this.connectionSettings);
|
||||
this.databaseName = connection.database;
|
||||
|
||||
var client = this;
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
connection.connect(function(err, connection) {
|
||||
if (err) return rejecter(err);
|
||||
resolver(connection);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Used to explicitly close a connection, called internally by the pool
|
||||
// when a connection times out or the pool is shutdown.
|
||||
Client_FDB.prototype.destroyRawConnection = function(connection) {
|
||||
connection.end();
|
||||
};
|
||||
|
||||
// Position the bindings for the query.
|
||||
Client_FDB.prototype.positionBindings = function(sql) {
|
||||
return sql;
|
||||
};
|
||||
|
||||
module.exports = Client_FDB;
|
||||
22
lib/dialects/fdbsql/migrator.js
Normal file
22
lib/dialects/fdbsql/migrator.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer migrator
|
||||
// This file was adapted from the PostgreSQL migrator
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Migrator = require('../../migrate');
|
||||
var inherits = require('inherits');
|
||||
|
||||
// Inherit from the `Migrator` constructor's prototype,
|
||||
// so we can add the correct `then` method.
|
||||
function Migrator_FDB() {
|
||||
this.client = client;
|
||||
Migrator.apply(this, arguments);
|
||||
}
|
||||
inherits(Migrator_FDB, Migrator);
|
||||
|
||||
// Assign the newly extended `Migrator` constructor to the client object.
|
||||
client.Migrator = Migrator_FDB;
|
||||
|
||||
};
|
||||
21
lib/dialects/fdbsql/pool.js
Normal file
21
lib/dialects/fdbsql/pool.js
Normal file
@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer pool
|
||||
// This file was adapted from the PostgreSQL pool
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Pool = require('../../pool');
|
||||
var inherits = require('inherits');
|
||||
|
||||
// Inherit from the `Pool` constructor's prototype.
|
||||
function Pool_FDB() {
|
||||
this.client = client;
|
||||
Pool.apply(this, arguments);
|
||||
}
|
||||
inherits(Pool_FDB, Pool);
|
||||
|
||||
// Assign the newly extended `Pool` constructor to the client object.
|
||||
client.Pool = Pool_FDB;
|
||||
|
||||
};
|
||||
141
lib/dialects/fdbsql/query.js
Normal file
141
lib/dialects/fdbsql/query.js
Normal file
@ -0,0 +1,141 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer Query Builder & Compiler
|
||||
// This file was adapted from the PostgreSQL Query Builder & Compiler
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
|
||||
var QueryBuilder = require('../../query/builder');
|
||||
var QueryCompiler = require('../../query/compiler');
|
||||
|
||||
// Query Builder
|
||||
// ------
|
||||
|
||||
function QueryBuilder_FDB() {
|
||||
this.client = client;
|
||||
QueryBuilder.apply(this, arguments);
|
||||
if (client.defaultReturning) {
|
||||
this._single.returning = client.defaultReturning;
|
||||
}
|
||||
}
|
||||
inherits(QueryBuilder_FDB, QueryBuilder);
|
||||
|
||||
// Query Compiler
|
||||
// ------
|
||||
|
||||
function QueryCompiler_FDB() {
|
||||
this.formatter = new client.Formatter();
|
||||
QueryCompiler.apply(this, arguments);
|
||||
}
|
||||
inherits(QueryCompiler_FDB, QueryCompiler);
|
||||
|
||||
// Compiles a truncate query.
|
||||
QueryCompiler_FDB.prototype.truncate = function() {
|
||||
return 'truncate table ' + this.tableName;
|
||||
};
|
||||
|
||||
// Used when the insert call is empty.
|
||||
QueryCompiler_FDB.prototype._emptyInsertValue = 'default values';
|
||||
|
||||
// is used if the an array with multiple empty values supplied
|
||||
QueryCompiler_FDB.prototype._defaultInsertValue = 'default';
|
||||
|
||||
// The locks are not applicable in SQL Layer
|
||||
QueryCompiler_FDB.prototype.forShare =
|
||||
QueryCompiler_FDB.prototype.forUpdate = function() {
|
||||
return '';
|
||||
};
|
||||
|
||||
// Compiles an `insert` query, allowing for multiple
|
||||
// inserts using a single query statement.
|
||||
QueryCompiler_FDB.prototype.insert = function() {
|
||||
var self = this;
|
||||
var insertValues = this.single.insert;
|
||||
|
||||
var sql = 'insert into ' + this.tableName + ' ';
|
||||
|
||||
if (_.isArray(insertValues) && (insertValues.length === 1) && _.isEmpty(insertValues[0])) {
|
||||
insertValues = [];
|
||||
}
|
||||
|
||||
if (_.isEmpty(insertValues) && !_.isFunction(insertValues)) {
|
||||
sql += this._emptyInsertValue;
|
||||
} else {
|
||||
var insertData = this._prepInsert(insertValues);
|
||||
|
||||
if (_.isString(insertData)) {
|
||||
sql += insertData;
|
||||
} else {
|
||||
if (insertData.columns.length) {
|
||||
sql += '(' + this.formatter.columnize(insertData.columns) + ') values (' +
|
||||
_.map(insertData.values, this.formatter.parameterize, this.formatter).join('), (') + ')';
|
||||
} else {
|
||||
// if there is no target column only insert default values
|
||||
sql += 'values ' + _.map(insertData.values, function () { return '(' + (self._defaultInsertValue || '') + ')'; }).join(', ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var returning = this.single.returning;
|
||||
return {
|
||||
sql: sql + this._returning(returning),
|
||||
returning: returning
|
||||
};
|
||||
};
|
||||
|
||||
// Compiles an `update` query, allowing for a return value.
|
||||
QueryCompiler_FDB.prototype.update = function() {
|
||||
var updateData = this._prepUpdate(this.single.update);
|
||||
var wheres = this.where();
|
||||
var returning = this.single.returning;
|
||||
return {
|
||||
sql: 'update ' + this.tableName + ' set ' + updateData.join(', ') +
|
||||
(wheres ? ' ' + wheres : '') +
|
||||
this._returning(returning),
|
||||
returning: returning
|
||||
};
|
||||
};
|
||||
|
||||
// Compiles a `delete` query, allowing for a return value.
|
||||
QueryCompiler_FDB.prototype.del = function() {
|
||||
var sql = QueryCompiler.prototype.del.apply(this, arguments);
|
||||
var returning = this.single.returning;
|
||||
return {
|
||||
sql: sql + this._returning(returning),
|
||||
returning: returning
|
||||
};
|
||||
};
|
||||
|
||||
QueryCompiler_FDB.prototype._returning = function(value) {
|
||||
return value ? ' returning ' + this.formatter.columnize(value) : '';
|
||||
};
|
||||
|
||||
// Compiles a columnInfo query
|
||||
QueryCompiler_FDB.prototype.columnInfo = function() {
|
||||
var column = this.single.columnInfo;
|
||||
return {
|
||||
sql: 'select * from information_schema.columns where table_name = ? and table_schema = ? and table_schema = CURRENT_SCHEMA',
|
||||
bindings: [this.single.table, client.database()],
|
||||
output: function(resp) {
|
||||
var out = _.reduce(resp.rows, function(columns, val) {
|
||||
var maxLen = val.character_maximum_length;
|
||||
columns[val.column_name] = {
|
||||
type: val.data_type,
|
||||
maxLength: maxLen ? parseInt(maxLen) : null,
|
||||
nullable: (val.is_nullable === 'YES'),
|
||||
defaultValue: val.column_default
|
||||
};
|
||||
return columns;
|
||||
}, {});
|
||||
return column && out[column] || out;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
client.QueryBuilder = QueryBuilder_FDB;
|
||||
client.QueryCompiler = QueryCompiler_FDB;
|
||||
|
||||
};
|
||||
22
lib/dialects/fdbsql/raw.js
Normal file
22
lib/dialects/fdbsql/raw.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer raw
|
||||
// This file was adapted from the PostgreSQL raw
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Raw = require('../../raw');
|
||||
var inherits = require('inherits');
|
||||
|
||||
// Inherit from the `Raw` constructor's prototype,
|
||||
// so we can add the correct `then` method.
|
||||
function Raw_FDB() {
|
||||
this.client = client;
|
||||
Raw.apply(this, arguments);
|
||||
}
|
||||
inherits(Raw_FDB, Raw);
|
||||
|
||||
// Assign the newly extended `Raw` constructor to the client object.
|
||||
client.Raw = Raw_FDB;
|
||||
|
||||
};
|
||||
146
lib/dialects/fdbsql/runner.js
Normal file
146
lib/dialects/fdbsql/runner.js
Normal file
@ -0,0 +1,146 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
var Promise = require('../../promise');
|
||||
|
||||
var Runner = require('../../runner');
|
||||
|
||||
// Inherit from the `Runner` constructor's prototype,
|
||||
// so we can add the correct `then` method.
|
||||
function Runner_FDB() {
|
||||
this.client = client;
|
||||
Runner.apply(this, arguments);
|
||||
}
|
||||
inherits(Runner_FDB, Runner);
|
||||
|
||||
var FDBQueryStream;
|
||||
Runner_FDB.prototype._stream = Promise.method(function(obj, stream, options) {
|
||||
FDBQueryStream = FDBQueryStream || require('pg-query-stream');
|
||||
var runner = this;
|
||||
var sql = obj.sql = this.client.positionBindings(obj.sql);
|
||||
if (this.isDebugging()) this.debug(obj);
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
stream.on('error', rejecter);
|
||||
stream.on('end', resolver);
|
||||
runner.connection.query(new FDBQueryStream(sql, obj.bindings, options)).pipe(stream);
|
||||
});
|
||||
});
|
||||
|
||||
// TODO: Could also retry for STALE_STATEMENT_CODE = '0A50A' if pg library
|
||||
// allowed us to easily track preparation of statements.
|
||||
// If the error code from the SQL Layer begins with 40, we should retry the query.
|
||||
var RETRY_CODE = '40';
|
||||
function shouldRetry(err) {
|
||||
return (err.code.substring(0, RETRY_CODE.length) === RETRY_CODE);
|
||||
}
|
||||
|
||||
// Runs the query on the specified connection and retries upon failure when appropriate.
|
||||
var retryQuery = function(connection, resolver, rejecter, obj, sql) {
|
||||
connection.query(sql, obj.bindings, function(err, response) {
|
||||
if (err) {
|
||||
if(shouldRetry(err))
|
||||
return retryQuery(connection, resolver, rejecter, obj, sql);
|
||||
else
|
||||
return rejecter(err);
|
||||
} else {
|
||||
obj.response = response;
|
||||
resolver(obj);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Calls the query method that retries when appropriate, providing the bindings
|
||||
// and any other necessary prep work.
|
||||
Runner_FDB.prototype._query = Promise.method(function(obj) {
|
||||
var connection = this.connection;
|
||||
var sql = obj.sql = this.client.positionBindings(obj.sql);
|
||||
if (this.isDebugging()) this.debug(obj);
|
||||
if (obj.options) sql = _.extend({text: sql}, obj.options);
|
||||
// Handle beginnings and endings of transactions appropriately
|
||||
if (sql === this._beginTransaction) this.currentTransaction = [];
|
||||
if (this.currentTransaction) this.currentTransaction[this.currentTransaction.length] = [obj, sql];
|
||||
var currentTransaction = this.currentTransaction;
|
||||
if (sql === this._commitTransaction || sql === this._rollbackTransaction) this.currentTransaction = null;
|
||||
// Take this path if we're in a transaction, otherwise take the retryQuery path
|
||||
if (currentTransaction) {
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
queryOnce(connection, resolver, rejecter, currentTransaction, obj, sql);
|
||||
});
|
||||
}
|
||||
else {
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
retryQuery(connection, resolver, rejecter, obj, sql);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Specifically for transactions: tries to execute query and retries entire transaction upon failure
|
||||
var queryOnce = function(connection , resolver, rejecter, currentTransaction, obj, sql) {
|
||||
connection.query(sql, obj.bindings, function(err, response) {
|
||||
if (err) {
|
||||
if(shouldRetry(err))
|
||||
return retryTransaction(connection, resolver, rejecter, currentTransaction);
|
||||
else
|
||||
return rejecter(err);
|
||||
} else {
|
||||
obj.response = response;
|
||||
resolver(obj);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Runs the transaction query on the specified connection and retries upon failure when appropriate.
|
||||
var retryTransaction = function(connection, resolver, rejecter, currentTransaction) {
|
||||
currentTransaction.forEach(
|
||||
function(currentValue) {
|
||||
connection.query(currentValue[1], currentValue[0].bindings, function(err, response) {
|
||||
if (err) {
|
||||
if(shouldRetry(err))
|
||||
return retryTransaction(connection, resolver, rejecter, currentTransaction);
|
||||
else
|
||||
return rejecter(err);
|
||||
} else {
|
||||
currentValue[0].response = response;
|
||||
resolver(currentValue[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// Ensures the response is returned in the same format as other clients.
|
||||
Runner_FDB.prototype.processResponse = function(obj) {
|
||||
var resp = obj.response;
|
||||
if (obj.output) return obj.output.call(this, resp);
|
||||
if (obj.method === 'raw') return resp;
|
||||
var returning = obj.returning;
|
||||
if (resp.command === 'SELECT') {
|
||||
if (obj.method === 'first') return resp.rows[0];
|
||||
if (obj.method === 'pluck') return _.pluck(resp.rows, obj.pluck);
|
||||
return resp.rows;
|
||||
}
|
||||
if (returning) {
|
||||
var returns = [];
|
||||
for (var i = 0, l = resp.rows.length; i < l; i++) {
|
||||
var row = resp.rows[i];
|
||||
if (returning === '*' || _.isArray(returning)) {
|
||||
returns[i] = row;
|
||||
} else {
|
||||
returns[i] = row[returning];
|
||||
}
|
||||
}
|
||||
return returns;
|
||||
}
|
||||
if (resp.command === 'UPDATE' || resp.command === 'DELETE') {
|
||||
return resp.rowCount;
|
||||
}
|
||||
return resp;
|
||||
};
|
||||
|
||||
// Assign the newly extended `Runner` constructor to the client object.
|
||||
client.Runner = Runner_FDB;
|
||||
|
||||
};
|
||||
57
lib/dialects/fdbsql/schema/column.js
Normal file
57
lib/dialects/fdbsql/schema/column.js
Normal file
@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer Column Builder & Compiler
|
||||
// This file was adapted from the PostgreSQL Column Builder & Compiler
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var inherits = require('inherits');
|
||||
var Schema = require('../../../schema');
|
||||
|
||||
// Column Builder
|
||||
// ------
|
||||
|
||||
function ColumnBuilder_FDB() {
|
||||
this.client = client;
|
||||
Schema.ColumnBuilder.apply(this, arguments);
|
||||
}
|
||||
inherits(ColumnBuilder_FDB, Schema.ColumnBuilder);
|
||||
|
||||
function ColumnCompiler_FDB() {
|
||||
this.modifiers = ['nullable', 'defaultTo'];
|
||||
this.Formatter = client.Formatter;
|
||||
Schema.ColumnCompiler.apply(this, arguments);
|
||||
}
|
||||
inherits(ColumnCompiler_FDB, Schema.ColumnCompiler);
|
||||
|
||||
// Types
|
||||
// ------
|
||||
ColumnCompiler_FDB.prototype.bigincrements = 'bigserial primary key';
|
||||
ColumnCompiler_FDB.prototype.bigint = 'bigint';
|
||||
ColumnCompiler_FDB.prototype.binary = 'blob';
|
||||
|
||||
ColumnCompiler_FDB.prototype.bool = 'boolean';
|
||||
|
||||
ColumnCompiler_FDB.prototype.double = 'double precision';
|
||||
ColumnCompiler_FDB.prototype.floating = 'real';
|
||||
ColumnCompiler_FDB.prototype.increments = 'serial primary key';
|
||||
|
||||
ColumnCompiler_FDB.prototype.smallint =
|
||||
ColumnCompiler_FDB.prototype.tinyint = 'smallint';
|
||||
ColumnCompiler_FDB.prototype.datetime =
|
||||
ColumnCompiler_FDB.prototype.timestamp = 'datetime';
|
||||
ColumnCompiler_FDB.prototype.uuid = 'guid';
|
||||
|
||||
// ENUM not supported, fake with VARCHAR
|
||||
ColumnCompiler_FDB.prototype.enu = function (allowed) {
|
||||
var maxLength = (allowed || []).reduce(function (maxLength, name) {
|
||||
return Math.max(maxLength, String(name).length);
|
||||
}, 1);
|
||||
// TODO: Add CHECK when supported.
|
||||
return "varchar(" + maxLength + ")";
|
||||
};
|
||||
|
||||
client.ColumnBuilder = ColumnBuilder_FDB;
|
||||
client.ColumnCompiler = ColumnCompiler_FDB;
|
||||
|
||||
};
|
||||
7
lib/dialects/fdbsql/schema/index.js
Normal file
7
lib/dialects/fdbsql/schema/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(client) {
|
||||
require('./schema')(client);
|
||||
require('./table')(client);
|
||||
require('./column')(client);
|
||||
};
|
||||
76
lib/dialects/fdbsql/schema/schema.js
Normal file
76
lib/dialects/fdbsql/schema/schema.js
Normal file
@ -0,0 +1,76 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer Schema Builder & Compiler
|
||||
// This file was adapted from the PostgreSQL Schema Builder & Compiler
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var inherits = require('inherits');
|
||||
var Schema = require('../../../schema');
|
||||
|
||||
// Schema Builder
|
||||
// -------
|
||||
|
||||
function SchemaBuilder_FDB() {
|
||||
this.client = client;
|
||||
Schema.Builder.apply(this, arguments);
|
||||
}
|
||||
inherits(SchemaBuilder_FDB, Schema.Builder);
|
||||
|
||||
// Schema Compiler
|
||||
// -------
|
||||
|
||||
function SchemaCompiler_FDB() {
|
||||
this.client = client;
|
||||
this.Formatter = client.Formatter;
|
||||
Schema.Compiler.apply(this, arguments);
|
||||
}
|
||||
inherits(SchemaCompiler_FDB, Schema.Compiler);
|
||||
|
||||
// Check whether the current table
|
||||
SchemaCompiler_FDB.prototype.hasTable = function(tableName) {
|
||||
this.pushQuery({
|
||||
sql: 'select * from information_schema.tables where table_name = ? and table_schema = CURRENT_SCHEMA',
|
||||
bindings: [tableName],
|
||||
output: function(resp) {
|
||||
return resp.rows.length > 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Compile the query to determine if a column exists in a table.
|
||||
SchemaCompiler_FDB.prototype.hasColumn = function(tableName, columnName) {
|
||||
this.pushQuery({
|
||||
sql: 'select * from information_schema.columns where table_name = ? and column_name = ? and table_schema = CURRENT_SCHEMA',
|
||||
bindings: [tableName, columnName],
|
||||
output: function(resp) {
|
||||
return resp.rows.length > 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Compile a rename table command.
|
||||
SchemaCompiler_FDB.prototype.renameTable = function(from, to) {
|
||||
this.pushQuery('alter table ' + this.formatter.wrap(from) + ' rename to ' + this.formatter.wrap(to));
|
||||
};
|
||||
|
||||
SchemaCompiler_FDB.prototype.createSchema = function(schemaName) {
|
||||
this.pushQuery('create schema ' + this.formatter.wrap(schemaName));
|
||||
};
|
||||
|
||||
SchemaCompiler_FDB.prototype.createSchemaIfNotExists = function(schemaName) {
|
||||
this.pushQuery('create schema if not exists ' + this.formatter.wrap(schemaName));
|
||||
};
|
||||
|
||||
SchemaCompiler_FDB.prototype.dropSchema = function(schemaName) {
|
||||
this.pushQuery('drop schema ' + this.formatter.wrap(schemaName));
|
||||
};
|
||||
|
||||
SchemaCompiler_FDB.prototype.dropSchemaIfExists = function(schemaName) {
|
||||
this.pushQuery('drop schema if exists ' + this.formatter.wrap(schemaName));
|
||||
};
|
||||
|
||||
client.SchemaBuilder = SchemaBuilder_FDB;
|
||||
client.SchemaCompiler = SchemaCompiler_FDB;
|
||||
|
||||
};
|
||||
94
lib/dialects/fdbsql/schema/table.js
Normal file
94
lib/dialects/fdbsql/schema/table.js
Normal file
@ -0,0 +1,94 @@
|
||||
'use strict';
|
||||
|
||||
// FDBSQL Table Builder & Compiler
|
||||
// This file was adapted from the PostgreSQL Table Builder & Compiler
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
//var _ = require('lodash');
|
||||
var inherits = require('inherits');
|
||||
var Schema = require('../../../schema');
|
||||
var _ = require('lodash');
|
||||
|
||||
// Table
|
||||
// ------
|
||||
|
||||
function TableBuilder_FDB() {
|
||||
this.client = client;
|
||||
Schema.TableBuilder.apply(this, arguments);
|
||||
}
|
||||
inherits(TableBuilder_FDB, Schema.TableBuilder);
|
||||
|
||||
function TableCompiler_FDB() {
|
||||
this.client = client;
|
||||
this.Formatter = client.Formatter;
|
||||
Schema.TableCompiler.apply(this, arguments);
|
||||
}
|
||||
inherits(TableCompiler_FDB, Schema.TableCompiler);
|
||||
|
||||
// Compile a rename column command.
|
||||
TableCompiler_FDB.prototype.renameColumn = function(from, to) {
|
||||
return this.pushQuery({
|
||||
sql: 'alter table ' + this.tableName() + ' rename column '+ this.formatter.wrap(from) + ' to ' + this.formatter.wrap(to)
|
||||
});
|
||||
};
|
||||
|
||||
TableCompiler_FDB.prototype.addColumns = function(columns) {
|
||||
if (columns.sql.length > 0) {
|
||||
var columnSql = _.map(columns.sql, function(column) {
|
||||
return this.addColumnsPrefix + column;
|
||||
}, this);
|
||||
for (var i = 0; i < columns.sql.length; i++) {
|
||||
this.pushQuery({
|
||||
sql: 'alter table ' + this.tableName() + ' ' + columnSql[i],
|
||||
bindings: columns.bindings
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Adds the "create" query to the query sequence.
|
||||
TableCompiler_FDB.prototype.createQuery = function(columns, ifNot) {
|
||||
var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
||||
this.pushQuery({
|
||||
sql: createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')',
|
||||
bindings: columns.bindings
|
||||
});
|
||||
};
|
||||
|
||||
// Indexes:
|
||||
// -------
|
||||
|
||||
TableCompiler_FDB.prototype.primary = function(columns) {
|
||||
this.pushQuery('alter table ' + this.tableName() + " add primary key (" + this.formatter.columnize(columns) + ")");
|
||||
};
|
||||
TableCompiler_FDB.prototype.unique = function(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
||||
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName +
|
||||
' unique (' + this.formatter.columnize(columns) + ')');
|
||||
};
|
||||
TableCompiler_FDB.prototype.index = function(columns, indexName, indexType) {
|
||||
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
||||
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + (indexType && (' using ' + indexType) || '') +
|
||||
' (' + this.formatter.columnize(columns) + ')');
|
||||
};
|
||||
TableCompiler_FDB.prototype.dropPrimary = function() {
|
||||
this.pushQuery('alter table ' + this.tableName() + ' drop primary key');
|
||||
};
|
||||
TableCompiler_FDB.prototype.dropIndex = function(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
||||
this.pushQuery('drop index ' + indexName);
|
||||
};
|
||||
TableCompiler_FDB.prototype.dropUnique = function(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
||||
this.pushQuery('alter table ' + this.tableName() + ' drop unique ' + indexName);
|
||||
};
|
||||
TableCompiler_FDB.prototype.dropForeign = function(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('foreign', this.tableNameRaw, columns);
|
||||
this.pushQuery('alter table ' + this.tableName() + ' drop foreign key ' + indexName);
|
||||
};
|
||||
|
||||
client.TableBuilder = TableBuilder_FDB;
|
||||
client.TableCompiler = TableCompiler_FDB;
|
||||
|
||||
};
|
||||
22
lib/dialects/fdbsql/seeder.js
Normal file
22
lib/dialects/fdbsql/seeder.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer seeder
|
||||
// This file was adapted from the PostgreSQL seeder
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var Seeder = require('../../seed');
|
||||
var inherits = require('inherits');
|
||||
|
||||
// Inherit from the `Seeder` constructor's prototype,
|
||||
// so we can add the correct `then` method.
|
||||
function Seeder_FDB() {
|
||||
this.client = client;
|
||||
Seeder.apply(this, arguments);
|
||||
}
|
||||
inherits(Seeder_FDB, Seeder);
|
||||
|
||||
// Assign the newly extended `Seeder` constructor to the client object.
|
||||
client.Seeder = Seeder_FDB;
|
||||
|
||||
};
|
||||
16
lib/dialects/fdbsql/transaction.js
Normal file
16
lib/dialects/fdbsql/transaction.js
Normal file
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
var inherits = require('inherits');
|
||||
var Transaction = require('../../transaction');
|
||||
|
||||
function Transaction_FDB() {
|
||||
this.client = client;
|
||||
Transaction.apply(this, arguments);
|
||||
}
|
||||
inherits(Transaction_FDB, Transaction);
|
||||
|
||||
client.Transaction = Transaction_FDB;
|
||||
|
||||
};
|
||||
91
lib/dialects/fdbsql/utils.js
Normal file
91
lib/dialects/fdbsql/utils.js
Normal file
@ -0,0 +1,91 @@
|
||||
'use strict';
|
||||
|
||||
// FDB SQL Layer utils
|
||||
// This file was adapted from the PostgreSQL utils
|
||||
|
||||
function dateToString(date) {
|
||||
function pad(number, digits) {
|
||||
number = number.toString();
|
||||
while (number.length < digits) {
|
||||
number = "0" + number;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
var offset = -date.getTimezoneOffset();
|
||||
var ret = pad(date.getFullYear(), 4) + '-' +
|
||||
pad(date.getMonth() + 1, 2) + '-' +
|
||||
pad(date.getDate(), 2) + 'T' +
|
||||
pad(date.getHours(), 2) + ':' +
|
||||
pad(date.getMinutes(), 2) + ':' +
|
||||
pad(date.getSeconds(), 2) + '.' +
|
||||
pad(date.getMilliseconds(), 3);
|
||||
|
||||
if (offset < 0) {
|
||||
ret += "-";
|
||||
offset *= -1;
|
||||
} else {
|
||||
ret += "+";
|
||||
}
|
||||
|
||||
return ret + pad(Math.floor(offset / 60), 2) + ":" + pad(offset % 60, 2);
|
||||
}
|
||||
|
||||
var prepareObject;
|
||||
|
||||
//converts values from javascript types
|
||||
//to their 'raw' counterparts for use as a parameter
|
||||
//note: you can override this function to provide your own conversion mechanism
|
||||
//for complex types, etc...
|
||||
var prepareValue = function (val, seen) {
|
||||
if (val instanceof Buffer) {
|
||||
return val;
|
||||
}
|
||||
if (val instanceof Date) {
|
||||
return dateToString(val);
|
||||
}
|
||||
if (val === null || val === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (typeof val === 'number') {
|
||||
return val;
|
||||
}
|
||||
if (typeof val === 'object') {
|
||||
return prepareObject(val, seen);
|
||||
}
|
||||
return val.toString();
|
||||
};
|
||||
|
||||
prepareObject = function prepareObject(val, seen) {
|
||||
if (val && typeof val.toPostgres === 'function') {
|
||||
seen = seen || [];
|
||||
if (seen.indexOf(val) !== -1) {
|
||||
throw new Error('circular reference detected while preparing "' + val + '" for query');
|
||||
}
|
||||
seen.push(val);
|
||||
|
||||
return prepareValue(val.toPostgres(prepareValue), seen);
|
||||
}
|
||||
return JSON.stringify(val);
|
||||
};
|
||||
|
||||
function normalizeQueryConfig(config, values, callback) {
|
||||
//can take in strings or config objects
|
||||
config = (typeof config === 'string') ? { text: config } : config;
|
||||
if (values) {
|
||||
if (typeof values === 'function') {
|
||||
config.callback = values;
|
||||
} else {
|
||||
config.values = values;
|
||||
}
|
||||
}
|
||||
if (callback) {
|
||||
config.callback = callback;
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
prepareValue: prepareValue,
|
||||
normalizeQueryConfig: normalizeQueryConfig
|
||||
};
|
||||
@ -42,6 +42,10 @@ var clients = {
|
||||
oracle: {
|
||||
name: 'oracle',
|
||||
client: knex({client: 'oracle'}).client,
|
||||
},
|
||||
fdbsql: {
|
||||
name: 'fdbsql',
|
||||
client: knex({client: 'fdbsql'}).client,
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ module.exports = function(knex) {
|
||||
tester('postgresql', 'truncate "test_table_two" restart identity');
|
||||
tester('sqlite3', "delete from sqlite_sequence where name = \"test_table_two\"");
|
||||
tester('oracle', "truncate table \"test_table_two\"");
|
||||
tester('fdbsql', 'truncate table \"test_table_two\"');
|
||||
})
|
||||
.then(function() {
|
||||
|
||||
@ -37,7 +38,8 @@ module.exports = function(knex) {
|
||||
mariasql: 'SHOW TABLES',
|
||||
postgresql: "SELECT table_name FROM information_schema.tables WHERE table_schema='public'",
|
||||
sqlite3: "SELECT name FROM sqlite_master WHERE type='table';",
|
||||
oracle: "select TABLE_NAME from USER_TABLES"
|
||||
oracle: "select TABLE_NAME from USER_TABLES",
|
||||
fdbsql: "SELECT table_name FROM information_schema.tables WHERE table_schema='public'"
|
||||
};
|
||||
return knex.raw(tables[knex.client.dialect]).testSql(function(tester) {
|
||||
tester(knex.client.dialect, tables[knex.client.dialect]);
|
||||
@ -117,6 +119,25 @@ module.exports = function(knex) {
|
||||
}
|
||||
}
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select * from information_schema.columns where table_name = ? and table_schema = ? and table_schema = CURRENT_SCHEMA',
|
||||
null,
|
||||
{
|
||||
"enum_value": {
|
||||
"defaultValue": null,
|
||||
"maxLength": 1,
|
||||
"nullable": true,
|
||||
"type": "VARCHAR"
|
||||
},
|
||||
"uuid": {
|
||||
"defaultValue": null,
|
||||
"maxLength": null,
|
||||
"nullable": false,
|
||||
"type": "GUID"
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -153,6 +174,13 @@ module.exports = function(knex) {
|
||||
"type": "CHAR"
|
||||
}
|
||||
);
|
||||
tester('fdbsql', 'select * from information_schema.columns where table_name = ? and table_schema = ? and table_schema = CURRENT_SCHEMA',
|
||||
null, {
|
||||
"defaultValue": null,
|
||||
"maxLength": null,
|
||||
"nullable": false,
|
||||
"type": "GUID"
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -172,6 +200,7 @@ module.exports = function(knex) {
|
||||
tester('postgresql', ["alter table \"accounts\" rename \"about\" to \"about_col\""]);
|
||||
tester('sqlite3', ["PRAGMA table_info(\"accounts\")"]);
|
||||
tester('oracle', ["alter table \"accounts\" rename column \"about\" to \"about_col\""]);
|
||||
tester('fdbsql', ["alter table \"accounts\" rename column \"about\" to \"about_col\""]);
|
||||
});
|
||||
}).then(function() {
|
||||
return knex.count('*').from('accounts');
|
||||
@ -222,6 +251,7 @@ module.exports = function(knex) {
|
||||
tester('mysql', ["alter table `accounts` drop `first_name`"]);
|
||||
tester('postgresql', ['alter table "accounts" drop column "first_name"']);
|
||||
tester('sqlite3', ["PRAGMA table_info(\"accounts\")"]);
|
||||
tester('fdbsql', ['alter table "accounts" drop column "first_name"']);
|
||||
});
|
||||
}).then(function() {
|
||||
return knex.select('*').from('accounts').first();
|
||||
|
||||
@ -41,6 +41,14 @@ module.exports = function(knex) {
|
||||
'SUM("LOGINS")': 10
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select sum("logins") from "accounts"',
|
||||
[],
|
||||
[{
|
||||
_SQL_COL_1: '10'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -60,6 +68,8 @@ module.exports = function(knex) {
|
||||
tester('postgresql', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg'));
|
||||
// oracle: 1.66666666666667
|
||||
tester('oracle', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'AVG("LOGINS")'));
|
||||
// fdbsql: '1.6666666666666667'
|
||||
tester('fdbsql', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, '_SQL_COL_1'));
|
||||
});
|
||||
|
||||
});
|
||||
@ -99,6 +109,14 @@ module.exports = function(knex) {
|
||||
'COUNT("ID")': 6
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select count("id") from "accounts"',
|
||||
[],
|
||||
[{
|
||||
_SQL_COL_1: '6'
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -146,6 +164,16 @@ module.exports = function(knex) {
|
||||
'MIN("LOGINS")': 1
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select count("id"), max("logins"), min("logins") from "accounts"',
|
||||
[],
|
||||
[{
|
||||
_SQL_COL_1: '6',
|
||||
_SQL_COL_2: 2,
|
||||
_SQL_COL_3: 1
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -193,6 +221,16 @@ module.exports = function(knex) {
|
||||
'COUNT("ID")': 4
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select count("id") from "accounts" group by "logins"',
|
||||
[],
|
||||
[{
|
||||
_SQL_COL_1: '2'
|
||||
},{
|
||||
_SQL_COL_1: '4'
|
||||
}]
|
||||
);
|
||||
|
||||
|
||||
}).then(function() {
|
||||
@ -230,6 +268,14 @@ module.exports = function(knex) {
|
||||
'COUNT("ID")': 6
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select count("id") from "accounts" group by "first_name"',
|
||||
[],
|
||||
[{
|
||||
_SQL_COL_1: '6'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -35,10 +35,16 @@ module.exports = function(knex) {
|
||||
[1],
|
||||
1
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'delete from "accounts" where "id" = ?',
|
||||
[1],
|
||||
1
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow returning for deletes in postgresql', function() {
|
||||
it('should allow returning for deletes in postgresql and fdbsql', function() {
|
||||
return knex('accounts')
|
||||
.where('id', 2)
|
||||
.del('*')
|
||||
@ -77,6 +83,22 @@ module.exports = function(knex) {
|
||||
[2],
|
||||
1
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'delete from "accounts" where "id" = ? returning *',
|
||||
[2],
|
||||
[{
|
||||
id: '2',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test2@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -44,6 +44,12 @@ module.exports = function(knex) {
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d,'test@example.com','Test','User', 1, d, function (v) { return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[1]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id"',
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d,'test@example.com','Test','User', 1, d],
|
||||
['1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -92,6 +98,12 @@ module.exports = function(knex) {
|
||||
'Lorem ipsum Dolore labore incididunt enim.', d,'test3@example.com','Test','User',2, d, function (v) { return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[2, 3]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) returning "id"',
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d,'test2@example.com','Test','User',1, d,'Lorem ipsum Dolore labore incididunt enim.', d,'test3@example.com','Test','User',2, d],
|
||||
['2','3']
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -198,6 +210,12 @@ module.exports = function(knex) {
|
||||
],
|
||||
[4, 5]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) returning "id"',
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d,'test4@example.com','Test','User',2, d,'Lorem ipsum Dolore labore incididunt enim.', d,'test5@example.com','Test','User',2, d],
|
||||
['4','5']
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -237,6 +255,11 @@ module.exports = function(knex) {
|
||||
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?",
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d, function (v) {return v.toString() === '[object ReturningHelper:id]';}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id"',
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d]
|
||||
);
|
||||
})
|
||||
.then(function() {
|
||||
throw new Error('There should be a fail when multi-insert are made in unique col.');
|
||||
@ -282,6 +305,12 @@ module.exports = function(knex) {
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d, function (v) {return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[7]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id"',
|
||||
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d],
|
||||
['7']
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -334,7 +363,7 @@ module.exports = function(knex) {
|
||||
}).then(function() {
|
||||
// No errors happen in sqlite3 or mysql, which dont have native support
|
||||
// for the uuid type.
|
||||
if (knex.client.dialect === 'postgresql') {
|
||||
if (knex.client.dialect === 'postgresql' || knex.client.dialect === 'fdbsql') {
|
||||
throw new Error('There should be an error in postgresql for uuids');
|
||||
}
|
||||
}, function() {});
|
||||
@ -388,6 +417,12 @@ module.exports = function(knex) {
|
||||
[function (v) {return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[1]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "test_default_table" default values returning "id"',
|
||||
[],
|
||||
[1]
|
||||
);
|
||||
|
||||
});
|
||||
});
|
||||
@ -428,6 +463,12 @@ module.exports = function(knex) {
|
||||
[function (v) {return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[1]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "test_default_table2" default values returning "id"',
|
||||
[],
|
||||
[1]
|
||||
);
|
||||
|
||||
});
|
||||
});
|
||||
@ -465,6 +506,12 @@ module.exports = function(knex) {
|
||||
[function (v) {return v.toString() === '[object ReturningHelper:id]';}, function (v) {return v.toString() === '[object ReturningHelper:id]';}],
|
||||
[1, 2]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "test_default_table3" values (default), (default) returning "id"',
|
||||
[],
|
||||
[1, 2]
|
||||
);
|
||||
|
||||
});
|
||||
}).then(function () {
|
||||
@ -474,7 +521,7 @@ module.exports = function(knex) {
|
||||
});
|
||||
});
|
||||
|
||||
it('should take an array of columns to return in oracle or postgres', function() {
|
||||
it('should take an array of columns to return in oracle, postgres, or fdbsql', function() {
|
||||
var insertData = {
|
||||
account_id: 10,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
@ -516,9 +563,18 @@ module.exports = function(knex) {
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?) returning "account_id", "details"',
|
||||
[10,'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',0],
|
||||
[{
|
||||
account_id: 10,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}]
|
||||
);
|
||||
}).then(function(rows) {
|
||||
expect(rows.length).to.equal(1);
|
||||
if (knex.client.dialect === 'postgresql') {
|
||||
if (knex.client.dialect === 'postgresql' || knex.client.dialect === 'fdbsql') {
|
||||
expect(_.keys(rows[0]).length).to.equal(2);
|
||||
expect(rows[0].account_id).to.equal(insertData.account_id);
|
||||
expect(rows[0].details).to.equal(insertData.details);
|
||||
@ -526,7 +582,7 @@ module.exports = function(knex) {
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow a * for returning in postgres and oracle', function() {
|
||||
it('should allow a * for returning in postgres, oracle, and fdbsql', function() {
|
||||
var insertData = {
|
||||
account_id: 10,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
@ -564,9 +620,21 @@ module.exports = function(knex) {
|
||||
json_data: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?) returning *',
|
||||
[10,'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',0],
|
||||
[{
|
||||
id: 5,
|
||||
account_id: 10,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
status: 0,
|
||||
json_data: null
|
||||
}]
|
||||
);
|
||||
}).then(function(rows) {
|
||||
expect(rows.length).to.equal(1);
|
||||
if (knex.client.dialect === 'postgresql') {
|
||||
if (knex.client.dialect === 'postgresql' || knex.client.dialect === 'fdbsql') {
|
||||
expect(_.keys(rows[0]).length).to.equal(5);
|
||||
expect(rows[0].account_id).to.equal(insertData.account_id);
|
||||
expect(rows[0].details).to.equal(insertData.details);
|
||||
|
||||
@ -161,6 +161,43 @@ module.exports = function(knex) {
|
||||
details: null // Oracle implicitly converted '' to NULL
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
|
||||
id: '1',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: '2',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test2@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: '3',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test3@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: ''
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -451,6 +488,76 @@ module.exports = function(knex) {
|
||||
details: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
|
||||
id: '1',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: '2',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test2@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}, {
|
||||
id: '3',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test3@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: ''
|
||||
}, {
|
||||
id: '4',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test4@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null
|
||||
}, {
|
||||
id: '5',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test5@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null
|
||||
}, {
|
||||
id: '7',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test6@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
details: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -726,6 +833,94 @@ module.exports = function(knex) {
|
||||
json_data: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details" order by "accounts"."id" asc', [], [{
|
||||
id: 1,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
account_id: 1,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
status: 0,
|
||||
json_data: null
|
||||
}, {
|
||||
id: 2,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test2@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
account_id: 2,
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
||||
status: 1,
|
||||
json_data: null
|
||||
}, {
|
||||
id: 3,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test3@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
account_id: 3,
|
||||
details: '',
|
||||
status: 1,
|
||||
json_data: null
|
||||
}, {
|
||||
id: null,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test4@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
account_id: null,
|
||||
details: null,
|
||||
status: null,
|
||||
json_data: null
|
||||
}, {
|
||||
id: null,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test5@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
account_id: null,
|
||||
details: null,
|
||||
status: null,
|
||||
json_data: null
|
||||
}, {
|
||||
id: null,
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test6@example.com',
|
||||
logins: 2,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null,
|
||||
account_id: null,
|
||||
details: null,
|
||||
status: null,
|
||||
json_data: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -822,6 +1017,27 @@ module.exports = function(knex) {
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "a2"."email" <> "accounts"."email" where "a2"."email" = ? order by "e1" asc limit ?',
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test3@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test4@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test5@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test6@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}, {
|
||||
e1: 'test@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -921,6 +1137,27 @@ module.exports = function(knex) {
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? order by "e1" asc limit ?',
|
||||
['test2@example.com', 5],
|
||||
[{
|
||||
e1: 'test2@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test3@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test4@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test5@example.com',
|
||||
e2: 'test2@example.com'
|
||||
},{
|
||||
e1: 'test6@example.com',
|
||||
e2: 'test2@example.com'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -959,6 +1196,14 @@ module.exports = function(knex) {
|
||||
return res.length === 30;
|
||||
}
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "account_id" from "accounts" cross join "test_table_two" order by "account_id" asc',
|
||||
[],
|
||||
function (res) {
|
||||
return res.length === 30;
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -1022,6 +1267,18 @@ module.exports = function(knex) {
|
||||
email: 'test3@example.com'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "a1"."email", "a2"."email" from "accounts" as "a1" left join "accounts" as "a2" on "a1"."email" <> "a2"."email" where a1.id = 1 limit ?',
|
||||
[2],
|
||||
[{
|
||||
0: 'test@example.com',
|
||||
1: 'test2@example.com'
|
||||
},{
|
||||
0: 'test@example.com',
|
||||
1: 'test3@example.com'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -40,6 +40,12 @@ module.exports = function(knex) {
|
||||
[],
|
||||
[1, 2, 3, 4, 5, 7]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "id" from "accounts" order by "id" asc',
|
||||
[],
|
||||
['1', '2', '3', '4', '5', '7']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -70,6 +76,12 @@ module.exports = function(knex) {
|
||||
[10000000000002, 2],
|
||||
[3, 4, 5, 7]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "id" from "accounts" order by "id" asc offset ?',
|
||||
[2],
|
||||
['3', '4', '5', '7']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -100,6 +112,12 @@ module.exports = function(knex) {
|
||||
[1],
|
||||
{ id: 1, first_name: 'Test' }
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "id", "first_name" from "accounts" order by "id" asc limit ?',
|
||||
[1],
|
||||
{ id: '1', first_name: 'Test' }
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -211,6 +229,15 @@ module.exports = function(knex) {
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -257,6 +284,15 @@ module.exports = function(knex) {
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
||||
[1],
|
||||
[{
|
||||
first_name: 'Test',
|
||||
last_name: 'User'
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -286,6 +322,11 @@ module.exports = function(knex) {
|
||||
'select "email", "logins" from "accounts" where "id" > ?',
|
||||
[1]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "email", "logins" from "accounts" where "id" > ?',
|
||||
[1]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -359,6 +400,22 @@ module.exports = function(knex) {
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[1],
|
||||
[{
|
||||
id: '1',
|
||||
first_name: "Test",
|
||||
last_name: "User",
|
||||
email: "test@example.com",
|
||||
logins: 1,
|
||||
about: "Lorem ipsum Dolore labore incididunt enim.",
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -392,6 +449,12 @@ module.exports = function(knex) {
|
||||
[],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select "first_name", "email" from "accounts" where "id" is null',
|
||||
[],
|
||||
[]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
@ -426,6 +489,12 @@ module.exports = function(knex) {
|
||||
[0],
|
||||
[]
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'select * from "accounts" where "id" = ?',
|
||||
[0],
|
||||
[]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -516,6 +585,20 @@ module.exports = function(knex) {
|
||||
details: 'One, Two, Zero',
|
||||
status: 0
|
||||
}]);
|
||||
tester('fdbsql',
|
||||
'select * from "composite_key_test" where ("column_a","column_b") in ((?, ?),(?, ?))',
|
||||
[1,1,1,2],
|
||||
[{
|
||||
column_a: 1,
|
||||
column_b: 1,
|
||||
details: 'One, One, One',
|
||||
status: 1
|
||||
},{
|
||||
column_a: 1,
|
||||
column_b: 2,
|
||||
details: 'One, Two, Zero',
|
||||
status: 0
|
||||
}]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -554,6 +637,15 @@ module.exports = function(knex) {
|
||||
details: 'One, One, One',
|
||||
status: 1
|
||||
}]);
|
||||
tester('fdbsql',
|
||||
'select * from "composite_key_test" where "status" = ? and ("column_a","column_b") in ((?, ?),(?, ?))',
|
||||
[1,1,1,1,2],
|
||||
[{
|
||||
column_a: 1,
|
||||
column_b: 1,
|
||||
details: 'One, One, One',
|
||||
status: 1
|
||||
}]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -624,7 +716,7 @@ module.exports = function(knex) {
|
||||
});
|
||||
|
||||
it('always returns the response object from raw', function() {
|
||||
if (knex.client.dialect === 'postgresql') {
|
||||
if (knex.client.dialect === 'postgresql' || knex.client.dialect === 'fdbsql') {
|
||||
return knex.raw('select id from accounts').then(function(resp) {
|
||||
assert(Array.isArray(resp.rows) === true);
|
||||
});
|
||||
|
||||
@ -32,6 +32,12 @@ module.exports = function(knex) {
|
||||
['test100@example.com','User','Test',1],
|
||||
1
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
|
||||
['test100@example.com','User','Test',1],
|
||||
1
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -96,7 +102,7 @@ module.exports = function(knex) {
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow returning for updates in postgresql', function() {
|
||||
it('should allow returning for updates in postgresql and fdbsql', function() {
|
||||
|
||||
return knex('accounts').where('id', 1).update({
|
||||
first_name: 'UpdatedUser',
|
||||
@ -137,6 +143,22 @@ module.exports = function(knex) {
|
||||
['test100@example.com','UpdatedUser','UpdatedTest',1],
|
||||
1
|
||||
);
|
||||
tester(
|
||||
'fdbsql',
|
||||
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ? returning *',
|
||||
['test100@example.com','UpdatedUser','UpdatedTest',1],
|
||||
[{
|
||||
id: '1',
|
||||
first_name: 'UpdatedUser',
|
||||
last_name: 'UpdatedTest',
|
||||
email: 'test100@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: d,
|
||||
updated_at: d,
|
||||
phone: null
|
||||
}]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -13,7 +13,7 @@ module.exports = function(knex) {
|
||||
it('has a dropTableIfExists method', function() {
|
||||
return Promise.all([
|
||||
knex.schema.dropTableIfExists('test_foreign_table_two').testSql(function(tester) {
|
||||
tester(['sqlite3', 'postgresql'], ['drop table if exists "test_foreign_table_two"']);
|
||||
tester(['sqlite3', 'postgresql', 'fdbsql'], ['drop table if exists "test_foreign_table_two"']);
|
||||
tester('mysql', ['drop table if exists `test_foreign_table_two`']);
|
||||
tester('oracle', [
|
||||
"begin execute immediate 'drop table \"test_foreign_table_two\"'; exception when others then if sqlcode != -942 then raise; end if; end;",
|
||||
@ -83,6 +83,7 @@ module.exports = function(knex) {
|
||||
'create index "NkZo/dGRI9O73/NE2fHo+35d4jk" on "test_table_one" ("first_name")',
|
||||
'alter table "test_table_one" add constraint "test_table_one_email_unique" unique ("email")',
|
||||
'create index "test_table_one_logins_index" on "test_table_one" ("logins")']);
|
||||
tester('fdbsql', ['create table "test_table_one" ("id" bigserial primary key, "first_name" varchar(255), "last_name" varchar(255), "email" varchar(255) null, "logins" integer default \'1\', "about" text, "created_at" datetime, "updated_at" datetime)','create index test_table_one_first_name_index on "test_table_one" ("first_name")','alter table "test_table_one" add constraint test_table_one_email_unique unique ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -116,6 +117,7 @@ module.exports = function(knex) {
|
||||
tester('postgresql', ['create table "test_table_three" ("main" integer, "paragraph" text default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add primary key ("main")']);
|
||||
tester('sqlite3', ['create table "test_table_three" ("main" integer, "paragraph" text default \'Lorem ipsum Qui quis qui in.\', primary key ("main"))']);
|
||||
tester('oracle', ['create table "test_table_three" ("main" integer, "paragraph" clob default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add primary key ("main")']);
|
||||
tester('fdbsql', ['create table "test_table_three" ("main" integer, "paragraph" text default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add primary key ("main")']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -129,6 +131,7 @@ module.exports = function(knex) {
|
||||
tester('postgresql', ['create table "datatype_test" ("enum_value" text check ("enum_value" in (\'a\', \'b\', \'c\')), "uuid" uuid not null)']);
|
||||
tester('sqlite3', ['create table "datatype_test" ("enum_value" varchar, "uuid" char(36) not null)']);
|
||||
tester('oracle', ['create table "datatype_test" ("enum_value" varchar2(1) check ("enum_value" in (\'a\', \'b\', \'c\')), "uuid" char(36) not null)']);
|
||||
tester('fdbsql', ['create table "datatype_test" ("enum_value" varchar(1), "uuid" guid not null)']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -149,6 +152,7 @@ module.exports = function(knex) {
|
||||
"create or replace trigger \"test_foreign_table_two_id_trg\" before insert on \"test_foreign_table_two\" for each row when (new.\"id\" is null) begin select \"test_foreign_table_two_seq\".nextval into :new.\"id\" from dual; end;",
|
||||
'alter table "test_foreign_table_two" add constraint "q7TfvbIx3HUQbh+l+e5N+J+Guag" foreign key ("fkey_two") references "test_table_two" ("id")'
|
||||
]);
|
||||
tester('fdbsql', ['create table "test_foreign_table_two" ("id" serial primary key, "fkey_two" integer)','alter table "test_foreign_table_two" add constraint test_foreign_table_two_fkey_two_foreign foreign key ("fkey_two") references "test_table_two" ("id")']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -165,6 +169,7 @@ module.exports = function(knex) {
|
||||
tester('postgresql', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" smallint)','alter table "composite_key_test" add constraint composite_key_test_column_a_column_b_unique unique ("column_a", "column_b")']);
|
||||
tester('sqlite3', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" tinyint)','create unique index composite_key_test_column_a_column_b_unique on "composite_key_test" ("column_a", "column_b")']);
|
||||
tester('oracle', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" clob, "status" smallint)','alter table "composite_key_test" add constraint "zYmMt0VQwlLZ20XnrMicXZ0ufZk" unique ("column_a", "column_b")']);
|
||||
tester('fdbsql', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" smallint)','alter table "composite_key_test" add constraint composite_key_test_column_a_column_b_unique unique ("column_a", "column_b")']);
|
||||
}).then(function() {
|
||||
return knex('composite_key_test').insert([{
|
||||
column_a: 1,
|
||||
@ -204,6 +209,7 @@ module.exports = function(knex) {
|
||||
"begin execute immediate 'create sequence \"charset_collate_test_seq\"'; exception when others then if sqlcode != -955 then raise; end if; end;",
|
||||
"create or replace trigger \"charset_collate_test_id_trg\" before insert on \"charset_collate_test\" for each row when (new.\"id\" is null) begin select \"charset_collate_test_seq\".nextval into :new.\"id\" from dual; end;"
|
||||
]);
|
||||
tester('fdbsql', ['create table "charset_collate_test" ("id" serial primary key, "account_id" integer, "details" text, "status" smallint)']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -220,6 +226,7 @@ module.exports = function(knex) {
|
||||
tester('postgresql', ['create table "bool_test" ("one" boolean, "two" boolean default \'0\', "three" boolean default \'1\', "four" boolean default \'1\', "five" boolean default \'0\')']);
|
||||
tester('sqlite3', ['create table "bool_test" ("one" boolean, "two" boolean default \'0\', "three" boolean default \'1\', "four" boolean default \'1\', "five" boolean default \'0\')']);
|
||||
tester('oracle', ['create table "bool_test" ("one" number(1, 0) check ("one" in (\'0\', \'1\')), "two" number(1, 0) default \'0\' check ("two" in (\'0\', \'1\')), "three" number(1, 0) default \'1\' check ("three" in (\'0\', \'1\')), "four" number(1, 0) default \'1\' check ("four" in (\'0\', \'1\')), "five" number(1, 0) default \'0\' check ("five" in (\'0\', \'1\')))']);
|
||||
tester('fdbsql', ['create table "bool_test" ("one" boolean, "two" boolean default \'0\', "three" boolean default \'1\', "four" boolean default \'1\', "five" boolean default \'0\')']);
|
||||
}).then(function() {
|
||||
return knex.insert({one: false}).into('bool_test');
|
||||
});
|
||||
@ -295,6 +302,12 @@ module.exports = function(knex) {
|
||||
expect(exists).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('checks whether a column exists, resolving with a boolean', function() {
|
||||
return knex.schema.hasColumn('accounts', 'not_a_column').then(function(exists) {
|
||||
expect(exists).to.equal(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -109,7 +109,19 @@ var testConfigs = {
|
||||
}),
|
||||
migrations: migrations,
|
||||
seeds: seeds
|
||||
}
|
||||
},
|
||||
|
||||
fdbsql: {
|
||||
dialect: "fdbsql",
|
||||
connection: testConfig.fdb || {
|
||||
adapter: "fdbsql",
|
||||
database: "knex_test",
|
||||
user: "fdbsql",
|
||||
},
|
||||
pool: pool,
|
||||
migrations: migrations,
|
||||
seeds: seeds
|
||||
},
|
||||
};
|
||||
|
||||
// export only copy the specified dialects
|
||||
|
||||
@ -866,6 +866,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
sql: 'select * from (select row_.*, ROWNUM rownum_ from (select * from "users") row_ where rownum <= ?) where rownum_ > ?',
|
||||
bindings: [10000000000005, 5]
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'select * from "users" offset ?',
|
||||
bindings: [5]
|
||||
},
|
||||
default: {
|
||||
sql: 'select * from "users" limit ? offset ?',
|
||||
bindings: [10000000000005, 5]
|
||||
@ -1198,6 +1202,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
expect(bindings[5].toString()).to.equal('[object ReturningHelper:id]');
|
||||
}
|
||||
},
|
||||
fdbsql: {
|
||||
sql: "insert into \"users\" (\"email\", \"name\") values (?, ?), (?, ?) returning \"id\"",
|
||||
bindings: ['foo', 'taylor', 'bar', 'dayle']
|
||||
},
|
||||
default: {
|
||||
sql: 'als',
|
||||
bindings: ['foo', 'taylor', 'bar', 'dayle']
|
||||
@ -1231,6 +1239,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
expect(bindings[5].toString()).to.equal('[object ReturningHelper:id:name]');
|
||||
}
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'insert into "users" ("email", "name") values (?, ?), (?, ?) returning "id", "name"',
|
||||
bindings: ['foo', 'taylor', 'bar', 'dayle']
|
||||
},
|
||||
default: {
|
||||
sql: '',
|
||||
}
|
||||
@ -1324,6 +1336,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
expect(bindings[0].toString()).to.equal('[object ReturningHelper:id]');
|
||||
}
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'insert into "users" default values returning "id"',
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'insert into "users" default values',
|
||||
bindings: []
|
||||
@ -1352,6 +1368,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
expect(bindings[0].toString()).to.equal('[object ReturningHelper:id]');
|
||||
}
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'insert into "users" default values returning "id"',
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'insert into "users" default values',
|
||||
bindings: []
|
||||
@ -1380,6 +1400,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
expect(bindings[0].toString()).to.equal('[object ReturningHelper:id]');
|
||||
}
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'insert into "users" default values returning "id"',
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'insert into "users" default values',
|
||||
bindings: []
|
||||
@ -1411,6 +1435,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
sql: "insert into \"users\" (\"undefined\") values (default), (default)",
|
||||
bindings: []
|
||||
},
|
||||
fdbsql: {
|
||||
sql: "insert into \"users\" values (default), (default)",
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'insert into "users" default values',
|
||||
bindings: []
|
||||
@ -1442,6 +1470,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
sql: "insert into \"users\" (\"undefined\") values (default), (default)",
|
||||
bindings: []
|
||||
},
|
||||
fdbsql: {
|
||||
sql: "insert into \"users\" values (default), (default)",
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'insert into "users" default values',
|
||||
bindings: []
|
||||
@ -1472,6 +1504,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
sql: 'insert into "users" ("id") values (default), (default) returning "id"',
|
||||
bindings: []
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'insert into "users" values (default), (default) returning "id"',
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: 'not checked',
|
||||
bindings: []
|
||||
@ -1613,6 +1649,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
sql: 'truncate table "users"',
|
||||
bindings: []
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'truncate table "users"',
|
||||
bindings: []
|
||||
},
|
||||
default: {
|
||||
sql: '',
|
||||
bindings: []
|
||||
@ -1638,6 +1678,10 @@ module.exports = function(qb, clientName, aliasName) {
|
||||
expect(bindings[1].toString()).to.equal('[object ReturningHelper:id]');
|
||||
}
|
||||
},
|
||||
fdbsql: {
|
||||
sql: 'insert into "users" ("email") values (?) returning "id"',
|
||||
bindings: ['foo']
|
||||
},
|
||||
default: {
|
||||
sql: 'insert into "users" ("email") values (?)',
|
||||
bindings: ['foo']
|
||||
|
||||
425
test/unit/schema/fdbsql.js
Normal file
425
test/unit/schema/fdbsql.js
Normal file
@ -0,0 +1,425 @@
|
||||
/*global describe, expect, it*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = function(client) {
|
||||
|
||||
client.initSchema();
|
||||
|
||||
var tableSql;
|
||||
var SchemaBuilder = client.SchemaBuilder;
|
||||
var equal = require('assert').equal;
|
||||
|
||||
describe("FDBSQL SchemaBuilder", function() {
|
||||
|
||||
it("basic alter table", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.increments('id');
|
||||
table.string('email');
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" serial primary key');
|
||||
expect(tableSql[1].sql).to.equal('alter table "users" add column "email" varchar(255)');
|
||||
});
|
||||
|
||||
it("drop table", function() {
|
||||
tableSql = new SchemaBuilder().dropTable('users').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop table "users"');
|
||||
});
|
||||
|
||||
it("drop table if exists", function() {
|
||||
tableSql = new SchemaBuilder().dropTableIfExists('users').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop table if exists "users"');
|
||||
});
|
||||
|
||||
it("drop column", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropColumn('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop column "foo"');
|
||||
});
|
||||
|
||||
it("drop multiple columns", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropColumn(['foo', 'bar']);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop column "foo", drop column "bar"');
|
||||
});
|
||||
|
||||
it("drop multiple columns with arguments", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropColumn('foo', 'bar');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop column "foo", drop column "bar"');
|
||||
});
|
||||
|
||||
it("drop primary", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropPrimary();
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop primary key');
|
||||
});
|
||||
|
||||
it("drop unique", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropUnique('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop unique users_foo_unique');
|
||||
});
|
||||
|
||||
it("drop unique, custom", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropUnique(null, 'foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop unique foo');
|
||||
});
|
||||
|
||||
it("drop index", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropIndex('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop index users_foo_index');
|
||||
});
|
||||
|
||||
it("drop index, custom", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropIndex(null, 'foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop index foo');
|
||||
});
|
||||
|
||||
it("drop foreign", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropForeign('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop foreign key users_foo_foreign');
|
||||
});
|
||||
|
||||
it("drop foreign", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropForeign(null, 'foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop foreign key foo');
|
||||
});
|
||||
|
||||
it("drop timestamps", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dropTimestamps();
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" drop column "created_at", drop column "updated_at"');
|
||||
});
|
||||
|
||||
it("rename table", function() {
|
||||
tableSql = new SchemaBuilder().renameTable('users', 'foo').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" rename to "foo"');
|
||||
});
|
||||
|
||||
it("adding primary key", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.primary('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add primary key ("foo")');
|
||||
});
|
||||
|
||||
it("adding primary key fluently", function() {
|
||||
tableSql = new SchemaBuilder().createTable('users', function(table) {
|
||||
table.string('name').primary();
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('create table "users" ("name" varchar(255))');
|
||||
expect(tableSql[1].sql).to.equal('alter table "users" add primary key ("name")');
|
||||
});
|
||||
|
||||
it("adding foreign key", function() {
|
||||
tableSql = new SchemaBuilder().createTable('accounts', function(table) {
|
||||
table.integer('account_id').references('users.id');
|
||||
}).toSQL();
|
||||
expect(tableSql[1].sql).to.equal('alter table "accounts" add constraint accounts_account_id_foreign foreign key ("account_id") references "users" ("id")');
|
||||
});
|
||||
|
||||
it("adds foreign key with onUpdate and onDelete", function() {
|
||||
tableSql = new SchemaBuilder().createTable('person', function(table) {
|
||||
table.integer('user_id').notNull().references('users.id').onDelete('SET NULL');
|
||||
table.integer('account_id').notNull().references('id').inTable('accounts').onUpdate('cascade');
|
||||
}).toSQL();
|
||||
equal(3, tableSql.length);
|
||||
expect(tableSql[1].sql).to.equal('alter table "person" add constraint person_user_id_foreign foreign key ("user_id") references "users" ("id") on delete SET NULL');
|
||||
expect(tableSql[2].sql).to.equal('alter table "person" add constraint person_account_id_foreign foreign key ("account_id") references "accounts" ("id") on update cascade');
|
||||
});
|
||||
|
||||
it("adding unique key", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.unique('foo', 'bar');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add constraint bar unique ("foo")');
|
||||
});
|
||||
|
||||
it("adding unique key fluently", function() {
|
||||
tableSql = new SchemaBuilder().createTable('users', function(table) {
|
||||
table.string('email').unique();
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('create table "users" ("email" varchar(255))');
|
||||
expect(tableSql[1].sql).to.equal('alter table "users" add constraint users_email_unique unique ("email")');
|
||||
});
|
||||
|
||||
it("adding index without value", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.index(['foo', 'bar']);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('create index users_foo_bar_index on "users" ("foo", "bar")');
|
||||
});
|
||||
|
||||
it("adding index", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.index(['foo', 'bar'], 'baz');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('create index baz on "users" ("foo", "bar")');
|
||||
});
|
||||
|
||||
it("adding index fluently", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.string('name').index();
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "name" varchar(255)');
|
||||
expect(tableSql[1].sql).to.equal('create index users_name_index on "users" ("name")');
|
||||
});
|
||||
|
||||
it("adding index with an index type", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.index(['foo', 'bar'], 'baz', 'left join');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('create index baz on "users" using left join ("foo", "bar")');
|
||||
});
|
||||
|
||||
it("adding index with an index type fluently", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.string('name').index('baz', 'left join');
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "name" varchar(255)');
|
||||
expect(tableSql[1].sql).to.equal('create index baz on "users" using left join ("name")');
|
||||
});
|
||||
|
||||
it("adding index with an index type and default name fluently", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.string('name').index(null, 'left join');
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "name" varchar(255)');
|
||||
expect(tableSql[1].sql).to.equal('create index users_name_index on "users" using left join ("name")');
|
||||
});
|
||||
|
||||
it("adding incrementing id", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.increments('id');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" serial primary key');
|
||||
});
|
||||
|
||||
it("adding big incrementing id", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.bigIncrements('id');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" bigserial primary key');
|
||||
});
|
||||
|
||||
it("adding string", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.string('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" varchar(255)');
|
||||
});
|
||||
|
||||
it("adding varchar with length", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.string('foo', 100);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" varchar(100)');
|
||||
});
|
||||
|
||||
it("adding a string with a default", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.string('foo', 100).defaultTo('bar');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" varchar(100) default \'bar\'');
|
||||
});
|
||||
|
||||
it("adding text", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.text('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" text');
|
||||
});
|
||||
|
||||
it("adding big integer", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.bigInteger('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" bigint');
|
||||
});
|
||||
|
||||
it("tests a big integer as the primary autoincrement key", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.bigIncrements('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" bigserial primary key');
|
||||
});
|
||||
|
||||
it("adding integer", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.integer('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" integer');
|
||||
});
|
||||
|
||||
it("adding autoincrement integer", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.increments('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" serial primary key');
|
||||
});
|
||||
|
||||
it("adding medium integer", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.mediumint('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" integer');
|
||||
});
|
||||
|
||||
it("adding tiny integer", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.tinyint('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" smallint');
|
||||
});
|
||||
|
||||
it("adding small integer", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.smallint('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" smallint');
|
||||
});
|
||||
|
||||
it("adding float", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.float('foo', 5, 2);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" real');
|
||||
});
|
||||
|
||||
it("adding double", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.double('foo', 15, 8);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" double precision');
|
||||
});
|
||||
|
||||
it("adding decimal", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.decimal('foo', 5, 2);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" decimal(5, 2)');
|
||||
});
|
||||
|
||||
it("adding boolean", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.boolean('foo').defaultTo(false);
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" boolean default \'0\'');
|
||||
});
|
||||
|
||||
it("adding date", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.date('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" date');
|
||||
});
|
||||
|
||||
it("adding date time", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.dateTime('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" datetime');
|
||||
});
|
||||
|
||||
it("adding time", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.time('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" time');
|
||||
});
|
||||
|
||||
it("adding timestamp", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.timestamp('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" datetime');
|
||||
});
|
||||
|
||||
it("adding timestamps", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.timestamps();
|
||||
}).toSQL();
|
||||
equal(2, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "created_at" datetime');
|
||||
expect(tableSql[1].sql).to.equal('alter table "users" add column "updated_at" datetime');
|
||||
});
|
||||
|
||||
it("adding binary", function() {
|
||||
tableSql = new SchemaBuilder().table('users', function(table) {
|
||||
table.binary('foo');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" blob');
|
||||
});
|
||||
|
||||
it('sets specificType correctly', function() {
|
||||
tableSql = new SchemaBuilder().table('user', function(t) {
|
||||
t.specificType('email', 'guid').unique().notNullable();
|
||||
}).toSQL();
|
||||
expect(tableSql[0].sql).to.equal('alter table "user" add column "email" guid not null');
|
||||
});
|
||||
});
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user