| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqlparser |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
|
| 22 |
"github.com/xwb1989/sqlparser/dependency/sqltypes" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type Encodable interface { |
| 30 |
EncodeSQL(buf *bytes.Buffer) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type InsertValues [][]sqltypes.Value |
| 36 |
|
| 37 |
|
| 38 |
func (iv InsertValues) EncodeSQL(buf *bytes.Buffer) { |
| 39 |
for i, rows := range iv { |
| 40 |
if i != 0 { |
| 41 |
buf.WriteString(", ") |
| 42 |
} |
| 43 |
buf.WriteByte('(') |
| 44 |
for j, bv := range rows { |
| 45 |
if j != 0 { |
| 46 |
buf.WriteString(", ") |
| 47 |
} |
| 48 |
bv.EncodeSQL(buf) |
| 49 |
} |
| 50 |
buf.WriteByte(')') |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
type TupleEqualityList struct { |
| 57 |
Columns []ColIdent |
| 58 |
Rows [][]sqltypes.Value |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func (tpl *TupleEqualityList) EncodeSQL(buf *bytes.Buffer) { |
| 64 |
if len(tpl.Columns) == 1 { |
| 65 |
tpl.encodeAsIn(buf) |
| 66 |
return |
| 67 |
} |
| 68 |
tpl.encodeAsEquality(buf) |
| 69 |
} |
| 70 |
|
| 71 |
func (tpl *TupleEqualityList) encodeAsIn(buf *bytes.Buffer) { |
| 72 |
Append(buf, tpl.Columns[0]) |
| 73 |
buf.WriteString(" in (") |
| 74 |
for i, r := range tpl.Rows { |
| 75 |
if i != 0 { |
| 76 |
buf.WriteString(", ") |
| 77 |
} |
| 78 |
r[0].EncodeSQL(buf) |
| 79 |
} |
| 80 |
buf.WriteByte(')') |
| 81 |
} |
| 82 |
|
| 83 |
func (tpl *TupleEqualityList) encodeAsEquality(buf *bytes.Buffer) { |
| 84 |
for i, r := range tpl.Rows { |
| 85 |
if i != 0 { |
| 86 |
buf.WriteString(" or ") |
| 87 |
} |
| 88 |
buf.WriteString("(") |
| 89 |
for j, c := range tpl.Columns { |
| 90 |
if j != 0 { |
| 91 |
buf.WriteString(" and ") |
| 92 |
} |
| 93 |
Append(buf, c) |
| 94 |
buf.WriteString(" = ") |
| 95 |
r[j].EncodeSQL(buf) |
| 96 |
} |
| 97 |
buf.WriteByte(')') |
| 98 |
} |
| 99 |
} |