Skip to content

Commit d077dab

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

File tree

1 file changed

+60
-35
lines changed

1 file changed

+60
-35
lines changed

dialect/sql/builder.go

Lines changed: 60 additions & 35 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 {
@@ -846,8 +854,8 @@ func Delete(table string) *DeleteBuilder { return &DeleteBuilder{table: table} }
846854
//
847855
// Note: BATCH DELETE is only supported in YDB dialect.
848856
//
849-
// BatchDelete("/local/my_table")
850-
// .Where(GT("Key1", 1))
857+
// BatchDelete("/local/my_table").
858+
// Where(GT("Key1", 1))
851859
func BatchDelete(table string) *DeleteBuilder {
852860
return &DeleteBuilder{table: table, isBatch: true}
853861
}
@@ -886,7 +894,7 @@ func (d *DeleteBuilder) On(s *Selector) *DeleteBuilder {
886894
if d.ydb() {
887895
d.onSelect = s
888896
} else {
889-
d.AddError(fmt.Errorf("DELETE ON: unsupported dialect: %q", d.dialect))
897+
d.AddError(fmt.Errorf("DELETE ON is not supported by %q", d.dialect))
890898
}
891899
return d
892900
}
@@ -897,7 +905,7 @@ func (d *DeleteBuilder) Returning(columns ...string) *DeleteBuilder {
897905
if d.ydb() {
898906
d.returning = columns
899907
} else {
900-
d.AddError(fmt.Errorf("DELETE RETURNING: unsupported dialect: %q", d.dialect))
908+
d.AddError(fmt.Errorf("DELETE RETURNING is not supported by %q", d.dialect))
901909
}
902910
return d
903911
}
@@ -914,7 +922,7 @@ func (d *DeleteBuilder) QueryErr() (string, []any, error) {
914922
// BATCH DELETE (YDB-specific)
915923
if d.isBatch {
916924
if !b.ydb() {
917-
b.AddError(fmt.Errorf("BATCH DELETE: unsupported dialect: %q", b.dialect))
925+
b.AddError(fmt.Errorf("BATCH DELETE is not supported by %q", b.dialect))
918926
return "", nil, b.Err()
919927
}
920928
if len(d.returning) > 0 {
@@ -1323,6 +1331,8 @@ func (p *Predicate) NotIn(col string, args ...any) *Predicate {
13231331
}
13241332

13251333
// Exists returns the `Exists` predicate.
1334+
//
1335+
// Note: Correlated subqueries with EXISTS are not supported by YDB
13261336
func Exists(query Querier) *Predicate {
13271337
return P().Exists(query)
13281338
}
@@ -1338,6 +1348,8 @@ func (p *Predicate) Exists(query Querier) *Predicate {
13381348
}
13391349

13401350
// NotExists returns the `NotExists` predicate.
1351+
//
1352+
// Note: Correlated subqueries with NOT EXISTS are not supported by YDB
13411353
func NotExists(query Querier) *Predicate {
13421354
return P().NotExists(query)
13431355
}
@@ -1411,7 +1423,7 @@ func (p *Predicate) escapedLikeFold(col, left, substr, right string) *Predicate
14111423
// because this how it is defined in dialect/sql/schema.
14121424
b.Ident(col).WriteString(" COLLATE utf8mb4_general_ci LIKE ")
14131425
b.Arg(left + strings.ToLower(w) + right)
1414-
case dialect.Postgres:
1426+
case dialect.Postgres, dialect.YDB:
14151427
b.Ident(col).WriteString(" ILIKE ")
14161428
b.Arg(left + strings.ToLower(w) + right)
14171429
default: // SQLite.
@@ -1468,7 +1480,7 @@ func (p *Predicate) ColumnsHasPrefix(col, prefixC string) *Predicate {
14681480
p.WriteString(" ESCAPE ").Arg("\\")
14691481
}
14701482
default:
1471-
b.AddError(fmt.Errorf("ColumnsHasPrefix: unsupported dialect: %q", p.dialect))
1483+
b.AddError(fmt.Errorf("ColumnsHasPrefix is not supported by %q", p.dialect))
14721484
}
14731485
})
14741486
}
@@ -1815,7 +1827,7 @@ func (s *SelectTable) View(index string) *SelectTable {
18151827
if s.ydb() {
18161828
s.index = index
18171829
} else {
1818-
s.AddError(fmt.Errorf("VIEW: unsupported dialect: %q", s.dialect))
1830+
s.AddError(fmt.Errorf("VIEW is not supported by %q", s.dialect))
18191831
}
18201832
return s
18211833
}
@@ -2111,9 +2123,13 @@ func (s *Selector) From(t TableView) *Selector {
21112123

21122124
// AppendFrom appends a new TableView to the `FROM` clause.
21132125
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)
2126+
if s.ydb() && len(s.from) != 0 {
2127+
s.AddError(fmt.Errorf("multiple tables after FROM clause is not supported by %q", s.dialect))
2128+
} else {
2129+
s.from = append(s.from, t)
2130+
if st, ok := t.(state); ok {
2131+
st.SetDialect(s.dialect)
2132+
}
21172133
}
21182134
return s
21192135
}
@@ -2431,17 +2447,22 @@ func (s *Selector) UnionDistinct(t TableView) *Selector {
24312447

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

24412462
// ExceptAll appends the EXCEPT ALL clause to the query.
24422463
func (s *Selector) ExceptAll(t TableView) *Selector {
2443-
if s.sqlite() {
2444-
s.AddError(errors.New("EXCEPT ALL is not supported by SQLite"))
2464+
if s.sqlite() || s.ydb() {
2465+
s.AddError(fmt.Errorf("EXCEPT ALL is not supported by %q", s.dialect))
24452466
} else {
24462467
s.setOps = append(s.setOps, setOp{
24472468
Type: setOpTypeExcept,
@@ -2454,17 +2475,21 @@ func (s *Selector) ExceptAll(t TableView) *Selector {
24542475

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

24642489
// IntersectAll appends the INTERSECT ALL clause to the query.
24652490
func (s *Selector) IntersectAll(t TableView) *Selector {
2466-
if s.sqlite() {
2467-
s.AddError(errors.New("INTERSECT ALL is not supported by SQLite"))
2491+
if s.sqlite() || s.ydb() {
2492+
s.AddError(fmt.Errorf("INTERSECT ALL is not supported by %q", s.dialect))
24682493
} else {
24692494
s.setOps = append(s.setOps, setOp{
24702495
Type: setOpTypeIntersect,
@@ -2616,8 +2641,8 @@ func WithLockClause(clause string) LockOption {
26162641
// For sets the lock configuration for suffixing the `SELECT`
26172642
// statement with the `FOR [SHARE | UPDATE] ...` clause.
26182643
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"))
2644+
if s.sqlite() || s.ydb() {
2645+
s.AddError(fmt.Errorf("SELECT .. FOR UPDATE/SHARE is not supported by %q", s.dialect))
26212646
}
26222647
s.lock = &LockOptions{Strength: l}
26232648
for _, opt := range opts {
@@ -2754,7 +2779,7 @@ func (s *Selector) AssumeOrderBy(columns ...string) *Selector {
27542779

27552780
s.assumeOrder = append(s.assumeOrder, columns...)
27562781
} else {
2757-
s.AddError(fmt.Errorf("ASSUME ORDER BY: unsupported dialect: %q", s.dialect))
2782+
s.AddError(fmt.Errorf("ASSUME ORDER BY is not supported by %q", s.dialect))
27582783
}
27592784
return s
27602785
}

0 commit comments

Comments
 (0)