Browse Source

quote aliases, quote all table names and add more support for mysql

auto-join
Paul Winkler 12 years ago
parent
commit
1590255ac7
  1. 19
      lib/dialect/mysql.js
  2. 14
      lib/dialect/postgres.js
  3. 5
      lib/table.js

19
lib/dialect/mysql.js

@ -1,6 +1,7 @@
'use strict';
var util = require('util');
var assert = require('assert');
var Mysql = function() {
this.output = [];
@ -9,9 +10,9 @@ var Mysql = function() {
util.inherits(Mysql, require(__dirname + '/postgres'));
Mysql.prototype.quote = function(word) {
return '`' + word + '`';
};
Mysql.prototype._quoteCharacter = '`';
Mysql.prototype._arrayAggFunctionName = 'GROUP_CONCAT';
Mysql.prototype.visitParameter = function(parameter) {
this.params.push(parameter.value());
@ -19,9 +20,7 @@ Mysql.prototype.visitParameter = function(parameter) {
};
Mysql.prototype.visitDefault = function(parameter) {
var params = this.params;
this.params.push('DEFAULT');
return "?";
return "DEFAULT";
};
Mysql.prototype.visitOrderByColumn = function(column) {
@ -32,4 +31,12 @@ Mysql.prototype.visitOrderByColumn = function(column) {
}
};
Mysql.prototype.visitRenameColumn = function(renameColumn) {
var dataType = renameColumn.nodes[1].dataType || renameColumn.nodes[0].dataType;
assert(dataType, 'dataType missing for column ' + (renameColumn.nodes[1].name || renameColumn.nodes[0].name || '') +
' (CHANGE COLUMN statements require a dataType)');
return ['CHANGE COLUMN ' + this.visit(renameColumn.nodes[0]) + ' ' + this.visit(renameColumn.nodes[1])+' '+dataType];
};
module.exports = Mysql;

14
lib/dialect/postgres.js

@ -9,6 +9,8 @@ var Postgres = function() {
this.params = [];
};
Postgres.prototype._arrayAggFunctionName = 'array_agg';
Postgres.prototype.getQuery = function(queryNode) {
this.visitQuery(queryNode);
return { text: this.output.join(' '), values: this.params };
@ -52,8 +54,10 @@ Postgres.prototype.visit = function(node) {
}
};
Postgres.prototype._quoteCharacter = '"';
Postgres.prototype.quote = function(word) {
return '"' + word + '"';
var q = this._quoteCharacter;
return q + word.replace(q,q+q) + q;
};
Postgres.prototype.visitSelect = function(select) {
@ -245,7 +249,7 @@ Postgres.prototype.visitTable = function(tableNode) {
}
txt += this.quote(table.getName());
if(table.alias) {
txt += ' AS ' + table.alias;
txt += ' AS ' + this.quote(table.alias);
}
return txt;
};
@ -256,14 +260,14 @@ Postgres.prototype.visitColumn = function(columnNode) {
var txt = "";
if(inSelectClause) {
if (columnNode.asArray) {
txt += 'array_agg(';
txt += this._arrayAggFunctionName+'(';
} else if (columnNode.aggCount) {
txt += 'COUNT(';
}
}
if(!this._visitedInsert && !this._visitingUpdateTargetColumn && !this._visitingCreate && !this._visitingAlter) {
if(table.alias) {
txt = table.alias;
txt = this.quote(table.alias);
} else {
if(table.getSchema()) {
txt = this.quote(table.getSchema());
@ -282,7 +286,7 @@ Postgres.prototype.visitColumn = function(columnNode) {
txt += ')';
}
if(inSelectClause && columnNode.alias) {
txt += ' as ' + this.quote(columnNode.alias);
txt += ' AS ' + this.quote(columnNode.alias);
}
if(this._visitingCreate || this._visitingAddColumn) {
assert(columnNode.dataType, 'dataType missing for column ' + columnNode.name +

5
lib/table.js

@ -46,10 +46,7 @@ Table.prototype.getName = function() {
};
Table.prototype.star = function() {
var name = this.alias || this._name;
var Dialect = require(__dirname + '/../').dialect;
var dialect = new Dialect();
return new TextNode(dialect.quote(name)+'.*');
return new Column({table: this, star: true});
};
Table.prototype.count = function(alias) {

Loading…
Cancel
Save