@@ -595,7 +595,7 @@ func (m *MariaDB) Close() error {
595
595
}
596
596
597
597
// Query the database with provided intermediate query representation
598
- func (m * MariaDB ) Query (ctx context.Context , sql knowledge.SQLTranslation , includeDataSourceInResults bool ) (* knowledge.GraphQueryResult , error ) {
598
+ func (m * MariaDB ) Query (ctx context.Context , sql knowledge.SQLTranslation ) (* knowledge.GraphQueryResult , error ) {
599
599
deadline , ok := ctx .Deadline ()
600
600
// If there is a deadline, we make sure the query stops right after it has been reached.
601
601
if ok {
@@ -610,11 +610,69 @@ func (m *MariaDB) Query(ctx context.Context, sql knowledge.SQLTranslation, inclu
610
610
}
611
611
612
612
res := new (knowledge.GraphQueryResult )
613
- res .Cursor = NewMariaDBCursor (rows , sql .ProjectionTypes , includeDataSourceInResults )
613
+ res .Cursor = NewMariaDBCursor (rows , sql .ProjectionTypes )
614
614
res .Projections = sql .ProjectionTypes
615
615
return res , nil
616
616
}
617
617
618
+ func (m * MariaDB ) GetAssetSources (ctx context.Context , ids []string ) (map [string ][]string , error ) {
619
+ stmt , err := m .db .PrepareContext (ctx , `
620
+ SELECT sources.name FROM sources
621
+ INNER JOIN assets_by_source ON sources.id = assets_by_source.source_id
622
+ WHERE asset_id = ?` )
623
+ if err != nil {
624
+ return nil , fmt .Errorf ("Unable to prepare statement for retrieving asset sources: %w" , err )
625
+ }
626
+ idsSet := make (map [string ][]string )
627
+ for _ , id := range ids {
628
+ row , err := stmt .QueryContext (ctx , id )
629
+ if err != nil {
630
+ return nil , fmt .Errorf ("Unable to retrieve sources for asset id %s: %w" , id , err )
631
+ }
632
+
633
+ idsSet [id ] = []string {}
634
+
635
+ var source string
636
+ for row .Next () {
637
+ err = row .Scan (& source )
638
+ if err != nil {
639
+ return nil , fmt .Errorf ("Unable to scan row of asset source: %w" , err )
640
+ }
641
+ idsSet [id ] = append (idsSet [id ], source )
642
+ }
643
+ }
644
+ return idsSet , nil
645
+ }
646
+
647
+ func (m * MariaDB ) GetRelationSources (ctx context.Context , ids []string ) (map [string ][]string , error ) {
648
+ stmt , err := m .db .PrepareContext (ctx , `
649
+ SELECT sources.name FROM sources
650
+ INNER JOIN relations_by_source ON sources.id = relations_by_source.source_id
651
+ WHERE relation_id = ?` )
652
+ if err != nil {
653
+ return nil , fmt .Errorf ("Unable to prepare statement for retrieving relation sources: %w" , err )
654
+ }
655
+ idsSet := make (map [string ][]string )
656
+ for _ , id := range ids {
657
+ row , err := stmt .QueryContext (ctx , id )
658
+ if err != nil {
659
+ return nil , fmt .Errorf ("Unable to retrieve sources for relation id %s: %w" , id , err )
660
+ }
661
+
662
+ idsSet [id ] = []string {}
663
+
664
+ var source string
665
+ for row .Next () {
666
+ err = row .Scan (& source )
667
+ if err != nil {
668
+ return nil , fmt .Errorf ("Unable to scan row of relation source: %w" , err )
669
+ }
670
+ idsSet [id ] = append (idsSet [id ], source )
671
+ }
672
+ }
673
+ return idsSet , nil
674
+ }
675
+
618
676
// SaveSuccessfulQuery log an entry to mark a successful query
619
677
func (m * MariaDB ) SaveSuccessfulQuery (ctx context.Context , cypher , sql string , duration time.Duration ) error {
620
678
_ , err := m .db .ExecContext (ctx , "INSERT INTO query_history (id, timestamp, query_cypher, query_sql, status, execution_time_ms) VALUES (NULL, CURRENT_TIMESTAMP(), ?, ?, 'SUCCESS', ?)" ,
@@ -706,16 +764,14 @@ func (m *MariaDB) ListSources(ctx context.Context) (map[string]string, error) {
706
764
type MariaDBCursor struct {
707
765
* sql.Rows
708
766
709
- Projections []knowledge.Projection
710
- IncludeDataSourceInResults bool
767
+ Projections []knowledge.Projection
711
768
}
712
769
713
770
// NewMariaDBCursor create a new instance of MariaDBCursor
714
- func NewMariaDBCursor (rows * sql.Rows , projections []knowledge.Projection , includeDataSourceInResults bool ) * MariaDBCursor {
771
+ func NewMariaDBCursor (rows * sql.Rows , projections []knowledge.Projection ) * MariaDBCursor {
715
772
return & MariaDBCursor {
716
- Rows : rows ,
717
- Projections : projections ,
718
- IncludeDataSourceInResults : includeDataSourceInResults ,
773
+ Rows : rows ,
774
+ Projections : projections ,
719
775
}
720
776
}
721
777
@@ -726,16 +782,6 @@ func (mc *MariaDBCursor) HasMore() bool {
726
782
727
783
// Read read one more item from the cursor
728
784
func (mc * MariaDBCursor ) Read (ctx context.Context , doc interface {}) error {
729
- type RelationWithIDAndSource struct {
730
- Source string `json:"source,omitempty"`
731
- knowledge.RelationWithID
732
- }
733
-
734
- type AssetWithIDAndSource struct {
735
- Source string `json:"source,omitempty"`
736
- knowledge.AssetWithID
737
- }
738
-
739
785
var err error
740
786
var fArr []string
741
787
@@ -789,11 +835,9 @@ func (mc *MariaDBCursor) Read(ctx context.Context, doc interface{}) error {
789
835
Key : fmt .Sprintf ("%v" , reflect .ValueOf (items [1 ])),
790
836
}
791
837
792
- awi := AssetWithIDAndSource {
793
- AssetWithID : knowledge.AssetWithID {
794
- ID : reflect .ValueOf (items [0 ]).String (),
795
- Asset : asset ,
796
- },
838
+ awi := knowledge.AssetWithID {
839
+ ID : reflect .ValueOf (items [0 ]).String (),
840
+ Asset : asset ,
797
841
}
798
842
output [i ] = awi
799
843
case knowledge .EdgeExprType :
@@ -803,12 +847,11 @@ func (mc *MariaDBCursor) Read(ctx context.Context, doc interface{}) error {
803
847
return fmt .Errorf ("Unable to get %d items to build an edge: %v" , itemCount , err )
804
848
}
805
849
806
- r := RelationWithIDAndSource {
807
- RelationWithID : knowledge.RelationWithID {
808
- From : reflect .ValueOf (items [1 ]).String (),
809
- To : reflect .ValueOf (items [2 ]).String (),
810
- Type : schema .RelationKeyType (fmt .Sprintf ("%v" , reflect .ValueOf (items [3 ]))),
811
- },
850
+ r := knowledge.RelationWithID {
851
+ ID : reflect .ValueOf (items [0 ]).String (),
852
+ From : reflect .ValueOf (items [1 ]).String (),
853
+ To : reflect .ValueOf (items [2 ]).String (),
854
+ Type : schema .RelationKeyType (fmt .Sprintf ("%v" , reflect .ValueOf (items [3 ]))),
812
855
}
813
856
output [i ] = r
814
857
case knowledge .PropertyExprType :
@@ -820,29 +863,6 @@ func (mc *MariaDBCursor) Read(ctx context.Context, doc interface{}) error {
820
863
}
821
864
}
822
865
823
- if mc .IncludeDataSourceInResults {
824
- for i , pt := range mc .Projections {
825
- switch pt .ExpressionType {
826
- case knowledge .NodeExprType :
827
- a := output [i ].(AssetWithIDAndSource )
828
- items , err := q .Get (1 )
829
- if err != nil {
830
- return fmt .Errorf ("Unable to get source property from queue: %w" , err )
831
- }
832
- a .Source = reflect .ValueOf (items [0 ]).String ()
833
- output [i ] = a
834
- case knowledge .EdgeExprType :
835
- r := output [i ].(RelationWithIDAndSource )
836
- items , err := q .Get (1 )
837
- if err != nil {
838
- return fmt .Errorf ("Unable to get source property from queue: %w" , err )
839
- }
840
- r .Source = reflect .ValueOf (items [0 ]).String ()
841
- output [i ] = r
842
- }
843
- }
844
- }
845
-
846
866
val .Elem ().Set (reflect .ValueOf (output ))
847
867
return nil
848
868
}
0 commit comments