@@ -11,7 +11,6 @@ import (
11
11
"os"
12
12
"os/signal"
13
13
"runtime"
14
- "strconv"
15
14
"strings"
16
15
"time"
17
16
@@ -20,7 +19,6 @@ import (
20
19
"github.com/charmbracelet/lipgloss"
21
20
"github.com/julien040/anyquery/controller/config/model"
22
21
"github.com/julien040/anyquery/namespace"
23
- "github.com/julien040/anyquery/other/sqlparser"
24
22
ws_tunnel "github.com/julien040/anyquery/other/websocket_tunnel/client"
25
23
"github.com/julien040/anyquery/rpc"
26
24
"github.com/mark3labs/mcp-go/mcp"
@@ -183,108 +181,22 @@ func executeQueryLLM(
183
181
query string ,
184
182
w io.Writer ,
185
183
) error {
186
- stmt , _ , err := namespace .GetQueryType (query )
187
- if err != nil { // If we can't determine the query type, we assume it's a SELECT
188
- stmt = sqlparser .StmtSelect
189
- }
190
-
191
- if stmt == sqlparser .StmtSelect {
192
- // Make a context that'll cancel the query after 40 seconds
193
- ctx , cancel := context .WithTimeout (context .Background (), 40 * time .Second )
194
- defer cancel ()
195
-
196
- rows , err := db .QueryContext (ctx , query )
197
- if err != nil {
198
- return fmt .Errorf ("failed to run the query: %w" , err )
199
- }
200
-
201
- defer rows .Close ()
202
-
203
- columns , err := rows .Columns ()
204
- if err != nil {
205
- return fmt .Errorf ("failed to get the columns: %w" , err )
206
- }
207
-
208
- // Write the columns, and table data as a markdown table
209
- w .Write ([]byte ("|" ))
210
- for _ , column := range columns {
211
- w .Write ([]byte (" " ))
212
- w .Write ([]byte (column ))
213
- w .Write ([]byte (" |" ))
214
- }
215
- w .Write ([]byte ("\n |" ))
216
- for _ , column := range columns {
217
- w .Write ([]byte (" " ))
218
- for i := 0 ; i < len (column ); i ++ {
219
- w .Write ([]byte ("-" ))
220
- }
221
- w .Write ([]byte (" |" ))
222
- }
223
- w .Write ([]byte ("\n " ))
224
-
225
- for rows .Next () {
226
- values := make ([]interface {}, len (columns ))
227
- for i := range values {
228
- values [i ] = new (interface {})
229
- }
230
-
231
- if err := rows .Scan (values ... ); err != nil {
232
- return fmt .Errorf ("failed to scan the row: %w" , err )
233
- }
234
-
235
- w .Write ([]byte ("|" ))
236
- for _ , value := range values {
237
- w .Write ([]byte (" " ))
238
- unknown , ok := value .(* interface {})
239
- if ok && unknown != nil && * unknown != nil {
240
- switch parsed := (* unknown ).(type ) {
241
- case []byte :
242
- w .Write ([]byte (fmt .Sprintf ("%x" , parsed )))
243
- case string :
244
- w .Write ([]byte (fmt .Sprintf ("%s" , parsed )))
245
- case int64 :
246
- w .Write ([]byte (strconv .FormatInt (parsed , 10 )))
247
- case float64 :
248
- w .Write ([]byte (strconv .FormatFloat (parsed , 'f' , - 1 , 64 )))
249
- case bool :
250
- if parsed {
251
- w .Write ([]byte ("true" ))
252
- } else {
253
- w .Write ([]byte ("false" ))
254
- }
255
- case time.Time :
256
- w .Write ([]byte (parsed .Format (time .RFC3339 )))
257
-
258
- default :
259
- w .Write ([]byte (fmt .Sprintf ("%v" , * unknown )))
260
- }
261
-
262
- } else {
263
- w .Write ([]byte ("NULL" ))
264
- }
265
-
266
- w .Write ([]byte (" |" ))
267
- }
268
- w .Write ([]byte ("\n " ))
269
- }
270
-
271
- if rows .Err () != nil {
272
- return fmt .Errorf ("failed to iterate over the rows %w" , rows .Err ())
273
- }
274
-
275
- } else {
276
- res , err := db .Exec (query )
277
- if err != nil {
278
- return fmt .Errorf ("failed to execute the query: %w" , err )
279
- }
280
-
281
- rowsAffected , err := res .RowsAffected ()
282
- if err != nil {
283
- return fmt .Errorf ("failed to get the number of rows affected: %w" , err )
284
- }
285
-
286
- w .Write ([]byte (fmt .Sprintf ("Query executed, %d rows affected" , rowsAffected )))
287
- }
184
+ sh := shell {
185
+ DB : db ,
186
+ OutputFileDesc : w ,
187
+ Middlewares : []middleware {
188
+ middlewareMySQL ,
189
+ middlewareFileQuery ,
190
+ middlewareQuery ,
191
+ },
192
+ Config : middlewareConfiguration {
193
+ "mysql" : true ,
194
+ "doNotModifyOutput" : true , // Do not modify the output, just keep w
195
+ },
196
+ }
197
+
198
+ // Run the query
199
+ sh .Run (query )
288
200
289
201
return nil
290
202
}
0 commit comments