@@ -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