Skip to content

Commit ee79940

Browse files
dialect/sql: added checks for clauses which are not supported by YQL
1 parent a51ada8 commit ee79940

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

dialect/sql/builder.go

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ func (i *InsertBuilder) Values(values ...any) *InsertBuilder {
229229

230230
// Default sets the default values clause based on the dialect type.
231231
func (i *InsertBuilder) Default() *InsertBuilder {
232-
i.defaults = true
232+
if i.ydb() {
233+
i.AddError(fmt.Errorf("DEFAULT is not supported by %q", i.dialect))
234+
} else {
235+
i.defaults = true
236+
}
233237
return i
234238
}
235239

@@ -404,6 +408,10 @@ func ResolveWith(fn func(*UpdateSet)) ConflictOption {
404408
// sql.ResolveWithNewValues()
405409
// )
406410
func (i *InsertBuilder) OnConflict(opts ...ConflictOption) *InsertBuilder {
411+
if i.ydb() {
412+
i.AddError(fmt.Errorf("ON CONFLICT is not supported by %q", i.dialect))
413+
return i
414+
}
407415
if i.conflict == nil {
408416
i.conflict = &conflict{}
409417
}
@@ -486,13 +494,13 @@ func (i *InsertBuilder) QueryErr() (string, []any, error) {
486494
switch {
487495
case i.isUpsert:
488496
if !b.ydb() {
489-
b.AddError(fmt.Errorf("UPSERT INTO: unsupported dialect: %q", b.dialect))
497+
b.AddError(fmt.Errorf("UPSERT INTO is not supported by %q", b.dialect))
490498
return "", nil, b.Err()
491499
}
492500
b.WriteString("UPSERT INTO ")
493501
case i.isReplace:
494502
if !b.ydb() {
495-
b.AddError(fmt.Errorf("REPLACE INTO: unsupported dialect: %q", b.dialect))
503+
b.AddError(fmt.Errorf("REPLACE INTO is not supported by %q", b.dialect))
496504
return "", nil, b.Err()
497505
}
498506
b.WriteString("REPLACE INTO ")
@@ -676,8 +684,8 @@ func (u *UpdateBuilder) Empty() bool {
676684
// OrderBy appends the `ORDER BY` clause to the `UPDATE` statement.
677685
// Supported by SQLite and MySQL.
678686
func (u *UpdateBuilder) OrderBy(columns ...string) *UpdateBuilder {
679-
if u.postgres() {
680-
u.AddError(errors.New("ORDER BY is not supported by PostgreSQL"))
687+
if u.postgres() || u.ydb() {
688+
u.AddError(fmt.Errorf("ORDER BY is not supported by %q", u.dialect))
681689
return u
682690
}
683691
for i := range columns {
@@ -689,8 +697,8 @@ func (u *UpdateBuilder) OrderBy(columns ...string) *UpdateBuilder {
689697
// Limit appends the `LIMIT` clause to the `UPDATE` statement.
690698
// Supported by SQLite and MySQL.
691699
func (u *UpdateBuilder) Limit(limit int) *UpdateBuilder {
692-
if u.postgres() {
693-
u.AddError(errors.New("LIMIT is not supported by PostgreSQL"))
700+
if u.postgres() || u.ydb() {
701+
u.AddError(fmt.Errorf("LIMIT is not supported by %q", u.dialect))
694702
return u
695703
}
696704
u.limit = &limit
@@ -724,7 +732,7 @@ func (u *UpdateBuilder) On(s *Selector) *UpdateBuilder {
724732
if u.ydb() {
725733
u.onSelect = s
726734
} else {
727-
u.AddError(fmt.Errorf("UPDATE ON: unsupported dialect: %q", u.dialect))
735+
u.AddError(fmt.Errorf("UPDATE ON is not supported by %q", u.dialect))
728736
}
729737
return u
730738
}
@@ -745,7 +753,7 @@ func (u *UpdateBuilder) QueryErr() (string, []any, error) {
745753
// BATCH UPDATE (YDB-specific)
746754
if u.isBatch {
747755
if !b.ydb() {
748-
b.AddError(fmt.Errorf("BATCH UPDATE: unsupported dialect: %q", b.dialect))
756+
b.AddError(fmt.Errorf("BATCH UPDATE is not supported by %q", b.dialect))
749757
return "", nil, b.Err()
750758
}
751759
if len(u.returning) > 0 {
@@ -847,7 +855,8 @@ func Delete(table string) *DeleteBuilder { return &DeleteBuilder{table: table} }
847855
// Note: BATCH DELETE is only supported in YDB dialect.
848856
//
849857
// BatchDelete("/local/my_table")
850-
// .Where(GT("Key1", 1))
858+
//
859+
// .Where(GT("Key1", 1))
851860
func BatchDelete(table string) *DeleteBuilder {
852861
return &DeleteBuilder{table: table, isBatch: true}
853862
}
@@ -886,7 +895,7 @@ func (d *DeleteBuilder) On(s *Selector) *DeleteBuilder {
886895
if d.ydb() {
887896
d.onSelect = s
888897
} else {
889-
d.AddError(fmt.Errorf("DELETE ON: unsupported dialect: %q", d.dialect))
898+
d.AddError(fmt.Errorf("DELETE ON is not supported by %q", d.dialect))
890899
}
891900
return d
892901
}
@@ -897,7 +906,7 @@ func (d *DeleteBuilder) Returning(columns ...string) *DeleteBuilder {
897906
if d.ydb() {
898907
d.returning = columns
899908
} else {
900-
d.AddError(fmt.Errorf("DELETE RETURNING: unsupported dialect: %q", d.dialect))
909+
d.AddError(fmt.Errorf("DELETE RETURNING is not supported by %q", d.dialect))
901910
}
902911
return d
903912
}
@@ -914,7 +923,7 @@ func (d *DeleteBuilder) QueryErr() (string, []any, error) {
914923
// BATCH DELETE (YDB-specific)
915924
if d.isBatch {
916925
if !b.ydb() {
917-
b.AddError(fmt.Errorf("BATCH DELETE: unsupported dialect: %q", b.dialect))
926+
b.AddError(fmt.Errorf("BATCH DELETE is not supported by %q", b.dialect))
918927
return "", nil, b.Err()
919928
}
920929
if len(d.returning) > 0 {
@@ -1323,6 +1332,8 @@ func (p *Predicate) NotIn(col string, args ...any) *Predicate {
13231332
}
13241333

13251334
// Exists returns the `Exists` predicate.
1335+
//
1336+
// Note: Correlated subqueries with EXISTS are not supported by YDB
13261337
func Exists(query Querier) *Predicate {
13271338
return P().Exists(query)
13281339
}
@@ -1338,6 +1349,8 @@ func (p *Predicate) Exists(query Querier) *Predicate {
13381349
}
13391350

13401351
// NotExists returns the `NotExists` predicate.
1352+
//
1353+
// Note: Correlated subqueries with NOT EXISTS are not supported by YDB
13411354
func NotExists(query Querier) *Predicate {
13421355
return P().NotExists(query)
13431356
}
@@ -1411,7 +1424,7 @@ func (p *Predicate) escapedLikeFold(col, left, substr, right string) *Predicate
14111424
// because this how it is defined in dialect/sql/schema.
14121425
b.Ident(col).WriteString(" COLLATE utf8mb4_general_ci LIKE ")
14131426
b.Arg(left + strings.ToLower(w) + right)
1414-
case dialect.Postgres:
1427+
case dialect.Postgres, dialect.YDB:
14151428
b.Ident(col).WriteString(" ILIKE ")
14161429
b.Arg(left + strings.ToLower(w) + right)
14171430
default: // SQLite.
@@ -1468,7 +1481,7 @@ func (p *Predicate) ColumnsHasPrefix(col, prefixC string) *Predicate {
14681481
p.WriteString(" ESCAPE ").Arg("\\")
14691482
}
14701483
default:
1471-
b.AddError(fmt.Errorf("ColumnsHasPrefix: unsupported dialect: %q", p.dialect))
1484+
b.AddError(fmt.Errorf("ColumnsHasPrefix is not supported by %q", p.dialect))
14721485
}
14731486
})
14741487
}
@@ -1815,7 +1828,7 @@ func (s *SelectTable) View(index string) *SelectTable {
18151828
if s.ydb() {
18161829
s.index = index
18171830
} else {
1818-
s.AddError(fmt.Errorf("VIEW: unsupported dialect: %q", s.dialect))
1831+
s.AddError(fmt.Errorf("VIEW is not supported by %q", s.dialect))
18191832
}
18201833
return s
18211834
}
@@ -2111,9 +2124,13 @@ func (s *Selector) From(t TableView) *Selector {
21112124

21122125
// AppendFrom appends a new TableView to the `FROM` clause.
21132126
func (s *Selector) AppendFrom(t TableView) *Selector {
2114-
s.from = append(s.from, t)
2115-
if st, ok := t.(state); ok {
2116-
st.SetDialect(s.dialect)
2127+
if s.ydb() && len(s.from) != 0 {
2128+
s.AddError(fmt.Errorf("multiple tables in FROM clause is not supported by %q", s.dialect))
2129+
} else {
2130+
s.from = append(s.from, t)
2131+
if st, ok := t.(state); ok {
2132+
st.SetDialect(s.dialect)
2133+
}
21172134
}
21182135
return s
21192136
}
@@ -2431,17 +2448,22 @@ func (s *Selector) UnionDistinct(t TableView) *Selector {
24312448

24322449
// Except appends the EXCEPT clause to the query.
24332450
func (s *Selector) Except(t TableView) *Selector {
2434-
s.setOps = append(s.setOps, setOp{
2435-
Type: setOpTypeExcept,
2436-
TableView: t,
2437-
})
2451+
if s.ydb() {
2452+
s.AddError(fmt.Errorf("EXCEPT is not supported by %q", s.dialect))
2453+
return s
2454+
} else {
2455+
s.setOps = append(s.setOps, setOp{
2456+
Type: setOpTypeExcept,
2457+
TableView: t,
2458+
})
2459+
}
24382460
return s
24392461
}
24402462

24412463
// ExceptAll appends the EXCEPT ALL clause to the query.
24422464
func (s *Selector) ExceptAll(t TableView) *Selector {
2443-
if s.sqlite() {
2444-
s.AddError(errors.New("EXCEPT ALL is not supported by SQLite"))
2465+
if s.sqlite() || s.ydb() {
2466+
s.AddError(fmt.Errorf("EXCEPT ALL is not supported by %q", s.dialect))
24452467
} else {
24462468
s.setOps = append(s.setOps, setOp{
24472469
Type: setOpTypeExcept,
@@ -2454,17 +2476,21 @@ func (s *Selector) ExceptAll(t TableView) *Selector {
24542476

24552477
// Intersect appends the INTERSECT clause to the query.
24562478
func (s *Selector) Intersect(t TableView) *Selector {
2457-
s.setOps = append(s.setOps, setOp{
2458-
Type: setOpTypeIntersect,
2459-
TableView: t,
2460-
})
2479+
if s.ydb() {
2480+
s.AddError(fmt.Errorf("INTERSECT is not supported by %q", s.dialect))
2481+
} else {
2482+
s.setOps = append(s.setOps, setOp{
2483+
Type: setOpTypeIntersect,
2484+
TableView: t,
2485+
})
2486+
}
24612487
return s
24622488
}
24632489

24642490
// IntersectAll appends the INTERSECT ALL clause to the query.
24652491
func (s *Selector) IntersectAll(t TableView) *Selector {
2466-
if s.sqlite() {
2467-
s.AddError(errors.New("INTERSECT ALL is not supported by SQLite"))
2492+
if s.sqlite() || s.ydb() {
2493+
s.AddError(fmt.Errorf("INTERSECT ALL is not supported by %q", s.dialect))
24682494
} else {
24692495
s.setOps = append(s.setOps, setOp{
24702496
Type: setOpTypeIntersect,
@@ -2616,8 +2642,8 @@ func WithLockClause(clause string) LockOption {
26162642
// For sets the lock configuration for suffixing the `SELECT`
26172643
// statement with the `FOR [SHARE | UPDATE] ...` clause.
26182644
func (s *Selector) For(l LockStrength, opts ...LockOption) *Selector {
2619-
if s.Dialect() == dialect.SQLite {
2620-
s.AddError(errors.New("sql: SELECT .. FOR UPDATE/SHARE not supported in SQLite"))
2645+
if s.sqlite() || s.ydb() {
2646+
s.AddError(fmt.Errorf("SELECT .. FOR UPDATE/SHARE is not supported by %q", s.dialect))
26212647
}
26222648
s.lock = &LockOptions{Strength: l}
26232649
for _, opt := range opts {
@@ -2754,7 +2780,7 @@ func (s *Selector) AssumeOrderBy(columns ...string) *Selector {
27542780

27552781
s.assumeOrder = append(s.assumeOrder, columns...)
27562782
} else {
2757-
s.AddError(fmt.Errorf("ASSUME ORDER BY: unsupported dialect: %q", s.dialect))
2783+
s.AddError(fmt.Errorf("ASSUME ORDER BY is not supported by %q", s.dialect))
27582784
}
27592785
return s
27602786
}

0 commit comments

Comments
 (0)