11//common
22import type {
3- Order ,
3+ Join ,
4+ Selector ,
5+ Sort ,
6+ OrderType ,
7+ Table ,
8+ Where ,
9+ WhereJson ,
410 Reject ,
511 Resolve ,
6- Dialect ,
7- Relation ,
12+ Dialect ,
813 FlatValue ,
914 JSONScalarValue ,
1015 WhereBuilder
@@ -13,35 +18,20 @@ import Engine from '../Engine.js';
1318import Exception from '../Exception.js' ;
1419
1520export default class Select < R = unknown > implements WhereBuilder {
16- /**
17- * The columns to select.
18- */
19- protected _columns : string [ ] = [ ] ;
20-
2121 /**
2222 * Database engine
2323 */
2424 protected _engine ?: Engine ;
25-
26- /**
27- * The start
28- */
29- protected _offset : number = 0 ;
3025
3126 /**
32- * The filters to apply .
27+ * The table to select from .
3328 */
34- protected _filters : [ string , FlatValue [ ] ] [ ] = [ ] ;
29+ protected _from ?: Table ;
3530
3631 /**
3732 * The JSON filters to apply.
3833 */
39- protected _json : {
40- selector : string ,
41- query : string ,
42- replace : string ,
43- values : JSONScalarValue [ ]
44- } [ ] = [ ] ;
34+ protected _json : WhereJson [ ] = [ ] ;
4535
4636 /**
4737 * The range
@@ -51,14 +41,24 @@ export default class Select<R = unknown> implements WhereBuilder {
5141 /**
5242 * The relations to join.
5343 */
54- protected _relations : Relation [ ] = [ ] ;
44+ protected _joins : Join [ ] = [ ] ;
45+
46+ /**
47+ * The start
48+ */
49+ protected _offset : number = 0 ;
5550
5651 /**
5752 * Notation used to indicate to traverse through JSON
5853 * columns, default is colon (ex. data:info.name)
5954 */
6055 protected _selector = ':' ;
6156
57+ /**
58+ * The columns to select.
59+ */
60+ protected _selectors : Selector [ ] = [ ] ;
61+
6262 /**
6363 * The separator for JSON selectors,
6464 * default is dot (ex. data.info.name)
@@ -68,12 +68,12 @@ export default class Select<R = unknown> implements WhereBuilder {
6868 /**
6969 * The sort order.
7070 */
71- protected _sort : [ string , Order ] [ ] = [ ] ;
71+ protected _sort : Sort [ ] = [ ] ;
7272
7373 /**
74- * The table to select from .
74+ * The filters to apply .
7575 */
76- protected _table ?: [ string , string ] ;
76+ protected _where : Where [ ] = [ ] ;
7777
7878 /**
7979 * Sets the engine for the builder
@@ -108,7 +108,10 @@ export default class Select<R = unknown> implements WhereBuilder {
108108 /**
109109 * Set select, quote and action
110110 */
111- public constructor ( select : string | string [ ] = '*' , engine ?: Engine ) {
111+ public constructor (
112+ select : string | ( string | [ string , string ] ) [ ] = '*' ,
113+ engine ?: Engine
114+ ) {
112115 this . _engine = engine ;
113116 this . select ( select ) ;
114117 }
@@ -118,32 +121,69 @@ export default class Select<R = unknown> implements WhereBuilder {
118121 */
119122 public build ( ) {
120123 return {
121- columns : this . _columns ,
122- filters : this . _filters ,
124+ from : this . _from ,
125+ joins : this . _joins ,
123126 json : this . _json ,
124127 limit : this . _limit ,
125128 offset : this . _offset ,
126- relations : this . _relations ,
129+ selectors : this . _selectors ,
127130 selector : this . _selector ,
128131 separator : this . _separator ,
129132 sort : this . _sort ,
130- table : this . _table
133+ where : this . _where
131134 }
132135 }
133136
134137 /**
135138 * FROM clause
136139 */
137- public from ( table : string , as ?: string ) {
138- this . _table = [ table , as || table ] ;
140+ public from ( table : string | string [ ] , alias ?: string ) {
141+ if ( Array . isArray ( table ) && table . length === 0 ) {
142+ //throw error?
143+ return this ;
144+ }
145+ this . _from = ! Array . isArray ( table )
146+ ? { name : table , alias }
147+ : table . length === 1
148+ ? { name : table [ 0 ] , alias }
149+ : { name : table [ 0 ] , alias : table [ 1 ] || alias } ;
139150 return this ;
140151 }
141152
142153 /**
143154 * JOIN clause
144155 */
145- public join ( type : string , table : string , from : string , to : string , as ?: string ) {
146- this . _relations . push ( { type, table, as : as || table , from, to } ) ;
156+ public join (
157+ type : string ,
158+ table : string | string [ ] ,
159+ from : string | string [ ] ,
160+ to : string | string [ ]
161+ ) {
162+ if ( ( Array . isArray ( table ) && table . length === 0 )
163+ || ( Array . isArray ( from ) && from . length === 0 )
164+ || ( Array . isArray ( to ) && to . length === 0 )
165+ ) {
166+ //throw error?
167+ return this ;
168+ }
169+ this . _joins . push ( {
170+ type,
171+ table : ! Array . isArray ( table )
172+ ? { name : table }
173+ : table . length === 1
174+ ? { name : table [ 0 ] }
175+ : { name : table [ 0 ] , alias : table [ 1 ] } ,
176+ from : ! Array . isArray ( from )
177+ ? { name : from }
178+ : from . length === 1
179+ ? { name : from [ 0 ] }
180+ : { table : from [ 0 ] , name : from [ 1 ] } ,
181+ to : ! Array . isArray ( to )
182+ ? { name : to }
183+ : to . length === 1
184+ ? { name : to [ 0 ] }
185+ : { table : to [ 0 ] , name : to [ 1 ] }
186+ } ) ;
147187 return this ;
148188 }
149189
@@ -166,8 +206,22 @@ export default class Select<R = unknown> implements WhereBuilder {
166206 /**
167207 * ORDER BY clause
168208 */
169- public order ( column : string , direction : Order = 'ASC' ) {
170- this . _sort . push ( [ column , direction ] ) ;
209+ public order (
210+ column : string | string [ ] ,
211+ direction : OrderType = 'ASC'
212+ ) {
213+ if ( ( Array . isArray ( column ) && column . length === 0 ) ) {
214+ //throw error?
215+ return this ;
216+ }
217+ this . _sort . push ( {
218+ column : ! Array . isArray ( column )
219+ ? { name : column }
220+ : column . length === 1
221+ ? { name : column [ 0 ] }
222+ : { table : column [ 0 ] , name : column [ 1 ] } ,
223+ direction
224+ } ) ;
171225 return this ;
172226 }
173227
@@ -182,11 +236,58 @@ export default class Select<R = unknown> implements WhereBuilder {
182236 return dialect . select ( this ) ;
183237 }
184238
185- public select ( columns : string | string [ ] ) {
186- if ( Array . isArray ( columns ) ) {
187- this . _columns = columns ;
188- } else {
189- this . _columns = [ columns ] ;
239+ /**
240+ * SELECT clause
241+ */
242+ public select ( columns : string | ( string | string [ ] ) [ ] ) {
243+ //if the columns is a string
244+ if ( typeof columns === 'string' ) {
245+ if ( columns . indexOf ( ',' ) > - 1 ) {
246+ this . _selectors = columns
247+ . split ( ',' )
248+ . map ( column => column . trim ( ) )
249+ . filter ( Boolean )
250+ . map ( column => ( { name : column } ) ) ;
251+ } else {
252+ this . _selectors = [ { name : columns } ] ;
253+ }
254+ return this ;
255+ }
256+ //if columns is not an array at this point
257+ if ( ! Array . isArray ( columns ) ) {
258+ //then there's nothing we can do with it
259+ return this ;
260+ }
261+ //make a storage for the final tuples
262+ const select : Selector [ ] = [ ] ;
263+ //for each column
264+ for ( const column of columns ) {
265+ //if this column is a string
266+ if ( typeof column === 'string' ) {
267+ //make into tuple and push
268+ select . push ( { name : column } ) ;
269+ //if column is an array with 2 items, we assume it's a tuple and push
270+ } else if ( Array . isArray ( column )
271+ && column . every ( item => typeof item === 'string' )
272+ ) {
273+ column . length === 1 && select . push ( {
274+ name : column [ 0 ]
275+ } ) ;
276+ column . length === 2 && select . push ( {
277+ name : column [ 0 ] ,
278+ alias : column [ 1 ]
279+ } ) ;
280+ column . length > 2 && select . push ( {
281+ table : column [ 0 ] ,
282+ name : column [ 1 ] ,
283+ alias : column [ 2 ]
284+ } ) ;
285+ }
286+ }
287+ //if there are some valid columns
288+ if ( select . length > 0 ) {
289+ //then set the columns
290+ this . _selectors = select ;
190291 }
191292 return this ;
192293 }
@@ -205,8 +306,8 @@ export default class Select<R = unknown> implements WhereBuilder {
205306 /**
206307 * WHERE clause
207308 */
208- public where ( query : string , values : FlatValue [ ] = [ ] ) {
209- this . _filters . push ( [ query , values ] ) ;
309+ public where ( clause : string , values : FlatValue [ ] = [ ] ) {
310+ this . _where . push ( { clause , values } ) ;
210311 return this ;
211312 }
212313
@@ -246,4 +347,4 @@ export default class Select<R = unknown> implements WhereBuilder {
246347 } ) ;
247348 return this ;
248349 }
249- }
350+ } ;
0 commit comments