mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-12 05:29:40 +08:00
- 新增测试连接功能,修复底层驱动假成功问题,确保密码/端口验证准确 - 支持导入/导出连接配置(JSON),便于迁移与备份 - 优化侧边栏:实现虚拟滚动解决卡顿,增加数据库筛选与断开连接重连机制 - 优化交互:改进右键菜单体验(全行触发/禁用选文),完善新建查询的上下文自动关联 - 界面调整:精简连接弹窗,移除冗余的默认数据库输入
157 lines
3.4 KiB
Go
157 lines
3.4 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"GoNavi-Wails/internal/connection"
|
|
"GoNavi-Wails/internal/utils"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
type SQLiteDB struct {
|
|
conn *sql.DB
|
|
}
|
|
|
|
func (s *SQLiteDB) Connect(config connection.ConnectionConfig) error {
|
|
dsn := config.Host
|
|
db, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.conn = db
|
|
|
|
// Force verification
|
|
return s.Ping()
|
|
}
|
|
|
|
func (s *SQLiteDB) Close() error {
|
|
if s.conn != nil {
|
|
return s.conn.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteDB) Ping() error {
|
|
if s.conn == nil {
|
|
return fmt.Errorf("connection not open")
|
|
}
|
|
ctx, cancel := utils.ContextWithTimeout(5 * time.Second)
|
|
defer cancel()
|
|
return s.conn.PingContext(ctx)
|
|
}
|
|
|
|
func (s *SQLiteDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
|
if s.conn == nil {
|
|
return nil, nil, fmt.Errorf("connection not open")
|
|
}
|
|
|
|
rows, err := s.conn.Query(query)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
columns, err := rows.Columns()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var resultData []map[string]interface{}
|
|
|
|
for rows.Next() {
|
|
values := make([]interface{}, len(columns))
|
|
valuePtrs := make([]interface{}, len(columns))
|
|
for i := range columns {
|
|
valuePtrs[i] = &values[i]
|
|
}
|
|
|
|
if err := rows.Scan(valuePtrs...); err != nil {
|
|
continue
|
|
}
|
|
|
|
entry := make(map[string]interface{})
|
|
for i, col := range columns {
|
|
var v interface{}
|
|
val := values[i]
|
|
b, ok := val.([]byte)
|
|
if ok {
|
|
v = string(b)
|
|
} else {
|
|
v = val
|
|
}
|
|
entry[col] = v
|
|
}
|
|
resultData = append(resultData, entry)
|
|
}
|
|
|
|
return resultData, columns, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) Exec(query string) (int64, error) {
|
|
if s.conn == nil {
|
|
return 0, fmt.Errorf("connection not open")
|
|
}
|
|
res, err := s.conn.Exec(query)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.RowsAffected()
|
|
}
|
|
|
|
func (s *SQLiteDB) GetDatabases() ([]string, error) {
|
|
return []string{"main"}, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) GetTables(dbName string) ([]string, error) {
|
|
query := "SELECT name FROM sqlite_master WHERE type='table'"
|
|
data, _, err := s.Query(query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tables []string
|
|
for _, row := range data {
|
|
if val, ok := row["name"]; ok {
|
|
tables = append(tables, fmt.Sprintf("%v", val))
|
|
}
|
|
}
|
|
return tables, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) GetCreateStatement(dbName, tableName string) (string, error) {
|
|
query := fmt.Sprintf("SELECT sql FROM sqlite_master WHERE type='table' AND name='%s'", tableName)
|
|
data, _, err := s.Query(query)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(data) > 0 {
|
|
if val, ok := data[0]["sql"]; ok {
|
|
return fmt.Sprintf("%v", val), nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("create statement not found")
|
|
}
|
|
|
|
func (s *SQLiteDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
|
return []connection.ColumnDefinition{}, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
|
return []connection.IndexDefinition{}, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
|
return []connection.ForeignKeyDefinition{}, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
|
return []connection.TriggerDefinition{}, nil
|
|
}
|
|
|
|
func (s *SQLiteDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
|
return []connection.ColumnDefinitionWithTable{}, nil
|
|
}
|