Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/join on val and on val or on val #2746

Merged
merged 4 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/query/compiler.js
Expand Up @@ -589,6 +589,16 @@ assign(QueryCompiler.prototype, {
);
},

onVal(clause) {
return (
this.formatter.wrap(clause.column) +
' ' +
this.formatter.operator(clause.operator) +
' ' +
this.formatter.parameter(clause.value)
);
},

onRaw(clause) {
return this.formatter.unwrapRaw(clause.value);
},
Expand Down
99 changes: 68 additions & 31 deletions src/query/joinclause.js
Expand Up @@ -14,40 +14,24 @@ function JoinClause(table, type, schema) {
this.clauses = [];
}

assign(JoinClause.prototype, {
grouping: 'join',
function getClauseFromArguments(compilerType, bool, first, operator, second) {
let data = null;

// Adds an "on" clause to the current join object.
on(first, operator, second) {
if (typeof first === 'function') {
this.clauses.push({
type: 'onWrapped',
value: first,
bool: this._bool(),
});
return this;
}

let data;
const bool = this._bool();
if (typeof first === 'function') {
data = {
type: 'onWrapped',
value: first,
bool: bool,
};
} else {
switch (arguments.length) {
case 1: {
if (typeof first === 'object' && typeof first.toSQL !== 'function') {
const keys = Object.keys(first);
let i = -1;
const method = bool === 'or' ? 'orOn' : 'on';
while (++i < keys.length) {
this[method](keys[i], first[keys[i]]);
}
return this;
} else {
data = { type: 'onRaw', value: first, bool };
}
case 3: {
data = { type: 'onRaw', value: first, bool };
break;
}
case 2:
case 4:
data = {
type: 'onBasic',
type: compilerType,
column: first,
operator: '=',
value: operator,
Expand All @@ -56,14 +40,39 @@ assign(JoinClause.prototype, {
break;
default:
data = {
type: 'onBasic',
type: compilerType,
column: first,
operator,
value: second,
bool,
};
}
this.clauses.push(data);
}

return data;
}

assign(JoinClause.prototype, {
grouping: 'join',

// Adds an "on" clause to the current join object.
on(first) {
if (typeof first === 'object' && typeof first.toSQL !== 'function') {
const keys = Object.keys(first);
let i = -1;
const method = this._bool() === 'or' ? 'orOn' : 'on';
while (++i < keys.length) {
this[method](keys[i], first[keys[i]]);
}
return this;
}

const data = getClauseFromArguments('onBasic', this._bool(), ...arguments);

if (data) {
this.clauses.push(data);
}

return this;
},

Expand All @@ -82,6 +91,34 @@ assign(JoinClause.prototype, {
return this._bool('or').on.apply(this, arguments);
},

onVal(first) {
if (typeof first === 'object' && typeof first.toSQL !== 'function') {
const keys = Object.keys(first);
let i = -1;
const method = this._bool() === 'or' ? 'orOnVal' : 'onVal';
while (++i < keys.length) {
this[method](keys[i], first[keys[i]]);
}
return this;
}

const data = getClauseFromArguments('onVal', this._bool(), ...arguments);

if (data) {
this.clauses.push(data);
}

return this;
},

andOnVal() {
return this.onVal(...arguments);
},

orOnVal() {
return this._bool('or').onVal(...arguments);
},

onBetween(column, values) {
assert(
Array.isArray(values),
Expand Down
55 changes: 55 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -8149,4 +8149,59 @@ describe('QueryBuilder', function() {
oracledb: 'select 0',
});
});

it('join with onVal andOnVal orOnVal', () => {
testsql(
qb()
.select({
id: 'p.ID',
status: 'p.post_status',
name: 'p.post_title',
// type: 'terms.name',
price: 'price.meta_value',
createdAt: 'p.post_date_gmt',
updatedAt: 'p.post_modified_gmt',
})
.from({ p: 'wp_posts' })
.leftJoin({ price: 'wp_postmeta' }, function() {
this.on('p.id', '=', 'price.post_id')
.onVal(function() {
this.onVal('price.meta_key', '_regular_price').andOnVal(
'price_meta_key',
'_regular_price'
);
})
.orOnVal(function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need the extra closure here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't necessarily. It's just personal preference. I usually prefer keeping conditions within bounds of closures so that future additions don't accidentally end up in the wrong closure.

Calling without function would also yield valid syntax.

this.onVal('price_meta.key', '_regular_price');
});
}),
{
pg: {
sql:
'select "p"."ID" as "id", "p"."post_status" as "status", "p"."post_title" as "name", "price"."meta_value" as "price", "p"."post_date_gmt" as "createdAt", "p"."post_modified_gmt" as "updatedAt" from "wp_posts" as "p" left join "wp_postmeta" as "price" on "p"."id" = "price"."post_id" and ("price"."meta_key" = ? and "price_meta_key" = ?) or ("price_meta"."key" = ?)',
bindings: ['_regular_price', '_regular_price', '_regular_price'],
},
mysql: {
sql:
'select `p`.`ID` as `id`, `p`.`post_status` as `status`, `p`.`post_title` as `name`, `price`.`meta_value` as `price`, `p`.`post_date_gmt` as `createdAt`, `p`.`post_modified_gmt` as `updatedAt` from `wp_posts` as `p` left join `wp_postmeta` as `price` on `p`.`id` = `price`.`post_id` and (`price`.`meta_key` = ? and `price_meta_key` = ?) or (`price_meta`.`key` = ?)',
bindings: ['_regular_price', '_regular_price', '_regular_price'],
},
mssql: {
sql:
'select [p].[ID] as [id], [p].[post_status] as [status], [p].[post_title] as [name], [price].[meta_value] as [price], [p].[post_date_gmt] as [createdAt], [p].[post_modified_gmt] as [updatedAt] from [wp_posts] as [p] left join [wp_postmeta] as [price] on [p].[id] = [price].[post_id] and ([price].[meta_key] = ? and [price_meta_key] = ?) or ([price_meta].[key] = ?)',
bindings: ['_regular_price', '_regular_price', '_regular_price'],
},
'pg-redshift': {
sql:
'select "p"."ID" as "id", "p"."post_status" as "status", "p"."post_title" as "name", "price"."meta_value" as "price", "p"."post_date_gmt" as "createdAt", "p"."post_modified_gmt" as "updatedAt" from "wp_posts" as "p" left join "wp_postmeta" as "price" on "p"."id" = "price"."post_id" and ("price"."meta_key" = ? and "price_meta_key" = ?) or ("price_meta"."key" = ?)',
bindings: ['_regular_price', '_regular_price', '_regular_price'],
},
oracledb: {
sql:
'select "p"."ID" "id", "p"."post_status" "status", "p"."post_title" "name", "price"."meta_value" "price", "p"."post_date_gmt" "createdAt", "p"."post_modified_gmt" "updatedAt" from "wp_posts" "p" left join "wp_postmeta" "price" on "p"."id" = "price"."post_id" and ("price"."meta_key" = ? and "price_meta_key" = ?) or ("price_meta"."key" = ?)',
bindings: ['_regular_price', '_regular_price', '_regular_price'],
},
}
);
});
});