@@ -230,7 +230,23 @@ SQLConnector.prototype.tableEscaped = function(model) {
230
230
* @returns {String } The escaped column name
231
231
*/
232
232
SQLConnector . prototype . columnEscaped = function ( model , property ) {
233
- return this . tableEscaped ( model ) + '.' + this . escapeName ( this . column ( model , property ) ) ;
233
+ return this . escapeName ( this . column ( model , property ) ) ;
234
+ } ;
235
+
236
+ /**
237
+ * Get the escaped qualified column name (table.column for join)
238
+ * @param {String } model The model name
239
+ * @param {String } property The property name
240
+ * @returns {String } The escaped column name
241
+ */
242
+ SQLConnector . prototype . qualifiedColumnEscaped = function ( model , property ) {
243
+ var table = this . tableEscaped ( model ) ;
244
+ var index = table . indexOf ( '.' ) ;
245
+ if ( index !== - 1 ) {
246
+ // Remove the schema name
247
+ table = table . substring ( index ) ;
248
+ }
249
+ return table + '.' + this . escapeName ( this . column ( model , property ) ) ;
234
250
} ;
235
251
236
252
/*!
@@ -1017,18 +1033,7 @@ SQLConnector.prototype.buildSelect = function(model, filter, options) {
1017
1033
}
1018
1034
}
1019
1035
1020
- var haveRelationFilters = false ;
1021
- if ( filter . where ) {
1022
- var relations = this . getModelDefinition ( model ) . model . relations ;
1023
- if ( relations ) {
1024
- for ( var key in filter . where ) {
1025
- if ( key in relations ) {
1026
- haveRelationFilters = true ;
1027
- break ;
1028
- }
1029
- }
1030
- }
1031
- }
1036
+ var haveRelationFilters = this . hasRelationClause ( model , filter . where ) ;
1032
1037
var distinct = haveRelationFilters ? 'DISTINCT ' : '' ;
1033
1038
1034
1039
var selectStmt = new ParameterizedSQL ( 'SELECT ' + distinct +
@@ -1084,8 +1089,8 @@ SQLConnector.prototype.buildJoins = function(model, where) {
1084
1089
innerIdField [ keyTo ] = true ;
1085
1090
innerFilter . fields = assign ( { } , innerFilter . fields , innerIdField ) ;
1086
1091
1087
- var condition = this . columnEscaped ( modelFrom , keyFrom ) + '=' +
1088
- this . columnEscaped ( modelTo , keyTo ) ;
1092
+ var condition = this . qualifiedColumnEscaped ( modelFrom , keyFrom ) + '=' +
1093
+ this . qualifiedColumnEscaped ( modelTo , keyTo ) ;
1089
1094
1090
1095
var innerSelect = this . buildSelect ( modelTo , innerFilter , {
1091
1096
skipParameterize : true
@@ -1113,7 +1118,7 @@ SQLConnector.prototype.buildJoins = function(model, where) {
1113
1118
// n:m relation
1114
1119
var modelThrough = rel . modelThrough . definition . name ;
1115
1120
var keyThrough = rel . keyThrough ;
1116
- var modelToKey = rel . modelTo . definition . _ids [ 0 ] . name ;
1121
+ var modelToKey = rel . modelTo . definition . idName ( ) ;
1117
1122
var innerFilter = { fields : { } } ;
1118
1123
innerFilter . fields [ keyThrough ] = true ;
1119
1124
@@ -1229,25 +1234,36 @@ SQLConnector.prototype.find = function(model, id, options, cb) {
1229
1234
Connector . defineAliases ( SQLConnector . prototype , 'find' , [ 'findById' ] ) ;
1230
1235
1231
1236
/**
1232
- * Build a SQL SELECT statement to count rows
1233
- * @param {String } model Model name
1234
- * @param {Object } where Where object
1235
- * @param {Object } options Options object
1236
- * @returns {ParameterizedSQL } Statement object {sql: ..., params: [...]}
1237
+ * Check if the where statement contains relations
1238
+ * @param model
1239
+ * @param where
1240
+ * @returns {boolean }
1237
1241
*/
1238
- SQLConnector . prototype . buildCount = function ( model , where , options ) {
1239
- var haveRelationFilters = false ;
1242
+ SQLConnector . prototype . hasRelationClause = function ( model , where ) {
1243
+ var found = false ;
1240
1244
if ( where ) {
1241
1245
var relations = this . getModelDefinition ( model ) . model . relations ;
1242
1246
if ( relations ) {
1243
1247
for ( var key in where ) {
1244
1248
if ( key in relations ) {
1245
- haveRelationFilters = true ;
1249
+ found = true ;
1246
1250
break ;
1247
1251
}
1248
1252
}
1249
1253
}
1250
1254
}
1255
+ return found ;
1256
+ } ;
1257
+
1258
+ /**
1259
+ * Build a SQL SELECT statement to count rows
1260
+ * @param {String } model Model name
1261
+ * @param {Object } where Where object
1262
+ * @param {Object } options Options object
1263
+ * @returns {ParameterizedSQL } Statement object {sql: ..., params: [...]}
1264
+ */
1265
+ SQLConnector . prototype . buildCount = function ( model , where , options ) {
1266
+ var haveRelationFilters = this . hasRelationClause ( model , where ) ;
1251
1267
1252
1268
var count = 'count(*)' ;
1253
1269
if ( haveRelationFilters ) {
0 commit comments