2022-05-21 19:59:22 +03:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2022-08-07 05:17:56 +03:00
|
|
|
"context"
|
2022-07-01 15:39:48 +03:00
|
|
|
"database/sql"
|
2022-05-21 19:59:22 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Table struct {
|
|
|
|
Name string
|
|
|
|
SQL string
|
|
|
|
}
|
|
|
|
|
2022-07-09 08:34:14 +03:00
|
|
|
//lint:ignore U1000 Ignore unused function temporarily for debugging
|
2022-08-24 15:40:56 +03:00
|
|
|
//nolint:all
|
2022-08-07 05:17:56 +03:00
|
|
|
func findTable(ctx context.Context, tx *sql.Tx, tableName string) (*Table, error) {
|
2022-05-21 19:59:22 +03:00
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
|
|
|
|
|
|
|
where, args = append(where, "type = ?"), append(args, "table")
|
|
|
|
where, args = append(where, "name = ?"), append(args, tableName)
|
|
|
|
|
2022-08-07 05:17:56 +03:00
|
|
|
query := `
|
2022-05-21 19:59:22 +03:00
|
|
|
SELECT
|
|
|
|
tbl_name,
|
|
|
|
sql
|
|
|
|
FROM sqlite_schema
|
2022-08-07 05:17:56 +03:00
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
2022-05-21 19:59:22 +03:00
|
|
|
if err != nil {
|
2022-05-22 04:29:34 +03:00
|
|
|
return nil, err
|
2022-05-21 19:59:22 +03:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
tableList := make([]*Table, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var table Table
|
|
|
|
if err := rows.Scan(
|
|
|
|
&table.Name,
|
|
|
|
&table.SQL,
|
|
|
|
); err != nil {
|
2022-05-22 04:29:34 +03:00
|
|
|
return nil, err
|
2022-05-21 19:59:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tableList = append(tableList, &table)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2022-05-22 04:29:34 +03:00
|
|
|
return nil, err
|
2022-05-21 19:59:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tableList) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return tableList[0], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:56 +03:00
|
|
|
func createTable(ctx context.Context, tx *sql.Tx, stmt string) error {
|
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
2022-07-01 15:39:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-21 19:59:22 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:56 +03:00
|
|
|
return nil
|
2022-05-21 19:59:22 +03:00
|
|
|
}
|