2016-03-02 17:07:05 +01:00
|
|
|
// MySQL Schema Compiler
|
|
|
|
// -------
|
2019-06-04 00:37:17 +02:00
|
|
|
const SchemaCompiler = require('../../../schema/compiler');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 17:46:10 +02:00
|
|
|
class SchemaCompiler_MySQL extends SchemaCompiler {
|
|
|
|
constructor(client, builder) {
|
|
|
|
super(client, builder);
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Rename a table on the schema.
|
2016-05-17 01:01:34 +10:00
|
|
|
renameTable(tableName, to) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery(
|
|
|
|
`rename table ${this.formatter.wrap(tableName)} to ${this.formatter.wrap(
|
|
|
|
to
|
|
|
|
)}`
|
|
|
|
);
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-10-20 22:23:29 +02:00
|
|
|
renameView(from, to) {
|
|
|
|
this.renameTable(from, to);
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Check whether a table exists on the query.
|
2016-05-17 01:01:34 +10:00
|
|
|
hasTable(tableName) {
|
2017-06-09 14:19:37 -06:00
|
|
|
let sql = 'select * from information_schema.tables where table_name = ?';
|
2018-07-09 08:10:34 -04:00
|
|
|
const bindings = [tableName];
|
2017-06-09 14:19:37 -06:00
|
|
|
|
|
|
|
if (this.schema) {
|
|
|
|
sql += ' and table_schema = ?';
|
|
|
|
bindings.push(this.schema);
|
|
|
|
} else {
|
|
|
|
sql += ' and table_schema = database()';
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushQuery({
|
2017-06-09 14:19:37 -06:00
|
|
|
sql,
|
|
|
|
bindings,
|
|
|
|
output: function output(resp) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return resp.length > 0;
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Check whether a column exists on the schema.
|
2016-05-17 01:01:34 +10:00
|
|
|
hasColumn(tableName, column) {
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushQuery({
|
2019-03-13 22:58:59 +01:00
|
|
|
sql: `show columns from ${this.formatter.wrap(tableName)}`,
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
2021-01-01 17:46:10 +02:00
|
|
|
return resp.some((row) => {
|
2019-03-13 22:58:59 +01:00
|
|
|
return (
|
|
|
|
this.client.wrapIdentifier(row.Field) ===
|
|
|
|
this.client.wrapIdentifier(column)
|
|
|
|
);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = SchemaCompiler_MySQL;
|