compiler.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _inherits = require('inherits');
  4. var _inherits2 = _interopRequireDefault(_inherits);
  5. var _compiler = require('../../../schema/compiler');
  6. var _compiler2 = _interopRequireDefault(_compiler);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. // PostgreSQL Schema Compiler
  9. // -------
  10. function SchemaCompiler_PG() {
  11. _compiler2.default.apply(this, arguments);
  12. }
  13. (0, _inherits2.default)(SchemaCompiler_PG, _compiler2.default);
  14. // Check whether the current table
  15. SchemaCompiler_PG.prototype.hasTable = function (tableName) {
  16. var sql = 'select * from information_schema.tables where table_name = ?';
  17. var bindings = [tableName];
  18. if (this.schema) {
  19. sql += ' and table_schema = ?';
  20. bindings.push(this.schema);
  21. } else {
  22. sql += ' and table_schema = current_schema';
  23. }
  24. this.pushQuery({
  25. sql: sql,
  26. bindings: bindings,
  27. output: function output(resp) {
  28. return resp.rows.length > 0;
  29. }
  30. });
  31. };
  32. // Compile the query to determine if a column exists in a table.
  33. SchemaCompiler_PG.prototype.hasColumn = function (tableName, columnName) {
  34. var sql = 'select * from information_schema.columns where table_name = ? and column_name = ?';
  35. var bindings = [tableName, columnName];
  36. if (this.schema) {
  37. sql += ' and table_schema = ?';
  38. bindings.push(this.schema);
  39. } else {
  40. sql += ' and table_schema = current_schema';
  41. }
  42. this.pushQuery({
  43. sql: sql,
  44. bindings: bindings,
  45. output: function output(resp) {
  46. return resp.rows.length > 0;
  47. }
  48. });
  49. };
  50. SchemaCompiler_PG.prototype.qualifiedTableName = function (tableName) {
  51. var name = this.schema ? this.schema + '.' + tableName : tableName;
  52. return this.formatter.wrap(name);
  53. };
  54. // Compile a rename table command.
  55. SchemaCompiler_PG.prototype.renameTable = function (from, to) {
  56. this.pushQuery('alter table ' + this.qualifiedTableName(from) + ' rename to ' + this.formatter.wrap(to));
  57. };
  58. SchemaCompiler_PG.prototype.createSchema = function (schemaName) {
  59. this.pushQuery('create schema ' + this.formatter.wrap(schemaName));
  60. };
  61. SchemaCompiler_PG.prototype.createSchemaIfNotExists = function (schemaName) {
  62. this.pushQuery('create schema if not exists ' + this.formatter.wrap(schemaName));
  63. };
  64. SchemaCompiler_PG.prototype.dropSchema = function (schemaName) {
  65. this.pushQuery('drop schema ' + this.formatter.wrap(schemaName));
  66. };
  67. SchemaCompiler_PG.prototype.dropSchemaIfExists = function (schemaName) {
  68. this.pushQuery('drop schema if exists ' + this.formatter.wrap(schemaName));
  69. };
  70. SchemaCompiler_PG.prototype.dropExtension = function (extensionName) {
  71. this.pushQuery('drop extension ' + this.formatter.wrap(extensionName));
  72. };
  73. SchemaCompiler_PG.prototype.dropExtensionIfExists = function (extensionName) {
  74. this.pushQuery('drop extension if exists ' + this.formatter.wrap(extensionName));
  75. };
  76. SchemaCompiler_PG.prototype.createExtension = function (extensionName) {
  77. this.pushQuery('create extension ' + this.formatter.wrap(extensionName));
  78. };
  79. SchemaCompiler_PG.prototype.createExtensionIfNotExists = function (extensionName) {
  80. this.pushQuery('create extension if not exists ' + this.formatter.wrap(extensionName));
  81. };
  82. exports.default = SchemaCompiler_PG;
  83. module.exports = exports['default'];