不要怂,就是干,撸起袖子干!

Commit 31113cec by Tommy Montgomery Committed by Sushant

feat: allow querying sqlite_master table (#9645)

1 parent b6c2e421
......@@ -3,6 +3,7 @@
const _ = require('lodash');
const Promise = require('../../promise');
const sequelizeErrors = require('../../errors');
const QueryTypes = require('../../query-types');
/**
Returns an object that treats SQLite's inabilities to do certain queries.
......@@ -152,7 +153,7 @@ function addConstraint(tableName, options) {
const describeCreateTableSql = this.QueryGenerator.describeCreateTableQuery(tableName);
let createTableSql;
return this.sequelize.query(describeCreateTableSql, options)
return this.sequelize.query(describeCreateTableSql, _.assign({}, options, { type: QueryTypes.SELECT, raw: true }))
.then(constraints => {
const sql = constraints[0].sql;
const index = sql.length - 1;
......
......@@ -144,14 +144,12 @@ class Query extends AbstractQuery {
}
}
if (query.sql.indexOf('sqlite_master') !== -1) {
if (query.sql.indexOf('SELECT sql FROM sqlite_master WHERE tbl_name') !== -1) {
result = results;
if (result && result[0] && result[0].sql.indexOf('CONSTRAINT') !== -1) {
result = query.parseConstraintsFromSql(results[0].sql);
}
} else {
result = results.map(resultSet => resultSet.name);
if (query.isShowTablesQuery()) {
result = results.map(row => row.name);
} else if (query.isShowConstraintsQuery()) {
result = results;
if (results && results[0] && results[0].sql) {
result = query.parseConstraintsFromSql(results[0].sql);
}
} else if (query.isSelectQuery()) {
if (!query.options.raw) {
......
......@@ -639,7 +639,10 @@ describe(Support.getTestDialectTeaser('DataTypes'), () => {
return record.reload();
}).then(record => {
expect(typeof record.stamp).to.be.eql('string');
expect(new Date(record.stamp)).to.equalDate(newDate);
const recordDate = new Date(record.stamp);
expect(recordDate.getUTCFullYear()).to.equal(newDate.getUTCFullYear());
expect(recordDate.getUTCDate()).to.equal(newDate.getUTCDate());
expect(recordDate.getUTCMonth()).to.equal(newDate.getUTCMonth());
});
});
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../../support'),
dialect = Support.getTestDialect(),
DataTypes = require(__dirname + '/../../../../lib/data-types');
if (dialect === 'sqlite') {
describe('[SQLITE Specific] sqlite_master raw queries', () => {
beforeEach(function() {
this.sequelize.define('SomeTable', {
someColumn: DataTypes.INTEGER
}, {
freezeTableName: true,
timestamps: false
});
return this.sequelize.sync({ force: true });
});
it('should be able to select with tbl_name filter', function() {
return this.sequelize.query('SELECT * FROM sqlite_master WHERE tbl_name=\'SomeTable\'')
.then(result => {
const rows = result[0];
expect(rows).to.have.length(1);
const row = rows[0];
expect(row).to.have.property('type', 'table');
expect(row).to.have.property('name', 'SomeTable');
expect(row).to.have.property('tbl_name', 'SomeTable');
expect(row).to.have.property('sql');
});
});
it('should be able to select *', function() {
return this.sequelize.query('SELECT * FROM sqlite_master')
.then(result => {
const rows = result[0];
expect(rows).to.have.length(2);
rows.forEach(row => {
expect(row).to.have.property('type');
expect(row).to.have.property('name');
expect(row).to.have.property('tbl_name');
expect(row).to.have.property('rootpage');
expect(row).to.have.property('sql');
});
});
});
it('should be able to select just "sql" column and get rows back', function() {
return this.sequelize.query('SELECT sql FROM sqlite_master WHERE tbl_name=\'SomeTable\'')
.then(result => {
const rows = result[0];
expect(rows).to.have.length(1);
const row = rows[0];
expect(row).to.have.property('sql',
'CREATE TABLE `SomeTable` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `someColumn` INTEGER)');
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!