This commit is contained in:
Lizandro Guarnizo
2026-06-02 10:35:37 -05:00
parent f8b352eb03
commit ba76754331
4 changed files with 992 additions and 21 deletions
+92
View File
@@ -300,6 +300,98 @@ func ListTables(conx models.ConxDb, database string) ([]string, error) {
return tables, nil
}
// ColumnInfo describe una columna de tabla.
type ColumnInfo struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable string `json:"nullable"`
Key string `json:"key"`
Default string `json:"default"`
}
// GetTableColumns devuelve la info de columnas de una tabla.
func GetTableColumns(conx models.ConxDb, database, table string) ([]ColumnInfo, error) {
driver := strings.ToLower(conx.TipoDb.Nombre)
db, err := openDynamicDB(conx)
if err != nil {
return nil, err
}
defer db.Close()
// Seleccionar base de datos si es necesario
if database != "" {
if strings.Contains(driver, "postgres") {
db2, err2 := openDynamicDBWithName(conx, database)
if err2 == nil {
db.Close()
db = db2
}
} else {
if _, err2 := db.Exec("USE " + quoteIdentifier(database, conx.TipoDb.Nombre)); err2 != nil {
// Intentar de todas formas
}
}
}
var query string
switch {
case strings.Contains(driver, "postgres"):
query = fmt.Sprintf(`SELECT column_name, data_type, is_nullable,
COALESCE(column_default,'') as column_default,
'' as column_key
FROM information_schema.columns
WHERE table_name = '%s' AND table_schema = 'public'
ORDER BY ordinal_position`, table)
case strings.Contains(driver, "mysql") || strings.Contains(driver, "mariadb"):
query = fmt.Sprintf("SHOW COLUMNS FROM `%s`", table)
case strings.Contains(driver, "sqlserver") || strings.Contains(driver, "mssql"):
query = fmt.Sprintf(`SELECT COLUMN_NAME as column_name, DATA_TYPE as data_type,
IS_NULLABLE as is_nullable, COALESCE(COLUMN_DEFAULT,'') as column_default,
'' as column_key
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '%s' ORDER BY ORDINAL_POSITION`, table)
default:
query = fmt.Sprintf("PRAGMA table_info('%s')", table)
}
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
var cols []ColumnInfo
driverLower := strings.ToLower(driver)
isMySQL := strings.Contains(driverLower, "mysql") || strings.Contains(driverLower, "mariadb")
for rows.Next() {
var ci ColumnInfo
if isMySQL {
var field, colType, null, key, extra string
var defaultVal *string
if err := rows.Scan(&field, &colType, &null, &key, &defaultVal, &extra); err != nil {
continue
}
ci.Name = field
ci.Type = colType
ci.Nullable = null
ci.Key = key
if defaultVal != nil {
ci.Default = *defaultVal
}
} else {
if err := rows.Scan(&ci.Name, &ci.Type, &ci.Nullable, &ci.Default, &ci.Key); err != nil {
continue
}
}
cols = append(cols, ci)
}
if cols == nil {
cols = []ColumnInfo{}
}
return cols, nil
}
// TestConnection verifica si la conexión es válida.
func TestDBConnection(conx models.ConxDb) error {
if isMongoDriver(strings.ToLower(conx.TipoDb.Nombre)) {