|
| 1 | +// Copyright 2019 The Xorm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package xorm |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "xorm.io/core" |
| 12 | +) |
| 13 | + |
| 14 | +// QuotePolicy describes quote handle policy |
| 15 | +type QuotePolicy int |
| 16 | + |
| 17 | +// All QuotePolicies |
| 18 | +const ( |
| 19 | + QuoteAddAlways QuotePolicy = iota |
| 20 | + QuoteNoAdd |
| 21 | + QuoteAddReserved |
| 22 | +) |
| 23 | + |
| 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 | + |
| 34 | +// Quoter represents an object has Quote method |
| 35 | +type Quoter interface { |
| 36 | + Quotes() (byte, byte) |
| 37 | + QuotePolicy() QuotePolicy |
| 38 | + QuoteMode() QuoteMode |
| 39 | + IsReserved(string) bool |
| 40 | +} |
| 41 | + |
| 42 | +type quoter struct { |
| 43 | + dialect core.Dialect |
| 44 | + quoteMode QuoteMode |
| 45 | + quotePolicy QuotePolicy |
| 46 | +} |
| 47 | + |
| 48 | +func newQuoter(dialect core.Dialect, quoteMode QuoteMode, quotePolicy QuotePolicy) Quoter { |
| 49 | + return "er{ |
| 50 | + dialect: dialect, |
| 51 | + quoteMode: quoteMode, |
| 52 | + quotePolicy: quotePolicy, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (q *quoter) Quotes() (byte, byte) { |
| 57 | + quotes := q.dialect.Quote("") |
| 58 | + return quotes[0], quotes[1] |
| 59 | +} |
| 60 | + |
| 61 | +func (q *quoter) QuotePolicy() QuotePolicy { |
| 62 | + return q.quotePolicy |
| 63 | +} |
| 64 | + |
| 65 | +func (q *quoter) QuoteMode() QuoteMode { |
| 66 | + return q.quoteMode |
| 67 | +} |
| 68 | + |
| 69 | +func (q *quoter) IsReserved(value string) bool { |
| 70 | + return q.dialect.IsReserved(value) |
| 71 | +} |
| 72 | + |
| 73 | +func quoteColumns(quoter Quoter, columnStr string) string { |
| 74 | + columns := strings.Split(columnStr, ",") |
| 75 | + return quoteJoin(quoter, columns) |
| 76 | +} |
| 77 | + |
| 78 | +func quoteJoin(quoter Quoter, columns []string) string { |
| 79 | + for i := 0; i < len(columns); i++ { |
| 80 | + columns[i] = quote(quoter, columns[i], true) |
| 81 | + } |
| 82 | + return strings.Join(columns, ",") |
| 83 | +} |
| 84 | + |
| 85 | +// quote Use QuoteStr quote the string sql |
| 86 | +func quote(quoter Quoter, value string, isColumn bool) string { |
| 87 | + buf := strings.Builder{} |
| 88 | + quoteTo(quoter, &buf, value, isColumn) |
| 89 | + return buf.String() |
| 90 | +} |
| 91 | + |
| 92 | +// Quote add quotes to the value |
| 93 | +func (engine *Engine) quote(value string, isColumn bool) string { |
| 94 | + return quote(engine, value, isColumn) |
| 95 | +} |
| 96 | + |
| 97 | +// Quote add quotes to the value |
| 98 | +func (engine *Engine) Quote(value string, isColumn bool) string { |
| 99 | + return engine.quote(value, isColumn) |
| 100 | +} |
| 101 | + |
| 102 | +// Quotes return the left quote and right quote |
| 103 | +func (engine *Engine) Quotes() (byte, byte) { |
| 104 | + quotes := engine.dialect.Quote("") |
| 105 | + return quotes[0], quotes[1] |
| 106 | +} |
| 107 | + |
| 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 | + |
| 118 | +// IsReserved return true if the value is a reserved word of the database |
| 119 | +func (engine *Engine) IsReserved(value string) bool { |
| 120 | + return engine.dialect.IsReserved(value) |
| 121 | +} |
| 122 | + |
| 123 | +// 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) |
| 137 | + 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 | + } |
| 149 | + } |
| 150 | + buf.WriteString(value) |
| 151 | + return |
| 152 | +} |
| 153 | + |
| 154 | +func realQuoteTo(quoter Quoter, buf *strings.Builder, value string) { |
| 155 | + if buf == nil { |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + value = strings.TrimSpace(value) |
| 160 | + if value == "" { |
| 161 | + return |
| 162 | + } else if value == "*" { |
| 163 | + buf.WriteString("*") |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + quoteLeft, quoteRight := quoter.Quotes() |
| 168 | + |
| 169 | + if value[0] == '`' || value[0] == quoteLeft { // no quote |
| 170 | + _, _ = buf.WriteString(value) |
| 171 | + return |
| 172 | + } else { |
| 173 | + _ = buf.WriteByte(quoteLeft) |
| 174 | + for i := 0; i < len(value); i++ { |
| 175 | + if value[i] == '.' { |
| 176 | + _ = buf.WriteByte(quoteRight) |
| 177 | + _ = buf.WriteByte('.') |
| 178 | + _ = buf.WriteByte(quoteLeft) |
| 179 | + } else { |
| 180 | + _ = buf.WriteByte(value[i]) |
| 181 | + } |
| 182 | + } |
| 183 | + _ = buf.WriteByte(quoteRight) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func unQuote(quoter Quoter, value string) string { |
| 188 | + left, right := quoter.Quotes() |
| 189 | + return strings.Trim(value, fmt.Sprintf("%v%v`", left, right)) |
| 190 | +} |
| 191 | + |
| 192 | +func quoteJoinFunc(cols []string, quoteFunc func(string) string, sep string) string { |
| 193 | + for i := range cols { |
| 194 | + cols[i] = quoteFunc(cols[i]) |
| 195 | + } |
| 196 | + return strings.Join(cols, sep+" ") |
| 197 | +} |
0 commit comments