@@ -21,34 +21,21 @@ const (
2121 QuoteAddReserved
2222)
2323
24- // QuoteMode quote on which types
25- type QuoteMode int
26-
27- // All QuoteModes
28- const (
29- QuoteTableAndColumns QuoteMode = iota
30- QuoteTableOnly
31- QuoteColumnsOnly
32- )
33-
3424// Quoter represents an object has Quote method
3525type Quoter interface {
3626 Quotes () (byte , byte )
3727 QuotePolicy () QuotePolicy
38- QuoteMode () QuoteMode
3928 IsReserved (string ) bool
4029}
4130
4231type quoter struct {
4332 dialect core.Dialect
44- quoteMode QuoteMode
4533 quotePolicy QuotePolicy
4634}
4735
48- func newQuoter (dialect core.Dialect , quoteMode QuoteMode , quotePolicy QuotePolicy ) Quoter {
36+ func newQuoter (dialect core.Dialect , quotePolicy QuotePolicy ) Quoter {
4937 return & quoter {
5038 dialect : dialect ,
51- quoteMode : quoteMode ,
5239 quotePolicy : quotePolicy ,
5340 }
5441}
@@ -62,10 +49,6 @@ func (q *quoter) QuotePolicy() QuotePolicy {
6249 return q .quotePolicy
6350}
6451
65- func (q * quoter ) QuoteMode () QuoteMode {
66- return q .quoteMode
67- }
68-
6952func (q * quoter ) IsReserved (value string ) bool {
7053 return q .dialect .IsReserved (value )
7154}
@@ -77,21 +60,24 @@ func quoteColumns(quoter Quoter, columnStr string) string {
7760
7861func quoteJoin (quoter Quoter , columns []string ) string {
7962 for i := 0 ; i < len (columns ); i ++ {
80- columns [i ] = quote (quoter , columns [i ], true )
63+ columns [i ] = quote (quoter , columns [i ])
8164 }
8265 return strings .Join (columns , "," )
8366}
8467
8568// quote Use QuoteStr quote the string sql
86- func quote (quoter Quoter , value string , isColumn bool ) string {
69+ func quote (quoter Quoter , value string ) string {
8770 buf := strings.Builder {}
88- quoteTo (quoter , & buf , value , isColumn )
71+ quoteTo (quoter , & buf , value )
8972 return buf .String ()
9073}
9174
9275// Quote add quotes to the value
9376func (engine * Engine ) quote (value string , isColumn bool ) string {
94- return quote (engine , value , isColumn )
77+ if isColumn {
78+ return quote (engine .colQuoter , value )
79+ }
80+ return quote (engine .tableQuoter , value )
9581}
9682
9783// Quote add quotes to the value
@@ -105,53 +91,25 @@ func (engine *Engine) Quotes() (byte, byte) {
10591 return quotes [0 ], quotes [1 ]
10692}
10793
108- // QuoteMode returns quote mode
109- func (engine * Engine ) QuoteMode () QuoteMode {
110- return engine .quoteMode
111- }
112-
113- // QuotePolicy returns quote policy
114- func (engine * Engine ) QuotePolicy () QuotePolicy {
115- return engine .quotePolicy
116- }
117-
11894// IsReserved return true if the value is a reserved word of the database
11995func (engine * Engine ) IsReserved (value string ) bool {
12096 return engine .dialect .IsReserved (value )
12197}
12298
12399// quoteTo quotes string and writes into the buffer
124- func quoteTo (quoter Quoter , buf * strings.Builder , value string , isColumn bool ) {
125- if isColumn {
126- if quoter .QuoteMode () == QuoteTableAndColumns ||
127- quoter .QuoteMode () == QuoteColumnsOnly {
128- if quoter .QuotePolicy () == QuoteAddAlways {
129- realQuoteTo (quoter , buf , value )
130- return
131- } else if quoter .QuotePolicy () == QuoteAddReserved && quoter .IsReserved (value ) {
132- realQuoteTo (quoter , buf , value )
133- return
134- }
135- }
136- buf .WriteString (value )
100+ func quoteTo (quoter Quoter , buf * strings.Builder , value string ) {
101+ left , right := quoter .Quotes ()
102+ if quoter .QuotePolicy () == QuoteAddAlways {
103+ realQuoteTo (left , right , buf , value )
104+ return
105+ } else if quoter .QuotePolicy () == QuoteAddReserved && quoter .IsReserved (value ) {
106+ realQuoteTo (left , right , buf , value )
137107 return
138- }
139-
140- if quoter .QuoteMode () == QuoteTableAndColumns ||
141- quoter .QuoteMode () == QuoteTableOnly {
142- if quoter .QuotePolicy () == QuoteAddAlways {
143- realQuoteTo (quoter , buf , value )
144- return
145- } else if quoter .QuotePolicy () == QuoteAddReserved && quoter .IsReserved (value ) {
146- realQuoteTo (quoter , buf , value )
147- return
148- }
149108 }
150109 buf .WriteString (value )
151- return
152110}
153111
154- func realQuoteTo (quoter Quoter , buf * strings.Builder , value string ) {
112+ func realQuoteTo (quoteLeft , quoteRight byte , buf * strings.Builder , value string ) {
155113 if buf == nil {
156114 return
157115 }
@@ -164,8 +122,6 @@ func realQuoteTo(quoter Quoter, buf *strings.Builder, value string) {
164122 return
165123 }
166124
167- quoteLeft , quoteRight := quoter .Quotes ()
168-
169125 if value [0 ] == '`' || value [0 ] == quoteLeft { // no quote
170126 _ , _ = buf .WriteString (value )
171127 return
0 commit comments