You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.6 KiB
101 lines
2.6 KiB
package vService
|
|
|
|
import (
|
|
"context"
|
|
_ "dm" // 达梦数据库驱动
|
|
"fmt"
|
|
"github.com/jmoiron/sqlx"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
// DMDBService 数据库服务实现
|
|
type DMDBService struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func NewDMDBService(dsn string) (*DMDBService, error) {
|
|
db, err := sqlx.Connect("dm", dsn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &DMDBService{db: db}, nil
|
|
}
|
|
|
|
func (s *DMDBService) Close() error {
|
|
return s.db.Close()
|
|
}
|
|
|
|
func (s *DMDBService) FetchData(ctx context.Context, dbName, tableName string, limit int) ([]map[string]interface{}, error) {
|
|
query := fmt.Sprintf("SELECT * FROM %s.%s WHERE ROWNUM <= %d", dbName, tableName, limit)
|
|
|
|
results, i, err2 := s.queryBySQL(ctx, query)
|
|
if err2 != nil {
|
|
return i, err2
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
func (s *DMDBService) queryBySQL(ctx context.Context, query string) ([]map[string]interface{}, []map[string]interface{}, error) {
|
|
rows, err := s.db.QueryxContext(ctx, query)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []map[string]interface{}
|
|
for rows.Next() {
|
|
row := make(map[string]interface{})
|
|
if err := rows.MapScan(row); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
results = append(results, row)
|
|
}
|
|
return results, nil, nil
|
|
}
|
|
|
|
func (s *DMDBService) AddColumn(ctx context.Context, dbName, tableName, columnName, columnType string) error {
|
|
query := fmt.Sprintf("ALTER TABLE %s.%s ADD %s %s", dbName, tableName, columnName, columnType)
|
|
if _, err := s.db.ExecContext(ctx, query); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *DMDBService) AddComment(ctx context.Context, dbName, tableName, columnName, comment string) error {
|
|
if comment != "" {
|
|
commentQuery := fmt.Sprintf("COMMENT ON COLUMN %s.%s.%s IS '%s'",
|
|
dbName, tableName, columnName, strings.ReplaceAll(comment, "'", "''"))
|
|
if _, err := s.db.ExecContext(ctx, commentQuery); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *DMDBService) DropColumn(ctx context.Context, dbName, tableName, columnName string) error {
|
|
query := fmt.Sprintf("ALTER TABLE %s.%s DROP COLUMN %s", dbName, tableName, columnName)
|
|
_, err := s.db.ExecContext(ctx, query)
|
|
return err
|
|
}
|
|
|
|
func (s *DMDBService) AllTable(ctx context.Context, dbName string) (interface{}, error) {
|
|
query := fmt.Sprintf("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = '%s'", dbName)
|
|
rows, err := s.db.QueryxContext(ctx, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func(rows *sqlx.Rows) {
|
|
err := rows.Close()
|
|
if err != nil {
|
|
log.Fatalf("发生异常: %v", err)
|
|
}
|
|
}(rows)
|
|
|
|
results, i, err := s.queryBySQL(ctx, query)
|
|
if err != nil {
|
|
return i, err
|
|
}
|
|
return results, nil
|
|
}
|
|
|