@@ -52,7 +52,7 @@ public struct Query {
5252
5353 }
5454
55- private var columns : [ Expressible ] = [ Expression < ( ) > ( literal : " * " ) ]
55+ private var columns : [ Expressible ] ?
5656 private var distinct : Bool = false
5757 internal var tableName : Expression < ( ) >
5858 private var joins : [ ( type: JoinType , table: Query , condition: Expression < Bool > ) ] = [ ]
@@ -96,7 +96,9 @@ public struct Query {
9696 ///
9797 /// :returns: A query with SELECT * applied.
9898 public func select( all star: Star ) -> Query {
99- return select ( star ( nil , nil ) )
99+ var query = self
100+ ( query. distinct, query. columns) = ( false , nil )
101+ return query
100102 }
101103
102104 /// Sets the SELECT DISTINCT * clause on the query.
@@ -392,15 +394,16 @@ public struct Query {
392394 private var selectClause : Expressible {
393395 var expressions : [ Expressible ] = [ Expression < ( ) > ( literal: " SELECT " ) ]
394396 if distinct { expressions. append ( Expression < ( ) > ( literal: " DISTINCT " ) ) }
395- expressions. append ( Expression < ( ) > . join ( " , " , columns. map { $0. expression. aliased } ) )
397+ expressions. append ( Expression < ( ) > . join ( " , " , ( columns ?? [ Expression < ( ) > ( literal : " * " ) ] ) . map { $0. expression. aliased } ) )
396398 expressions. append ( Expression < ( ) > ( literal: " FROM \( tableName. aliased. SQL) " ) )
397399 return Expression < ( ) > . join ( " " , expressions)
398400 }
399401
400402 private var joinClause : Expressible ? {
401403 if joins. count == 0 { return nil }
402404 return Expression < ( ) > . join ( " " , joins. map { type, table, condition in
403- Expression < ( ) > ( literal: " \( type. rawValue) JOIN \( table. tableName. aliased. SQL) ON \( condition. SQL) " , condition. bindings)
405+ let join = ( table. columns == nil ? table. tableName : table. expression) . aliased
406+ return Expression < ( ) > ( literal: " \( type. rawValue) JOIN \( join. SQL) ON \( condition. SQL) " , join. bindings + condition. bindings)
404407 } )
405408 }
406409
@@ -824,7 +827,7 @@ public struct QueryGenerator: GeneratorType {
824827
825828 private lazy var columnNames : [ String : Int ] = {
826829 var ( columnNames, idx) = ( [ String: Int] ( ) , 0 )
827- column: for each in self . query. columns {
830+ column: for each in self . query. columns ?? [ Expression < ( ) > ( literal : " * " ) ] {
828831 let pair = split ( each . expression. SQL) { $0 == " . " }
829832 let ( tableName, column) = ( pair. count > 1 ? pair. first : nil , pair. last!)
830833
0 commit comments