Repository navigation
Expand file tree
/
Copy pathdialect.go
More file actions
649 lines (616 loc) · 26.9 KB
/
Copy pathdialect.go
File metadata and controls
649 lines (616 loc) · 26.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
package dbmeta
import (
"context"
"database/sql"
"fmt"
"strings"
"sync"
)
// Dialect names one database family. Its value is the dburl driver name of
// the scheme that is canonical for the product, and a consumer with a dburl
// URL reads it from URL.Dialect, which dburl sets from v0.32.0.
//
// Read URL.Dialect and never URL.Driver. A product with two Go drivers has two
// schemes, and URL.Driver names the driver: pgx and postgres, moderncsqlite
// and sqlite3. Each pair has one Dialect. dbmeta holds no
// such list, because hard rule 1 keeps that taxonomy in dburl. See D99.
//
// A product that speaks another product's wire protocol has a Dialect of its
// own, as dburl gives it from v0.36.0. CockroachDB, CrateDB, QuestDB and
// Redshift speak PostgreSQL's protocol, and SingleStore, TiDB and Vitess
// speak MySQL's, and each has its own dialect, because its catalog is its
// own. A model for one of them can share the statements of the product it
// imitates, as the CockroachDB model does (dburl D30 and D37, D123, D125).
//
// A dialect names the family and never the release. A release arrives as a
// [VersionSet] value, because D8 resolves version differences at run time. See
// [New].
type Dialect string
// Dialect values. One per database family.
//
// The name is what the database calls itself. The value is the `dburl` driver
// name, which is not always the same word. PostgreSQL calls itself PostgreSQL
// and its driver is postgres. Cassandra calls itself Cassandra and its driver
// is cassandra, since the driver moved from github.com/xo/cql to
// github.com/xo/cassandra and dburl v0.47.0 renamed its scheme (D196).
// Couchbase was n1ql until dburl v0.33.0 renamed its scheme couchbase, when the
// driver moved to github.com/xo/dbimp
// (D101). Read these as constants and never as literals.
const (
// ArangoDB is queried in AQL rather than SQL, over HTTP.
ArangoDB Dialect = "arangodb"
// Athena is Amazon Athena. The value was awsathena while dburl v0.49.0 named
// the scheme that way, and it is athena since dburl renamed it, as the
// driver of dbimp is named. See D227.
Athena Dialect = "athena"
// Avatica is the standalone Avatica server, which stands in front of
// HSQLDB and which dbimp's avatica driver reaches. Phoenix speaks the same
// protocol and has no model. See D186.
Avatica Dialect = "avatica"
BigQuery Dialect = "bigquery"
Cassandra Dialect = "cassandra"
Chai Dialect = "chai"
ClickHouse Dialect = "clickhouse"
// CockroachDB speaks PostgreSQL's protocol, and pgx reaches it.
CockroachDB Dialect = "cockroachdb"
Couchbase Dialect = "couchbase"
Cosmos Dialect = "cosmos"
// CrateDB speaks PostgreSQL's protocol on its port 5432, and pgx reaches
// it.
CrateDB Dialect = "cratedb"
CSVQ Dialect = "csvq"
Databricks Dialect = "databricks"
Databend Dialect = "databend"
// Drill is Apache Drill, which dbimp's drill driver reaches on the REST
// interface of a Drillbit. See D178.
Drill Dialect = "drill"
// Druid is Apache Druid, which dbimp's druid driver reaches on the SQL
// API of the Router. See D171.
Druid Dialect = "druid"
DuckDB Dialect = "duckdb"
DynamoDB Dialect = "dynamodb"
// Elasticsearch reaches its SQL on /_sql, through dbimp's elasticsearch
// driver. See D177.
Elasticsearch Dialect = "elasticsearch"
Exasol Dialect = "exasol"
Firebird Dialect = "firebirdsql"
Hive Dialect = "hive"
HANA Dialect = "hdb"
// Impala is Apache Impala, which serves HiveServer2's protocol and has
// a catalog of its own. See D145.
Impala Dialect = "impala"
InfluxDB Dialect = "influxdb"
InfluxQL Dialect = "influxql"
// LibSQL is libSQL, the fork of SQLite by Turso, which sqld serves
// over HTTP.
LibSQL Dialect = "libsql"
MaxCompute Dialect = "maxcompute"
// MemSQL is SingleStore, which speaks MySQL's protocol. dburl names
// its scheme memsql, the name SingleStore had until 2020.
MemSQL Dialect = "memsql"
MySQL Dialect = "mysql"
Neo4j Dialect = "neo4j"
// OpenSearch reaches its SQL on /_plugins/_sql, through dbimp's opensearch
// driver. See D181.
OpenSearch Dialect = "opensearch"
Oracle Dialect = "oracle"
Pinot Dialect = "pinot"
Presto Dialect = "presto"
PostgreSQL Dialect = "postgres"
// QuestDB speaks PostgreSQL's protocol on its port 8812, and pgx
// reaches it.
QuestDB Dialect = "questdb"
// Redshift speaks PostgreSQL's protocol, and pgx reaches it.
Redshift Dialect = "redshift"
// Rqlite is rqlite, which runs SQLite behind an HTTP API.
Rqlite Dialect = "rqlite"
Snowflake Dialect = "snowflake"
// Solr is Apache Solr, which dbimp's solr driver reaches on the SQL handler
// of a collection. See D179.
Solr Dialect = "solr"
Spanner Dialect = "spanner"
SQLite3 Dialect = "sqlite3"
SQLServer Dialect = "sqlserver"
SurrealDB Dialect = "surrealdb"
Tablestore Dialect = "tablestore"
// TiDB speaks MySQL's protocol, and the mysql driver reaches it.
TiDB Dialect = "tidb"
Trino Dialect = "trino"
Vertica Dialect = "vertica"
// Vitess speaks MySQL's protocol, and the mysql driver reaches it.
Vitess Dialect = "vitess"
// YDB is Yandex's distributed database, which ydb-go-sdk reaches over
// gRPC. See D161.
YDB Dialect = "ydb"
)
// Info is what a model declares about its database.
type Info struct {
// Embedded says the database is a library rather than a server.
//
// There is nothing to connect to over a network, nothing to start, no
// release to pin beyond whatever the driver links, and no user to be. Two
// are: SQLite and DuckDB.
//
// A consumer reads it to decide what is worth offering. There is no host
// to report, no server version separate from the driver's, and no second
// principal, so a command that asks about any of those has nothing to
// say. dbmeta branches on it nowhere itself: every query runs the same way
// against a library as against a server.
//
// Hard rule 1 normally sends a fact about a scheme to dburl, and
// dburl carries this one from v0.29.0: it marks file, sqlite3,
// moderncsqlite, csvq, duckdb and chai DeploymentEmbedded, which is the
// same statement about the same six schemes. It is still declared
// here, because dbmeta has no dependencies and never opens a connection,
// so a caller holding a Dialect has no URL to hand to dburl.
//
// The two do not even count the same objects. dburl counts schemes and
// marks three for the two products here, because SQLite has two drivers.
// dbmeta counts models and marks two. See D80.
Embedded bool
// Placeholder writes the bind parameter for position n, counting from 1.
// PostgreSQL writes $1, MySQL writes ?, Oracle writes :1 and SQL Server
// writes @p1. A product that numbers none ignores n. Every model sets it,
// including one that binds with [Info.Literal], because a client writes
// its own INSERT with it. [Dialect.Placeholder] is the accessor, and it
// also answers for a dialect with no model. See D230.
Placeholder func(n int) string
// Named says that the drivers take a named argument only, so that a
// value is bound by its name and never by its position. Under it, the
// parameter at position n is written $p1, $p2 and so on, and its value
// is passed as sql.Named("p1", v), so [Query.Build] still returns the
// values in order and database/sql carries the names. Placeholder is not
// called. False for every product but SurrealDB.
//
// SurrealQL has named parameters only, and $1 is a parse error. dbimp's
// surrealdb driver refuses a positional argument for that reason
// (dbimp D50). See D164.
Named bool
// Literal renders one parameter value as a SQL literal, for a product
// whose protocol cannot carry a parameter at all. Nil for every
// product that can bind, which is every one but Apache Hive, and a
// nil Literal is what says the dialect binds.
//
// This exists because HiveServer2 has no parameter channel. Its
// Thrift request carries a session handle, a statement, a
// configuration overlay, an asynchronous flag and a timeout, and
// nothing else, so no driver can bind and Hive's own JDBC
// PreparedStatement substitutes on the client. A dialect in that
// position either renders its values into the statement or answers
// nothing at all. See D78.
//
// It is deliberately not a general literal mode and it is not a
// switch. The dialect supplies the function, because escaping is per
// product knowledge and one shared rule is not enough: Hive does not
// accept a doubled quote the way [QuoteLiteral] writes one. It reads
// 'a''b' as two literals joined and returns ab, silently, so a
// doubled quote there is a wrong answer rather than an error.
//
// This is the same reasoning D56 used to let ChangePassword build a
// statement: the escaping is the product's and the value cannot be
// bound.
//
// What it renders is a parameter this project declared, never a
// statement. The statement is written here and no caller supplies
// one. A value still reaches it from outside, so an implementation
// refuses what it cannot render rather than guessing, and returns an
// error for a type it does not know and for a string it cannot
// escape safely.
Literal func(v any) (string, error)
// BindValue converts one parameter value before it is bound, for a
// product whose drivers cannot bind every type a parameter here takes.
// Nil for every product but Oracle, and a nil BindValue binds the value
// as it is.
//
// Oracle is the case because it has no boolean before 23ai, and
// go-ora/v3 refuses a Go bool with "no parameter coder registered for
// go type bool". The Oracle model compares with_system with 1, so it
// binds a bool as 1 or 0, which every go-ora release carries. usql found
// it on 2026-09-30. See D136.
BindValue func(v any) any
// VersionQuery reads the server version. An empty string means the database
// reports no version, and a caller uses an unknown version.
VersionQuery string
// VersionColumns is how many columns VersionQuery returns. Most return
// one. SQL Server returns five, and CockroachDB, CrateDB, SingleStore and
// Vitess return two.
VersionColumns int
// ParseVersion turns the columns of the first row into a version set and a
// display line.
ParseVersion func(cols []string) (VersionSet, error)
// FollowUpQuery returns a second version statement, and how many columns
// it returns, when what the first one found calls for it. It returns an
// empty statement when none is needed. Nil for every dialect but cassandra.
//
// It exists because two products share the cassandra dialect and only one
// table both have. ScyllaDB reports the Cassandra release it keeps
// compatible with in system.local, and keeps its own release in
// system.versions, which Cassandra does not have. The first statement
// finds ScyllaDB and this one reads its release. See D92.
FollowUpQuery func(s VersionSet) (query string, cols int)
// ParseFollowUp adds the columns of the follow-up row to the set the
// first statement produced.
ParseFollowUp func(s VersionSet, cols []string) (VersionSet, error)
// QuotingQuery reads the session state that decides how a string literal is
// escaped. Empty when the product has no such state. See D56.
QuotingQuery string
// QuotingColumns is how many columns QuotingQuery returns.
QuotingColumns int
// ParseQuoting turns those columns into the state.
ParseQuoting func(cols []string) (Quoting, error)
// ChangePassword builds the statement that sets a password. It returns
// text and never runs anything. Nil when the product has no such
// statement or the model builds none, as for every embedded database
// here.
ChangePassword func(c PasswordChange, q Quoting) (string, error)
// DescribeResult builds the statement that describes a query without
// running it. Nil when the product has none. See D273.
DescribeResult func(query string) (ResultDescription, error)
// TypeNames builds the statement that names the types a driver reports as
// numbers. Nil when the product has none. See D273.
TypeNames func(types []TypeRef) (string, error)
// OldPassword says ChangePassword needs PasswordChange.Old: the product
// refuses a user's own password change without the current one. SQL
// Server is the case. A caller asks for the old password when it is
// true. See D143.
OldPassword bool
// Syntax is the lexical forms the product's SQL has, for a client that
// splits text into statements. See D143.
Syntax Syntax
// Terminator says what the product does with a semicolon that ends a
// statement. The zero value keeps it. See D143.
Terminator Terminator
// Batches are the statements that open and close a batch, which a client
// sends as one statement with everything between them. See D143.
Batches []Batch
// Fold is what the product does to the case of a name that is not
// quoted. See [Dialect.FoldIdentifier] and D143.
Fold Fold
// Product is the text a client prints for the version of a product that
// has no statement that reads its version, such as "Amazon DynamoDB". Empty
// when the model has a VersionQuery. A dialect with no model has its
// text in a table in the root package, and [Dialect.Product] reads both.
// See D230.
Product string
// EveryStatementIsAQuery says that every statement of the language
// returns a result and the server sends no count, so a client runs each
// statement as a query and never as an exec. True for ArangoDB, Neo4j,
// SurrealDB, InfluxDB and InfluxQL. See D230.
EveryStatementIsAQuery bool
// WritesNeedAutocommit says that a write must not run inside a
// transaction. Trino and Presto start one on BEGIN and then refuse the
// write with AUTOCOMMIT_WRITE_CONFLICT, so a client writes each row on its
// own. Every other product is found by a BeginTx that fails with
// ErrNotSupported in the driver, and is not flagged. See D230.
WritesNeedAutocommit bool
// ScanTypes says that a client must scan each column into the Go type
// that the driver reports, where it otherwise scans into any, so that
// numbers align and times format. True for the MySQL family and for
// Databend. See D230.
ScanTypes bool
// ColumnAlign says on which side a client prints a column, from the type
// that a driver reports for it. Nil when the model records no alignment.
// Each model measured the type names of its drivers against a live server,
// and [RightAligned] builds the function. See D245.
ColumnAlign func(*sql.ColumnType) (Align, bool)
// IsBinary says whether a column holds binary data, from the type that a
// driver reports for it. Nil when the model records nothing. The driver
// scans more than binary types into bytes, such as the text columns of
// MySQL, so the name decides. [Dialect.FormatBinary] writes the value in
// one form for every dialect, and [BinaryNames] builds the function. See
// D258.
IsBinary func(*sql.ColumnType) bool
// SuperuserQuery reads whether the session user is a superuser, as one
// column that [Dialect.ParseSuperuser] reads. Empty when the product has no
// such user or the model measured none. A client shows it as the # of a
// prompt, and it is a read like the version query. See D309.
SuperuserQuery string
// VersionNum builds the one integer form of the version that the product
// reports, such as 180006 for PostgreSQL 18.6, from the version set that
// [Info.ParseVersion] returned. Nil when the product has no such form. See
// [PostgresVersionNum], [MySQLVersionNum] and D309.
VersionNum func(VersionSet) (int, bool)
// TimeKind says whether a column holds a date, a time or a timestamp, with
// or without a zone, from the type that a driver reports for it. Nil when
// the model records none. Each model measured the type names of its drivers
// against a live server, and [TimeKinds] builds the function. See D309.
TimeKind func(*sql.ColumnType) TimeKind
// Statements says what the statements of the product return, for
// [Dialect.Classify]. See D264.
Statements Statements
// IsGUID says whether a binary column holds a GUID, from the type that a
// driver reports for it. Nil when the model records none. [Dialect.FormatBinary]
// writes a value of 16 bytes in such a column as a UUID. Oracle has no UUID
// type and keeps a GUID in a RAW(16). See D263.
IsGUID func(*sql.ColumnType) bool
}
var (
infoMu sync.RWMutex
infos = make(map[Dialect]*Info)
)
// RegisterDialect records what a model declares about its database. A model
// package calls this from its init. Registering twice panics, the way
// [database/sql.Register] does, because two models for one dialect is a build
// mistake rather than a run time condition.
func RegisterDialect(d Dialect, info *Info) {
infoMu.Lock()
defer infoMu.Unlock()
if _, ok := infos[d]; ok {
panic("dbmeta: dialect registered twice: " + string(d))
}
infos[d] = info
}
// Dialects returns every dialect built into this binary, which depends on the
// build tags. See D31.
func Dialects() []Dialect {
infoMu.RLock()
defer infoMu.RUnlock()
out := make([]Dialect, 0, len(infos))
for d := range infos {
out = append(out, d)
}
return out
}
// Info returns what d declares. The second result is false when no model for d
// was built into this binary, which is not the same as the database being
// unable to answer. See [ErrModelNotBuilt].
func (d Dialect) Info() (*Info, bool) {
infoMu.RLock()
defer infoMu.RUnlock()
info, ok := infos[d]
return info, ok
}
// Embedded reports whether the database is a library rather than a server.
//
// It is false for a dialect no model was built for, the same as for a server,
// because the question cannot be answered without the model. See
// [Info.Embedded].
func (d Dialect) Embedded() bool {
info, found := d.Info()
return found && info.Embedded
}
// VersionQuery returns the statement that reads the server version, and how
// many columns it returns.
//
// dbmeta cannot run it. The caller runs it as its first statement, scans the
// columns as strings, and passes them to [Dialect.ParseVersion]. See D38.
//
// The second result is false when the model was not built or the database
// reports no version.
func (d Dialect) VersionQuery() (query string, cols int, ok bool) {
info, found := d.Info()
if !found || info.VersionQuery == "" {
return "", 0, false
}
return info.VersionQuery, info.VersionColumns, true
}
// ParseVersion turns the columns returned by [Dialect.VersionQuery] into a
// version set carrying both the comparable version and the line to show a
// person.
func (d Dialect) ParseVersion(cols []string) (VersionSet, error) {
info, ok := d.Info()
if !ok {
return VersionSet{}, ErrModelNotBuilt
}
if info.ParseVersion == nil {
return VersionSet{Display: "unknown"}, nil
}
return info.ParseVersion(cols)
}
// SuperuserQuery returns the statement that reads whether the session user is
// a superuser, and how it reads. The caller runs it, scans the one column as a
// string and passes it to [Dialect.ParseSuperuser].
//
// The second result is false when the model was not built, or the product has
// no superuser, or the model measured no statement for it. A client treats that
// as unknown. See D309.
func (d Dialect) SuperuserQuery() (query string, ok bool) {
info, found := d.Info()
if !found || info.SuperuserQuery == "" {
return "", false
}
return info.SuperuserQuery, true
}
// ParseSuperuser reads the column that [Dialect.SuperuserQuery] returns. It
// accepts on, true, t, yes and 1, and off, false, f, no and 0, in any case, which
// are the forms that the products give. Any other text is [ErrInvalidSuperuser].
func (d Dialect) ParseSuperuser(col string) (bool, error) {
switch strings.ToLower(strings.TrimSpace(col)) {
case "on", "true", "t", "yes", "y", "1":
return true, nil
case "off", "false", "f", "no", "n", "0":
return false, nil
}
return false, fmt.Errorf("reading %q: %w", col, ErrInvalidSuperuser)
}
// VersionNum returns the integer form of the version in s, such as 180006 for
// PostgreSQL 18.6, which a client shows as SERVER_VERSION_NUM. The second result
// is false when the product has no such form, when no model was built for d, or
// when the version is unknown. See [Info.VersionNum].
func (d Dialect) VersionNum(s VersionSet) (int, bool) {
info, ok := d.Info()
if !ok || info.VersionNum == nil {
return 0, false
}
return info.VersionNum(s)
}
// FollowUpQuery returns the second version statement for s, the set that
// [Dialect.ParseVersion] returned, and how many columns it returns.
//
// The caller runs it after the first statement and passes its columns to
// [Dialect.ParseFollowUp]. The third result is false when the dialect needs no
// second statement for this server, which is every dialect but cassandra, and cassandra
// on Cassandra. See D92.
func (d Dialect) FollowUpQuery(s VersionSet) (query string, cols int, ok bool) {
info, found := d.Info()
if !found || info.FollowUpQuery == nil {
return "", 0, false
}
query, cols = info.FollowUpQuery(s)
return query, cols, query != ""
}
// ParseFollowUp adds the columns returned by [Dialect.FollowUpQuery] to s.
func (d Dialect) ParseFollowUp(s VersionSet, cols []string) (VersionSet, error) {
info, ok := d.Info()
if !ok {
return VersionSet{}, ErrModelNotBuilt
}
if info.ParseFollowUp == nil {
return s, nil
}
return info.ParseFollowUp(s, cols)
}
// Version runs the version statement against db and returns the parsed
// result.
//
// It is the steps above done together, because every caller needs both and
// doing them by hand means building a slice of pointers into a slice of
// strings. A caller that wants the statement without running it, to print it
// or to run it its own way, uses [Dialect.VersionQuery] and
// [Dialect.ParseVersion] instead.
//
// This does not detect anything on the caller's behalf. The caller decides
// whether to call it, and is free to build a [VersionSet] by hand and pass
// that to [New] instead. Overriding the version is the point of D36 and this
// method does not take it away.
//
// A database that reports no version returns an unknown version and no error.
// An unknown version selects the newest fragment of every piece.
func (d Dialect) Version(ctx context.Context, db Queryer) (VersionSet, error) {
query, n, ok := d.VersionQuery()
if !ok {
if _, built := d.Info(); !built {
return VersionSet{}, ErrModelNotBuilt
}
return VersionSet{Display: "unknown"}, nil
}
cols, err := readRow(ctx, db, d, query, n)
if err != nil {
return VersionSet{}, err
}
s, err := d.ParseVersion(cols)
if err != nil {
return VersionSet{}, err
}
query, n, ok = d.FollowUpQuery(s)
if !ok {
return s, nil
}
// The follow-up refines what the first statement found, and the server
// can refuse it to a principal that the first statement served.
// ScyllaDB refuses system.versions to a role granted nothing on it and
// serves system.local to the same role. So a refusal leaves the set as
// the first statement read it, which still names the product. Only a
// canceled context is an error, because then nothing that follows can
// run either.
cols, err = readRow(ctx, db, d, query, n)
switch {
case err == nil:
return d.ParseFollowUp(s, cols)
case ctx.Err() != nil:
return VersionSet{}, fmt.Errorf("reading from %s: %w", d, ctx.Err())
}
return s, nil
}
// readRow runs a statement that returns one row of n columns and hands back
// the values as text.
//
// Every column is scanned as nullable and flattened to empty. A property the
// server does not have comes back NULL, and scanning that straight into a
// string is a hard error naming neither the column nor the dialect. SQL Server
// has one: productupdatelevel arrived after the oldest release supported here.
//
// Flattening a NULL is right for these two callers and nowhere else.
// docs/NULLS.md forbids it for a catalog column, where NULL and empty are two
// facts a caller has to tell apart. Here the result is a version line shown to
// a person or a session setting read as a word, and absent and empty mean the
// same thing to both.
func readRow(ctx context.Context, db Queryer, d Dialect, query string, n int) ([]string, error) {
// QueryContext rather than QueryRowContext, so that Queryer needs one
// method. The cost is closing the rows by hand, which is four lines.
rows, err := db.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("reading from %s: %w", d, err)
}
defer rows.Close()
scanned := make([]sql.Null[string], n)
dest := make([]any, n)
for i := range scanned {
dest[i] = &scanned[i]
}
if !rows.Next() {
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("reading from %s: %w", d, err)
}
return nil, fmt.Errorf("reading from %s: %w", d, sql.ErrNoRows)
}
if err := rows.Scan(dest...); err != nil {
return nil, fmt.Errorf("reading from %s: %w", d, err)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("reading from %s: %w", d, err)
}
if err := rows.Close(); err != nil {
return nil, fmt.Errorf("reading from %s: %w", d, err)
}
cols := make([]string, n)
for i, v := range scanned {
cols[i] = v.V
}
return cols, nil
}
// Meta is what a caller holds. It names the dialect and the server version,
// and it decides which fragment of a query applies.
//
// The caller builds it. dbmeta never detects a dialect and never detects a
// version. See D36.
type Meta struct {
dialect Dialect
versions VersionSet
}
// New returns the Meta for a dialect at a version.
//
// Pass the version the caller read with [Dialect.VersionQuery], or any version
// the caller wants to force. Overriding matters for a proxy that hides the
// server, for a compatible product that reports a version it does not behave
// like, for a generator with no server at all, and for finding out whether a
// fault is a version gate.
//
// An unknown version selects the newest fragment of every piece, which is what
// D21 requires above the ceiling.
func New(d Dialect, versions VersionSet) (*Meta, error) {
if _, ok := d.Info(); !ok {
return nil, ErrModelNotBuilt
}
return &Meta{dialect: d, versions: versions}, nil
}
// Open reads the version of the server behind db and returns the metadata
// for it, which is Version followed by New. If the version cannot be read,
// Open returns the error and no Meta. A caller that wants to go on without a
// version calls New with an empty VersionSet, which takes the newest
// fragments, and decides that for itself, because on an old server those
// statements can be wrong.
func Open(ctx context.Context, d Dialect, db Queryer) (*Meta, error) {
versions, err := d.Version(ctx, db)
if err != nil {
return nil, fmt.Errorf("reading the version of %s: %w", d, err)
}
return New(d, versions)
}
// Dialect returns the dialect m was built for.
func (m *Meta) Dialect() Dialect { return m.dialect }
// String satisfies the [fmt.Stringer] interface. It returns the line to show
// a person, such as "PostgreSQL 16.2", which is what usql prints for
// \conninfo and in its prompt.
func (m *Meta) String() string {
if s := m.versions.String(); s != "" && s != "unknown" {
return s
}
return string(m.dialect)
}
// Version returns the version set m was built with. Call [VersionSet.Main] for
// the one number most callers want.
//
// The two accessors mirror the arguments of [New], so there is nothing to
// remember about their names.
func (m *Meta) Version() VersionSet { return m.versions }