2019-08-17 18:32:01 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
2021-07-27 17:39:21 +02:00
|
|
|
"context"
|
2019-08-17 18:32:01 +02:00
|
|
|
"database/sql"
|
2023-07-21 13:20:44 +02:00
|
|
|
"fmt"
|
2023-09-18 17:35:58 +03:00
|
|
|
"runtime"
|
2022-12-05 22:45:45 +05:30
|
|
|
"strings"
|
2023-09-18 17:35:58 +03:00
|
|
|
"sync"
|
2022-12-05 22:45:45 +05:30
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
"github.com/go-jet/jet/v2/generator/metadata"
|
|
|
|
|
"github.com/go-jet/jet/v2/qrm"
|
2019-08-17 18:32:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// mySqlQuerySet is dialect query set for MySQL
|
|
|
|
|
type mySqlQuerySet struct{}
|
|
|
|
|
|
2023-07-21 13:20:44 +02:00
|
|
|
func (m mySqlQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableType metadata.TableType) ([]metadata.Table, error) {
|
2021-07-27 17:39:21 +02:00
|
|
|
query := `
|
|
|
|
|
SELECT table_name as "table.name"
|
2019-08-17 18:32:01 +02:00
|
|
|
FROM INFORMATION_SCHEMA.tables
|
2022-12-05 22:45:45 +05:30
|
|
|
WHERE table_schema = ? and table_type = ?
|
|
|
|
|
ORDER BY table_name;
|
2019-08-17 18:32:01 +02:00
|
|
|
`
|
2021-07-27 17:39:21 +02:00
|
|
|
var tables []metadata.Table
|
2019-08-17 18:32:01 +02:00
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
_, err := qrm.Query(context.Background(), db, query, []interface{}{schemaName, tableType}, &tables)
|
2023-07-21 13:20:44 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to query %s metadata result: %w", tableType, err)
|
|
|
|
|
}
|
2021-07-27 17:39:21 +02:00
|
|
|
|
2023-09-18 17:35:58 +03:00
|
|
|
tblChan := make(chan int, len(tables))
|
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
for i := 0; i < runtime.NumCPU(); i++ {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
var err1 error
|
|
|
|
|
for tblIdx := range tblChan {
|
|
|
|
|
tables[tblIdx].Columns, err1 = m.GetTableColumnsMetaData(db, schemaName, tables[tblIdx].Name)
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
select {
|
|
|
|
|
case errChan <- fmt.Errorf("failed to get '%s' table columns metadata: %w", tables[tblIdx].Name, err1):
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
for i := range tables {
|
2023-09-18 17:35:58 +03:00
|
|
|
tblChan <- i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(tblChan)
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case err = <-errChan:
|
|
|
|
|
return nil, err
|
|
|
|
|
default:
|
2021-07-27 17:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 13:20:44 +02:00
|
|
|
return tables, nil
|
2019-08-17 18:32:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 13:20:44 +02:00
|
|
|
func (m mySqlQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, tableName string) ([]metadata.Column, error) {
|
2021-07-27 17:39:21 +02:00
|
|
|
query := `
|
2023-09-18 17:35:11 +03:00
|
|
|
SELECT
|
|
|
|
|
col.COLUMN_NAME AS "column.Name",
|
|
|
|
|
col.IS_NULLABLE = "YES" AS "column.IsNullable",
|
|
|
|
|
col.COLUMN_COMMENT AS "column.Comment",
|
|
|
|
|
pk.IsPrimaryKey AS "column.IsPrimaryKey",
|
|
|
|
|
IF (col.COLUMN_TYPE = 'tinyint(1)',
|
|
|
|
|
'boolean',
|
|
|
|
|
IF (col.DATA_TYPE = 'enum',
|
|
|
|
|
CONCAT(col.TABLE_NAME, '_', col.COLUMN_NAME),
|
|
|
|
|
col.DATA_TYPE)
|
|
|
|
|
) AS "dataType.Name",
|
|
|
|
|
IF (col.DATA_TYPE = 'enum', 'enum', 'base') AS "dataType.Kind",
|
|
|
|
|
col.COLUMN_TYPE LIKE '%unsigned%' AS "dataType.IsUnsigned"
|
|
|
|
|
FROM
|
|
|
|
|
information_schema.columns AS col
|
|
|
|
|
LEFT JOIN (
|
|
|
|
|
SELECT k.column_name, 1 AS IsPrimaryKey
|
2021-12-14 18:22:58 +01:00
|
|
|
FROM information_schema.table_constraints t
|
2023-09-18 17:35:11 +03:00
|
|
|
JOIN information_schema.key_column_usage k USING(constraint_name, table_schema, table_name)
|
|
|
|
|
WHERE t.table_schema = ?
|
|
|
|
|
AND t.table_name = ?
|
|
|
|
|
AND t.constraint_type = 'PRIMARY KEY'
|
|
|
|
|
) AS pk ON col.COLUMN_NAME = pk.column_name
|
|
|
|
|
WHERE
|
|
|
|
|
col.table_schema = ?
|
|
|
|
|
AND col.table_name = ?
|
|
|
|
|
ORDER BY
|
|
|
|
|
col.ordinal_position;
|
|
|
|
|
`
|
|
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
var columns []metadata.Column
|
2022-01-12 19:03:50 +01:00
|
|
|
_, err := qrm.Query(context.Background(), db, query, []interface{}{schemaName, tableName, schemaName, tableName}, &columns)
|
2023-07-21 13:20:44 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to query %s column meta data: %w", tableName, err)
|
|
|
|
|
}
|
2021-07-27 17:39:21 +02:00
|
|
|
|
2023-07-21 13:20:44 +02:00
|
|
|
return columns, nil
|
2019-08-17 18:32:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 13:20:44 +02:00
|
|
|
func (m mySqlQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) ([]metadata.Enum, error) {
|
2021-07-27 17:39:21 +02:00
|
|
|
query := `
|
|
|
|
|
SELECT (CASE c.DATA_TYPE WHEN 'enum' then CONCAT(c.TABLE_NAME, '_', c.COLUMN_NAME) ELSE '' END ) as "name",
|
|
|
|
|
SUBSTRING(c.COLUMN_TYPE,5) as "values"
|
2019-08-17 18:32:01 +02:00
|
|
|
FROM information_schema.columns as c
|
|
|
|
|
INNER JOIN information_schema.tables as t on (t.table_schema = c.table_schema AND t.table_name = c.table_name)
|
2019-09-20 12:53:52 +02:00
|
|
|
WHERE c.table_schema = ? AND DATA_TYPE = 'enum';
|
2019-08-17 18:32:01 +02:00
|
|
|
`
|
2021-07-27 17:39:21 +02:00
|
|
|
var queryResult []struct {
|
|
|
|
|
Name string
|
|
|
|
|
Values string
|
|
|
|
|
}
|
2019-08-17 18:32:01 +02:00
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
_, err := qrm.Query(context.Background(), db, query, []interface{}{schemaName}, &queryResult)
|
2023-07-21 13:20:44 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to query enums meta data: %w", err)
|
|
|
|
|
}
|
2019-08-17 18:32:01 +02:00
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
var ret []metadata.Enum
|
2019-08-17 18:32:01 +02:00
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
for _, result := range queryResult {
|
|
|
|
|
enumValues := strings.Replace(result.Values[1:len(result.Values)-1], "'", "", -1)
|
2019-08-17 18:32:01 +02:00
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
ret = append(ret, metadata.Enum{
|
|
|
|
|
Name: result.Name,
|
|
|
|
|
Values: strings.Split(enumValues, ","),
|
2019-08-17 18:32:01 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 13:20:44 +02:00
|
|
|
return ret, nil
|
2019-08-17 18:32:01 +02:00
|
|
|
}
|