@@ -12,7 +12,6 @@ import (
12
12
13
13
"github.com/cheggaaa/pb"
14
14
"github.com/clems4ever/go-graphkb/internal/knowledge"
15
- "github.com/clems4ever/go-graphkb/internal/query"
16
15
"github.com/clems4ever/go-graphkb/internal/schema"
17
16
"github.com/clems4ever/go-graphkb/internal/utils"
18
17
mapset "github.com/deckarep/golang-set"
@@ -111,6 +110,23 @@ CREATE TABLE IF NOT EXISTS importers (
111
110
return err
112
111
}
113
112
defer q .Close ()
113
+
114
+ // Create the table storing importers tokens
115
+ q , err = m .db .QueryContext (context .Background (), `
116
+ CREATE TABLE IF NOT EXISTS query_history (
117
+ id INTEGER AUTO_INCREMENT NOT NULL,
118
+ timestamp TIMESTAMP,
119
+ query_cypher TEXT NOT NULL,
120
+ query_sql TEXT NOT NULL,
121
+ execution_time_ms INT,
122
+ status ENUM('SUCCESS', 'FAILURE'),
123
+ error TEXT,
124
+ CONSTRAINT pk_history PRIMARY KEY (id)
125
+ )` )
126
+ if err != nil {
127
+ return err
128
+ }
129
+ defer q .Close ()
114
130
return nil
115
131
}
116
132
@@ -494,12 +510,7 @@ func (m *MariaDB) Close() error {
494
510
}
495
511
496
512
// Query the database with provided intermediate query representation
497
- func (m * MariaDB ) Query (ctx context.Context , query * query.QueryCypher ) (* knowledge.GraphQueryResult , error ) {
498
- sql , err := knowledge .NewSQLQueryTranslator ().Translate (query )
499
- if err != nil {
500
- return nil , err
501
- }
502
-
513
+ func (m * MariaDB ) Query (ctx context.Context , sql knowledge.SQLTranslation ) (* knowledge.GraphQueryResult , error ) {
503
514
deadline , ok := ctx .Deadline ()
504
515
// If there is a deadline, we make sure the query stops right after it has been reached.
505
516
if ok {
@@ -522,6 +533,24 @@ func (m *MariaDB) Query(ctx context.Context, query *query.QueryCypher) (*knowled
522
533
return res , nil
523
534
}
524
535
536
+ func (m * MariaDB ) SaveSuccessfulQuery (ctx context.Context , cypher , sql string , duration time.Duration ) error {
537
+ _ , err := m .db .ExecContext (ctx , "INSERT INTO query_history (id, timestamp, query_cypher, query_sql, status, execution_time_ms) VALUES (NULL, CURRENT_TIMESTAMP(), ?, ?, 'SUCCESS', ?)" ,
538
+ cypher , sql , duration )
539
+ if err != nil {
540
+ return err
541
+ }
542
+ return err
543
+ }
544
+
545
+ func (m * MariaDB ) SaveFailedQuery (ctx context.Context , cypher , sql string , err error ) error {
546
+ _ , inErr := m .db .ExecContext (ctx , "INSERT INTO query_history (id, timestamp, query_cypher, query_sql, status, error) VALUES (NULL, CURRENT_TIMESTAMP(), ?, ?, 'FAILURE', ?)" ,
547
+ cypher , sql , err .Error ())
548
+ if inErr != nil {
549
+ return inErr
550
+ }
551
+ return nil
552
+ }
553
+
525
554
// SaveSchema save the schema graph in database
526
555
func (m * MariaDB ) SaveSchema (ctx context.Context , sourceName string , schema schema.SchemaGraph ) error {
527
556
b , err := json .Marshal (schema )
0 commit comments