2020-08-06 20:58:47 +03:00
|
|
|
package sqlserver
|
|
|
|
|
|
|
|
import (
|
2020-08-16 00:06:40 +03:00
|
|
|
"context"
|
2020-08-06 20:58:47 +03:00
|
|
|
"database/sql"
|
2020-08-16 00:06:40 +03:00
|
|
|
"fmt"
|
2023-08-12 21:54:14 +03:00
|
|
|
"log/slog"
|
2020-08-16 00:06:40 +03:00
|
|
|
"strconv"
|
2020-08-06 20:58:47 +03:00
|
|
|
"strings"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/c2h5oh/datasize"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2023-05-03 15:36:10 +03:00
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
2023-04-03 00:17:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgm"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/progress"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/record"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
2020-08-16 00:06:40 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/metadata"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
// kindFromDBTypeName determines the kind.Kind from the database
|
|
|
|
// type name. For example, "VARCHAR" -> kind.Text.
|
2023-04-02 22:49:45 +03:00
|
|
|
func kindFromDBTypeName(log *slog.Logger, colName, dbTypeName string) kind.Kind {
|
2020-08-23 13:42:15 +03:00
|
|
|
var knd kind.Kind
|
2020-08-06 20:58:47 +03:00
|
|
|
dbTypeName = strings.ToUpper(dbTypeName)
|
|
|
|
|
|
|
|
switch dbTypeName {
|
|
|
|
default:
|
2023-08-12 21:54:14 +03:00
|
|
|
log.Warn("Unknown SQLServer database type for column: using default kind",
|
|
|
|
"db_type", dbTypeName, lga.Col, colName, lga.Kind, kind.Unknown)
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Unknown
|
2020-08-06 20:58:47 +03:00
|
|
|
case "INT", "BIGINT", "SMALLINT", "TINYINT":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Int
|
2020-08-06 20:58:47 +03:00
|
|
|
case "CHAR", "NCHAR", "VARCHAR", "JSON", "NVARCHAR", "NTEXT", "TEXT":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Text
|
2020-08-06 20:58:47 +03:00
|
|
|
case "BIT":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Bool
|
2020-08-06 20:58:47 +03:00
|
|
|
case "BINARY", "VARBINARY", "IMAGE":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Bytes
|
2020-08-06 20:58:47 +03:00
|
|
|
case "DECIMAL", "NUMERIC":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Decimal
|
2020-08-06 20:58:47 +03:00
|
|
|
case "MONEY", "SMALLMONEY":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Decimal
|
2020-08-06 20:58:47 +03:00
|
|
|
case "DATETIME", "DATETIME2", "SMALLDATETIME", "DATETIMEOFFSET":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Datetime
|
2020-08-06 20:58:47 +03:00
|
|
|
case "DATE":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Date
|
2020-08-06 20:58:47 +03:00
|
|
|
case "TIME":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Time
|
2020-08-06 20:58:47 +03:00
|
|
|
case "FLOAT", "REAL":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Float
|
2020-08-06 20:58:47 +03:00
|
|
|
case "XML":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Text
|
2020-08-06 20:58:47 +03:00
|
|
|
case "UNIQUEIDENTIFIER":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Text
|
2020-08-06 20:58:47 +03:00
|
|
|
case "ROWVERSION", "TIMESTAMP":
|
2020-08-23 13:42:15 +03:00
|
|
|
knd = kind.Int
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
return knd
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setScanType does some manipulation of ct's scan type.
|
2023-11-22 00:49:52 +03:00
|
|
|
// Most importantly, if ct is nullable column, we set colTypeData.ScanType
|
|
|
|
// to a nullable type. This is because the driver doesn't report nullable
|
|
|
|
// scan types.
|
2023-05-22 18:08:14 +03:00
|
|
|
func setScanType(ct *record.ColumnTypeData, knd kind.Kind) {
|
2020-08-23 13:42:15 +03:00
|
|
|
if knd == kind.Decimal {
|
2023-11-22 00:49:52 +03:00
|
|
|
// The driver wants us to use []byte for DECIMAL, so
|
|
|
|
// we override that here to use decimal.Decimal.
|
2020-08-06 20:58:47 +03:00
|
|
|
if ct.Nullable {
|
2023-11-22 00:49:52 +03:00
|
|
|
ct.ScanType = sqlz.RTypeNullDecimal
|
2020-08-06 20:58:47 +03:00
|
|
|
} else {
|
2023-11-22 00:49:52 +03:00
|
|
|
ct.ScanType = sqlz.RTypeDecimal
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ct.Nullable {
|
|
|
|
// If the col type is not nullable, there's nothing
|
|
|
|
// to do here.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ct.ScanType {
|
|
|
|
default:
|
|
|
|
ct.ScanType = sqlz.RTypeNullString
|
|
|
|
|
|
|
|
case sqlz.RTypeInt64:
|
|
|
|
ct.ScanType = sqlz.RTypeNullInt64
|
|
|
|
|
|
|
|
case sqlz.RTypeBool:
|
|
|
|
ct.ScanType = sqlz.RTypeNullBool
|
|
|
|
|
|
|
|
case sqlz.RTypeFloat64:
|
|
|
|
ct.ScanType = sqlz.RTypeNullFloat64
|
|
|
|
|
|
|
|
case sqlz.RTypeString:
|
|
|
|
ct.ScanType = sqlz.RTypeNullString
|
|
|
|
|
|
|
|
case sqlz.RTypeTime:
|
|
|
|
ct.ScanType = sqlz.RTypeNullTime
|
|
|
|
|
|
|
|
case sqlz.RTypeBytes:
|
|
|
|
ct.ScanType = sqlz.RTypeBytes // no change
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
func getSourceMetadata(ctx context.Context, src *source.Source, db sqlz.DB, noSchema bool) (*metadata.Source, error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
|
|
|
ctx = options.NewContext(ctx, src.Options)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
2020-08-16 00:06:40 +03:00
|
|
|
const query = `SELECT DB_NAME(), SCHEMA_NAME(), SERVERPROPERTY('ProductVersion'), @@VERSION,
|
|
|
|
(SELECT SUM(size) * 8192
|
|
|
|
FROM sys.master_files WITH(NOWAIT)
|
|
|
|
WHERE database_id = DB_ID()
|
|
|
|
GROUP BY database_id) AS total_size_bytes`
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
md := &metadata.Source{Driver: Type, DBDriver: Type}
|
2020-08-16 00:06:40 +03:00
|
|
|
md.Handle = src.Handle
|
|
|
|
md.Location = src.Location
|
|
|
|
|
|
|
|
var catalog, schema string
|
|
|
|
err := db.QueryRowContext(ctx, query).
|
|
|
|
Scan(&catalog, &schema, &md.DBVersion, &md.DBProduct, &md.Size)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
md.Name = catalog
|
|
|
|
md.FQName = catalog + "." + schema
|
2023-11-19 17:21:38 +03:00
|
|
|
md.Catalog = catalog
|
2023-03-22 09:17:34 +03:00
|
|
|
md.Schema = schema
|
2020-08-16 00:06:40 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
if md.DBProperties, err = getDBProperties(ctx, db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
if noSchema {
|
|
|
|
return md, nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
tblNames, tblTypes, err := getAllTables(ctx, db)
|
2020-08-16 00:06:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-03 00:17:15 +03:00
|
|
|
g, gCtx := errgroup.WithContext(ctx)
|
2023-05-03 15:36:10 +03:00
|
|
|
g.SetLimit(driver.OptTuningErrgroupLimit.Get(src.Options))
|
2023-11-21 00:42:38 +03:00
|
|
|
tblMetas := make([]*metadata.Table, len(tblNames))
|
2020-08-16 00:06:40 +03:00
|
|
|
for i := range tblNames {
|
2023-04-03 00:17:15 +03:00
|
|
|
i := i
|
2020-08-16 00:06:40 +03:00
|
|
|
g.Go(func() error {
|
2023-04-08 21:09:27 +03:00
|
|
|
select {
|
|
|
|
case <-gCtx.Done():
|
|
|
|
return gCtx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-05-24 03:42:36 +03:00
|
|
|
tblMeta, gErr := getTableMetadata(gCtx, db, catalog, schema, tblNames[i], tblTypes[i])
|
|
|
|
if gErr != nil {
|
|
|
|
if hasErrCode(gErr, errCodeObjectNotExist) {
|
2020-08-16 00:06:40 +03:00
|
|
|
// This can happen if the table is dropped while
|
|
|
|
// we're collecting metadata. We log a warning and continue.
|
2023-04-03 00:17:15 +03:00
|
|
|
log.Warn("Table metadata: table not found (continuing regardless)",
|
|
|
|
lga.Table, tblNames[i],
|
2023-05-24 03:42:36 +03:00
|
|
|
lga.Err, gErr,
|
2023-04-03 00:17:15 +03:00
|
|
|
)
|
2022-12-18 09:07:38 +03:00
|
|
|
|
2020-08-16 00:06:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-23 01:31:28 +03:00
|
|
|
|
2023-05-24 03:42:36 +03:00
|
|
|
return gErr
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
tblMetas[i] = tblMeta
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.Wait()
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a table wasn't found (possibly dropped while querying), then
|
|
|
|
// its entry could be nil. We copy the non-nil elements to the
|
|
|
|
// final slice.
|
2023-11-21 00:42:38 +03:00
|
|
|
md.Tables = make([]*metadata.Table, 0, len(tblMetas))
|
2020-08-16 00:06:40 +03:00
|
|
|
for i := range tblMetas {
|
|
|
|
if tblMetas[i] != nil {
|
|
|
|
md.Tables = append(md.Tables, tblMetas[i])
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 06:58:43 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
for _, tbl := range md.Tables {
|
|
|
|
if tbl.TableType == sqlz.TableTypeTable {
|
|
|
|
md.TableCount++
|
|
|
|
} else if tbl.TableType == sqlz.TableTypeView {
|
|
|
|
md.ViewCount++
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 00:06:40 +03:00
|
|
|
return md, nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
func getTableMetadata(ctx context.Context, db sqlz.DB, tblCatalog,
|
|
|
|
tblSchema, tblName, tblType string,
|
2023-11-21 00:42:38 +03:00
|
|
|
) (*metadata.Table, error) {
|
2020-08-16 00:06:40 +03:00
|
|
|
const tplTblUsage = `sp_spaceused '%s'`
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
tblMeta := &metadata.Table{Name: tblName, DBTableType: tblType}
|
2020-08-16 00:06:40 +03:00
|
|
|
tblMeta.FQName = tblCatalog + "." + tblSchema + "." + tblName
|
|
|
|
|
|
|
|
switch tblMeta.DBTableType {
|
|
|
|
case "BASE TABLE":
|
|
|
|
tblMeta.TableType = sqlz.TableTypeTable
|
|
|
|
case "VIEW":
|
|
|
|
tblMeta.TableType = sqlz.TableTypeView
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var rowCount, reserved, data, indexSize, unused sql.NullString
|
|
|
|
row := db.QueryRowContext(ctx, fmt.Sprintf(tplTblUsage, tblName))
|
2023-11-19 03:05:48 +03:00
|
|
|
|
|
|
|
// REVISIT: This error can occur:
|
|
|
|
//
|
|
|
|
// sql: Scan error on column index 0, name "name": converting NULL to string is unsupported
|
|
|
|
//
|
|
|
|
// We should probably use sql.NullString? This situation can arise if the table
|
|
|
|
// is deleted while this process is taking place. Maybe wrap the entire thing
|
|
|
|
// in a transaction? Or figure out how to fail more gracefully?
|
|
|
|
|
2020-08-16 00:06:40 +03:00
|
|
|
err := row.Scan(&tblMeta.Name, &rowCount, &reserved, &data, &indexSize, &unused)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
if rowCount.Valid {
|
|
|
|
tblMeta.RowCount, err = strconv.ParseInt(strings.TrimSpace(rowCount.String), 10, 64)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We can't get the "row count" for a VIEW from sp_spaceused,
|
|
|
|
// so we need to select it the old-fashioned way.
|
|
|
|
err = db.QueryRowContext(ctx, fmt.Sprintf("SELECT COUNT(*) FROM %q", tblName)).Scan(&tblMeta.RowCount)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if reserved.Valid {
|
|
|
|
var byteCount datasize.ByteSize
|
|
|
|
err = byteCount.UnmarshalText([]byte(reserved.String))
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
size := int64(byteCount.Bytes())
|
|
|
|
tblMeta.Size = &size
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbCols []columnMeta
|
2023-04-02 22:49:45 +03:00
|
|
|
dbCols, err = getColumnMeta(ctx, db, tblCatalog, tblSchema, tblName)
|
2020-08-16 00:06:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbConstraints []constraintMeta
|
2023-04-02 22:49:45 +03:00
|
|
|
dbConstraints, err = getConstraints(ctx, db, tblCatalog, tblSchema, tblName)
|
2020-08-16 00:06:40 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
cols := make([]*metadata.Column, len(dbCols))
|
2020-08-16 00:06:40 +03:00
|
|
|
for i := range dbCols {
|
2023-11-21 00:42:38 +03:00
|
|
|
cols[i] = &metadata.Column{
|
2020-08-16 00:06:40 +03:00
|
|
|
Name: dbCols[i].ColumnName,
|
|
|
|
Position: dbCols[i].OrdinalPosition,
|
|
|
|
BaseType: dbCols[i].DataType,
|
2023-05-03 15:36:10 +03:00
|
|
|
Kind: kindFromDBTypeName(lg.FromContext(ctx), dbCols[i].ColumnName, dbCols[i].DataType),
|
2020-08-16 00:06:40 +03:00
|
|
|
Nullable: dbCols[i].Nullable.Bool,
|
|
|
|
DefaultValue: dbCols[i].ColumnDefault.String,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to output something like VARCHAR(255) for ColType
|
|
|
|
|
|
|
|
// REVISIT: This is all a bit messy and inconsistent with other drivers
|
|
|
|
var colLength *int64
|
2022-12-18 09:07:38 +03:00
|
|
|
switch {
|
|
|
|
case dbCols[i].CharMaxLength.Valid:
|
2020-08-16 00:06:40 +03:00
|
|
|
colLength = &dbCols[i].CharMaxLength.Int64
|
2022-12-18 09:07:38 +03:00
|
|
|
case dbCols[i].NumericPrecision.Valid:
|
2020-08-16 00:06:40 +03:00
|
|
|
colLength = &dbCols[i].NumericPrecision.Int64
|
2022-12-18 09:07:38 +03:00
|
|
|
case dbCols[i].DateTimePrecision.Valid:
|
2020-08-16 00:06:40 +03:00
|
|
|
colLength = &dbCols[i].DateTimePrecision.Int64
|
|
|
|
}
|
|
|
|
|
|
|
|
if colLength != nil {
|
|
|
|
cols[i].ColumnType = fmt.Sprintf("%s(%v)", dbCols[i].DataType, *colLength)
|
|
|
|
} else {
|
|
|
|
cols[i].ColumnType = dbCols[i].DataType
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbConstraint := range dbConstraints {
|
|
|
|
if dbCols[i].ColumnName == dbConstraint.ColumnName {
|
|
|
|
if dbConstraint.ConstraintType == "PRIMARY KEY" {
|
|
|
|
cols[i].PrimaryKey = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tblMeta.Columns = cols
|
|
|
|
return tblMeta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllTables returns all of the table names, and the table types
|
|
|
|
// (i.e. "BASE TABLE" or "VIEW").
|
2023-04-02 22:49:45 +03:00
|
|
|
func getAllTables(ctx context.Context, db sqlz.DB) (tblNames, tblTypes []string, err error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
2020-08-16 00:06:40 +03:00
|
|
|
const query = `SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES
|
|
|
|
WHERE TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW'
|
|
|
|
ORDER BY TABLE_NAME ASC, TABLE_TYPE ASC`
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var tblName, tblType string
|
|
|
|
err = rows.Scan(&tblName, &tblType)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
tblNames = append(tblNames, tblName)
|
|
|
|
tblTypes = append(tblTypes, tblType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Err() != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, nil, errw(rows.Err())
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return tblNames, tblTypes, nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
func getColumnMeta(ctx context.Context, db sqlz.DB, tblCatalog, tblSchema, tblName string) ([]columnMeta, error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
// TODO: sq doesn't use all of these columns, no need to select them all.
|
2020-08-16 00:06:40 +03:00
|
|
|
const query = `SELECT
|
|
|
|
TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,
|
|
|
|
COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE,
|
|
|
|
CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH,
|
|
|
|
NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE,
|
|
|
|
DATETIME_PRECISION,
|
|
|
|
CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, CHARACTER_SET_NAME,
|
|
|
|
COLLATION_CATALOG, COLLATION_SCHEMA, COLLATION_NAME,
|
|
|
|
DOMAIN_CATALOG, DOMAIN_SCHEMA, DOMAIN_NAME
|
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
|
|
WHERE TABLE_CATALOG = @p1 AND TABLE_SCHEMA = @p2 AND TABLE_NAME = @p3`
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query, tblCatalog, tblSchema, tblName)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
var cols []columnMeta
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
c := columnMeta{}
|
|
|
|
err = rows.Scan(&c.TableCatalog, &c.TableSchema, &c.TableName, &c.ColumnName, &c.OrdinalPosition,
|
|
|
|
&c.ColumnDefault, &c.Nullable, &c.DataType, &c.CharMaxLength, &c.CharOctetLength, &c.NumericPrecision,
|
|
|
|
&c.NumericPrecisionRadix, &c.NumericScale, &c.DateTimePrecision, &c.CharSetCatalog, &c.CharSetSchema,
|
2022-12-18 05:43:53 +03:00
|
|
|
&c.CharSetName, &c.CollationCatalog, &c.CollationSchema, &c.CollationName, &c.DomainCatalog,
|
|
|
|
&c.DomainSchema, &c.DomainName)
|
2020-08-16 00:06:40 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
cols = append(cols, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return cols, nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
func getConstraints(ctx context.Context, db sqlz.DB, tblCatalog, tblSchema, tblName string) ([]constraintMeta, error) {
|
2023-05-03 15:36:10 +03:00
|
|
|
log := lg.FromContext(ctx)
|
2023-04-02 22:49:45 +03:00
|
|
|
|
2022-12-18 05:43:53 +03:00
|
|
|
const query = `SELECT kcu.TABLE_CATALOG, kcu.TABLE_SCHEMA, kcu.TABLE_NAME, tc.CONSTRAINT_TYPE,
|
|
|
|
kcu.COLUMN_NAME, kcu.CONSTRAINT_NAME
|
2020-08-16 00:06:40 +03:00
|
|
|
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
|
|
|
|
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
|
|
|
|
ON tc.TABLE_NAME = kcu.TABLE_NAME
|
|
|
|
AND tc.CONSTRAINT_CATALOG = kcu.CONSTRAINT_CATALOG
|
|
|
|
AND tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
|
|
|
|
AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
|
|
|
|
WHERE tc.TABLE_CATALOG = @p1 AND tc.TABLE_SCHEMA = @p2 AND tc.TABLE_NAME = @p3
|
|
|
|
ORDER BY kcu.TABLE_NAME, tc.CONSTRAINT_TYPE, kcu.CONSTRAINT_NAME`
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query, tblCatalog, tblSchema, tblName)
|
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
var constraints []constraintMeta
|
|
|
|
for rows.Next() {
|
|
|
|
c := constraintMeta{}
|
2022-12-18 05:43:53 +03:00
|
|
|
err = rows.Scan(&c.TableCatalog, &c.TableSchema, &c.TableName, &c.ConstraintType, &c.ColumnName,
|
|
|
|
&c.ConstraintName)
|
2020-08-16 00:06:40 +03:00
|
|
|
if err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
progress.Incr(ctx, 1)
|
2024-01-15 06:56:54 +03:00
|
|
|
progress.DebugSleep(ctx)
|
2020-08-16 00:06:40 +03:00
|
|
|
|
|
|
|
constraints = append(constraints, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
2023-05-22 18:08:14 +03:00
|
|
|
return nil, errw(err)
|
2020-08-16 00:06:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return constraints, nil
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:22:53 +03:00
|
|
|
// constraintMeta models constraint metadata from information schema.
|
|
|
|
type constraintMeta struct {
|
2024-01-15 04:45:34 +03:00
|
|
|
TableCatalog string `db:"TABLE_CATALOG"` // REVISIT: why do we have the `db` tag here?
|
2020-08-06 20:58:47 +03:00
|
|
|
TableSchema string `db:"TABLE_SCHEMA"`
|
|
|
|
TableName string `db:"TABLE_NAME"`
|
|
|
|
ConstraintType string `db:"CONSTRAINT_TYPE"`
|
|
|
|
ColumnName string `db:"COLUMN_NAME"`
|
|
|
|
ConstraintName string `db:"CONSTRAINT_NAME"`
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:22:53 +03:00
|
|
|
// columnMeta models column metadata from information schema.
|
|
|
|
type columnMeta struct {
|
2020-08-06 20:58:47 +03:00
|
|
|
TableCatalog string `db:"TABLE_CATALOG"`
|
|
|
|
TableSchema string `db:"TABLE_SCHEMA"`
|
|
|
|
TableName string `db:"TABLE_NAME"`
|
|
|
|
ColumnName string `db:"COLUMN_NAME"`
|
|
|
|
OrdinalPosition int64 `db:"ORDINAL_POSITION"`
|
|
|
|
ColumnDefault sql.NullString `db:"COLUMN_DEFAULT"`
|
|
|
|
Nullable sqlz.NullBool `db:"IS_NULLABLE"`
|
|
|
|
DataType string `db:"DATA_TYPE"`
|
|
|
|
CharMaxLength sql.NullInt64 `db:"CHARACTER_MAXIMUM_LENGTH"`
|
|
|
|
CharOctetLength sql.NullString `db:"CHARACTER_OCTET_LENGTH"`
|
|
|
|
NumericPrecision sql.NullInt64 `db:"NUMERIC_PRECISION"`
|
|
|
|
NumericPrecisionRadix sql.NullInt64 `db:"NUMERIC_PRECISION_RADIX"`
|
|
|
|
NumericScale sql.NullInt64 `db:"NUMERIC_SCALE"`
|
|
|
|
DateTimePrecision sql.NullInt64 `db:"DATETIME_PRECISION"`
|
|
|
|
CharSetCatalog sql.NullString `db:"CHARACTER_SET_CATALOG"`
|
|
|
|
CharSetSchema sql.NullString `db:"CHARACTER_SET_SCHEMA"`
|
|
|
|
CharSetName sql.NullString `db:"CHARACTER_SET_NAME"`
|
|
|
|
CollationCatalog sql.NullString `db:"COLLATION_CATALOG"`
|
|
|
|
CollationSchema sql.NullString `db:"COLLATION_SCHEMA"`
|
|
|
|
CollationName sql.NullString `db:"COLLATION_NAME"`
|
|
|
|
DomainCatalog sql.NullString `db:"DOMAIN_CATALOG"`
|
|
|
|
DomainSchema sql.NullString `db:"DOMAIN_SCHEMA"`
|
|
|
|
DomainName sql.NullString `db:"DOMAIN_NAME"`
|
|
|
|
}
|