dialect

package
v1.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dialect is what differs between the four SQL engines the SQL extension speaks to (SPEC §10.21 "Drivers", D23): PostgreSQL, MySQL and MariaDB, SQLite and SQL Server. A Dialect is only text and tables: it writes the catalog queries that list databases, schemas, tables, views, columns, indexes, foreign keys and routines, quotes identifiers, says how to take the first rows of a table, how to cancel a running statement and how to make a session read-only, and generates statements from what the catalog said. It opens no connection and imports no driver, so every part of it is tested without a database; the SQL extension runs the queries and gives the rows back to the Decode functions.

The shape of a catalog query

Each query returns its columns in the order its method documents, so that a caller can read the rows by position whatever the engine. A value that is a yes or no is 1 or 0. A query takes the names it filters on as arguments (placeholders in the style of the driver), and a name that has to be written in the statement itself (the database of SQL Server's [db].sys.tables, SQLite's schema, a SHOW CREATE) is quoted with Quote. Nothing is ever put in a statement unquoted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCreate

func GenerateCreate(d Dialect, t Table) string

GenerateCreate writes the CREATE TABLE statement of a table from its columns, then its other indexes. A column is its name, type, NOT NULL and DEFAULT as the catalog gave them; the primary key and the foreign keys follow as named constraints.

func GenerateDelete

func GenerateDelete(d Dialect, t Table) string

GenerateDelete writes DELETE with a WHERE on the primary key; see GenerateUpdate for the value.

func GenerateDrop

func GenerateDrop(d Dialect, kind Kind, r Ref) string

GenerateDrop writes the DROP statement of an object. It is only ever shown to the user, never run for them.

func GenerateInsert

func GenerateInsert(d Dialect, t Table) string

GenerateInsert writes INSERT with a NULL for each column, each with a remark that names the column and its type.

func GenerateSelect

func GenerateSelect(d Dialect, t Table) string

GenerateSelect writes SELECT with every column of the table.

func GenerateUpdate

func GenerateUpdate(d Dialect, t Table) string

GenerateUpdate writes UPDATE that sets every column that is not part of the primary key, with a WHERE on the primary key (or on all columns' absence, a condition to fill in). The value in the WHERE is NULL, which matches no row, so a statement that is run before it is filled in changes nothing.

func MarkForeignKeys

func MarkForeignKeys(cols []Column, fks []ForeignKey)

MarkForeignKeys sets ForeignKey on the columns that a foreign key uses.

Types

type CancelPlan

type CancelPlan struct {
	// ByContext says that ending the context of the call cancels the statement (the driver sends the cancel request, the attention
	// or the interrupt). When Kill is set it is the better way, because it leaves the session as it is; ByContext is then what
	// remains when Kill cannot be run.
	ByContext bool
	// SessionID is a query that returns the id of the session, which Kill needs; empty when Kill is nil.
	SessionID string
	// Kill returns the statement that cancels the running statement of the session with the id, to be run on another connection
	// (MySQL's KILL QUERY, PostgreSQL's pg_cancel_backend). Nil when ending the context is the way.
	Kill func(sessionID string) string
}

CancelPlan says how a running statement is cancelled.

type Class

type Class int

Class says what a statement does to the database, as far as its words tell (SPEC §10.21 "Safety").

const (
	// ClassEmpty is a statement with nothing in it.
	ClassEmpty Class = iota
	// ClassRead only reads: SELECT, WITH over reads, VALUES, SHOW, DESCRIBE, EXPLAIN, a reading PRAGMA.
	ClassRead
	// ClassWrite changes data, the schema, the privileges or the settings that make a session read-only; and anything the words do
	// not show to be harmless (CALL, EXEC, a function in a statement cannot be seen at all).
	ClassWrite
	// ClassTransaction begins, ends or marks a transaction (BEGIN, COMMIT, ROLLBACK, SAVEPOINT).
	ClassTransaction
	// ClassSession changes the session only: USE, SET.
	ClassSession
)

The classes.

func (Class) String

func (c Class) String() string

String names the class.

type Column

type Column struct {
	Name, Type string
	// Nullable says that the column accepts NULL.
	Nullable bool
	// Default is the default expression; HasDefault tells whether there is one (an empty text is a default too).
	Default    string
	HasDefault bool
	// PrimaryKey says that the column is part of the primary key; ForeignKey that it is part of a foreign key.
	PrimaryKey, ForeignKey bool
}

Column is a column of a table or view.

func DecodeColumns

func DecodeColumns(rows []Row) []Column

DecodeColumns reads the rows of Columns.

type Dialect

type Dialect interface {
	// ID is the id of the engine, the Engine.ID of the connection: "postgres", "mysql", "sqlite", "sqlserver".
	ID() string
	// Name is the name for the user.
	Name() string
	// DriverName is the name the engine's driver registers with database/sql.
	DriverName() string
	// Levels says which levels the engine has.
	Levels() Levels

	// Quote quotes an identifier. QuoteString writes a string literal.
	Quote(identifier string) string
	QuoteString(s string) string
	// Qualified writes the name of an object as a statement uses it: PostgreSQL schema.name (a connection serves one database),
	// MySQL database.name, SQL Server database.schema.name (the database when the ref has one, as VS Code's SQL Server extension writes
	// it), SQLite name (schema.name for an attached database).
	Qualified(r Ref) string

	// Databases lists the databases: name. system adds the ones the engine keeps for itself.
	Databases(system bool) Query
	// Schemas lists the schemas of a database: name.
	Schemas(database string, system bool) Query
	// Tables lists the tables of a schema (of a database, for MySQL): name, comment.
	Tables(r Ref, system bool) Query
	// Views lists the views: name, comment, kind (Kind, as text).
	Views(r Ref) Query
	// Routines lists the functions and procedures: name, kind (Kind, as text), arguments, result type, id.
	Routines(r Ref) Query
	// Columns lists the columns of a table or view in order: name, type, nullable, default, primary key.
	Columns(r Ref) Query
	// Indexes lists the columns of the indexes of a table, in order: index, column, unique, primary, position.
	Indexes(r Ref) Query
	// ForeignKeys lists the columns of the foreign keys of a table: name, column, referenced schema, referenced table, referenced
	// column, position. Referenced schema is the database for MySQL.
	ForeignKeys(r Ref) Query
	// Definition asks the catalog for the text that creates the object (the SQL of a view, the body of a routine). It is empty for
	// a table where Create writes the statement from the columns. Wrap turns the text into a statement.
	Definition(kind Kind, r Ref) Query
	// Wrap makes the statement that creates the object from what Definition returned.
	Wrap(kind Kind, r Ref, definition string) string

	// SelectTop writes a statement that reads the first n rows of a table.
	SelectTop(r Ref, n int) string
	// Cancel says how a running statement is cancelled.
	Cancel() CancelPlan
	// Syntax says how the engine's SQL is written: the quotes, the comments and the blocks that splitting a script and reading a
	// statement need to know about (Split, Classify).
	Syntax() Syntax
	// Transaction gives the statements that begin, commit and roll back a transaction in a session.
	Transaction() TxStatements
	// UseDatabase returns the statement that makes a session work in the database, or "" when the engine has no such statement or
	// reaches the database another way (PostgreSQL and SQLite).
	UseDatabase(database string) string
	// Keywords lists the keywords of the engine's SQL in capitals, for completion.
	Keywords() []string
	// ReadOnlySession lists the statements that make a session read-only, run on each new connection of a read-only connection;
	// the engine's other ways (SQLite's mode=ro, SQL Server's ApplicationIntent) are in the connection string the extension builds.
	ReadOnlySession() []string
}

Dialect is one engine.

func All

func All() []Dialect

All lists the dialects in the order the engines are shown.

func ByID

func ByID(id string) (Dialect, bool)

ByID returns the dialect with the engine id.

type ForeignKey

type ForeignKey struct {
	Name    string
	Columns []string
	// Schema and Table are the referenced table (Schema is the database for MySQL), and RefColumns its columns.
	Schema, Table string
	RefColumns    []string
}

ForeignKey is a foreign key with its columns in order.

func DecodeForeignKeys

func DecodeForeignKeys(rows []Row) []ForeignKey

DecodeForeignKeys reads the rows of ForeignKeys, which come ordered by key and position.

type Index

type Index struct {
	Name    string
	Columns []string
	// Unique and Primary say what the index enforces.
	Unique, Primary bool
}

Index is an index with its columns in order.

func DecodeIndexes

func DecodeIndexes(rows []Row) []Index

DecodeIndexes reads the rows of Indexes, which come ordered by index and position.

type Info

type Info struct {
	// Class is what the statement does.
	Class Class
	// Verb is the first word, in capitals ("SELECT", "DELETE"); empty for an empty statement.
	Verb string
	// Destructive says that the statement can lose data that is not easily brought back: DROP, TRUNCATE, ALTER, and DELETE or UPDATE
	// without a WHERE. Why says which, for the question the user is asked.
	Destructive bool
	Why         string
	// Rows says that the statement probably returns rows (a read, a statement with RETURNING or OUTPUT, CALL, EXEC), so it is run as
	// a query and not as a command that counts the rows it changed.
	Rows bool
	// Schema says that the statement changes the schema (CREATE, ALTER, DROP, RENAME, TRUNCATE...), so what the editor knows of the
	// catalog is out of date.
	Schema bool
	// Begins and Ends say that a transaction starts or ends with the statement (BEGIN, START TRANSACTION; COMMIT,
	// ROLLBACK, END), as the session must know whether one is open.
	Begins, Ends bool
}

Info is what Classify finds out about a statement.

func Classify

func Classify(d Dialect, text string) Info

Classify reads the first words of a statement and says what it does. It is a check of the words, not of the meaning: a SELECT that calls a function that writes is a read to it, which is why a read-only connection is also read-only in the engine where the engine can do that (Dialect.ReadOnlySession).

func (Info) Allowed

func (i Info) Allowed() bool

Allowed tells whether a statement of this class may run on a connection that is read-only.

type Kind

type Kind string

Kind is the kind of an object of a schema.

const (
	// KindTable is a table.
	KindTable Kind = "table"
	// KindView is a view.
	KindView Kind = "view"
	// KindMaterializedView is a view whose rows are stored (PostgreSQL).
	KindMaterializedView Kind = "materialized view"
	// KindFunction is a function.
	KindFunction Kind = "function"
	// KindProcedure is a stored procedure.
	KindProcedure Kind = "procedure"
)

The kinds of object the explorer shows.

type Levels

type Levels struct {
	// Databases says that the connection reaches several databases (PostgreSQL, MySQL, SQL Server).
	Databases bool
	// Schemas says that a database has schemas (PostgreSQL, SQL Server; for SQLite the attached databases, main among them).
	Schemas bool
	// Routines says that the engine has stored functions and procedures (not SQLite).
	Routines bool
	// SeparateConnections says that a connection serves one database, so another database needs a connection of its own
	// (PostgreSQL). The other engines read the catalog of another database through the connection they have.
	SeparateConnections bool
}

Levels tell which levels of the tree an engine has above its objects.

type MySQL

type MySQL struct{}

MySQL is MySQL and MariaDB, through go-sql-driver/mysql. A MySQL "schema" is a database, so there is no schema level.

func (MySQL) Cancel

func (MySQL) Cancel() CancelPlan

Cancel implements Dialect: the driver closes the connection when a context ends and the server goes on with the statement, so KILL QUERY is run from another connection.

func (MySQL) Columns

func (MySQL) Columns(r Ref) Query

Columns implements Dialect.

func (MySQL) Databases

func (MySQL) Databases(system bool) Query

Databases implements Dialect.

func (MySQL) Definition

func (m MySQL) Definition(kind Kind, r Ref) Query

Definition implements Dialect: SHOW CREATE, which writes the whole statement.

func (MySQL) DriverName

func (MySQL) DriverName() string

DriverName implements Dialect.

func (MySQL) ForeignKeys

func (MySQL) ForeignKeys(r Ref) Query

ForeignKeys implements Dialect.

func (MySQL) ID

func (MySQL) ID() string

ID implements Dialect.

func (MySQL) Indexes

func (MySQL) Indexes(r Ref) Query

Indexes implements Dialect.

func (MySQL) Keywords

func (MySQL) Keywords() []string

Keywords implements Dialect.

func (MySQL) Levels

func (MySQL) Levels() Levels

Levels implements Dialect.

func (MySQL) Name

func (MySQL) Name() string

Name implements Dialect.

func (MySQL) Qualified

func (m MySQL) Qualified(r Ref) string

Qualified implements Dialect: `database`.`name`.

func (MySQL) Quote

func (MySQL) Quote(identifier string) string

Quote implements Dialect: `name`, a backtick doubled.

func (MySQL) QuoteString

func (MySQL) QuoteString(s string) string

QuoteString implements Dialect: a backslash and a quote are escaped (the backslash is an escape unless NO_BACKSLASH_ESCAPES is on, and the doubled one is a backslash in both modes only when it is on, so a string with a backslash is written with its hex form).

func (MySQL) ReadOnlySession

func (MySQL) ReadOnlySession() []string

ReadOnlySession implements Dialect.

func (MySQL) Routines

func (MySQL) Routines(r Ref) Query

Routines implements Dialect. The arguments are written from information_schema.PARAMETERS (position 0 is the result of a function).

func (MySQL) Schemas

func (MySQL) Schemas(string, bool) Query

Schemas implements Dialect: MySQL has no schema level.

func (MySQL) SelectTop

func (m MySQL) SelectTop(r Ref, n int) string

SelectTop implements Dialect: LIMIT.

func (MySQL) Syntax

func (MySQL) Syntax() Syntax

Syntax implements Dialect: backslash escapes, double-quoted strings, backticks, # comments, DELIMITER.

func (MySQL) Tables

func (MySQL) Tables(r Ref, _ bool) Query

Tables implements Dialect. MariaDB's system-versioned tables are tables too.

func (MySQL) Transaction

func (MySQL) Transaction() TxStatements

Transaction implements Dialect.

func (MySQL) UseDatabase

func (m MySQL) UseDatabase(database string) string

UseDatabase implements Dialect: USE `database`.

func (MySQL) Views

func (MySQL) Views(r Ref) Query

Views implements Dialect.

func (MySQL) Wrap

func (MySQL) Wrap(_ Kind, _ Ref, definition string) string

Wrap implements Dialect: SHOW CREATE gave the statement.

type Object

type Object struct {
	Ref
	// Kind is what it is.
	Kind Kind
	// Comment is the comment of the object in the catalog, if it has one.
	Comment string
	// Result is the result type of a function.
	Result string
}

Object is a table, view or routine of a listing.

func DecodeRoutines

func DecodeRoutines(rows []Row, r Ref) []Object

DecodeRoutines reads the rows of Routines.

func DecodeTables

func DecodeTables(rows []Row, r Ref) []Object

DecodeTables reads the rows of Tables.

func DecodeViews

func DecodeViews(rows []Row, r Ref) []Object

DecodeViews reads the rows of Views.

type Postgres

type Postgres struct{}

Postgres is PostgreSQL, through pgx's database/sql driver.

func (Postgres) Cancel

func (Postgres) Cancel() CancelPlan

Cancel implements Dialect: pg_cancel_backend from another connection stops the statement and keeps the session (and so an open transaction), where ending the context would make pgx close the connection.

func (Postgres) Columns

func (Postgres) Columns(r Ref) Query

Columns implements Dialect.

func (Postgres) Databases

func (Postgres) Databases(system bool) Query

Databases implements Dialect. The databases a user cannot connect to are left out, and the templates unless system is asked.

func (Postgres) Definition

func (Postgres) Definition(kind Kind, r Ref) Query

Definition implements Dialect: pg_get_viewdef and pg_get_functiondef; a table is written from its columns.

func (Postgres) DriverName

func (Postgres) DriverName() string

DriverName implements Dialect: the name pgx's stdlib package registers.

func (Postgres) ForeignKeys

func (Postgres) ForeignKeys(r Ref) Query

ForeignKeys implements Dialect.

func (Postgres) ID

func (Postgres) ID() string

ID implements Dialect.

func (Postgres) Indexes

func (Postgres) Indexes(r Ref) Query

Indexes implements Dialect. An expression index shows the expression where a column would be.

func (Postgres) Keywords

func (Postgres) Keywords() []string

Keywords implements Dialect.

func (Postgres) Levels

func (Postgres) Levels() Levels

Levels implements Dialect.

func (Postgres) Name

func (Postgres) Name() string

Name implements Dialect.

func (Postgres) Qualified

func (p Postgres) Qualified(r Ref) string

Qualified implements Dialect: "schema"."name".

func (Postgres) Quote

func (Postgres) Quote(identifier string) string

Quote implements Dialect: "name", a quote doubled.

func (Postgres) QuoteString

func (Postgres) QuoteString(s string) string

QuoteString implements Dialect: standard strings, a quote doubled. A backslash is nothing special (standard_conforming_strings is on since PostgreSQL 9.1).

func (Postgres) ReadOnlySession

func (Postgres) ReadOnlySession() []string

ReadOnlySession implements Dialect.

func (Postgres) Routines

func (Postgres) Routines(r Ref) Query

Routines implements Dialect. The OID is the id: functions of one name differ by their arguments.

func (Postgres) Schemas

func (Postgres) Schemas(_ string, system bool) Query

Schemas implements Dialect. The schemas the user may not use are left out, and the system ones unless system is asked.

func (Postgres) SelectTop

func (p Postgres) SelectTop(r Ref, n int) string

SelectTop implements Dialect: LIMIT.

func (Postgres) Syntax

func (Postgres) Syntax() Syntax

Syntax implements Dialect: dollar quotes, E” strings, nested comments; BEGIN ATOMIC bodies.

func (Postgres) Tables

func (Postgres) Tables(r Ref, _ bool) Query

Tables implements Dialect: ordinary, partitioned and foreign tables.

func (Postgres) Transaction

func (Postgres) Transaction() TxStatements

Transaction implements Dialect.

func (Postgres) UseDatabase

func (Postgres) UseDatabase(string) string

UseDatabase implements Dialect: a PostgreSQL connection serves one database, so there is nothing to switch.

func (Postgres) Views

func (Postgres) Views(r Ref) Query

Views implements Dialect: views and materialized views.

func (Postgres) Wrap

func (p Postgres) Wrap(kind Kind, r Ref, definition string) string

Wrap implements Dialect: a view's query gets its CREATE; pg_get_functiondef is a statement already.

type Query

type Query struct {
	// SQL is the statement; empty means that the engine has nothing to ask (a level it does not have).
	SQL string
	// Args are the values of its placeholders, in order.
	Args []any
	// Text is, for a query that returns one text (a definition), the index of the column that holds it.
	Text int
}

Query is a statement and its arguments.

func (Query) Empty

func (q Query) Empty() bool

Empty tells whether there is nothing to ask.

type Ref

type Ref struct {
	// Database and Schema hold the parts above the object; one of them is empty for an engine that has no such level (MySQL has no
	// schema level: its databases are what other engines call schemas).
	Database, Schema string
	// Name is the object.
	Name string
	// Args is the argument list of a routine as the catalog writes it ("integer, text"), which PostgreSQL needs to tell routines of
	// one name apart.
	Args string
	// ID is the engine's own key of the object when the name is not enough (a PostgreSQL routine's OID); empty otherwise.
	ID string
}

Ref names an object of the catalog. Which parts an engine uses depends on its Levels.

type Row

type Row []sql.NullString

Row is one row of a catalog query, read as text; Null marks a NULL.

type SQLServer

type SQLServer struct{}

SQLServer is Microsoft SQL Server through go-mssqldb. The catalog of another database is read as [db].sys.tables, so one connection reaches all of them.

func (SQLServer) Cancel

func (SQLServer) Cancel() CancelPlan

Cancel implements Dialect: the driver sends an attention to the server when the context ends and the session stays as it was.

func (SQLServer) Columns

func (s SQLServer) Columns(r Ref) Query

Columns implements Dialect: the type is written with its length, precision and scale.

func (SQLServer) Databases

func (SQLServer) Databases(system bool) Query

Databases implements Dialect: the databases the login may use, without master, tempdb, model and msdb unless system is asked.

func (SQLServer) Definition

func (s SQLServer) Definition(kind Kind, r Ref) Query

Definition implements Dialect: the text of sys.sql_modules is the statement that made the view or routine.

func (SQLServer) DriverName

func (SQLServer) DriverName() string

DriverName implements Dialect: the name go-mssqldb registers, which takes @p1 placeholders.

func (SQLServer) ForeignKeys

func (s SQLServer) ForeignKeys(r Ref) Query

ForeignKeys implements Dialect.

func (SQLServer) ID

func (SQLServer) ID() string

ID implements Dialect.

func (SQLServer) Indexes

func (s SQLServer) Indexes(r Ref) Query

Indexes implements Dialect: the key columns of the indexes that have a name (a heap has none).

func (SQLServer) Keywords

func (SQLServer) Keywords() []string

Keywords implements Dialect.

func (SQLServer) Levels

func (SQLServer) Levels() Levels

Levels implements Dialect.

func (SQLServer) Name

func (SQLServer) Name() string

Name implements Dialect.

func (SQLServer) Qualified

func (s SQLServer) Qualified(r Ref) string

Qualified implements Dialect: [database].[schema].[name], with the database when the ref has one, so that a statement runs in whichever database its session is in (a connection to SQL Server is in one database at a time, and the explorer lists them all).

func (SQLServer) Quote

func (SQLServer) Quote(identifier string) string

Quote implements Dialect: [name], a closing bracket doubled.

func (SQLServer) QuoteString

func (SQLServer) QuoteString(s string) string

QuoteString implements Dialect: N'..', a quote doubled (a Unicode literal, so that no character is lost).

func (SQLServer) ReadOnlySession

func (SQLServer) ReadOnlySession() []string

ReadOnlySession implements Dialect: SQL Server has no statement for it. The connection string says ApplicationIntent=ReadOnly, which only tells an availability group where to route the session; the extension's own check refuses the statements that write.

func (SQLServer) Routines

func (s SQLServer) Routines(r Ref) Query

Routines implements Dialect: stored procedures and the functions of every kind (scalar, table-valued, CLR). The arguments are written from sys.parameters (parameter 0 is the result of a scalar function).

func (SQLServer) Schemas

func (s SQLServer) Schemas(database string, system bool) Query

Schemas implements Dialect. The schemas of the fixed roles and of the system are left out unless system is asked.

func (SQLServer) SelectTop

func (s SQLServer) SelectTop(r Ref, n int) string

SelectTop implements Dialect: TOP.

func (SQLServer) Syntax

func (SQLServer) Syntax() Syntax

Syntax implements Dialect: [brackets], nested comments, GO, blocks and the procedures that run to the end of the batch.

func (SQLServer) Tables

func (s SQLServer) Tables(r Ref, _ bool) Query

Tables implements Dialect. The comment is the MS_Description extended property.

func (SQLServer) Transaction

func (SQLServer) Transaction() TxStatements

Transaction implements Dialect.

func (SQLServer) UseDatabase

func (s SQLServer) UseDatabase(database string) string

UseDatabase implements Dialect: USE [database].

func (SQLServer) Views

func (s SQLServer) Views(r Ref) Query

Views implements Dialect.

func (SQLServer) Wrap

func (SQLServer) Wrap(_ Kind, _ Ref, definition string) string

Wrap implements Dialect: the module's text is the statement.

type SQLite

type SQLite struct{}

SQLite is SQLite through the pure Go driver modernc.org/sqlite. A file is one database; its "schemas" are the databases attached to the connection, main among them.

func (SQLite) Cancel

func (SQLite) Cancel() CancelPlan

Cancel implements Dialect: the driver interrupts the statement when the context ends.

func (SQLite) Columns

func (s SQLite) Columns(r Ref) Query

Columns implements Dialect with the table-valued form of PRAGMA table_info. A column of the primary key counts as NOT NULL, which is what an INTEGER PRIMARY KEY is although table_info says otherwise.

func (SQLite) Databases

func (SQLite) Databases(bool) Query

Databases implements Dialect: a file has no databases level.

func (SQLite) Definition

func (s SQLite) Definition(_ Kind, r Ref) Query

Definition implements Dialect: the sql column of the catalog is the statement that made the object.

func (SQLite) DriverName

func (SQLite) DriverName() string

DriverName implements Dialect.

func (SQLite) ForeignKeys

func (s SQLite) ForeignKeys(r Ref) Query

ForeignKeys implements Dialect. SQLite does not name them, so they are fk0, fk1... in the order it lists them.

func (SQLite) ID

func (SQLite) ID() string

ID implements Dialect.

func (SQLite) Indexes

func (s SQLite) Indexes(r Ref) Query

Indexes implements Dialect: index_list gives the indexes, index_info their columns.

func (SQLite) Keywords

func (SQLite) Keywords() []string

Keywords implements Dialect.

func (SQLite) Levels

func (SQLite) Levels() Levels

Levels implements Dialect: no databases, schemas (attached files), no routines.

func (SQLite) Name

func (SQLite) Name() string

Name implements Dialect.

func (SQLite) Qualified

func (s SQLite) Qualified(r Ref) string

Qualified implements Dialect: "name", or "schema"."name" for an attached database.

func (SQLite) Quote

func (SQLite) Quote(identifier string) string

Quote implements Dialect: "name", a quote doubled.

func (SQLite) QuoteString

func (SQLite) QuoteString(s string) string

QuoteString implements Dialect.

func (SQLite) ReadOnlySession

func (SQLite) ReadOnlySession() []string

ReadOnlySession implements Dialect. The connection string says mode=ro as well.

func (SQLite) Routines

func (SQLite) Routines(Ref) Query

Routines implements Dialect: SQLite has none.

func (SQLite) Schemas

func (SQLite) Schemas(string, bool) Query

Schemas implements Dialect: the attached databases.

func (SQLite) SelectTop

func (s SQLite) SelectTop(r Ref, n int) string

SelectTop implements Dialect: LIMIT.

func (SQLite) Syntax

func (SQLite) Syntax() Syntax

Syntax implements Dialect: [brackets], backticks, and the BEGIN ... END body of a trigger.

func (SQLite) Tables

func (s SQLite) Tables(r Ref, system bool) Query

Tables implements Dialect. The tables SQLite keeps for itself (sqlite_sequence, sqlite_stat1) are left out unless system is asked.

func (SQLite) Transaction

func (SQLite) Transaction() TxStatements

Transaction implements Dialect.

func (SQLite) UseDatabase

func (SQLite) UseDatabase(string) string

UseDatabase implements Dialect: SQLite has the one file and its attached schemas, which are named in the statements.

func (SQLite) Views

func (s SQLite) Views(r Ref) Query

Views implements Dialect.

func (SQLite) Wrap

func (SQLite) Wrap(_ Kind, _ Ref, definition string) string

Wrap implements Dialect: the catalog kept the statement.

type Statement

type Statement struct {
	// Text is the statement without the semicolon, the delimiter or the GO that ends it. It is exactly src[Start:End]: the comments
	// before it and inside it are kept (the engine ignores them, and a position it reports in the statement is a position in the
	// source after Start is added).
	Text string
	// Start and End are the bounds of Text in the source, in bytes.
	Start, End int
	// Batch numbers the batches of the script (SQL Server's GO ends one; every other engine has the one batch 0). The statements of
	// a batch run one after the other.
	Batch int
	// Repeat is how many times the batch of the statement is to run: the count of the GO that ended it ("GO 5"), else 1.
	Repeat int
}

Statement is one statement of a script, as the engine is to receive it.

func At

func At(src string, stmts []Statement, offset int) (Statement, bool)

At returns the statement the cursor at the byte offset is in: the one that contains the offset, or the one that ends on the line of the cursor before it (the cursor is after the semicolon), else the next one, else the last. It reports false when the script has no statement. src is the script the statements were cut from.

func Split

func Split(d Dialect, src string) []Statement

Split cuts a script into the statements the engine would see by its own rules (the dialect's Syntax):

  • a semicolon ends a statement, except inside a string, a quoted name, a comment, a dollar-quoted body, and a BEGIN ... END body of a SQLite trigger, of a PostgreSQL BEGIN ATOMIC function and of a SQL Server block;
  • MySQL: a DELIMITER line changes what ends a statement, and the line is not a statement;
  • SQL Server: a line GO (with a count, perhaps) ends a batch, and a CREATE or ALTER of a procedure, function, trigger or view goes on to the end of its batch, semicolons or not;
  • a piece with nothing but comments and white space is not a statement.

The statements come in the order of the script.

type Syntax

type Syntax struct {
	// DollarQuotes says that $tag$ ... $tag$ is a string (PostgreSQL).
	DollarQuotes bool
	// EscapeStrings says that E'...' is a string in which a backslash escapes the next character (PostgreSQL).
	EscapeStrings bool
	// BackslashEscapes says that a backslash escapes the next character in every string (MySQL, whose default mode is this one).
	BackslashEscapes bool
	// DoubleQuotedStrings says that "..." is a string, not an identifier (MySQL's default mode).
	DoubleQuotedStrings bool
	// HashComments says that # starts a comment that ends with the line (MySQL).
	HashComments bool
	// DashesNeedSpace says that -- starts a comment only when a space or a control character follows (MySQL).
	DashesNeedSpace bool
	// NestedComments says that /* */ comments nest (PostgreSQL, SQL Server).
	NestedComments bool
	// Brackets says that [...] quotes an identifier (SQL Server, SQLite).
	Brackets bool
	// Backticks says that `...` quotes an identifier (MySQL, SQLite).
	Backticks bool
	// DelimiterCommand says that a line DELIMITER x changes the end of a statement (MySQL's client).
	DelimiterCommand bool
	// GoSeparator says that a line with GO alone, and perhaps a count, ends a batch (SQL Server's tools).
	GoSeparator bool
	// TriggerBlocks says that CREATE TRIGGER has a BEGIN ... END body in which semicolons do not end the statement (SQLite).
	TriggerBlocks bool
	// AtomicBlocks says that BEGIN ATOMIC ... END is the body of a function or procedure (PostgreSQL 14).
	AtomicBlocks bool
	// Batches says that BEGIN ... END blocks are statements of their own and that CREATE PROCEDURE and its kin run to the end of
	// the batch (SQL Server).
	Batches bool
}

Syntax says how an engine's SQL is written, as far as splitting a script into statements and reading the first words of a statement need it: which quotes, comments and block structures there are. It is data: the lexer reads it.

type Table

type Table struct {
	Ref
	// Kind is KindTable or one of the view kinds.
	Kind        Kind
	Columns     []Column
	Indexes     []Index
	ForeignKeys []ForeignKey
}

Table is what the generators need to know about a table: its columns, indexes and foreign keys as the catalog listed them.

type TxStatements

type TxStatements struct {
	Begin, Commit, Rollback string
}

TxStatements are the statements of a transaction.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL