query-generator.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const MySQLQueryGenerator = require('../mysql/query-generator');
  3. const Utils = require('./../../utils');
  4. class MariaDBQueryGenerator extends MySQLQueryGenerator {
  5. createSchema(schema, options) {
  6. options = {
  7. charset: null,
  8. collate: null,
  9. ...options
  10. };
  11. return Utils.joinSQLFragments([
  12. 'CREATE SCHEMA IF NOT EXISTS',
  13. this.quoteIdentifier(schema),
  14. options.charset && `DEFAULT CHARACTER SET ${this.escape(options.charset)}`,
  15. options.collate && `DEFAULT COLLATE ${this.escape(options.collate)}`,
  16. ';'
  17. ]);
  18. }
  19. dropSchema(schema) {
  20. return `DROP SCHEMA IF EXISTS ${this.quoteIdentifier(schema)};`;
  21. }
  22. showSchemasQuery(options) {
  23. const schemasToSkip = [
  24. '\'MYSQL\'',
  25. '\'INFORMATION_SCHEMA\'',
  26. '\'PERFORMANCE_SCHEMA\''
  27. ];
  28. if (options.skip && Array.isArray(options.skip) && options.skip.length > 0) {
  29. for (const schemaName of options.skip) {
  30. schemasToSkip.push(this.escape(schemaName));
  31. }
  32. }
  33. return Utils.joinSQLFragments([
  34. 'SELECT SCHEMA_NAME as schema_name',
  35. 'FROM INFORMATION_SCHEMA.SCHEMATA',
  36. `WHERE SCHEMA_NAME NOT IN (${schemasToSkip.join(', ')})`,
  37. ';'
  38. ]);
  39. }
  40. showTablesQuery(database) {
  41. let query = 'SELECT TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\'';
  42. if (database) {
  43. query += ` AND TABLE_SCHEMA = ${this.escape(database)}`;
  44. } else {
  45. query += ' AND TABLE_SCHEMA NOT IN (\'MYSQL\', \'INFORMATION_SCHEMA\', \'PERFORMANCE_SCHEMA\')';
  46. }
  47. return `${query};`;
  48. }
  49. }
  50. module.exports = MariaDBQueryGenerator;