Update query_runner_service.go
This commit is contained in:
@@ -395,7 +395,19 @@ func mongoListCollections(c models.ConxDb, database string) ([]string, error) {
|
||||
return client.Database(database).ListCollectionNames(ctx, bson.M{})
|
||||
}
|
||||
|
||||
var mongoQueryRe = regexp.MustCompile(`(?s)^db\.(\w[\w\d_]*)\.([\w]+)\((.*)\)\s*$`)
|
||||
var mongoQueryRe = regexp.MustCompile(`(?s)^db\.(\w[\w\d_]*)\((.*)\)\s*$|^db\.(\w[\w\d_]*)\.(\w+)\((.*)\)\s*$`)
|
||||
|
||||
func mongoStripComments(text string) string {
|
||||
var lines []string
|
||||
for _, line := range strings.Split(text, "\n") {
|
||||
t := strings.TrimSpace(line)
|
||||
if t == "" || strings.HasPrefix(t, "--") || strings.HasPrefix(t, "//") || strings.HasPrefix(t, "#") {
|
||||
continue
|
||||
}
|
||||
lines = append(lines, t)
|
||||
}
|
||||
return strings.TrimSpace(strings.Join(lines, "\n"))
|
||||
}
|
||||
|
||||
func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult {
|
||||
start := time.Now()
|
||||
@@ -420,12 +432,30 @@ func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult
|
||||
}
|
||||
|
||||
func mongoRunQuery(ctx context.Context, db *mongo.Database, query string) (*QueryResult, error) {
|
||||
// Eliminar comentarios y líneas vacías
|
||||
query = mongoStripComments(query)
|
||||
|
||||
m := mongoQueryRe.FindStringSubmatch(query)
|
||||
if m == nil {
|
||||
return nil, fmt.Errorf("sintaxis no reconocida. Use: db.coleccion.metodo({...})")
|
||||
}
|
||||
collName, method, argsRaw := m[1], strings.ToLower(m[2]), strings.TrimSpace(m[3])
|
||||
if collName == "runCommand" {
|
||||
|
||||
var collName, method, argsRaw string
|
||||
// El regex tiene dos alternativas:
|
||||
// Alt 1 (m[1],m[2]): db.metodo(args) → para db.runCommand(...)
|
||||
// Alt 2 (m[3],m[4],m[5]): db.coleccion.metodo(args)
|
||||
if m[1] != "" {
|
||||
// db.runCommand(args)
|
||||
collName = m[1]
|
||||
method = strings.ToLower(m[1])
|
||||
argsRaw = strings.TrimSpace(m[2])
|
||||
} else {
|
||||
collName = m[3]
|
||||
method = strings.ToLower(m[4])
|
||||
argsRaw = strings.TrimSpace(m[5])
|
||||
}
|
||||
|
||||
if strings.ToLower(collName) == "runcommand" {
|
||||
return mongoRunCommand(ctx, db, argsRaw)
|
||||
}
|
||||
coll := db.Collection(collName)
|
||||
|
||||
Reference in New Issue
Block a user