88 "fmt"
99 "strings"
1010
11+ "xorm.io/builder"
1112 "xorm.io/core"
1213)
1314
@@ -26,6 +27,7 @@ type Quoter interface {
2627 Quotes () (byte , byte )
2728 QuotePolicy () QuotePolicy
2829 IsReserved (string ) bool
30+ WriteTo (w * builder.BytesWriter , value string ) error
2931}
3032
3133type quoter struct {
@@ -53,6 +55,29 @@ func (q *quoter) IsReserved(value string) bool {
5355 return q .dialect .IsReserved (value )
5456}
5557
58+ func (q * quoter ) needQuote (value string ) bool {
59+ return q .quotePolicy == QuoteAddAlways || (q .quotePolicy == QuoteAddReserved && q .IsReserved (value ))
60+ }
61+
62+ func (q * quoter ) WriteTo (w * builder.BytesWriter , name string ) error {
63+ leftQuote , rightQuote := q .Quotes ()
64+ needQuote := q .needQuote (name )
65+ if needQuote && name [0 ] != '`' {
66+ if err := w .WriteByte (leftQuote ); err != nil {
67+ return err
68+ }
69+ }
70+ if _ , err := w .WriteString (name ); err != nil {
71+ return err
72+ }
73+ if needQuote && name [len (name )- 1 ] != '`' {
74+ if err := w .WriteByte (rightQuote ); err != nil {
75+ return err
76+ }
77+ }
78+ return nil
79+ }
80+
5681func quoteColumns (quoter Quoter , columnStr string ) string {
5782 columns := strings .Split (columnStr , "," )
5883 return quoteJoin (quoter , columns )
@@ -96,13 +121,21 @@ func (engine *Engine) IsReserved(value string) bool {
96121 return engine .dialect .IsReserved (value )
97122}
98123
124+ // SetTableQuotePolicy set table quote policy
125+ func (engine * Engine ) SetTableQuotePolicy (policy QuotePolicy ) {
126+ engine .tableQuoter = newQuoter (engine .dialect , policy )
127+ }
128+
129+ // SetColumnQuotePolicy set column quote policy
130+ func (engine * Engine ) SetColumnQuotePolicy (policy QuotePolicy ) {
131+ engine .colQuoter = newQuoter (engine .dialect , policy )
132+ }
133+
99134// quoteTo quotes string and writes into the buffer
100135func quoteTo (quoter Quoter , buf * strings.Builder , value string ) {
101136 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 ) {
137+ if (quoter .QuotePolicy () == QuoteAddAlways ) ||
138+ (quoter .QuotePolicy () == QuoteAddReserved && quoter .IsReserved (value )) {
106139 realQuoteTo (left , right , buf , value )
107140 return
108141 }
0 commit comments