Skip to content

Commit c0a41e3

Browse files
committed
[update] refactoring db drivers
1 parent 12aa0b9 commit c0a41e3

File tree

4 files changed

+333
-220
lines changed

4 files changed

+333
-220
lines changed

mysql.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package querysql
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type MySQL struct{}
8+
9+
func (m MySQL) Mark() string {
10+
return "?"
11+
}
12+
13+
func (m MySQL) IsJSON(name string) (string, bool) {
14+
return name, false
15+
}
16+
17+
func (m MySQL) Contains(v string, isJSON bool) string {
18+
return fmt.Sprintf("INSTR(%s, ?) > 0", v)
19+
}
20+
21+
func (m MySQL) NotContains(v string, isJSON bool) string {
22+
return fmt.Sprintf("INSTR(%s, ?) = 0", v)
23+
}
24+
25+
func (m MySQL) BeginsWith(v string, isJSON bool) string {
26+
search := "CONCAT(?, '%')"
27+
return fmt.Sprintf("%s LIKE %s", v, search)
28+
}
29+
30+
func (m MySQL) NotBeginsWith(v string, isJSON bool) string {
31+
search := "CONCAT(?, '%')"
32+
return fmt.Sprintf("%s NOT LIKE %s", v, search)
33+
}
34+
35+
func (m MySQL) EndsWith(v string, isJSON bool) string {
36+
search := "CONCAT('%', ?)"
37+
return fmt.Sprintf("%s LIKE %s", v, search)
38+
}
39+
40+
func (m MySQL) NotEndsWith(v string, isJSON bool) string {
41+
search := "CONCAT('%', ?)"
42+
return fmt.Sprintf("%s NOT LIKE %s", v, search)
43+
}

postgresql.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package querysql
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type PostgreSQL struct {
9+
counter int
10+
}
11+
12+
func (m *PostgreSQL) Reset() {
13+
m.counter = 1
14+
}
15+
16+
func (m *PostgreSQL) Mark() string {
17+
m.counter += 1
18+
t := fmt.Sprintf("$%d", m.counter)
19+
return t
20+
}
21+
22+
func (m *PostgreSQL) IsJSON(name string) (string, bool) {
23+
if !strings.HasPrefix(name, "json:") {
24+
return name, false
25+
}
26+
27+
//json:table.field.name:type
28+
meta := strings.Split(name, ":")
29+
parts := strings.SplitN(meta[1], ".", 3)
30+
31+
//apply type
32+
tp := "text"
33+
if len(meta) > 2 {
34+
tp = meta[2]
35+
}
36+
var s, e string
37+
if tp == "date" {
38+
s = "CAST("
39+
e = " AS DATE)"
40+
tp = "text"
41+
}
42+
43+
if len(parts) == 3 {
44+
name = fmt.Sprintf("%s(\"%s\".%s->'%s')::%s%s", s, parts[0], parts[1], parts[2], tp, e)
45+
} else if len(parts) == 2 {
46+
name = fmt.Sprintf("%s(%s->'%s')::%s%s", s, parts[0], parts[1], tp, e)
47+
}
48+
return name, true
49+
}
50+
51+
func (m *PostgreSQL) Contains(v string, isJSON bool) string {
52+
if isJSON {
53+
// Quotes (" ... ") are needed for correct work. Fields of type text in JSONB are wrapped by default
54+
return fmt.Sprintf("%s LIKE '\"%%' || %s || '%%\"'", v, m.Mark())
55+
}
56+
return fmt.Sprintf("%s LIKE '%%' || %s || '%%'", v, m.Mark())
57+
}
58+
59+
func (m *PostgreSQL) NotContains(v string, isJSON bool) string {
60+
if isJSON {
61+
return fmt.Sprintf("%s NOT LIKE '\"%%' || %s || '%%\"'", v, m.Mark())
62+
}
63+
return fmt.Sprintf("%s NOT LIKE '%%' || %s || '%%'", v, m.Mark())
64+
}
65+
66+
func (m *PostgreSQL) BeginsWith(v string, isJSON bool) string {
67+
var search string
68+
if isJSON {
69+
search = "'\"' || " + m.Mark() + " || '%'"
70+
} else {
71+
search = m.Mark() + " || '%'"
72+
}
73+
return fmt.Sprintf("%s LIKE %s", v, search)
74+
}
75+
76+
func (m *PostgreSQL) NotBeginsWith(v string, isJSON bool) string {
77+
var search string
78+
if isJSON {
79+
search = "'\"' || " + m.Mark() + " || '%'"
80+
} else {
81+
search = m.Mark() + " || '%'"
82+
}
83+
return fmt.Sprintf("%s NOT LIKE %s", v, search)
84+
}
85+
86+
func (m *PostgreSQL) EndsWith(v string, isJSON bool) string {
87+
var search string
88+
if isJSON {
89+
search = "'%' || " + m.Mark() + " || '\"'"
90+
} else {
91+
search = "'%' || " + m.Mark() + " "
92+
}
93+
return fmt.Sprintf("%s LIKE %s", v, search)
94+
}
95+
96+
func (m *PostgreSQL) NotEndsWith(v string, isJSON bool) string {
97+
var search string
98+
if isJSON {
99+
search = "'%' || " + m.Mark() + " || '\"'"
100+
} else {
101+
search = "'%' || " + m.Mark() + " "
102+
}
103+
return fmt.Sprintf("%s NOT LIKE %s", v, search)
104+
}

0 commit comments

Comments
 (0)