@@ -131,35 +131,40 @@ func (b Buffer) escape(table, value string) string {
131131 return escapedValue .(string )
132132 }
133133
134- var escaped_table string
134+ table , alias := extractAlias (table )
135+ var escapedTable string
135136 if table != "" {
136- if i := strings .Index (strings .ToLower (table ), " as " ); i > - 1 {
137- return b .escape (table [:i ], "" ) + " AS " + b .Quoter .ID (table [i + 4 :])
137+ if table != alias {
138+ if value == "" {
139+ return b .escape (table , "" ) + " AS " + b .Quoter .ID (alias )
140+ } else {
141+ escapedTable = b .Quoter .ID (alias )
142+ }
138143 }
139144 if b .AllowTableSchema && strings .IndexByte (table , '.' ) >= 0 {
140145 parts := strings .Split (table , "." )
141146 for i , part := range parts {
142147 part = strings .TrimSpace (part )
143148 parts [i ] = b .Quoter .ID (part )
144149 }
145- escaped_table = strings .Join (parts , "." )
150+ escapedTable = strings .Join (parts , "." )
146151 } else {
147- escaped_table = b .Quoter .ID (strings .ReplaceAll (table , "." , "_" ))
152+ escapedTable = b .Quoter .ID (strings .ReplaceAll (table , "." , "_" ))
148153 }
149154 }
150155
151156 if value == "" {
152- escapedValue = escaped_table
157+ escapedValue = escapedTable
153158 } else if value == "*" {
154- escapedValue = escaped_table + ".*"
159+ escapedValue = escapedTable + ".*"
155160 } else if len (value ) > 0 && value [0 ] == UnescapeCharacter {
156161 escapedValue = value [1 :]
157162 } else if _ , err := strconv .Atoi (value ); err == nil {
158163 escapedValue = value
159164 } else if i := strings .Index (strings .ToLower (value ), " as " ); i > - 1 {
160- escapedValue = b .escape (table , value [:i ]) + " AS " + b .Quoter .ID (value [i + 4 :])
165+ escapedValue = b .escape (alias , value [:i ]) + " AS " + b .Quoter .ID (value [i + 4 :])
161166 } else if start , end := strings .IndexRune (value , '(' ), strings .IndexRune (value , ')' ); start >= 0 && end >= 0 && end > start {
162- escapedValue = value [:start + 1 ] + b .escape (table , value [start + 1 :end ]) + value [end :]
167+ escapedValue = value [:start + 1 ] + b .escape (alias , value [start + 1 :end ]) + value [end :]
163168 } else {
164169 parts := strings .Split (value , "." )
165170 for i , part := range parts {
@@ -171,7 +176,7 @@ func (b Buffer) escape(table, value string) string {
171176 }
172177 result := strings .Join (parts , "." )
173178 if len (parts ) == 1 && table != "" {
174- result = escaped_table + "." + result
179+ result = escapedTable + "." + result
175180 }
176181 escapedValue = result
177182 }
@@ -228,3 +233,13 @@ func (bf BufferFactory) Create() Buffer {
228233 BoolFalseValue : bf .BoolFalseValue ,
229234 }
230235}
236+
237+ // extract alias in the form of table as alias
238+ // if no alias, table will be returned as alias
239+ func extractAlias (input string ) (string , string ) {
240+ if i := strings .Index (strings .ToLower (input ), " as " ); i > - 1 {
241+ return input [:i ], input [i + 4 :]
242+ }
243+
244+ return input , input
245+ }
0 commit comments