MCPcopy
hub / github.com/dosco/graphjin / executeForDatabaseRoots

Method executeForDatabaseRoots

core/database_join.go:765–889  ·  view source on GitHub ↗

executeForDatabaseRoots builds a sub-query for the specified root fields, compiles it using the target database's compilers, and executes it.

(ctx context.Context, dbName string, rootFields []string)

Source from the content-addressed store, hash-verified

763// executeForDatabaseRoots builds a sub-query for the specified root fields,
764// compiles it using the target database's compilers, and executes it.
765func (s *gstate) executeForDatabaseRoots(ctx context.Context, dbName string, rootFields []string) (json.RawMessage, error) {
766 // Get database context
767 var db *sql.DB
768 var qcodeCompiler *qcode.Compiler
769 var psqlCompiler *psql.Compiler
770
771 dbCtx, ok := s.gj.GetDatabase(dbName)
772 if !ok {
773 return nil, fmt.Errorf("database not found: %s", dbName)
774 }
775 db = dbCtx.db
776 qcodeCompiler = dbCtx.qcodeCompiler
777 psqlCompiler = dbCtx.psqlCompiler
778
779 // Block mutations on read-only databases (absolute, independent of roles)
780 if s.r.operation == qcode.QTMutation {
781 if dbConf, ok := s.gj.conf.Databases[dbName]; ok && dbConf.ReadOnly {
782 return nil, fmt.Errorf("mutations blocked: database %s is read-only", dbName)
783 }
784 }
785
786 // Build a sub-query with only this database's root fields
787 subQuery, err := s.buildDatabaseQuery(rootFields)
788 if err != nil {
789 return nil, fmt.Errorf("failed to build sub-query for %s: %w", dbName, err)
790 }
791
792 // Compile QCode
793 var vars map[string]json.RawMessage
794 if len(s.r.aschema) != 0 {
795 vars = s.r.aschema
796 } else {
797 vars = s.vmap
798 }
799
800 qc, err := qcodeCompiler.Compile(subQuery, vars, s.role, s.r.namespace)
801 if err != nil {
802 return nil, fmt.Errorf("qcode compile failed for %s: %w", dbName, err)
803 }
804
805 if dbCtx.nano != nil {
806 raw, err := s.renderNanoDBQuery(dbCtx, qc)
807 if err != nil {
808 return nil, fmt.Errorf("nanodb query failed for %s: %w", dbName, err)
809 }
810 encrypted, _, err := encryptResultFragment(raw, s.gj.printFormat, s.gj.encryptionKey)
811 if err != nil {
812 return nil, fmt.Errorf("encryption failed for %s: %w", dbName, err)
813 }
814 return encrypted, nil
815 }
816
817 // Compile SQL
818 var sqlBuf bytes.Buffer
819 md, err := psqlCompiler.Compile(&sqlBuf, qc)
820 if err != nil {
821 return nil, fmt.Errorf("sql compile failed for %s: %w", dbName, err)
822 }

Callers 1

executeParallelRootsMethod · 0.80

Calls 15

buildDatabaseQueryMethod · 0.95
CompileMethod · 0.95
renderNanoDBQueryMethod · 0.95
dbFragmentKeyMethod · 0.95
fragmentCacheGetMethod · 0.95
fragmentCacheSetMethod · 0.95
encryptResultFragmentFunction · 0.85
prepareQueryArgsForDBFunction · 0.85
scanJSONRowFunction · 0.85
GetDatabaseMethod · 0.65

Tested by

no test coverage detected