Skip to content

Commit b10f282

Browse files
committed
๐Ÿน ๐Ÿฅ‚ ๐ŸŒฎ (cache_plan) condition/update target ใซ placeholder ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’ๅ…ฅใ‚ŒใŸ
1 parent 9d7c759 commit b10f282

File tree

3 files changed

+74
-29
lines changed

3 files changed

+74
-29
lines changed

โ€ŽREADME.mdโ€Ž

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ type Format = {
4949

5050
type Query = SelectQuery | UpdateQuery | DeleteQuery | InsertQuery
5151

52+
type Placeholder = {
53+
index: number;
54+
extra?: boolean
55+
}
56+
5257
type Condition = {
5358
column: string
54-
value?: number | string
5559
operator: 'eq' | 'in'
60+
placeholder: Placeholder
5661
}
5762

5863
type SelectQuery = CachableSelectQuery | NonCachableSelectQuery
@@ -63,8 +68,6 @@ type CachableSelectQuery = {
6368
cache: true
6469
table: string
6570
targets: string[]
66-
// ?ใฎไฝ็ฝฎใจไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใช้ †็•ช
67-
// ๅ›บๅฎšๅ€คใงWHEREใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏๆœ€ๅพŒใซ
6871
conditions: Condition[]
6972
orders: {
7073
column: string

โ€Ždomains/cache_plan.goโ€Ž

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ const (
2626
CachePlanQueryType_INSERT CachePlanQueryType = "insert"
2727
)
2828

29+
type CachePlanPlaceholder struct {
30+
Index int `yaml:"index"`
31+
Extra bool `yaml:"extra,omitempty"`
32+
}
33+
2934
type CachePlanCondition struct {
30-
Column string `yaml:"column"`
31-
Value interface{} `yaml:"value,omitempty"`
32-
Operator CachePlanOperatorEnum `yaml:"operator,omitempty"`
35+
Column string `yaml:"column"`
36+
Operator CachePlanOperatorEnum `yaml:"operator,omitempty"`
37+
Placeholder CachePlanPlaceholder `yaml:"placeholder"`
3338
}
3439

3540
type CachePlanOperatorEnum string
@@ -59,10 +64,15 @@ type CachePlanSelectQuery struct {
5964
Orders []CachePlanOrder `yaml:"orders,omitempty"`
6065
}
6166

67+
type CachePlanUpdateTarget struct {
68+
Column string `yaml:"column"`
69+
Placeholder CachePlanPlaceholder `yaml:"placeholder"`
70+
}
71+
6272
type CachePlanUpdateQuery struct {
63-
Table string `yaml:"table"`
64-
Targets []string `yaml:"targets"`
65-
Conditions []CachePlanCondition `yaml:"conditions,omitempty"`
73+
Table string `yaml:"table"`
74+
Targets []CachePlanUpdateTarget `yaml:"targets"`
75+
Conditions []CachePlanCondition `yaml:"conditions,omitempty"`
6676
}
6777

6878
type CachePlanDeleteQuery struct {

โ€Ždomains/cache_plan_repository_test.goโ€Ž

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var formatted = `queries:
2222
conditions:
2323
- column: livestream_id
2424
operator: eq
25+
placeholder:
26+
index: 0
2527
orders:
2628
- column: created_at
2729
order: desc
@@ -37,31 +39,45 @@ var formatted = `queries:
3739
conditions:
3840
- column: livestream_id
3941
operator: eq
42+
placeholder:
43+
index: 0
4044
- query: DELETE FROM livecomments WHERE id = ? AND livestream_id = ? AND (SELECT COUNT(*) FROM (SELECT ? AS text) AS texts INNER JOIN (SELECT CONCAT('%', ?, '%') AS pattern) AS patterns ON texts.text LIKE patterns.pattern) >= 1;
4145
type: delete
4246
table: livecomments
4347
conditions:
4448
- column: id
4549
operator: eq
50+
placeholder:
51+
index: 0
4652
- column: livestream_id
4753
operator: eq
54+
placeholder:
55+
index: 1
4856
- query: DELETE FROM livestream_viewers_history WHERE user_id = ? AND livestream_id = ?
4957
type: delete
5058
table: livestream_viewers_history
5159
conditions:
5260
- column: user_id
5361
operator: eq
62+
placeholder:
63+
index: 0
5464
- column: livestream_id
5565
operator: eq
56-
- query: UPDATE settings SET value = ? WHERE name = 'payment_gateway_url'
66+
placeholder:
67+
index: 1
68+
- query: UPDATE settings SET value = ? WHERE name = ?
5769
type: update
5870
table: settings
5971
targets:
60-
- value
72+
- column: value
73+
placeholder:
74+
index: 0
6175
conditions:
6276
- column: name
63-
value: payment_gateway_url
6477
operator: eq
78+
placeholder:
79+
index: 0
80+
extra: true
6581
`
6682

6783
var parsed = &CachePlan{
@@ -72,11 +88,15 @@ var parsed = &CachePlan{
7288
Query: "SELECT * FROM livecomments WHERE livestream_id = ? ORDER BY created_at DESC",
7389
},
7490
Select: &CachePlanSelectQuery{
75-
Table: "livecomments",
76-
Cache: true,
77-
Targets: []string{"id", "user_id", "livestream_id", "comment", "tip", "created_at"},
78-
Conditions: []CachePlanCondition{{Column: "livestream_id", Operator: CachePlanOperator_EQ}},
79-
Orders: []CachePlanOrder{{Column: "created_at", Order: "desc"}},
91+
Table: "livecomments",
92+
Cache: true,
93+
Targets: []string{"id", "user_id", "livestream_id", "comment", "tip", "created_at"},
94+
Conditions: []CachePlanCondition{
95+
{Column: "livestream_id", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 0}},
96+
},
97+
Orders: []CachePlanOrder{
98+
{Column: "created_at", Order: "desc"},
99+
},
80100
},
81101
},
82102
{
@@ -94,10 +114,12 @@ var parsed = &CachePlan{
94114
Type: CachePlanQueryType_SELECT,
95115
},
96116
Select: &CachePlanSelectQuery{
97-
Table: "livestream_viewers_history",
98-
Cache: true,
99-
Targets: []string{"COUNT()"},
100-
Conditions: []CachePlanCondition{{Column: "livestream_id", Operator: CachePlanOperator_EQ}},
117+
Table: "livestream_viewers_history",
118+
Cache: true,
119+
Targets: []string{"COUNT()"},
120+
Conditions: []CachePlanCondition{
121+
{Column: "livestream_id", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 0}},
122+
},
101123
},
102124
},
103125
{
@@ -106,8 +128,11 @@ var parsed = &CachePlan{
106128
Type: CachePlanQueryType_DELETE,
107129
},
108130
Delete: &CachePlanDeleteQuery{
109-
Table: "livecomments",
110-
Conditions: []CachePlanCondition{{Column: "id", Operator: CachePlanOperator_EQ}, {Column: "livestream_id", Operator: CachePlanOperator_EQ}},
131+
Table: "livecomments",
132+
Conditions: []CachePlanCondition{
133+
{Column: "id", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 0}},
134+
{Column: "livestream_id", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 1}},
135+
},
111136
},
112137
},
113138
{
@@ -116,19 +141,26 @@ var parsed = &CachePlan{
116141
Type: CachePlanQueryType_DELETE,
117142
},
118143
Delete: &CachePlanDeleteQuery{
119-
Table: "livestream_viewers_history",
120-
Conditions: []CachePlanCondition{{Column: "user_id", Operator: CachePlanOperator_EQ}, {Column: "livestream_id", Operator: CachePlanOperator_EQ}},
144+
Table: "livestream_viewers_history",
145+
Conditions: []CachePlanCondition{
146+
{Column: "user_id", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 0}},
147+
{Column: "livestream_id", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 1}},
148+
},
121149
},
122150
},
123151
{
124152
CachePlanQueryBase: &CachePlanQueryBase{
125-
Query: "UPDATE settings SET value = ? WHERE name = 'payment_gateway_url'",
153+
Query: "UPDATE settings SET value = ? WHERE name = ?",
126154
Type: CachePlanQueryType_UPDATE,
127155
},
128156
Update: &CachePlanUpdateQuery{
129-
Table: "settings",
130-
Targets: []string{"value"},
131-
Conditions: []CachePlanCondition{{Column: "name", Value: "payment_gateway_url", Operator: CachePlanOperator_EQ}},
157+
Table: "settings",
158+
Targets: []CachePlanUpdateTarget{
159+
{Column: "value", Placeholder: CachePlanPlaceholder{Index: 0}},
160+
},
161+
Conditions: []CachePlanCondition{
162+
{Column: "name", Operator: CachePlanOperator_EQ, Placeholder: CachePlanPlaceholder{Index: 0, Extra: true}},
163+
},
132164
},
133165
},
134166
},

0 commit comments

Comments
ย (0)