2020-08-06 20:58:47 +03:00
|
|
|
// Package sqlserver implements the sq driver for SQL Server.
|
|
|
|
package sqlserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-12-18 07:31:06 +03:00
|
|
|
"errors"
|
2020-08-06 20:58:47 +03:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-23 21:09:06 +03:00
|
|
|
mssql "github.com/microsoft/go-mssqldb"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/lg"
|
|
|
|
|
2020-08-23 14:52:09 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/ast/sqlbuilder"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
2020-08-23 22:00:13 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlmodel"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Type is the SQL Server source driver type.
|
|
|
|
Type = source.Type("sqlserver")
|
|
|
|
|
|
|
|
// dbDrvr is the backing SQL Server driver impl name.
|
|
|
|
dbDrvr = "sqlserver"
|
|
|
|
)
|
|
|
|
|
2020-08-20 23:19:16 +03:00
|
|
|
var _ driver.Provider = (*Provider)(nil)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Provider is the SQL Server implementation of driver.Provider.
|
|
|
|
type Provider struct {
|
|
|
|
Log lg.Log
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverFor implements driver.Provider.
|
|
|
|
func (p *Provider) DriverFor(typ source.Type) (driver.Driver, error) {
|
|
|
|
if typ != Type {
|
|
|
|
return nil, errz.Errorf("unsupported driver type %q", typ)
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:40:03 +03:00
|
|
|
return &driveri{log: p.Log}, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-20 23:19:16 +03:00
|
|
|
var _ driver.Driver = (*driveri)(nil)
|
|
|
|
|
2020-08-13 06:40:03 +03:00
|
|
|
// driveri is the SQL Server implementation of driver.Driver.
|
|
|
|
type driveri struct {
|
2020-08-06 20:58:47 +03:00
|
|
|
log lg.Log
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverMetadata implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) DriverMetadata() driver.Metadata {
|
2020-08-06 20:58:47 +03:00
|
|
|
return driver.Metadata{
|
|
|
|
Type: Type,
|
2022-12-25 07:04:18 +03:00
|
|
|
Description: "Microsoft SQL Server / Azure SQL Edge",
|
2022-12-23 21:09:06 +03:00
|
|
|
Doc: "https://github.com/microsoft/go-mssqldb",
|
2020-08-06 20:58:47 +03:00
|
|
|
IsSQL: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dialect implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) Dialect() driver.Dialect {
|
2020-08-06 20:58:47 +03:00
|
|
|
return driver.Dialect{
|
2020-08-12 21:24:01 +03:00
|
|
|
Type: Type,
|
|
|
|
Placeholders: placeholders,
|
|
|
|
Quote: '"',
|
|
|
|
MaxBatchValues: 1000,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
func placeholders(numCols, numRows int) string {
|
|
|
|
rows := make([]string, numRows)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
n := 1
|
2020-08-06 20:58:47 +03:00
|
|
|
var sb strings.Builder
|
2020-08-12 21:24:01 +03:00
|
|
|
for i := 0; i < numRows; i++ {
|
|
|
|
sb.Reset()
|
|
|
|
sb.WriteRune('(')
|
|
|
|
for j := 1; j <= numCols; j++ {
|
|
|
|
sb.WriteString("@p")
|
|
|
|
sb.WriteString(strconv.Itoa(n))
|
|
|
|
n++
|
|
|
|
if j < numCols {
|
|
|
|
sb.WriteString(driver.Comma)
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2020-08-12 21:24:01 +03:00
|
|
|
sb.WriteRune(')')
|
|
|
|
rows[i] = sb.String()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2020-08-12 21:24:01 +03:00
|
|
|
|
|
|
|
return strings.Join(rows, driver.Comma)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SQLBuilder implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) SQLBuilder() (sqlbuilder.FragmentBuilder, sqlbuilder.QueryBuilder) {
|
2020-08-06 20:58:47 +03:00
|
|
|
return newFragmentBuilder(d.log), &queryBuilder{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open implements driver.Driver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) Open(ctx context.Context, src *source.Source) (driver.Database, error) {
|
2020-08-13 06:22:53 +03:00
|
|
|
db, err := sql.Open(dbDrvr, src.Location)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.PingContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
d.log.WarnIfCloseError(db)
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &database{log: d.log, db: db, src: src, drvr: d}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateSource implements driver.Driver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) ValidateSource(src *source.Source) (*source.Source, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if src.Type != Type {
|
|
|
|
return nil, errz.Errorf("expected source type %q but got %q", Type, src.Type)
|
|
|
|
}
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping implements driver.Driver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) Ping(ctx context.Context, src *source.Source) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
db, err := sql.Open(dbDrvr, src.Location)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer d.log.WarnIfCloseError(db)
|
|
|
|
|
|
|
|
err = db.PingContext(ctx)
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate implements driver.Driver. Due to a quirk of SQL Server, the
|
|
|
|
// operation is implemented in two statements. First "DELETE FROM tbl" to
|
|
|
|
// delete all rows. Then, if reset is true, the table sequence counter
|
|
|
|
// is reset via RESEED.
|
2022-12-18 05:43:53 +03:00
|
|
|
//
|
|
|
|
//nolint:lll
|
|
|
|
func (d *driveri) Truncate(ctx context.Context, src *source.Source, tbl string, reset bool) (affected int64,
|
2022-12-18 11:35:59 +03:00
|
|
|
err error,
|
|
|
|
) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql?view=sql-server-ver15
|
|
|
|
|
|
|
|
// When there are foreign key constraints on mssql tables,
|
|
|
|
// it's not possible to TRUNCATE the table. An alternative is
|
|
|
|
// to delete all rows and reseed the identity column.
|
|
|
|
//
|
|
|
|
// DELETE FROM "table1"; DBCC CHECKIDENT ('table1', RESEED, 1);
|
|
|
|
//
|
|
|
|
// See: https://stackoverflow.com/questions/253849/cannot-truncate-table-because-it-is-being-referenced-by-a-foreign-key-constraint
|
|
|
|
|
|
|
|
db, err := sql.Open(dbDrvr, src.Location)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
defer d.log.WarnIfFuncError(db.Close)
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
affected, err = sqlz.ExecAffected(ctx, db, fmt.Sprintf("DELETE FROM %q", tbl))
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return affected, errz.Wrapf(err, "truncate: failed to delete from %q", tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reset {
|
|
|
|
_, err = db.ExecContext(ctx, fmt.Sprintf("DBCC CHECKIDENT ('%s', RESEED, 1)", tbl))
|
|
|
|
if err != nil {
|
|
|
|
return affected, errz.Wrapf(err, "truncate: deleted %d rows from %q but RESEED failed", affected, tbl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableColumnTypes implements driver.SQLDriver.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName string,
|
2022-12-18 11:35:59 +03:00
|
|
|
colNames []string,
|
|
|
|
) ([]*sql.ColumnType, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// SQLServer has this unusual incantation for its LIMIT equivalent:
|
|
|
|
//
|
|
|
|
// SELECT username, email, address_id FROM person
|
|
|
|
// ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
|
|
|
|
const queryTpl = "SELECT %s FROM %s ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"
|
|
|
|
|
|
|
|
dialect := d.Dialect()
|
|
|
|
quote := string(dialect.Quote)
|
|
|
|
tblNameQuoted := stringz.Surround(tblName, quote)
|
|
|
|
|
2022-12-18 11:35:59 +03:00
|
|
|
colsClause := "*"
|
2020-08-06 20:58:47 +03:00
|
|
|
if len(colNames) > 0 {
|
|
|
|
colNamesQuoted := stringz.SurroundSlice(colNames, quote)
|
|
|
|
colsClause = strings.Join(colNamesQuoted, driver.Comma)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf(queryTpl, colsClause, tblNameQuoted)
|
|
|
|
rows, err := db.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
colTypes, err := rows.ColumnTypes()
|
|
|
|
if err != nil {
|
|
|
|
d.log.WarnIfFuncError(rows.Close)
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
d.log.WarnIfFuncError(rows.Close)
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return colTypes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordMeta implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (sqlz.RecordMeta, driver.NewRecordFunc, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
recMeta := make([]*sqlz.FieldMeta, len(colTypes))
|
|
|
|
for i, colType := range colTypes {
|
|
|
|
kind := kindFromDBTypeName(d.log, colType.Name(), colType.DatabaseTypeName())
|
|
|
|
colTypeData := sqlz.NewColumnTypeData(colType, kind)
|
|
|
|
setScanType(colTypeData, kind)
|
|
|
|
recMeta[i] = sqlz.NewFieldMeta(colTypeData)
|
|
|
|
}
|
|
|
|
|
2022-12-17 02:34:33 +03:00
|
|
|
mungeFn := func(vals []any) (sqlz.Record, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
// sqlserver doesn't need to do any special munging, so we
|
|
|
|
// just use the default munging.
|
|
|
|
rec, skipped := driver.NewRecordFromScanRow(recMeta, vals, nil)
|
|
|
|
if len(skipped) > 0 {
|
|
|
|
return nil, errz.Errorf("expected zero skipped cols but have %d", skipped)
|
|
|
|
}
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return recMeta, mungeFn, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 05:56:22 +03:00
|
|
|
// TableExists implements driver.SQLDriver.
|
|
|
|
func (d *driveri) TableExists(ctx context.Context, db sqlz.DB, tbl string) (bool, error) {
|
|
|
|
const query = `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
|
|
|
|
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = @p1`
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
err := db.QueryRowContext(ctx, query, tbl).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return false, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return count == 1, nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// CreateTable implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
stmt := buildCreateTableStmt(tblDef)
|
|
|
|
|
|
|
|
_, err := db.ExecContext(ctx, stmt)
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
2020-08-21 06:08:59 +03:00
|
|
|
// AlterTableAddColumn implements driver.SQLDriver.
|
2022-12-18 11:35:59 +03:00
|
|
|
func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl, col string, kind kind.Kind) error {
|
2020-08-21 06:08:59 +03:00
|
|
|
q := fmt.Sprintf("ALTER TABLE %q ADD %q ", tbl, col) + dbTypeNameFromKind(kind)
|
|
|
|
|
|
|
|
_, err := db.ExecContext(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Wrapf(err, "alter table: failed to add column %q to table %q", col, tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// CopyTable implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) CopyTable(ctx context.Context, db sqlz.DB, fromTable, toTable string, copyData bool) (int64, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
var stmt string
|
|
|
|
|
|
|
|
if copyData {
|
|
|
|
stmt = fmt.Sprintf("SELECT * INTO %q FROM %q", toTable, fromTable)
|
|
|
|
} else {
|
|
|
|
stmt = fmt.Sprintf("SELECT TOP(0) * INTO %q FROM %q", toTable, fromTable)
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
affected, err := sqlz.ExecAffected(ctx, db, stmt)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DropTable implements driver.SQLDriver.
|
2020-08-13 06:40:03 +03:00
|
|
|
func (d *driveri) DropTable(ctx context.Context, db sqlz.DB, tbl string, ifExists bool) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
var stmt string
|
|
|
|
|
|
|
|
if ifExists {
|
|
|
|
stmt = fmt.Sprintf("IF OBJECT_ID('dbo.%s', 'U') IS NOT NULL DROP TABLE dbo.%q", tbl, tbl)
|
|
|
|
} else {
|
|
|
|
stmt = fmt.Sprintf("DROP TABLE dbo.%q", tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := db.ExecContext(ctx, stmt)
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareInsertStmt implements driver.SQLDriver.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string,
|
2022-12-18 11:35:59 +03:00
|
|
|
numRows int,
|
|
|
|
) (*driver.StmtExecer, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
destColsMeta, err := d.getTableColsMeta(ctx, db, destTbl, destColNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
stmt, err := driver.PrepareInsertStmt(ctx, d, db, destTbl, destColsMeta.Names(), numRows)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:43:53 +03:00
|
|
|
execer := driver.NewStmtExecer(stmt, driver.DefaultInsertMungeFunc(destTbl, destColsMeta),
|
|
|
|
newStmtExecFunc(stmt, db, destTbl), destColsMeta)
|
2020-08-06 20:58:47 +03:00
|
|
|
return execer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareUpdateStmt implements driver.SQLDriver.
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) PrepareUpdateStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string,
|
2022-12-18 11:35:59 +03:00
|
|
|
where string,
|
|
|
|
) (*driver.StmtExecer, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
destColsMeta, err := d.getTableColsMeta(ctx, db, destTbl, destColNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query, err := buildUpdateStmt(destTbl, destColNames, where)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.PrepareContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:43:53 +03:00
|
|
|
execer := driver.NewStmtExecer(stmt, driver.DefaultInsertMungeFunc(destTbl, destColsMeta),
|
|
|
|
newStmtExecFunc(stmt, db, destTbl), destColsMeta)
|
2020-08-06 20:58:47 +03:00
|
|
|
return execer, nil
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:43:53 +03:00
|
|
|
func (d *driveri) getTableColsMeta(ctx context.Context, db sqlz.DB, tblName string, colNames []string) (sqlz.RecordMeta,
|
2022-12-18 11:35:59 +03:00
|
|
|
error,
|
|
|
|
) {
|
2020-08-16 00:06:40 +03:00
|
|
|
// SQLServer has this unusual incantation for its LIMIT equivalent:
|
|
|
|
//
|
|
|
|
// SELECT username, email, address_id FROM person
|
|
|
|
// ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
|
|
|
|
const queryTpl = "SELECT %s FROM %s ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"
|
|
|
|
|
|
|
|
dialect := d.Dialect()
|
|
|
|
quote := string(dialect.Quote)
|
|
|
|
tblNameQuoted := stringz.Surround(tblName, quote)
|
|
|
|
colNamesQuoted := stringz.SurroundSlice(colNames, quote)
|
|
|
|
colsJoined := strings.Join(colNamesQuoted, driver.Comma)
|
|
|
|
|
|
|
|
query := fmt.Sprintf(queryTpl, colsJoined, tblNameQuoted)
|
|
|
|
rows, err := db.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
colTypes, err := rows.ColumnTypes()
|
|
|
|
if err != nil {
|
|
|
|
d.log.WarnIfFuncError(rows.Close)
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Err() != nil {
|
|
|
|
return nil, errz.Err(rows.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
destCols, _, err := d.RecordMeta(colTypes)
|
|
|
|
if err != nil {
|
|
|
|
d.log.WarnIfFuncError(rows.Close)
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return destCols, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// database implements driver.Database.
|
|
|
|
type database struct {
|
|
|
|
log lg.Log
|
|
|
|
drvr *driveri
|
|
|
|
db *sql.DB
|
|
|
|
src *source.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ driver.Database = (*database)(nil)
|
|
|
|
|
|
|
|
// DB implements driver.Database.
|
|
|
|
func (d *database) DB() *sql.DB {
|
|
|
|
return d.db
|
|
|
|
}
|
|
|
|
|
|
|
|
// SQLDriver implements driver.Database.
|
|
|
|
func (d *database) SQLDriver() driver.SQLDriver {
|
|
|
|
return d.drvr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source implements driver.Database.
|
|
|
|
func (d *database) Source() *source.Source {
|
|
|
|
return d.src
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableMetadata implements driver.Database.
|
|
|
|
func (d *database) TableMetadata(ctx context.Context, tblName string) (*source.TableMetadata, error) {
|
|
|
|
const query = `SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_TYPE
|
|
|
|
FROM INFORMATION_SCHEMA.TABLES
|
|
|
|
WHERE TABLE_NAME = @p1`
|
|
|
|
|
|
|
|
var catalog, schema, tblType string
|
|
|
|
err := d.db.QueryRowContext(ctx, query, tblName).Scan(&catalog, &schema, &tblType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTableMetadata(ctx, d.log, d.db, catalog, schema, tblName, tblType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SourceMetadata implements driver.Database.
|
|
|
|
func (d *database) SourceMetadata(ctx context.Context) (*source.Metadata, error) {
|
|
|
|
return getSourceMetadata(ctx, d.log, d.src, d.db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Database.
|
|
|
|
func (d *database) Close() error {
|
|
|
|
d.log.Debugf("Close database: %s", d.src)
|
|
|
|
|
|
|
|
return errz.Err(d.db.Close())
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// newStmtExecFunc returns a StmtExecFunc that has logic to deal with
|
|
|
|
// the "identity insert" error. If the error is encountered, setIdentityInsert
|
|
|
|
// is called and stmt is executed again.
|
|
|
|
func newStmtExecFunc(stmt *sql.Stmt, db sqlz.DB, tbl string) driver.StmtExecFunc {
|
2022-12-17 02:34:33 +03:00
|
|
|
return func(ctx context.Context, args ...any) (int64, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
res, err := stmt.ExecContext(ctx, args...)
|
|
|
|
if err == nil {
|
|
|
|
var affected int64
|
|
|
|
affected, err = res.RowsAffected()
|
|
|
|
return affected, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasErrCode(err, errCodeIdentityInsert) {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idErr := setIdentityInsert(ctx, db, tbl, true)
|
|
|
|
if idErr != nil {
|
|
|
|
return 0, errz.Combine(err, idErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = stmt.ExecContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
return affected, errz.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setIdentityInsert enables (or disables) "identity insert" for tbl on db.
|
|
|
|
// SQLServer is fussy about inserting values to the identity col. This
|
|
|
|
// error can be returned from the driver:
|
|
|
|
//
|
2022-12-17 01:54:09 +03:00
|
|
|
// mssql: Cannot insert explicit value for identity column in table 'payment' when IDENTITY_INSERT is set to OFF
|
2020-08-06 20:58:47 +03:00
|
|
|
//
|
|
|
|
// The solution is "SET IDENTITY_INSERT tbl ON".
|
|
|
|
//
|
|
|
|
// See: https://docs.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql?view=sql-server-ver15
|
|
|
|
func setIdentityInsert(ctx context.Context, db sqlz.DB, tbl string, on bool) error {
|
2022-12-18 11:35:59 +03:00
|
|
|
mode := "ON"
|
2020-08-06 20:58:47 +03:00
|
|
|
if !on {
|
|
|
|
mode = "OFF"
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf("SET IDENTITY_INSERT %q %s", tbl, mode)
|
|
|
|
_, err := db.ExecContext(ctx, query)
|
|
|
|
return errz.Wrapf(err, "failed to SET IDENTITY INSERT %s %s", tbl, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mssql error codes
|
2022-12-18 05:43:53 +03:00
|
|
|
//
|
|
|
|
//nolint:lll
|
2020-08-06 20:58:47 +03:00
|
|
|
const (
|
2022-12-18 05:43:53 +03:00
|
|
|
// See: https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors?view=sql-server-ver15
|
2020-08-06 20:58:47 +03:00
|
|
|
errCodeIdentityInsert int32 = 544
|
|
|
|
errCodeObjectNotExist int32 = 15009
|
|
|
|
)
|
|
|
|
|
|
|
|
// hasErrCode returns true if err (or its cause err) is
|
|
|
|
// of type mssql.Error and err.Number equals code.
|
|
|
|
func hasErrCode(err error, code int32) bool {
|
2022-12-18 07:31:06 +03:00
|
|
|
if err == nil {
|
|
|
|
return false
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2022-12-18 07:31:06 +03:00
|
|
|
|
|
|
|
var msErr mssql.Error
|
|
|
|
if errors.As(err, &msErr) {
|
|
|
|
return msErr.Number == code
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
return false
|
|
|
|
}
|