2014-09-01 17:18:45 +02:00
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// SQLite3: Column Builder & Compiler
|
|
|
|
// -------
|
2015-05-09 14:01:19 -04:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var _ = require('lodash');
|
2014-04-15 11:43:47 -04:00
|
|
|
var inherits = require('inherits');
|
2015-05-09 13:58:18 -04:00
|
|
|
var SchemaCompiler = require('../../../schema/compiler');
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
// Schema Compiler
|
|
|
|
// -------
|
|
|
|
|
|
|
|
function SchemaCompiler_SQLite3() {
|
2015-04-19 16:31:52 -04:00
|
|
|
SchemaCompiler.apply(this, arguments);
|
2014-04-15 11:43:47 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(SchemaCompiler_SQLite3, SchemaCompiler);
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
// Compile the query to determine if a table exists.
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_SQLite3.prototype.hasTable = function (tableName) {
|
2014-04-15 11:43:47 -04:00
|
|
|
this.pushQuery({
|
2015-05-09 13:58:18 -04:00
|
|
|
sql: 'select * from sqlite_master where type = \'table\' and name = ' + this.formatter.parameter(tableName),
|
|
|
|
output: function output(resp) {
|
2014-04-15 11:43:47 -04:00
|
|
|
return resp.length > 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile the query to determine if a column exists.
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_SQLite3.prototype.hasColumn = function (tableName, column) {
|
2014-04-15 11:43:47 -04:00
|
|
|
this.pushQuery({
|
|
|
|
sql: 'PRAGMA table_info(' + this.formatter.wrap(tableName) + ')',
|
2015-05-09 13:58:18 -04:00
|
|
|
output: function output(resp) {
|
|
|
|
return _.some(resp, { name: column });
|
2014-04-15 11:43:47 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a rename table command.
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_SQLite3.prototype.renameTable = function (from, to) {
|
2014-04-15 11:43:47 -04:00
|
|
|
this.pushQuery('alter table ' + this.formatter.wrap(from) + ' rename to ' + this.formatter.wrap(to));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
module.exports = SchemaCompiler_SQLite3;
|