Skip to content

Commit 43b8779

Browse files
committed
string inputs are no longer formatted
1 parent c7fccfb commit 43b8779

9 files changed

Lines changed: 278 additions & 202 deletions

File tree

packages/inquire/src/builder/Select.ts

Lines changed: 22 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//common
22
import type {
3+
Column,
34
Join,
45
Selector,
56
Sort,
@@ -26,7 +27,7 @@ export default class Select<R = unknown> implements WhereBuilder {
2627
/**
2728
* The table to select from.
2829
*/
29-
protected _from?: Table;
30+
protected _from?: { table: string | Table, alias?: string };
3031

3132
/**
3233
* The JSON filters to apply.
@@ -51,7 +52,7 @@ export default class Select<R = unknown> implements WhereBuilder {
5152
/**
5253
* The columns to select.
5354
*/
54-
protected _selectors: Selector[] = [];
55+
protected _selectors: (Selector|string)[] = [];
5556

5657
/**
5758
* The sort order.
@@ -81,7 +82,7 @@ export default class Select<R = unknown> implements WhereBuilder {
8182
* Set select, quote and action
8283
*/
8384
public constructor(
84-
select: string | (string | string[])[] = '*',
85+
select: string | (string | Selector)[] = '*',
8586
engine?: Engine
8687
) {
8788
this._engine = engine;
@@ -107,16 +108,8 @@ export default class Select<R = unknown> implements WhereBuilder {
107108
/**
108109
* FROM clause
109110
*/
110-
public from(table: string | string[], alias?: string) {
111-
if (Array.isArray(table) && table.length === 0) {
112-
//throw error?
113-
return this;
114-
}
115-
this._from = !Array.isArray(table)
116-
? { name: table, alias }
117-
: table.length === 1
118-
? { name: table[0], alias }
119-
: { name: table[0], alias: table[1] || alias };
111+
public from(table: string | Table, alias?: string) {
112+
this._from = { table, alias };
120113
return this;
121114
}
122115

@@ -125,35 +118,11 @@ export default class Select<R = unknown> implements WhereBuilder {
125118
*/
126119
public join(
127120
type: string,
128-
table: string | string[],
129-
from: string | string[],
130-
to: string | string[]
121+
table: string | Table,
122+
from: string | Column,
123+
to: string | Column
131124
) {
132-
if ((Array.isArray(table) && table.length === 0)
133-
|| (Array.isArray(from) && from.length === 0)
134-
|| (Array.isArray(to) && to.length === 0)
135-
) {
136-
//throw error?
137-
return this;
138-
}
139-
this._joins.push({
140-
type,
141-
table: !Array.isArray(table)
142-
? { name: table }
143-
: table.length === 1
144-
? { name: table[0] }
145-
: { name: table[0], alias: table[1] },
146-
from: !Array.isArray(from)
147-
? { name: from }
148-
: from.length === 1
149-
? { name: from[0] }
150-
: { table: from[0], name: from[1] },
151-
to: !Array.isArray(to)
152-
? { name: to }
153-
: to.length === 1
154-
? { name: to[0] }
155-
: { table: to[0], name: to[1] }
156-
});
125+
this._joins.push({ type, table, from, to });
157126
return this;
158127
}
159128

@@ -177,21 +146,10 @@ export default class Select<R = unknown> implements WhereBuilder {
177146
* ORDER BY clause
178147
*/
179148
public order(
180-
column: string | string[],
149+
column: string | Column,
181150
direction: OrderType = 'ASC'
182151
) {
183-
if ((Array.isArray(column) && column.length === 0)) {
184-
//throw error?
185-
return this;
186-
}
187-
this._sort.push({
188-
column: !Array.isArray(column)
189-
? { name: column }
190-
: column.length === 1
191-
? { name: column[0] }
192-
: { table: column[0], name: column[1] },
193-
direction
194-
});
152+
this._sort.push({ column, direction });
195153
return this;
196154
}
197155

@@ -209,56 +167,24 @@ export default class Select<R = unknown> implements WhereBuilder {
209167
/**
210168
* SELECT clause
211169
*/
212-
public select(columns: string | (string | string[])[]) {
170+
public select(columns: string | (string | Selector)[]) {
213171
//if the columns is a string
214172
if (typeof columns === 'string') {
173+
//if a comma separated string
215174
if (columns.indexOf(',') > -1) {
216-
this._selectors = columns
217-
.split(',')
175+
//set the selectors to a list of raw strings. These should be
176+
// processed as is to indicate to the dialect not to format it...
177+
this._selectors = columns.split(',')
218178
.map(column => column.trim())
219-
.filter(Boolean)
220-
.map(column => ({ name: column }));
179+
.filter(Boolean);
221180
} else {
222-
this._selectors = [{ name: columns }];
181+
//it's just a raw string, so set the selectors as
182+
// is to indicate to the dialect not to format it...
183+
this._selectors = [ columns.trim() ];
223184
}
224185
return this;
225186
}
226-
//if columns is not an array at this point
227-
if (!Array.isArray(columns)) {
228-
//then there's nothing we can do with it
229-
return this;
230-
}
231-
//make a storage for the final tuples
232-
const select: Selector[] = [];
233-
//for each column
234-
for (const column of columns) {
235-
//if this column is a string
236-
if (typeof column === 'string') {
237-
//make into tuple and push
238-
select.push({ name: column });
239-
//if column is an array with 2 items, we assume it's a tuple and push
240-
} else if (Array.isArray(column)
241-
&& column.every(item => typeof item === 'string')
242-
) {
243-
column.length === 1 && select.push({
244-
name: column[0]
245-
});
246-
column.length === 2 && select.push({
247-
name: column[0],
248-
alias: column[1]
249-
});
250-
column.length > 2 && select.push({
251-
table: column[0],
252-
name: column[1],
253-
alias: column[2]
254-
});
255-
}
256-
}
257-
//if there are some valid columns
258-
if (select.length > 0) {
259-
//then set the columns
260-
this._selectors = select;
261-
}
187+
this._selectors = columns;
262188
return this;
263189
}
264190

packages/inquire/src/dialect/Mysql.ts

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -474,49 +474,82 @@ export class MysqlDialect extends JsonTrait implements Dialect {
474474
const values: FlatValue[] = [];
475475

476476
const columns = build.selectors.map(selector => {
477-
const name = selector.name !== '*'
478-
? `${this.q}${selector.name}${this.q}`
479-
: '*';
477+
//if the selector is a string
478+
if (typeof selector === 'string') {
479+
//no matter what that string is, just use it...
480+
return selector;
481+
}
482+
//extract the column, table and alias from the selector
483+
const { column, table, alias } = selector;
484+
//selector types can be formatted and quoted...
480485
return selector.table && selector.alias
481-
? [
482-
`${this.q}${selector.table}${this.q}.${name}`,
483-
`${this.q}${selector.alias}${this.q}`
484-
].join(' AS ')
486+
? `${this.q}${table}${this.q}.`
487+
+ `${this.q}${column}${this.q} `
488+
+ `AS ${this.q}${alias}${this.q}`
485489
: selector.table
486-
? `${this.q}${selector.table}${this.q}.${name}`
487-
: selector.alias && this._isJsonic(selector.name)
490+
//cant json here because:
491+
// "table.JSON_UNQUOTE(JSON_EXTRACT(column, '$.path')) AS alias"
492+
// is not valid syntax...
493+
? `${this.q}${table}${this.q}.${this.q}${column}${this.q}`
494+
: alias && this._isJsonic(column)
488495
? [
489-
this._jsonReplace(selector.name),
490-
`${this.q}${selector.alias}${this.q}`
496+
this._jsonReplace(column),
497+
`${this.q}${alias}${this.q}`
491498
].join(' AS ')
492-
: selector.alias
493-
? `${name} AS ${this.q}${selector.alias}${this.q}`
494-
: this._isJsonic(selector.name)
495-
? this._jsonReplace(selector.name)
496-
: name
499+
: alias
500+
? `${this.q}${column}${this.q} AS ${this.q}${alias}${this.q}`
501+
: this._isJsonic(column)
502+
? this._jsonReplace(column)
503+
: `${this.q}${column}${this.q}`
497504
});
498505

499506
query.push(`SELECT ${columns.join(', ')}`);
500507

501-
const table = `${this.q}${build.from.name}${this.q}`;
502-
if (build.from.alias) {
503-
const alias = `${this.q}${build.from.alias}${this.q}`;
504-
query.push(`FROM ${table} AS ${alias}`);
508+
//if from table is a string
509+
if (typeof build.from.table === 'string') {
510+
const { table, alias } = build.from;
511+
if (alias) {
512+
//just use the string as is...
513+
query.push(`FROM ${table} AS ${alias}`);
514+
} else {
515+
//just use the string as is...
516+
query.push(`FROM ${table}`);
517+
}
505518
} else {
506-
query.push(`FROM ${table}`);
519+
const { name, alias } = build.from.table;
520+
if (alias) {
521+
//format with quotes
522+
query.push(`FROM ${this.q}${name}${this.q} AS ${this.q}${alias}${this.q}`);
523+
} else {
524+
//format with quotes
525+
query.push(`FROM ${this.q}${name}${this.q}`);
526+
}
507527
}
508528

509529
if (build.joins.length) {
510530
const joins = build.joins.map(relation => {
531+
//const { type, table, from, to } = relation;
511532
const type = joinTypes[relation.type as JoinType];
512-
const table = relation.table.alias
533+
//for table, if it's a string
534+
const table = typeof relation.table === 'string'
535+
//just use the string as is...
536+
? relation.table
537+
: relation.table.alias
513538
? `${this.q}${relation.table.name}${this.q}`
514539
+ ` AS ${this.q}${relation.table.alias}${this.q}`
515540
: `${this.q}${relation.table.name}${this.q}`;
516-
const from = relation.from.table
541+
//for from, if it's a string
542+
const from = typeof relation.from === 'string'
543+
//just use the string as is...
544+
? relation.from
545+
: relation.from.table
517546
? `${this.q}${relation.from.table}${this.q}.${this.q}${relation.from.name}${this.q}`
518547
: `${this.q}${relation.from.name}${this.q}`;
519-
const to = relation.to.table
548+
//for to, if it's a string
549+
const to = typeof relation.to === 'string'
550+
//just use the string as is...
551+
? relation.to
552+
: relation.to.table
520553
? `${this.q}${relation.to.table}${this.q}.${this.q}${relation.to.name}${this.q}`
521554
: `${this.q}${relation.to.name}${this.q}`;
522555
return `${type} JOIN ${table} ON (${from} = ${to})`;
@@ -576,12 +609,19 @@ export class MysqlDialect extends JsonTrait implements Dialect {
576609

577610
if (build.sort.length) {
578611
const sort = build.sort.map(sort => {
612+
//if the sort column is a string
613+
if (typeof sort.column === 'string') {
614+
//just use it as is...
615+
return `${sort.column} ${sort.direction.toUpperCase()}`;
616+
}
579617
//if the sort column is using the selector ":" notation
580618
if (this._isJsonic(sort.column.name)) {
581-
return `${this._jsonReplace(sort.column.name)} ${sort.direction.toUpperCase()}`;
619+
return `${this._jsonReplace(sort.column.name)} `
620+
+ sort.direction.toUpperCase();
582621
}
583622
const column = sort.column.table
584-
? `${this.q}${sort.column.table}${this.q}.${this.q}${sort.column.name}${this.q}`
623+
? `${this.q}${sort.column.table}${this.q}.`
624+
+ `${this.q}${sort.column.name}${this.q}`
585625
: `${this.q}${sort.column.name}${this.q}`;
586626
return `${column} ${sort.direction.toUpperCase()}`;
587627
}).filter(Boolean);

0 commit comments

Comments
 (0)