From f9e49bed1c9d3644b1bbd6395840ed6c8c5570c1 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Tue, 4 Aug 2026 20:31:58 +0800 Subject: [PATCH] feat(neo4j): replace JDBC agent with native Go driver --- .github/scripts/bump-agent-versions.mjs | 3 +- .github/scripts/bump-agent-versions.test.mjs | 13 + .github/scripts/label-pull-request.mjs | 1 + .github/scripts/label-pull-request.test.mjs | 4 +- .github/workflows/agents-release.yml | 48 +- .github/workflows/ci.yml | 10 +- agents/README.md | 2 +- agents/README.zh-CN.md | 2 +- agents/build.gradle | 2 +- agents/drivers/neo4j-go/README.md | 36 ++ agents/drivers/neo4j-go/driver.go | 270 +++++++++ agents/drivers/neo4j-go/go.mod | 5 + agents/drivers/neo4j-go/go.sum | 2 + agents/drivers/neo4j-go/integration_test.go | 69 +++ agents/drivers/neo4j-go/main.go | 561 ++++++++++++++++++ agents/drivers/neo4j-go/main_test.go | 142 +++++ agents/drivers/neo4j-go/metadata.go | 426 +++++++++++++ agents/drivers/neo4j-go/protocol_error.go | 149 +++++ agents/drivers/neo4j-go/query.go | 488 +++++++++++++++ agents/drivers/neo4j/build.gradle | 10 - .../java/com/dbx/agent/neo4j/Neo4jAgent.java | 318 ---------- .../com/dbx/agent/neo4j/Neo4jAgentTest.java | 276 --------- agents/metadata-constraint-coverage.tsv | 2 +- agents/scripts/validate_agents.py | 1 + agents/scripts/validate_agents_test.py | 3 +- agents/settings.gradle | 2 +- 26 files changed, 2230 insertions(+), 615 deletions(-) create mode 100644 agents/drivers/neo4j-go/README.md create mode 100644 agents/drivers/neo4j-go/driver.go create mode 100644 agents/drivers/neo4j-go/go.mod create mode 100644 agents/drivers/neo4j-go/go.sum create mode 100644 agents/drivers/neo4j-go/integration_test.go create mode 100644 agents/drivers/neo4j-go/main.go create mode 100644 agents/drivers/neo4j-go/main_test.go create mode 100644 agents/drivers/neo4j-go/metadata.go create mode 100644 agents/drivers/neo4j-go/protocol_error.go create mode 100644 agents/drivers/neo4j-go/query.go delete mode 100644 agents/drivers/neo4j/build.gradle delete mode 100644 agents/drivers/neo4j/src/main/java/com/dbx/agent/neo4j/Neo4jAgent.java delete mode 100644 agents/drivers/neo4j/src/test/java/com/dbx/agent/neo4j/Neo4jAgentTest.java diff --git a/.github/scripts/bump-agent-versions.mjs b/.github/scripts/bump-agent-versions.mjs index ea0686cd75..e2a582d3ca 100644 --- a/.github/scripts/bump-agent-versions.mjs +++ b/.github/scripts/bump-agent-versions.mjs @@ -49,11 +49,12 @@ const nativeDriverDirectories = { duckdb: "duckdb", oracle: "oracle-go", kingbase: "kingbase-go", + neo4j: "neo4j-go", vastbase: "vastbase-go", rabbitmq: "rabbitmq", tdengine: "tdengine", }; -const nativeDriverModules = new Set(["cassandra", "duckdb", "oracle", "xugu", "kingbase", "vastbase", "rabbitmq", "tdengine"]); +const nativeDriverModules = new Set(["cassandra", "duckdb", "oracle", "xugu", "kingbase", "neo4j", "vastbase", "rabbitmq", "tdengine"]); function resolveAgentModule(moduleName, { legacyStandaloneModules, moduleExists, readModuleFile }) { let checkDir = null; diff --git a/.github/scripts/bump-agent-versions.test.mjs b/.github/scripts/bump-agent-versions.test.mjs index dee7627262..ea133dd710 100644 --- a/.github/scripts/bump-agent-versions.test.mjs +++ b/.github/scripts/bump-agent-versions.test.mjs @@ -83,6 +83,19 @@ test("bumps Cassandra from its native Go source directory", () => { assert.deepEqual(result.nativeModules, ["cassandra"]); }); +test("bumps Neo4j from its native Go source directory", () => { + const result = evaluateAgentVersionBump({ + versions: { neo4j: "0.1.39" }, + changedFiles: ["agents/drivers/neo4j-go/main.go"], + moduleExists: (path) => path === "agents/drivers/neo4j-go", + readModuleFile: () => "", + }); + + assert.equal(result.versions.neo4j, "0.1.40"); + assert.deepEqual(result.javaModules, []); + assert.deepEqual(result.nativeModules, ["neo4j"]); +}); + test("builds a manually versioned module even without runtime file changes", () => { const result = evaluateAgentVersionBump({ versions: { duckdb: "0.1.1" }, diff --git a/.github/scripts/label-pull-request.mjs b/.github/scripts/label-pull-request.mjs index 36b1e7baad..6e09acec0e 100644 --- a/.github/scripts/label-pull-request.mjs +++ b/.github/scripts/label-pull-request.mjs @@ -53,6 +53,7 @@ const DRIVER_DATABASE_ALIASES = { "h2-legacy": "h2", kafka: "mq", "kingbase-go": "kingbase", + "neo4j-go": "neo4j", "oracle-10g": "oracle", "oracle-go": "oracle", "oracle-legacy": "oracle", diff --git a/.github/scripts/label-pull-request.test.mjs b/.github/scripts/label-pull-request.test.mjs index 1737ae8a0f..ba7f372ab6 100644 --- a/.github/scripts/label-pull-request.test.mjs +++ b/.github/scripts/label-pull-request.test.mjs @@ -20,6 +20,7 @@ const knownDatabaseTypes = new Set([ "mongodb", "mq", "mysql", + "neo4j", "oceanbase-oracle", "oracle", "postgres", @@ -57,12 +58,13 @@ test("maps agent and dialect paths to existing database types", () => { inferDatabaseTypes([ "agents/drivers/oracle-go/go.mod", "agents/drivers/cassandra-go/go.mod", + "agents/drivers/neo4j-go/go.mod", "agents/drivers/vastbase-go/go.mod", "agents/drivers/kafka/build.gradle", "plugins/dialects/postgresql.yaml", "plugins/dialects/oceanbase.yaml", ], knownDatabaseTypes), - ["cassandra", "mq", "oceanbase-oracle", "oracle", "postgres", "vastbase"], + ["cassandra", "mq", "neo4j", "oceanbase-oracle", "oracle", "postgres", "vastbase"], ); }); diff --git a/.github/workflows/agents-release.yml b/.github/workflows/agents-release.yml index 99f809c1c9..c5ea324149 100644 --- a/.github/workflows/agents-release.yml +++ b/.github/workflows/agents-release.yml @@ -396,6 +396,46 @@ jobs: name: vastbase-native path: "release-native/dbx-agent-vastbase-*" + build-neo4j-native: + needs: [bump-versions] + if: ${{ contains(fromJSON(needs.bump-versions.outputs.native_modules), 'neo4j') }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: "1.24.x" + - name: Test Neo4j native agent + working-directory: agents/drivers/neo4j-go + run: go test ./... + - name: Cross-compile Neo4j native agent + shell: bash + run: | + mkdir -p release-native + cd agents/drivers/neo4j-go + declare -A TARGETS=( + ["macos-aarch64"]="darwin/arm64" + ["macos-x64"]="darwin/amd64" + ["linux-aarch64"]="linux/arm64" + ["linux-x64"]="linux/amd64" + ["windows-aarch64"]="windows/arm64" + ["windows-x64"]="windows/amd64" + ) + for platform in "${!TARGETS[@]}"; do + IFS=/ read -r goos goarch <<< "${TARGETS[$platform]}" + output="../../../release-native/dbx-agent-neo4j-${platform}" + if [[ "$goos" == "windows" ]]; then + output="${output}.exe" + fi + echo "Building $platform ($goos/$goarch)" + CGO_ENABLED=0 GOOS="$goos" GOARCH="$goarch" go build -trimpath -ldflags="-s -w" -o "$output" . + done + ls -lh ../../../release-native + - uses: actions/upload-artifact@v4 + with: + name: neo4j-native + path: "release-native/dbx-agent-neo4j-*" + build-duckdb-native: needs: [bump-versions] if: ${{ contains(fromJSON(needs.bump-versions.outputs.native_modules), 'duckdb') }} @@ -735,7 +775,7 @@ jobs: retention-days: 1 release: - needs: [bump-versions, commit-versions, build-agents, build-oracle-native, build-xugu-native, build-rabbitmq-native, build-cassandra-native, build-kingbase-native, build-vastbase-native, build-duckdb-native, build-tdengine-native, build-jre, reuse-previous-assets] + needs: [bump-versions, commit-versions, build-agents, build-oracle-native, build-xugu-native, build-rabbitmq-native, build-cassandra-native, build-kingbase-native, build-vastbase-native, build-neo4j-native, build-duckdb-native, build-tdengine-native, build-jre, reuse-previous-assets] if: ${{ always() && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled') }} runs-on: ubuntu-latest steps: @@ -858,6 +898,7 @@ jobs: xugu) echo "虚谷 XuguDB" ;; rabbitmq) echo "RabbitMQ" ;; cassandra) echo "Apache Cassandra" ;; + neo4j) echo "Neo4j" ;; tdengine) echo "TDengine" ;; *) echo "$name" ;; esac @@ -922,7 +963,7 @@ jobs: [ -n "$DRIVERS" ] && DRIVERS="${DRIVERS},"$'\n' DRIVERS="${DRIVERS}$(generate_jar_entry "$name" "$label" "$f" "$jre_key" "$version" "$external_driver" "$native_json")" done - for name in oracle xugu kingbase vastbase duckdb rabbitmq cassandra tdengine; do + for name in oracle xugu kingbase vastbase neo4j duckdb rabbitmq cassandra tdengine; do version=$(get_module_version "$name") [ -f "release/dbx-agent-${name}-${version}.jar" ] && continue native_json=$(generate_native_platforms "$name" "$version") @@ -993,6 +1034,7 @@ jobs: xugu) echo "虚谷 XuguDB" ;; rabbitmq) echo "RabbitMQ" ;; cassandra) echo "Apache Cassandra" ;; + neo4j) echo "Neo4j" ;; tdengine) echo "TDengine" ;; *) echo "$name" ;; esac @@ -1020,6 +1062,8 @@ jobs: LOG_PATH="agents/drivers/vastbase-go/" elif [ "$name" = "cassandra" ]; then LOG_PATH="agents/drivers/cassandra-go/" + elif [ "$name" = "neo4j" ]; then + LOG_PATH="agents/drivers/neo4j-go/" elif [ -d "agents/drivers/$name" ]; then LOG_PATH="agents/drivers/$name/" else diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dae8f4571f..a63738e427 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -554,7 +554,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: "1.22.x" + go-version: "1.24.x" - name: Setup Rust uses: dtolnay/rust-toolchain@1.97.1 @@ -601,6 +601,10 @@ jobs: run: go test ./... working-directory: agents/drivers/vastbase-go + - name: Neo4j native agent tests + run: go test ./... + working-directory: agents/drivers/neo4j-go + - name: Oracle native agent build run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o /tmp/dbx-agent-oracle-linux-x64 . working-directory: agents/drivers/oracle-go @@ -621,6 +625,10 @@ jobs: run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o /tmp/dbx-agent-vastbase-linux-x64 . working-directory: agents/drivers/vastbase-go + - name: Neo4j native agent build + run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o /tmp/dbx-agent-neo4j-linux-x64 . + working-directory: agents/drivers/neo4j-go + - name: TDengine native agent build run: cargo build --manifest-path drivers/tdengine/Cargo.toml --locked --release --bin dbx-tdengine-driver diff --git a/agents/README.md b/agents/README.md index 024fb6b2c2..2056c8a2a9 100644 --- a/agents/README.md +++ b/agents/README.md @@ -33,7 +33,7 @@ Each agent runs as a standalone process and communicates with DBX via stdin/stdo | hive | Apache Hive | Hive JDBC | | db2 | IBM DB2 | DB2 JDBC | | informix | IBM Informix | Informix JDBC | -| neo4j | Neo4j | Neo4j JDBC | +| neo4j | Neo4j | Official Neo4j Go Driver native agent | | cassandra | Apache Cassandra 2.1+ | Apache cassandra-gocql-driver native agent | | bigquery | Google BigQuery | BigQuery JDBC | | kylin | Apache Kylin | Kylin JDBC | diff --git a/agents/README.zh-CN.md b/agents/README.zh-CN.md index ff6d950df7..9a9acc6d66 100644 --- a/agents/README.zh-CN.md +++ b/agents/README.zh-CN.md @@ -33,7 +33,7 @@ DBX 的 Agent 驱动 —— 通过 JDBC 和原生数据库驱动支持各种数 | hive | Apache Hive | Hive JDBC | | db2 | IBM DB2 | DB2 JDBC | | informix | IBM Informix | Informix JDBC | -| neo4j | Neo4j | Neo4j JDBC | +| neo4j | Neo4j | 官方 Neo4j Go Driver 原生 Agent | | cassandra | Apache Cassandra 2.1+ | Apache cassandra-gocql-driver 原生 Agent | | bigquery | Google BigQuery | BigQuery JDBC | | kylin | Apache Kylin | Kylin JDBC | diff --git a/agents/build.gradle b/agents/build.gradle index 69446e11f0..04413a0d8e 100644 --- a/agents/build.gradle +++ b/agents/build.gradle @@ -7,7 +7,7 @@ def legacyStandaloneProjects = ['mongodb', 'kafka', 'rocketmq'] as Set def pooledJdbcProjects = [ 'access', 'bigquery', 'dameng', 'databend', 'databricks', 'db2', 'exasol', 'firebird', 'gbase8a', 'gbase8s', 'goldendb', 'h2', 'h2-legacy', 'highgo', 'hive', - 'informix', 'iotdb', 'iris', 'kylin', 'neo4j', 'oceanbase-oracle', 'oscar', 'saphana', + 'informix', 'iotdb', 'iris', 'kylin', 'oceanbase-oracle', 'oscar', 'saphana', 'snowflake', 'spark', 'sqlserver-legacy', 'sundb', 'teradata', 'trino', 'uxdb', 'vertica', 'yashandb' ] as Set diff --git a/agents/drivers/neo4j-go/README.md b/agents/drivers/neo4j-go/README.md new file mode 100644 index 0000000000..ba391840cc --- /dev/null +++ b/agents/drivers/neo4j-go/README.md @@ -0,0 +1,36 @@ +# Neo4j native Agent + +This module replaces the Neo4j JDBC Agent with the official Neo4j Go Driver. + +## Runtime model + +- One Neo4j `Driver` connection pool is shared by DBX Agent sessions with the same connection identity. +- Each DBX Agent session is serialized independently, while different sessions can execute concurrently. +- Query and table-read paging keep the Neo4j session and result cursor open until the cursor is exhausted, closed, or cancelled. +- The driver uses a 1 MiB network read buffer. Bounded non-paged queries with an explicit numeric `LIMIT` can use `FetchAll`; unbounded queries keep batched fetching to bound memory use. +- The default Go scheduler parallelism is capped at four OS threads on high-core hosts. Set `GOMAXPROCS` or `DBX_AGENT_NEO4J_GOMAXPROCS` to override it. + +## Compatibility + +- Neo4j databases are discovered with `SHOW DATABASES`, with the configured database retained as a fallback for Community-compatible servers such as Memgraph. +- Node labels use `CALL db.labels()`. +- Properties use `db.schema.nodeTypeProperties()` with a sampled-node fallback. +- Index uniqueness is derived from `SHOW INDEXES ... owningConstraint`, which is compatible with Neo4j 5.x. +- Query row values retain the previous Agent behavior: scalar and graph values are returned as displayable strings, while null remains null. + +## Validation + +```bash +go test ./... +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" . +``` + +Run the optional live test against a real server: + +```bash +DBX_NEO4J_LIVE=1 \ +DBX_NEO4J_HOST=127.0.0.1 \ +DBX_NEO4J_USER=neo4j \ +DBX_NEO4J_PASSWORD=password \ +go test -run TestLiveNeo4jAgent +``` diff --git a/agents/drivers/neo4j-go/driver.go b/agents/drivers/neo4j-go/driver.go new file mode 100644 index 0000000000..5b9fec8930 --- /dev/null +++ b/agents/drivers/neo4j-go/driver.go @@ -0,0 +1,270 @@ +package main + +import ( + "context" + "crypto/tls" + "crypto/x509" + "errors" + "fmt" + "net" + "net/url" + "os" + "strconv" + "strings" + "time" + + neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j" + neo4jauth "github.com/neo4j/neo4j-go-driver/v6/neo4j/auth" + "github.com/neo4j/neo4j-go-driver/v6/neo4j/config" +) + +const ( + defaultNeo4jPort = 7687 + defaultDatabase = "neo4j" + defaultConnectTimeout = 15 * time.Second + optimizedReadBuffer = 1024 * 1024 + defaultConnectionLimit = 64 +) + +func newConnectionRuntime(params connectParams) (*connectionRuntime, error) { + driver, err := openDriver(params) + if err != nil { + return nil, err + } + ctx, cancel := context.WithTimeout(context.Background(), defaultConnectTimeout) + defer cancel() + if err := driver.VerifyConnectivity(ctx); err != nil { + _ = driver.Close(context.Background()) + return nil, err + } + return &connectionRuntime{driver: driver, params: params}, nil +} + +func openDriver(params connectParams) (neo4j.Driver, error) { + uri, err := buildNeo4jURI(params) + if err != nil { + return nil, err + } + authToken := neo4j.NoAuth() + if params.Username != "" || params.Password != "" { + authToken = neo4j.BasicAuth(params.Username, params.Password, "") + } + configurers := []func(*config.Config){func(driverConfig *config.Config) { + driverConfig.ReadBufferSize = optimizedReadBuffer + driverConfig.MaxConnectionPoolSize = defaultConnectionLimit + driverConfig.ConnectionAcquisitionTimeout = 30 * time.Second + driverConfig.SocketConnectTimeout = defaultConnectTimeout + driverConfig.MaxConnectionLifetime = time.Hour + driverConfig.TelemetryDisabled = true + }} + tlsConfigurer, err := neo4jTLSConfigurer(params) + if err != nil { + return nil, err + } + if tlsConfigurer != nil { + configurers = append(configurers, tlsConfigurer) + } + return neo4j.NewDriver(uri, authToken, configurers...) +} + +func neo4jTLSConfigurer(params connectParams) (func(*config.Config), error) { + var tlsConfig *tls.Config + if params.CACertPath != "" { + certificate, err := os.ReadFile(params.CACertPath) + if err != nil { + return nil, fmt.Errorf("read Neo4j CA certificate: %w", err) + } + roots, err := x509.SystemCertPool() + if err != nil || roots == nil { + roots = x509.NewCertPool() + } + if !roots.AppendCertsFromPEM(certificate) { + return nil, errors.New("Neo4j CA certificate contains no valid PEM certificate") + } + tlsConfig = &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots} + } + var clientCertificateProvider neo4jauth.ClientCertificateProvider + if params.ClientCertPath != "" || params.ClientKeyPath != "" { + if params.ClientCertPath == "" || params.ClientKeyPath == "" { + return nil, errors.New("both client certificate and client key are required") + } + provider, err := neo4jauth.NewStaticClientCertificateProvider(neo4jauth.ClientCertificate{ + CertFile: params.ClientCertPath, + KeyFile: params.ClientKeyPath, + }) + if err != nil { + return nil, fmt.Errorf("load Neo4j client certificate: %w", err) + } + clientCertificateProvider = provider + } + if tlsConfig == nil && clientCertificateProvider == nil { + return nil, nil + } + return func(driverConfig *config.Config) { + if tlsConfig != nil { + driverConfig.TlsConfig = tlsConfig + } + if clientCertificateProvider != nil { + driverConfig.ClientCertificateProvider = clientCertificateProvider + } + }, nil +} + +func buildNeo4jURI(params connectParams) (string, error) { + if value := strings.TrimSpace(params.ConnectionString); value != "" { + value = strings.TrimPrefix(value, "jdbc:") + parsed, err := url.Parse(value) + if err == nil && isNeo4jScheme(parsed.Scheme) && parsed.Host != "" { + parsed.User = nil + parsed.Path = "" + parsed.RawPath = "" + parsed.RawQuery = filterDriverURIQuery(parsed.Query()).Encode() + return parsed.String(), nil + } + } + host := strings.TrimSpace(params.Host) + if host == "" { + return "", errors.New("Neo4j host is required") + } + port := params.Port + if port <= 0 { + port = defaultNeo4jPort + } + scheme := "neo4j" + query := parseURLParams(params.URLParams) + if configured := strings.ToLower(strings.TrimSpace(query.Get("scheme"))); isNeo4jScheme(configured) { + scheme = configured + } else if params.SSL || queryBool(query, "ssl") || queryBool(query, "encrypted") || params.CACertPath != "" || params.ClientCertPath != "" { + scheme = "neo4j+s" + } + return (&url.URL{Scheme: scheme, Host: net.JoinHostPort(host, strconv.Itoa(port))}).String(), nil +} + +func isNeo4jScheme(value string) bool { + switch strings.ToLower(value) { + case "neo4j", "neo4j+s", "neo4j+ssc", "bolt", "bolt+s", "bolt+ssc": + return true + default: + return false + } +} + +func parseURLParams(value string) url.Values { + values := url.Values{} + for _, pair := range strings.FieldsFunc(value, func(char rune) bool { return char == '&' || char == ';' }) { + key, rawValue, found := strings.Cut(pair, "=") + key = strings.TrimSpace(key) + if key == "" { + continue + } + if !found { + rawValue = "" + } + values.Set(key, strings.TrimSpace(rawValue)) + } + return values +} + +func filterDriverURIQuery(values url.Values) url.Values { + result := url.Values{} + for key, items := range values { + switch strings.ToLower(key) { + case "database", "scheme", "ssl", "encrypted": + continue + default: + for _, item := range items { + result.Add(key, item) + } + } + } + return result +} + +func testConnection(params connectParams) error { + driver, err := openDriver(params) + if err != nil { + return err + } + defer driver.Close(context.Background()) + ctx, cancel := context.WithTimeout(context.Background(), defaultConnectTimeout) + defer cancel() + return driver.VerifyConnectivity(ctx) +} + +func (s *server) validateConnection() error { + if s.runtime == nil || s.runtime.driver == nil { + return errors.New("not connected") + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + return s.runtime.driver.VerifyConnectivity(ctx) +} + +func (s *server) databaseName(override string) string { + if value := strings.TrimSpace(override); value != "" { + return value + } + return configuredDatabase(s.params) +} + +func configuredDatabase(params connectParams) string { + if value := strings.TrimSpace(params.Database); value != "" { + return value + } + value := strings.TrimPrefix(strings.TrimSpace(params.ConnectionString), "jdbc:") + if parsed, err := url.Parse(value); err == nil { + if database := strings.TrimSpace(parsed.Query().Get("database")); database != "" { + return database + } + if database := strings.Trim(strings.TrimSpace(parsed.Path), "/"); database != "" { + return database + } + } + return defaultDatabase +} + +func queryBool(values url.Values, key string) bool { + switch strings.ToLower(strings.TrimSpace(values.Get(key))) { + case "1", "true", "yes", "on", "require", "required": + return true + default: + return false + } +} + +func (s *server) newSession(ctx context.Context, database string, accessMode neo4j.AccessMode, fetchSize int) neo4j.Session { + if fetchSize == 0 { + fetchSize = neo4j.FetchDefault + } + return s.runtime.driver.NewSession(ctx, neo4j.SessionConfig{ + DatabaseName: s.databaseName(database), + AccessMode: accessMode, + FetchSize: fetchSize, + }) +} + +func (s *server) connectionInfo() (map[string]any, error) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + serverInfo, err := s.runtime.driver.GetServerInfo(ctx) + if err != nil { + return nil, err + } + version := serverInfo.Agent() + return map[string]any{ + "database": s.databaseName(""), + "schema": "", + "username": s.params.Username, + "version": version, + "identifierQuote": "`", + "compatibilityMode": "cypher", + "databaseInfo": map[string]string{ + "productName": "Neo4j", + "productVersion": version, + "unquotedIdentifierCase": "mixed", + "quotedIdentifierCase": "mixed", + "driverName": "Neo4j Go Driver", + "driverVersion": "6.2.0", + }, + }, nil +} diff --git a/agents/drivers/neo4j-go/go.mod b/agents/drivers/neo4j-go/go.mod new file mode 100644 index 0000000000..336195d164 --- /dev/null +++ b/agents/drivers/neo4j-go/go.mod @@ -0,0 +1,5 @@ +module github.com/t8y2/dbx/agents/drivers/neo4j-go + +go 1.24 + +require github.com/neo4j/neo4j-go-driver/v6 v6.2.0 diff --git a/agents/drivers/neo4j-go/go.sum b/agents/drivers/neo4j-go/go.sum new file mode 100644 index 0000000000..b95e4e39e0 --- /dev/null +++ b/agents/drivers/neo4j-go/go.sum @@ -0,0 +1,2 @@ +github.com/neo4j/neo4j-go-driver/v6 v6.2.0 h1:5gxdBkCAY+jD/Tv6goqctLk3AdaVFU8MQU4t3n04SsU= +github.com/neo4j/neo4j-go-driver/v6 v6.2.0/go.mod h1:hzSTfNfM31p1uRSzL1F/BAYOgaiTarE6OAQBajfsm+I= diff --git a/agents/drivers/neo4j-go/integration_test.go b/agents/drivers/neo4j-go/integration_test.go new file mode 100644 index 0000000000..1e40e6d74b --- /dev/null +++ b/agents/drivers/neo4j-go/integration_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "encoding/json" + "os" + "strconv" + "testing" +) + +func TestLiveNeo4jAgent(t *testing.T) { + if os.Getenv("DBX_NEO4J_LIVE") != "1" { + t.Skip("set DBX_NEO4J_LIVE=1 to run against a real Neo4j server") + } + params := connectParams{ + Host: envOr("DBX_NEO4J_HOST", "127.0.0.1"), + Port: envIntOr("DBX_NEO4J_PORT", defaultNeo4jPort), + Database: envOr("DBX_NEO4J_DATABASE", defaultDatabase), + Username: envOr("DBX_NEO4J_USER", "neo4j"), + Password: os.Getenv("DBX_NEO4J_PASSWORD"), + URLParams: "scheme=" + envOr("DBX_NEO4J_SCHEME", "bolt"), + } + runtime, err := newConnectionRuntime(params) + if err != nil { + t.Fatal(err) + } + defer runtime.close() + server := newServer(runtime, params) + + databases, err := server.listDatabases() + if err != nil || len(databases) == 0 { + t.Fatalf("listDatabases() = %#v, %v", databases, err) + } + result, err := server.executeQuery(queryOptions{SQL: "RETURN 1 AS value", MaxRows: 10}) + if err != nil { + t.Fatal(err) + } + if len(result.Rows) != 1 || result.Rows[0][0] != "1" { + t.Fatalf("unexpected query result: %#v", result) + } + transaction, err := server.executeTransaction(rawParams(map[string]any{ + "statements": []string{"CREATE (n:DBXNeo4jGoAgentSmoke {createdAt: datetime()})", "MATCH (n:DBXNeo4jGoAgentSmoke) DELETE n"}, + })) + if err != nil || transaction.AffectedRows != 0 { + t.Fatalf("unexpected transaction result: %#v, %v", transaction, err) + } +} + +func rawParams(values map[string]any) map[string]json.RawMessage { + result := make(map[string]json.RawMessage, len(values)) + for key, value := range values { + data, _ := json.Marshal(value) + result[key] = data + } + return result +} + +func envOr(name, fallback string) string { + if value := os.Getenv(name); value != "" { + return value + } + return fallback +} + +func envIntOr(name string, fallback int) int { + if value, err := strconv.Atoi(os.Getenv(name)); err == nil && value > 0 { + return value + } + return fallback +} diff --git a/agents/drivers/neo4j-go/main.go b/agents/drivers/neo4j-go/main.go new file mode 100644 index 0000000000..a2783b923d --- /dev/null +++ b/agents/drivers/neo4j-go/main.go @@ -0,0 +1,561 @@ +package main + +import ( + "bufio" + "context" + "crypto/sha256" + "encoding/json" + "errors" + "fmt" + "os" + "runtime" + "strconv" + "strings" + "sync" + "time" + + neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j" +) + +const ( + protocolVersion = 2 + defaultMaxRows = 10000 + defaultPageSize = 1000 + legacyAgentSessionID = "__legacy__" + maxAgentSessions = 256 +) + +type request struct { + ID json.RawMessage `json:"id"` + Method string `json:"method"` + Params map[string]json.RawMessage `json:"params"` +} + +type response struct { + JSONRPC string `json:"jsonrpc,omitempty"` + ID json.RawMessage `json:"id,omitempty"` + Result any `json:"result,omitempty"` + Error *rpcError `json:"error,omitempty"` +} + +type connectParams struct { + Host string `json:"host"` + Port int `json:"port"` + Database string `json:"database"` + Username string `json:"username"` + Password string `json:"password"` + URLParams string `json:"url_params"` + ConnectionString string `json:"connection_string"` + SSL bool `json:"ssl"` + CACertPath string `json:"ca_cert_path"` + ClientCertPath string `json:"client_cert_path"` + ClientKeyPath string `json:"client_key_path"` +} + +type queryOptions struct { + SQL string `json:"sql"` + Database string `json:"database"` + Schema string `json:"schema"` + MaxRows int `json:"maxRows"` + FetchSize int `json:"fetchSize"` + TimeoutSecs int `json:"timeoutSecs"` +} + +type queryResult struct { + Columns []string `json:"columns"` + ColumnTypes []string `json:"column_types"` + Rows [][]any `json:"rows"` + AffectedRows int64 `json:"affected_rows"` + ExecutionTimeMS int64 `json:"execution_time_ms"` + Truncated bool `json:"truncated"` +} + +type queryPageResult struct { + Columns []string `json:"columns"` + ColumnTypes []string `json:"column_types"` + Rows [][]any `json:"rows"` + AffectedRows int64 `json:"affected_rows"` + ExecutionTimeMS int64 `json:"execution_time_ms"` + Truncated bool `json:"truncated"` + SessionID *string `json:"session_id"` + HasMore bool `json:"has_more"` +} + +type querySession struct { + session neo4j.Session + result neo4j.Result + ctx context.Context + cancel context.CancelFunc + columns []string + columnTypes []string + remaining int +} + +type connectionRuntime struct { + driver neo4j.Driver + params connectParams + references int + closed bool + mu sync.Mutex +} + +type server struct { + runtime *connectionRuntime + params connectParams + querySessions map[string]*querySession + nextSessionID uint64 + activeCancelMu sync.Mutex + activeCancel context.CancelFunc +} + +type agentSession struct { + server *server + runtimeKey string + mu sync.Mutex +} + +type runtimeServer struct { + mu sync.RWMutex + sessions map[string]*agentSession + runtimesMu sync.Mutex + runtimes map[string]*connectionRuntime +} + +func main() { + configureRuntimeParallelism() + runtime := newRuntimeServer() + encoder := json.NewEncoder(os.Stdout) + var encoderMu sync.Mutex + var requests sync.WaitGroup + fmt.Fprintln(os.Stdout, `{"ready":true}`) + + scanner := bufio.NewScanner(os.Stdin) + scanner.Buffer(make([]byte, 0, 64*1024), 512*1024*1024) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line == "" { + continue + } + var envelope request + if json.Unmarshal([]byte(line), &envelope) == nil && envelope.Method == "shutdown" { + requests.Wait() + result, _ := runtime.handleLine(line) + encoderMu.Lock() + _ = encoder.Encode(result) + encoderMu.Unlock() + return + } + requests.Add(1) + go func(line string) { + defer requests.Done() + result, _ := runtime.handleLine(line) + encoderMu.Lock() + defer encoderMu.Unlock() + if err := encoder.Encode(result); err != nil { + fmt.Fprintf(os.Stderr, "failed to write response: %v\n", err) + } + }(line) + } + requests.Wait() +} + +func configureRuntimeParallelism() { + if raw := strings.TrimSpace(os.Getenv("DBX_AGENT_NEO4J_GOMAXPROCS")); raw != "" { + if configured, err := strconv.Atoi(raw); err == nil && configured > 0 { + runtime.GOMAXPROCS(configured) + return + } + } + if strings.TrimSpace(os.Getenv("GOMAXPROCS")) != "" { + return + } + runtime.GOMAXPROCS(min(runtime.NumCPU(), 4)) +} + +func newRuntimeServer() *runtimeServer { + return &runtimeServer{ + sessions: map[string]*agentSession{}, + runtimes: map[string]*connectionRuntime{}, + } +} + +func (r *runtimeServer) handleLine(line string) (response, bool) { + var req request + if err := json.Unmarshal([]byte(line), &req); err != nil { + return errorResponse(nil, "", "", err), false + } + if len(req.ID) == 0 { + req.ID = json.RawMessage("1") + } + result, shutdown, err := r.dispatch(req.Method, req.Params) + if err != nil { + return errorResponse(req.ID, req.Method, stringParam(req.Params, "agentSessionId"), err), false + } + return response{JSONRPC: "2.0", ID: req.ID, Result: result}, shutdown +} + +func (r *runtimeServer) dispatch(method string, params map[string]json.RawMessage) (any, bool, error) { + switch method { + case "handshake": + return handshakeResult(), false, nil + case "open_session": + id := stringParam(params, "agentSessionId") + if id == "" { + return nil, false, errors.New("agentSessionId is required") + } + var connection connectParams + if err := decodeParams(params, &connection); err != nil { + return nil, false, err + } + return map[string]bool{"ok": true}, false, r.openSession(id, connection) + case "close_session": + return map[string]bool{"ok": true}, false, r.closeSession(stringParam(params, "agentSessionId")) + case "validate_session": + session, err := r.session(stringParam(params, "agentSessionId")) + if err != nil { + return nil, false, err + } + session.mu.Lock() + defer session.mu.Unlock() + return map[string]bool{"ok": true}, false, session.server.validateConnection() + case "cancel_session": + session, err := r.session(stringParam(params, "agentSessionId")) + if err != nil { + return nil, false, err + } + session.server.cancelActiveQuery() + return map[string]bool{"ok": true}, false, nil + case "test_connection": + var connection connectParams + if err := decodeParams(params, &connection); err != nil { + return nil, false, err + } + return map[string]bool{"ok": true}, false, testConnection(connection) + case "connect": + var connection connectParams + if err := decodeParams(params, &connection); err != nil { + return nil, false, err + } + _ = r.closeSession(legacyAgentSessionID) + return map[string]bool{"ok": true}, false, r.openSession(legacyAgentSessionID, connection) + case "disconnect": + return map[string]bool{"ok": true}, false, r.closeSession(legacyAgentSessionID) + case "shutdown": + return map[string]bool{"ok": true}, true, r.closeAllSessions() + default: + id := stringParam(params, "agentSessionId") + if id == "" { + id = legacyAgentSessionID + } + session, err := r.session(id) + if err != nil { + return nil, false, err + } + session.mu.Lock() + defer session.mu.Unlock() + return session.server.dispatch(method, params) + } +} + +func (r *runtimeServer) openSession(id string, params connectParams) error { + r.mu.Lock() + if _, exists := r.sessions[id]; exists { + r.mu.Unlock() + return fmt.Errorf("agent session already exists: %s", id) + } + if len(r.sessions) >= maxAgentSessions { + r.mu.Unlock() + return fmt.Errorf("agent session limit reached: %d", maxAgentSessions) + } + r.mu.Unlock() + + runtime, key, err := r.acquireRuntime(params) + if err != nil { + return err + } + server := newServer(runtime, params) + if err := server.validateConnection(); err != nil { + r.releaseRuntime(key) + return err + } + + r.mu.Lock() + defer r.mu.Unlock() + if _, exists := r.sessions[id]; exists { + r.releaseRuntime(key) + return fmt.Errorf("agent session already exists: %s", id) + } + r.sessions[id] = &agentSession{server: server, runtimeKey: key} + return nil +} + +func (r *runtimeServer) session(id string) (*agentSession, error) { + r.mu.RLock() + session := r.sessions[id] + r.mu.RUnlock() + if session == nil { + return nil, fmt.Errorf("agent session not found: %s", id) + } + return session, nil +} + +func (r *runtimeServer) closeSession(id string) error { + r.mu.Lock() + session := r.sessions[id] + delete(r.sessions, id) + r.mu.Unlock() + if session == nil { + return nil + } + session.server.cancelActiveQuery() + session.mu.Lock() + err := session.server.disconnect() + session.mu.Unlock() + r.releaseRuntime(session.runtimeKey) + return err +} + +func (r *runtimeServer) closeAllSessions() error { + r.mu.RLock() + ids := make([]string, 0, len(r.sessions)) + for id := range r.sessions { + ids = append(ids, id) + } + r.mu.RUnlock() + var firstErr error + for _, id := range ids { + if err := r.closeSession(id); err != nil && firstErr == nil { + firstErr = err + } + } + r.runtimesMu.Lock() + runtimes := r.runtimes + r.runtimes = map[string]*connectionRuntime{} + r.runtimesMu.Unlock() + for _, runtime := range runtimes { + if err := runtime.close(); err != nil && firstErr == nil { + firstErr = err + } + } + return firstErr +} + +func (r *runtimeServer) acquireRuntime(params connectParams) (*connectionRuntime, string, error) { + key := connectionRuntimeKey(params) + r.runtimesMu.Lock() + defer r.runtimesMu.Unlock() + runtime := r.runtimes[key] + if runtime == nil { + var err error + runtime, err = newConnectionRuntime(params) + if err != nil { + return nil, "", err + } + r.runtimes[key] = runtime + } + runtime.references++ + return runtime, key, nil +} + +func (r *runtimeServer) releaseRuntime(key string) { + if key == "" { + return + } + r.runtimesMu.Lock() + runtime := r.runtimes[key] + shouldClose := false + if runtime != nil && runtime.references > 0 { + runtime.references-- + } + if runtime != nil && runtime.references == 0 { + delete(r.runtimes, key) + shouldClose = true + } + r.runtimesMu.Unlock() + if shouldClose { + _ = runtime.close() + } +} + +func connectionRuntimeKey(params connectParams) string { + data, _ := json.Marshal(params) + digest := sha256.Sum256(data) + return fmt.Sprintf("%x", digest[:]) +} + +func (runtime *connectionRuntime) close() error { + runtime.mu.Lock() + if runtime.closed { + runtime.mu.Unlock() + return nil + } + runtime.closed = true + driver := runtime.driver + runtime.mu.Unlock() + if driver == nil { + return nil + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + return driver.Close(ctx) +} + +func newServer(runtime *connectionRuntime, params connectParams) *server { + return &server{runtime: runtime, params: params, querySessions: map[string]*querySession{}} +} + +func handshakeResult() map[string]any { + return map[string]any{ + "protocolVersion": protocolVersion, + "agentProtocolVersion": protocolVersion, + "capabilities": []string{ + "connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", + "structured_error_v1", "multi_session", + }, + } +} + +func (s *server) dispatch(method string, params map[string]json.RawMessage) (any, bool, error) { + switch method { + case "handshake": + return handshakeResult(), false, nil + case "validate_connection": + return map[string]bool{"ok": true}, false, s.validateConnection() + case "connection_info": + result, err := s.connectionInfo() + return result, false, err + case "list_databases": + result, err := s.listDatabases() + return result, false, err + case "list_schemas": + return []string{}, false, nil + case "list_tables": + result, err := s.listTables(metadataListConstraintsFromParams(params)) + return result, false, err + case "get_table_comment": + return nil, false, nil + case "list_objects": + result, err := s.listObjects(metadataListConstraintsFromParams(params)) + return result, false, err + case "list_data_types": + return neo4jDataTypes(), false, nil + case "completion_assistant_search_v1": + result, err := s.completionAssistantSearch(params) + return result, false, err + case "get_columns": + result, err := s.getColumns(stringParam(params, "table")) + return result, false, err + case "list_indexes": + result, err := s.listIndexes(stringParam(params, "table")) + return result, false, err + case "list_foreign_keys", "list_triggers", "list_constraints", "list_partitions", "list_subpartitions": + return []any{}, false, nil + case "get_object_source", "get_table_ddl": + return nil, false, nil + case "get_explain_info": + return map[string]any{"plan": "", "has_actual_stats": false}, false, nil + case "execute_query": + var options queryOptions + if err := decodeParams(params, &options); err != nil { + return nil, false, err + } + result, err := s.executeQuery(options) + return result, false, err + case "execute_query_page", "start_table_read": + var options queryOptions + if err := decodeParams(params, &options); err != nil { + return nil, false, err + } + result, err := s.executeQueryPage(options, intParam(params, "pageSize")) + return result, false, err + case "fetch_query_page", "fetch_table_read_page": + result, err := s.fetchQueryPage(stringParam(params, "sessionId"), intParam(params, "pageSize")) + return result, false, err + case "close_query_session", "close_table_read_session": + return s.closeQuerySession(stringParam(params, "sessionId")), false, nil + case "execute_transaction": + result, err := s.executeTransaction(params) + return result, false, err + case "execute_batch": + result, err := s.executeBatch(params) + return result, false, err + case "disconnect": + return map[string]bool{"ok": true}, false, s.disconnect() + case "shutdown": + return map[string]bool{"ok": true}, true, s.disconnect() + default: + return nil, false, fmt.Errorf("unknown method: %s", method) + } +} + +func (s *server) beginOperation(timeoutSecs int) (context.Context, context.CancelFunc) { + ctx := context.Background() + var cancel context.CancelFunc + if timeoutSecs > 0 { + ctx, cancel = context.WithTimeout(ctx, time.Duration(timeoutSecs)*time.Second) + } else { + ctx, cancel = context.WithCancel(ctx) + } + s.activeCancelMu.Lock() + s.activeCancel = cancel + s.activeCancelMu.Unlock() + return ctx, cancel +} + +func (s *server) endOperation(cancel context.CancelFunc) { + cancel() + s.activeCancelMu.Lock() + s.activeCancel = nil + s.activeCancelMu.Unlock() +} + +func (s *server) cancelActiveQuery() { + s.activeCancelMu.Lock() + cancel := s.activeCancel + s.activeCancelMu.Unlock() + if cancel != nil { + cancel() + } +} + +func (s *server) disconnect() error { + s.cancelActiveQuery() + return s.closeAllQuerySessions() +} + +func decodeParams(params map[string]json.RawMessage, target any) error { + data, err := json.Marshal(params) + if err != nil { + return err + } + return json.Unmarshal(data, target) +} + +func stringParam(params map[string]json.RawMessage, key string) string { + if params == nil { + return "" + } + var result string + _ = json.Unmarshal(params[key], &result) + return result +} + +func intParam(params map[string]json.RawMessage, key string) int { + if params == nil { + return 0 + } + var result int + _ = json.Unmarshal(params[key], &result) + return result +} + +func stringSliceParam(params map[string]json.RawMessage, key string) []string { + if params == nil { + return []string{} + } + var result []string + if json.Unmarshal(params[key], &result) != nil || result == nil { + return []string{} + } + return result +} diff --git a/agents/drivers/neo4j-go/main_test.go b/agents/drivers/neo4j-go/main_test.go new file mode 100644 index 0000000000..85ad576234 --- /dev/null +++ b/agents/drivers/neo4j-go/main_test.go @@ -0,0 +1,142 @@ +package main + +import ( + "context" + "encoding/json" + "reflect" + "testing" + + neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j" + neo4jdb "github.com/neo4j/neo4j-go-driver/v6/neo4j/db" +) + +func TestHandshakeAdvertisesNativeCapabilities(t *testing.T) { + result, shutdown, err := newRuntimeServer().dispatch("handshake", nil) + if err != nil || shutdown { + t.Fatalf("unexpected handshake: shutdown=%t err=%v", shutdown, err) + } + capabilities := result.(map[string]any)["capabilities"].([]string) + want := []string{ + "connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", + "structured_error_v1", "multi_session", + } + if !reflect.DeepEqual(capabilities, want) { + t.Fatalf("unexpected capabilities: %#v", capabilities) + } +} + +func TestHandleLineClassifiesMissingSession(t *testing.T) { + response, _ := newRuntimeServer().handleLine( + `{"jsonrpc":"2.0","id":7,"method":"validate_session","params":{"agentSessionId":"missing"}}`, + ) + if response.Error == nil || response.Error.Data == nil { + t.Fatalf("expected structured error: %#v", response) + } + if response.Error.Data.Stage != "validate" || response.Error.Data.Category != "protocol" { + t.Fatalf("unexpected error classification: %#v", response.Error.Data) + } +} + +func TestConnectionRuntimeIdentityIncludesCredentials(t *testing.T) { + first := connectionRuntimeKey(connectParams{Host: "localhost", Username: "neo4j", Password: "one"}) + second := connectionRuntimeKey(connectParams{Host: "localhost", Username: "neo4j", Password: "two"}) + if first == second { + t.Fatal("runtime identities must not share drivers across credentials") + } +} + +func TestBuildNeo4jURI(t *testing.T) { + for _, testCase := range []struct { + name string + params connectParams + want string + }{ + {name: "default", params: connectParams{Host: "127.0.0.1", Port: 7687}, want: "neo4j://127.0.0.1:7687"}, + {name: "ssl", params: connectParams{Host: "db.example.com", Port: 7687, SSL: true}, want: "neo4j+s://db.example.com:7687"}, + {name: "ssl url param", params: connectParams{Host: "db.example.com", Port: 7687, URLParams: "encrypted=true"}, want: "neo4j+s://db.example.com:7687"}, + {name: "direct", params: connectParams{Host: "db", Port: 7687, URLParams: "scheme=bolt"}, want: "bolt://db:7687"}, + {name: "jdbc migration", params: connectParams{ConnectionString: "jdbc:neo4j://user:secret@db:7687?database=movies"}, want: "neo4j://db:7687"}, + } { + t.Run(testCase.name, func(t *testing.T) { + got, err := buildNeo4jURI(testCase.params) + if err != nil { + t.Fatal(err) + } + if got != testCase.want { + t.Fatalf("buildNeo4jURI() = %q, want %q", got, testCase.want) + } + }) + } +} + +func TestConfiguredDatabaseUsesConnectionString(t *testing.T) { + if got := configuredDatabase(connectParams{ConnectionString: "jdbc:neo4j://db:7687?database=movies"}); got != "movies" { + t.Fatalf("configuredDatabase() = %q, want movies", got) + } + if got := configuredDatabase(connectParams{ConnectionString: "neo4j://db:7687/archive"}); got != "archive" { + t.Fatalf("configuredDatabase() = %q, want archive", got) + } +} + +func TestMetadataWindowAndFiltering(t *testing.T) { + values := []string{"alpha", "beta", "gamma"} + if got := applyMetadataWindow(values, 1, 1); !reflect.DeepEqual(got, []string{"beta"}) { + t.Fatalf("unexpected metadata window: %#v", got) + } + if !metadataNameMatches("Employee", "ploy") || metadataNameMatches("Employee", "customer") { + t.Fatal("unexpected metadata name matching") + } +} + +func TestNormalizeQueryValuesPreservesLegacyStringRows(t *testing.T) { + node := neo4j.Node{ElementId: "4:abc:7", Labels: []string{"Person"}, Props: map[string]any{"name": "Ada", "age": int64(37)}} + if got := normalizeQueryValue(int64(42)); got != "42" { + t.Fatalf("unexpected integer normalization: %#v", got) + } + if got := normalizeQueryValue(node); got != `(:Person {"age":37,"name":"Ada"})` { + t.Fatalf("unexpected node normalization: %#v", got) + } +} + +func TestClassifyNeo4jErrors(t *testing.T) { + syntax := classifyRPCError("execute_query", "session-1", &neo4jdb.Neo4jError{ + Code: "Neo.ClientError.Statement.SyntaxError", Msg: "invalid input", + }) + if syntax.Data.Category != "sql" || syntax.Data.SQLState == "" { + t.Fatalf("unexpected syntax classification: %#v", syntax) + } + canceled := classifyRPCError("execute_query", "session-1", context.Canceled) + if canceled.Data.Category != "canceled" || canceled.Data.SessionDisposition != "quarantine" { + t.Fatalf("unexpected cancellation classification: %#v", canceled) + } +} + +func TestDecodeQueryOptions(t *testing.T) { + params := map[string]json.RawMessage{ + "sql": json.RawMessage(`"RETURN 1"`), + "maxRows": json.RawMessage(`100`), + "timeoutSecs": json.RawMessage(`5`), + } + var options queryOptions + if err := decodeParams(params, &options); err != nil { + t.Fatal(err) + } + if options.SQL != "RETURN 1" || options.MaxRows != 100 || options.TimeoutSecs != 5 { + t.Fatalf("unexpected query options: %#v", options) + } +} + +func TestBoundedQueryUsesFetchAll(t *testing.T) { + options := queryOptions{SQL: "MATCH (n) RETURN n LIMIT 10000", MaxRows: 10000} + if got := effectiveFetchSize(options); got != neo4j.FetchAll { + t.Fatalf("effectiveFetchSize() = %d, want FetchAll", got) + } + options.SQL = "MATCH (n) RETURN n" + if got := effectiveFetchSize(options); got == neo4j.FetchAll { + t.Fatal("unbounded query must not use FetchAll") + } + options = queryOptions{SQL: "MATCH (n) RETURN n LIMIT 100000", MaxRows: 100000} + if got := effectiveFetchSize(options); got == neo4j.FetchAll { + t.Fatal("large bounded query must keep batched fetching") + } +} diff --git a/agents/drivers/neo4j-go/metadata.go b/agents/drivers/neo4j-go/metadata.go new file mode 100644 index 0000000000..df87452e1f --- /dev/null +++ b/agents/drivers/neo4j-go/metadata.go @@ -0,0 +1,426 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "sort" + "strings" + + neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j" +) + +var neo4jTypes = []string{ + "Any", "Boolean", "Date", "DateTime", "Duration", "Float", "Integer", "List", "LocalDateTime", + "LocalTime", "Map", "Node", "Null", "Path", "Point", "Relationship", "String", "Time", "Vector", +} + +type databaseInfo struct { + Name string `json:"name"` +} + +type tableInfo struct { + Name string `json:"name"` + TableType string `json:"table_type"` + Comment *string `json:"comment"` + ParentSchema *string `json:"parent_schema,omitempty"` + ParentName *string `json:"parent_name,omitempty"` +} + +type objectInfo struct { + Name string `json:"name"` + ObjectType string `json:"object_type"` + Schema string `json:"schema"` + Comment *string `json:"comment"` + Valid *bool `json:"valid,omitempty"` +} + +type columnInfo struct { + Name string `json:"name"` + DataType string `json:"data_type"` + IsNullable bool `json:"is_nullable"` + ColumnDefault *string `json:"column_default"` + IsPrimaryKey bool `json:"is_primary_key"` + Extra *string `json:"extra"` + Comment *string `json:"comment"` + NumericPrecision *int `json:"numeric_precision"` + NumericScale *int `json:"numeric_scale"` + CharacterMaximumLength *int `json:"character_maximum_length"` +} + +type indexInfo struct { + Name string `json:"name"` + Columns []string `json:"columns"` + IsUnique bool `json:"is_unique"` + IsPrimary bool `json:"is_primary"` + Filter *string `json:"filter"` + IndexType *string `json:"index_type"` + IncludedColumns []string `json:"included_columns"` + Comment *string `json:"comment"` +} + +type metadataListConstraints struct { + Filter string + Limit int + Offset int + ObjectTypes []string +} + +type completionAssistantRequest struct { + ConnectionID string `json:"connection_id"` + Database string `json:"database"` + Schema string `json:"schema"` + ObjectKinds []string `json:"object_kinds"` + Mask string `json:"mask"` + CaseSensitive bool `json:"case_sensitive"` + GlobalSearch bool `json:"global_search"` + MaxResults int `json:"max_results"` + ParentSchema string `json:"parent_schema"` + ParentName string `json:"parent_name"` + MatchMode string `json:"match_mode"` +} + +type completionAssistantCandidate struct { + Name string `json:"name"` + Kind string `json:"kind"` + Database *string `json:"database"` + Schema *string `json:"schema"` + ParentSchema *string `json:"parent_schema"` + ParentName *string `json:"parent_name"` + Comment *string `json:"comment"` + DataType *string `json:"data_type"` +} + +type completionAssistantResponse struct { + Candidates []completionAssistantCandidate `json:"candidates"` + Incomplete bool `json:"incomplete"` + FallbackUsed bool `json:"fallback_used"` +} + +func neo4jDataTypes() []string { + return append([]string(nil), neo4jTypes...) +} + +func (s *server) listDatabases() ([]databaseInfo, error) { + records, err := s.runMetadataQuery( + "SHOW DATABASES YIELD name RETURN name ORDER BY name", + nil, + ) + if err != nil { + return []databaseInfo{{Name: s.databaseName("")}}, nil + } + seen := map[string]struct{}{} + result := make([]databaseInfo, 0, len(records)) + for _, record := range records { + name := recordString(record, "name") + if name == "" { + continue + } + if _, exists := seen[name]; exists { + continue + } + seen[name] = struct{}{} + result = append(result, databaseInfo{Name: name}) + } + if len(result) == 0 { + result = append(result, databaseInfo{Name: s.databaseName("")}) + } + sort.Slice(result, func(left, right int) bool { return result[left].Name < result[right].Name }) + return result, nil +} + +func (s *server) listTables(constraints metadataListConstraints) ([]tableInfo, error) { + records, err := s.runMetadataQuery( + "CALL db.labels() YIELD label RETURN label ORDER BY label", + nil, + ) + if err != nil { + return nil, err + } + result := make([]tableInfo, 0, len(records)) + for _, record := range records { + name := recordString(record, "label") + if name == "" || !metadataNameMatches(name, constraints.Filter) || !acceptsNodeType(constraints.ObjectTypes) { + continue + } + result = append(result, tableInfo{Name: name, TableType: "TABLE"}) + } + return applyMetadataWindow(result, constraints.Offset, constraints.Limit), nil +} + +func (s *server) listObjects(constraints metadataListConstraints) ([]objectInfo, error) { + tables, err := s.listTables(constraints) + if err != nil { + return nil, err + } + result := make([]objectInfo, 0, len(tables)) + for _, table := range tables { + result = append(result, objectInfo{Name: table.Name, ObjectType: "TABLE", Schema: ""}) + } + return result, nil +} + +func (s *server) getColumns(label string) ([]columnInfo, error) { + if strings.TrimSpace(label) == "" { + return []columnInfo{}, nil + } + records, err := s.runMetadataQuery( + "CALL db.schema.nodeTypeProperties() "+ + "YIELD nodeLabels, propertyName, propertyTypes, mandatory "+ + "WHERE $label IN nodeLabels "+ + "RETURN propertyName, propertyTypes, mandatory ORDER BY propertyName", + map[string]any{"label": label}, + ) + if err != nil { + records, err = s.runMetadataQuery( + fmt.Sprintf( + "MATCH (n:%s) UNWIND keys(n) AS propertyName "+ + "WITH DISTINCT propertyName RETURN propertyName, ['Unknown'] AS propertyTypes, false AS mandatory "+ + "ORDER BY propertyName LIMIT 10000", + quoteCypherIdentifier(label), + ), + nil, + ) + if err != nil { + return nil, err + } + } + result := make([]columnInfo, 0, len(records)) + seen := map[string]struct{}{} + for _, record := range records { + name := recordString(record, "propertyName") + if name == "" { + continue + } + if _, exists := seen[name]; exists { + continue + } + seen[name] = struct{}{} + types := recordStringSlice(record, "propertyTypes") + dataType := "Unknown" + if len(types) > 0 { + dataType = strings.Join(types, " | ") + } + result = append(result, columnInfo{ + Name: name, + DataType: dataType, + IsNullable: !recordBool(record, "mandatory"), + }) + } + sort.Slice(result, func(left, right int) bool { return result[left].Name < result[right].Name }) + return result, nil +} + +func (s *server) listIndexes(label string) ([]indexInfo, error) { + if strings.TrimSpace(label) == "" { + return []indexInfo{}, nil + } + records, err := s.runMetadataQuery( + "SHOW INDEXES YIELD name, type, labelsOrTypes, properties, owningConstraint "+ + "WHERE $label IN labelsOrTypes "+ + "RETURN name, type, properties, owningConstraint ORDER BY name", + map[string]any{"label": label}, + ) + if err != nil { + return nil, err + } + result := make([]indexInfo, 0, len(records)) + for _, record := range records { + indexType := recordString(record, "type") + result = append(result, indexInfo{ + Name: recordString(record, "name"), + Columns: recordStringSlice(record, "properties"), + IsUnique: recordString(record, "owningConstraint") != "", + IndexType: stringPtr(indexType), + IncludedColumns: []string{}, + }) + } + return result, nil +} + +func (s *server) completionAssistantSearch(params map[string]json.RawMessage) (completionAssistantResponse, error) { + var request completionAssistantRequest + if err := decodeParams(params, &request); err != nil { + return completionAssistantResponse{}, err + } + limit := request.MaxResults + if limit <= 0 { + limit = 100 + } + kinds := map[string]bool{} + for _, kind := range request.ObjectKinds { + kinds[strings.ToLower(kind)] = true + } + includeTables := len(kinds) == 0 || kinds["table"] || kinds["node"] + includeColumns := len(kinds) == 0 || kinds["column"] || kinds["property"] + constraints := metadataListConstraints{Filter: request.Mask} + tables, err := s.listTables(constraints) + if err != nil { + return completionAssistantResponse{}, err + } + database := s.databaseName(request.Database) + candidates := make([]completionAssistantCandidate, 0, min(limit+1, len(tables))) + for _, table := range tables { + if includeTables && completionNameMatches(table.Name, request) { + candidates = append(candidates, completionAssistantCandidate{ + Name: table.Name, Kind: "table", Database: stringPtr(database), + }) + } + if includeColumns && (request.ParentName == "" || request.ParentName == table.Name) { + columns, columnErr := s.getColumns(table.Name) + if columnErr != nil { + continue + } + for _, column := range columns { + if !completionNameMatches(column.Name, request) { + continue + } + parent := table.Name + dataType := column.DataType + candidates = append(candidates, completionAssistantCandidate{ + Name: column.Name, Kind: "column", Database: stringPtr(database), ParentName: &parent, DataType: &dataType, + }) + if len(candidates) > limit { + return completionAssistantResponse{Candidates: candidates[:limit], Incomplete: true}, nil + } + } + } + if len(candidates) > limit { + return completionAssistantResponse{Candidates: candidates[:limit], Incomplete: true}, nil + } + } + return completionAssistantResponse{Candidates: candidates, Incomplete: false}, nil +} + +func (s *server) runMetadataQuery(cypher string, params map[string]any) ([]*neo4j.Record, error) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + session := s.newSession(ctx, "", neo4j.AccessModeRead, 256) + defer session.Close(ctx) + result, err := session.Run(ctx, cypher, params) + if err != nil { + return nil, err + } + records, err := result.Collect(ctx) + if err != nil { + return nil, err + } + return records, nil +} + +func metadataListConstraintsFromParams(params map[string]json.RawMessage) metadataListConstraints { + return metadataListConstraints{ + Filter: stringParam(params, "filter"), + Limit: intParam(params, "limit"), + Offset: intParam(params, "offset"), + ObjectTypes: stringSliceParam(params, "object_types"), + } +} + +func metadataNameMatches(name, filter string) bool { + return filter == "" || strings.Contains(strings.ToLower(name), strings.ToLower(filter)) +} + +func acceptsNodeType(objectTypes []string) bool { + if len(objectTypes) == 0 { + return true + } + for _, objectType := range objectTypes { + switch strings.ToLower(objectType) { + case "table", "node", "label": + return true + } + } + return false +} + +func applyMetadataWindow[T any](values []T, offset, limit int) []T { + if offset < 0 { + offset = 0 + } + if offset >= len(values) { + return []T{} + } + values = values[offset:] + if limit > 0 && limit < len(values) { + values = values[:limit] + } + return values +} + +func completionNameMatches(name string, request completionAssistantRequest) bool { + mask := request.Mask + if mask == "" { + return true + } + left, right := name, mask + if !request.CaseSensitive { + left, right = strings.ToLower(left), strings.ToLower(right) + } + switch strings.ToLower(request.MatchMode) { + case "exact": + return left == right + case "prefix": + return strings.HasPrefix(left, right) + default: + return strings.Contains(left, right) + } +} + +func recordValue(record *neo4j.Record, key string) any { + if record == nil { + return nil + } + value, found := record.Get(key) + if !found { + return nil + } + return value +} + +func recordString(record *neo4j.Record, key string) string { + value := recordValue(record, key) + if value == nil { + return "" + } + if text, ok := value.(string); ok { + return text + } + return fmt.Sprint(value) +} + +func recordStringSlice(record *neo4j.Record, key string) []string { + value := recordValue(record, key) + switch typed := value.(type) { + case []string: + return append([]string(nil), typed...) + case []any: + result := make([]string, 0, len(typed)) + for _, item := range typed { + if item != nil { + result = append(result, fmt.Sprint(item)) + } + } + return result + case nil: + return []string{} + default: + return []string{fmt.Sprint(typed)} + } +} + +func recordBool(record *neo4j.Record, key string) bool { + value, _ := recordValue(record, key).(bool) + return value +} + +func quoteCypherIdentifier(value string) string { + return "`" + strings.ReplaceAll(value, "`", "``") + "`" +} + +func stringPtr(value string) *string { + if value == "" { + return nil + } + return &value +} diff --git a/agents/drivers/neo4j-go/protocol_error.go b/agents/drivers/neo4j-go/protocol_error.go new file mode 100644 index 0000000000..dfee3641ae --- /dev/null +++ b/agents/drivers/neo4j-go/protocol_error.go @@ -0,0 +1,149 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "strings" + + neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j" + neo4jdb "github.com/neo4j/neo4j-go-driver/v6/neo4j/db" +) + +type rpcError struct { + Code int `json:"code"` + Message string `json:"message"` + Data *rpcErrorData `json:"data,omitempty"` +} + +type rpcErrorData struct { + Category string `json:"category"` + Retryable bool `json:"retryable"` + SessionDisposition string `json:"sessionDisposition"` + Stage string `json:"stage"` + ContractVersion int `json:"contractVersion"` + OperationOutcome string `json:"operationOutcome"` + SQLState string `json:"sqlState,omitempty"` + ExceptionClass string `json:"exceptionClass,omitempty"` + AgentSessionID string `json:"agentSessionId,omitempty"` +} + +func errorResponse(id json.RawMessage, method, agentSessionID string, err error) response { + return response{JSONRPC: "2.0", ID: id, Error: classifyRPCError(method, agentSessionID, err)} +} + +func classifyRPCError(method, agentSessionID string, err error) *rpcError { + stage := rpcErrorStage(method) + data := &rpcErrorData{ + Category: "protocol", + Retryable: false, + SessionDisposition: "keep", + Stage: stage, + ContractVersion: 1, + OperationOutcome: rpcOperationOutcome(stage), + ExceptionClass: safeRPCDiagnostic(fmt.Sprintf("%T", err), 160), + AgentSessionID: strings.TrimSpace(agentSessionID), + } + + var databaseError *neo4jdb.Neo4jError + if errors.As(err, &databaseError) { + data.SQLState = safeRPCDiagnostic(databaseError.Code, 160) + data.Retryable = neo4j.IsRetryable(err) + code := strings.ToLower(databaseError.Code) + switch { + case strings.Contains(code, ".security."): + data.Category = "connection" + data.Retryable = stage == "connect" || stage == "validate" + case strings.Contains(code, ".transienterror."), strings.Contains(code, "databaseunavailable"): + data.Category = "resource" + data.Retryable = true + case strings.Contains(code, ".statement."), strings.Contains(code, ".schema."), strings.Contains(code, ".procedure."): + data.Category = "sql" + default: + data.Category = "sql" + } + } else if errors.Is(err, context.Canceled) { + data.Category = "canceled" + data.SessionDisposition = "quarantine" + } else if errors.Is(err, context.DeadlineExceeded) || isTimeoutError(err) { + data.Category = "timeout" + data.SessionDisposition = "quarantine" + } else if isConnectionError(err) { + data.Category = "connection" + data.Retryable = stage == "connect" || stage == "validate" + if stage != "connect" { + data.SessionDisposition = "quarantine" + } + } + + return &rpcError{Code: -1, Message: err.Error(), Data: data} +} + +func rpcErrorStage(method string) string { + switch method { + case "connect", "open_session", "test_connection": + return "connect" + case "validate_connection", "validate_session": + return "validate" + case "cancel_session": + return "cancel" + case "close_session", "disconnect", "close_query_session", "close_table_read_session", "shutdown": + return "close" + case "fetch_query_page", "fetch_table_read_page": + return "fetch" + case "handshake", "": + return "request" + default: + return "execute" + } +} + +func rpcOperationOutcome(stage string) string { + switch stage { + case "request", "connect", "validate": + return "not_started" + default: + return "unknown" + } +} + +func isTimeoutError(err error) bool { + var timeout interface{ Timeout() bool } + return errors.As(err, &timeout) && timeout.Timeout() +} + +func isConnectionError(err error) bool { + if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) { + return true + } + var networkError *net.OpError + if errors.As(err, &networkError) { + return true + } + lower := strings.ToLower(err.Error()) + for _, marker := range []string{ + "connection refused", "connection reset", "broken pipe", "connection closed", "connection lost", + "unexpected eof", "no route to host", "no servers available", "routing table", + } { + if strings.Contains(lower, marker) { + return true + } + } + return false +} + +func safeRPCDiagnostic(value string, maxLength int) string { + var result strings.Builder + for _, char := range value { + if result.Len() >= maxLength { + break + } + if char >= 0x21 && char <= 0x7e { + result.WriteRune(char) + } + } + return result.String() +} diff --git a/agents/drivers/neo4j-go/query.go b/agents/drivers/neo4j-go/query.go new file mode 100644 index 0000000000..ad69a00505 --- /dev/null +++ b/agents/drivers/neo4j-go/query.go @@ -0,0 +1,488 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "reflect" + "regexp" + "strconv" + "strings" + "time" + + neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j" +) + +func (s *server) executeQuery(options queryOptions) (queryResult, error) { + started := time.Now() + cypher := trimCypher(options.SQL) + if cypher == "" { + return emptyQueryResult(time.Since(started)), nil + } + ctx, cancel := s.beginOperation(options.TimeoutSecs) + defer s.endOperation(cancel) + session := s.newSession(ctx, options.Database, neo4j.AccessModeWrite, effectiveFetchSize(options)) + defer session.Close(ctx) + result, err := session.Run(ctx, cypher, nil) + if err != nil { + return queryResult{}, err + } + maxRows := effectiveMaxRows(options.MaxRows) + rows, columns, columnTypes, truncated, err := readResultPage(ctx, result, maxRows) + if err != nil { + return queryResult{}, err + } + return queryResult{ + Columns: columns, + ColumnTypes: columnTypes, + Rows: rows, + AffectedRows: 0, + ExecutionTimeMS: time.Since(started).Milliseconds(), + Truncated: truncated, + }, nil +} + +func (s *server) executeQueryPage(options queryOptions, pageSize int) (queryPageResult, error) { + started := time.Now() + cypher := trimCypher(options.SQL) + if cypher == "" { + return emptyQueryPage(time.Since(started)), nil + } + if pageSize <= 0 { + pageSize = defaultPageSize + } + maxRows := effectiveMaxRows(options.MaxRows) + if pageSize > maxRows { + pageSize = maxRows + } + ctx, cancel := s.beginOperation(options.TimeoutSecs) + session := s.newSession(ctx, options.Database, neo4j.AccessModeWrite, effectivePageFetchSize(options, pageSize)) + result, err := session.Run(ctx, cypher, nil) + if err != nil { + _ = session.Close(ctx) + s.endOperation(cancel) + return queryPageResult{}, err + } + rows, columns, columnTypes, hasMore, err := readResultPage(ctx, result, pageSize) + if err != nil { + _ = session.Close(ctx) + s.endOperation(cancel) + return queryPageResult{}, err + } + remaining := maxRows - len(rows) + if !hasMore || remaining <= 0 { + _ = session.Close(ctx) + s.endOperation(cancel) + return queryPageResult{ + Columns: columns, ColumnTypes: columnTypes, Rows: rows, + ExecutionTimeMS: time.Since(started).Milliseconds(), Truncated: hasMore && remaining <= 0, + HasMore: false, + }, nil + } + id := s.nextQuerySessionID() + s.querySessions[id] = &querySession{ + session: session, result: result, ctx: ctx, cancel: cancel, + columns: columns, columnTypes: columnTypes, remaining: remaining, + } + return queryPageResult{ + Columns: columns, ColumnTypes: columnTypes, Rows: rows, + ExecutionTimeMS: time.Since(started).Milliseconds(), SessionID: &id, HasMore: true, + }, nil +} + +func (s *server) fetchQueryPage(id string, pageSize int) (queryPageResult, error) { + started := time.Now() + query := s.querySessions[id] + if query == nil { + return queryPageResult{}, fmt.Errorf("query session not found: %s", id) + } + if pageSize <= 0 { + pageSize = defaultPageSize + } + if pageSize > query.remaining { + pageSize = query.remaining + } + rows, _, _, hasMore, err := readResultPage(query.ctx, query.result, pageSize) + if err != nil { + s.closeQuerySession(id) + return queryPageResult{}, err + } + query.remaining -= len(rows) + truncated := hasMore && query.remaining <= 0 + if !hasMore || query.remaining <= 0 { + s.closeQuerySession(id) + return queryPageResult{ + Columns: query.columns, ColumnTypes: query.columnTypes, Rows: rows, + ExecutionTimeMS: time.Since(started).Milliseconds(), Truncated: truncated, HasMore: false, + }, nil + } + return queryPageResult{ + Columns: query.columns, ColumnTypes: query.columnTypes, Rows: rows, + ExecutionTimeMS: time.Since(started).Milliseconds(), SessionID: &id, HasMore: true, + }, nil +} + +func (s *server) closeQuerySession(id string) map[string]bool { + query := s.querySessions[id] + delete(s.querySessions, id) + if query == nil { + return map[string]bool{"ok": true} + } + _ = query.session.Close(query.ctx) + s.endOperation(query.cancel) + return map[string]bool{"ok": true} +} + +func (s *server) closeAllQuerySessions() error { + ids := make([]string, 0, len(s.querySessions)) + for id := range s.querySessions { + ids = append(ids, id) + } + for _, id := range ids { + s.closeQuerySession(id) + } + return nil +} + +func (s *server) nextQuerySessionID() string { + s.nextSessionID++ + return fmt.Sprintf("neo4j-query-%d", s.nextSessionID) +} + +func (s *server) executeTransaction(params map[string]json.RawMessage) (queryResult, error) { + started := time.Now() + statements := stringSliceParam(params, "statements") + ctx, cancel := s.beginOperation(intParam(params, "timeoutSecs")) + defer s.endOperation(cancel) + session := s.newSession(ctx, stringParam(params, "database"), neo4j.AccessModeWrite, neo4j.FetchDefault) + defer session.Close(ctx) + transaction, err := session.BeginTransaction(ctx) + if err != nil { + return queryResult{}, err + } + committed := false + defer func() { + if !committed { + _ = transaction.Rollback(ctx) + } + _ = transaction.Close(ctx) + }() + for _, statement := range statements { + cypher := trimCypher(statement) + if cypher == "" { + continue + } + result, runErr := transaction.Run(ctx, cypher, nil) + if runErr != nil { + return queryResult{}, runErr + } + if _, consumeErr := result.Consume(ctx); consumeErr != nil { + return queryResult{}, consumeErr + } + } + if err := transaction.Commit(ctx); err != nil { + return queryResult{}, err + } + committed = true + return emptyQueryResult(time.Since(started)), nil +} + +func (s *server) executeBatch(params map[string]json.RawMessage) (queryResult, error) { + started := time.Now() + for _, statement := range stringSliceParam(params, "statements") { + result, err := s.executeQuery(queryOptions{ + SQL: statement, Database: stringParam(params, "database"), TimeoutSecs: intParam(params, "timeoutSecs"), + }) + if err != nil { + return queryResult{}, err + } + _ = result + } + return emptyQueryResult(time.Since(started)), nil +} + +func readResultPage(ctx context.Context, result neo4j.Result, limit int) ([][]any, []string, []string, bool, error) { + if limit < 0 { + limit = 0 + } + columns, err := result.Keys() + if err != nil { + return nil, nil, nil, false, err + } + rows := make([][]any, 0, min(limit, 1024)) + columnTypes := make([]string, len(columns)) + for index := range columnTypes { + columnTypes[index] = "Unknown" + } + for len(rows) < limit && result.Next(ctx) { + record := result.Record() + if len(rows) == 0 { + columnTypes = recordColumnTypes(record, len(columns)) + } else { + refineColumnTypes(columnTypes, record) + } + rows = append(rows, normalizeRecord(record, len(columns))) + } + if err := result.Err(); err != nil { + return nil, nil, nil, false, err + } + hasMore := false + if result.IsOpen() { + hasMore = result.Peek(ctx) + if err := result.Err(); err != nil { + return nil, nil, nil, false, err + } + } + return rows, columns, columnTypes, hasMore, nil +} + +func normalizeRecord(record *neo4j.Record, width int) []any { + row := make([]any, width) + if record == nil { + return row + } + for index := 0; index < width && index < len(record.Values); index++ { + row[index] = normalizeQueryValue(record.Values[index]) + } + return row +} + +func normalizeQueryValue(value any) any { + if value == nil { + return nil + } + switch typed := value.(type) { + case string: + return typed + case bool: + return strconv.FormatBool(typed) + case int: + return strconv.Itoa(typed) + case int64: + return strconv.FormatInt(typed, 10) + case float64: + return strconv.FormatFloat(typed, 'g', -1, 64) + case []byte: + return string(typed) + case neo4j.Node: + return formatNode(typed) + case neo4j.Relationship: + return formatRelationship(typed) + case neo4j.Path: + return formatJSONValue(typed) + case fmt.Stringer: + return typed.String() + default: + return formatJSONValue(typed) + } +} + +func formatNode(node neo4j.Node) string { + labels := "" + if len(node.Labels) > 0 { + labels = ":" + strings.Join(node.Labels, ":") + } + properties := formatJSONValue(node.Props) + if properties == "{}" { + return "(" + labels + ")" + } + return "(" + labels + " " + properties + ")" +} + +func formatRelationship(relationship neo4j.Relationship) string { + properties := formatJSONValue(relationship.Props) + if properties == "{}" { + return "[:" + relationship.Type + "]" + } + return "[:" + relationship.Type + " " + properties + "]" +} + +func formatJSONValue(value any) string { + normalized := normalizeNestedValue(value) + data, err := json.Marshal(normalized) + if err == nil { + return string(data) + } + return fmt.Sprint(value) +} + +func normalizeNestedValue(value any) any { + switch typed := value.(type) { + case nil, bool, string, int, int64, float64: + return typed + case []any: + result := make([]any, len(typed)) + for index, item := range typed { + result[index] = normalizeNestedValue(item) + } + return result + case map[string]any: + result := make(map[string]any, len(typed)) + for key, item := range typed { + result[key] = normalizeNestedValue(item) + } + return result + case neo4j.Node: + return map[string]any{"elementId": typed.ElementId, "labels": typed.Labels, "properties": normalizeNestedValue(typed.Props)} + case neo4j.Relationship: + return map[string]any{ + "elementId": typed.ElementId, "startElementId": typed.StartElementId, "endElementId": typed.EndElementId, + "type": typed.Type, "properties": normalizeNestedValue(typed.Props), + } + case neo4j.Path: + nodes := make([]any, len(typed.Nodes)) + for index, node := range typed.Nodes { + nodes[index] = normalizeNestedValue(node) + } + relationships := make([]any, len(typed.Relationships)) + for index, relationship := range typed.Relationships { + relationships[index] = normalizeNestedValue(relationship) + } + return map[string]any{"nodes": nodes, "relationships": relationships} + case fmt.Stringer: + return typed.String() + default: + reflection := reflect.ValueOf(value) + if reflection.IsValid() && (reflection.Kind() == reflect.Slice || reflection.Kind() == reflect.Array) { + result := make([]any, reflection.Len()) + for index := 0; index < reflection.Len(); index++ { + result[index] = normalizeNestedValue(reflection.Index(index).Interface()) + } + return result + } + return fmt.Sprint(value) + } +} + +func recordColumnTypes(record *neo4j.Record, width int) []string { + types := make([]string, width) + for index := range types { + types[index] = "Unknown" + } + if record == nil { + return types + } + for index := 0; index < width && index < len(record.Values); index++ { + types[index] = neo4jTypeName(record.Values[index]) + } + return types +} + +func refineColumnTypes(types []string, record *neo4j.Record) { + if record == nil { + return + } + for index := 0; index < len(types) && index < len(record.Values); index++ { + if types[index] == "Unknown" || types[index] == "Null" { + if discovered := neo4jTypeName(record.Values[index]); discovered != "Null" { + types[index] = discovered + } + } + } +} + +func neo4jTypeName(value any) string { + switch value.(type) { + case nil: + return "Null" + case bool: + return "Boolean" + case int, int8, int16, int32, int64: + return "Integer" + case float32, float64: + return "Float" + case string: + return "String" + case []byte, []any: + return "List" + case map[string]any: + return "Map" + case neo4j.Node: + return "Node" + case neo4j.Relationship: + return "Relationship" + case neo4j.Path: + return "Path" + case neo4j.Point2D, neo4j.Point3D: + return "Point" + case neo4j.Date: + return "Date" + case neo4j.LocalDateTime: + return "LocalDateTime" + case neo4j.LocalTime: + return "LocalTime" + case neo4j.Time: + return "Time" + case neo4j.Duration: + return "Duration" + case time.Time: + return "DateTime" + default: + return "Any" + } +} + +func effectiveMaxRows(configured int) int { + if configured > 0 { + return configured + } + return defaultMaxRows +} + +func effectiveFetchSize(options queryOptions) int { + if options.FetchSize > 0 { + return options.FetchSize + } + maxRows := effectiveMaxRows(options.MaxRows) + if maxRows >= 1000 && queryHasBoundedLimit(options.SQL, min(maxRows, defaultMaxRows)) { + return neo4j.FetchAll + } + switch { + case maxRows <= 100: + return 100 + case maxRows <= 1000: + return 1000 + default: + return 4096 + } +} + +var numericLimitPattern = regexp.MustCompile(`(?i)\bLIMIT\s+([0-9]+)\b`) + +func queryHasBoundedLimit(cypher string, maxRows int) bool { + matches := numericLimitPattern.FindAllStringSubmatch(cypher, -1) + if len(matches) == 0 { + return false + } + limit, err := strconv.Atoi(matches[len(matches)-1][1]) + return err == nil && limit > 0 && limit <= maxRows +} + +func effectivePageFetchSize(options queryOptions, pageSize int) int { + if options.FetchSize > 0 { + return options.FetchSize + } + if pageSize <= 100 { + return 100 + } + if pageSize <= 1000 { + return 1000 + } + return 4096 +} + +func emptyQueryResult(duration time.Duration) queryResult { + return queryResult{ + Columns: []string{}, ColumnTypes: []string{}, Rows: [][]any{}, ExecutionTimeMS: duration.Milliseconds(), + } +} + +func emptyQueryPage(duration time.Duration) queryPageResult { + return queryPageResult{ + Columns: []string{}, ColumnTypes: []string{}, Rows: [][]any{}, ExecutionTimeMS: duration.Milliseconds(), + } +} + +func trimCypher(value string) string { + return strings.TrimRight(strings.TrimSpace(value), "; \t\r\n") +} diff --git a/agents/drivers/neo4j/build.gradle b/agents/drivers/neo4j/build.gradle deleted file mode 100644 index 232f5958d5..0000000000 --- a/agents/drivers/neo4j/build.gradle +++ /dev/null @@ -1,10 +0,0 @@ -dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'org.neo4j:neo4j-jdbc-full-bundle:6.10.0' -} - -tasks.named('shadowJar') { - manifest { - attributes('Agent-Label': 'Neo4j', 'Main-Class': 'com.dbx.agent.neo4j.Neo4jAgent') - } -} diff --git a/agents/drivers/neo4j/src/main/java/com/dbx/agent/neo4j/Neo4jAgent.java b/agents/drivers/neo4j/src/main/java/com/dbx/agent/neo4j/Neo4jAgent.java deleted file mode 100644 index 91f253589c..0000000000 --- a/agents/drivers/neo4j/src/main/java/com/dbx/agent/neo4j/Neo4jAgent.java +++ /dev/null @@ -1,318 +0,0 @@ -package com.dbx.agent.neo4j; - -import com.dbx.agent.AbstractJdbcAgent; -import com.dbx.agent.ColumnInfo; -import com.dbx.agent.ConnectParams; -import com.dbx.agent.DatabaseInfo; -import com.dbx.agent.ExecuteQueryOptions; -import com.dbx.agent.ForeignKeyInfo; -import com.dbx.agent.IndexInfo; -import com.dbx.agent.JdbcExecutor; -import com.dbx.agent.MultiSessionJsonRpcServer; -import com.dbx.agent.QueryResult; -import com.dbx.agent.StandardJdbcMetadata; -import com.dbx.agent.TableInfo; -import com.dbx.agent.TransactionExecutor; -import com.dbx.agent.TriggerInfo; - -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Locale; -import java.util.Set; - -public class Neo4jAgent extends AbstractJdbcAgent { - @Override - protected String driverClass() { - return "org.neo4j.jdbc.Neo4jDriver"; - } - - @Override - public String setSchemaSQL(String schema) { - return ""; - } - - @Override - protected String buildJdbcUrl(ConnectParams params) { - return buildNeo4jUrl(params); - } - - private static String buildNeo4jUrl(ConnectParams params) { - StringBuilder url = new StringBuilder(); - url.append("jdbc:neo4j://").append(params.getHost()).append(":").append(params.getPort()); - String database = params.getDatabase(); - if (database != null && !database.trim().isEmpty()) { - url.append("?database=").append(database.trim()); - } - return url.toString(); - } - - @Override - public List listDatabases() { - List result = StandardJdbcMetadata.INSTANCE.listDatabases( - requireConnected(), - getConfiguredDatabase() - ); - result.sort(Comparator.comparing(DatabaseInfo::getName)); - return result; - } - - @Override - public List listSchemas() { - return Collections.emptyList(); - } - - @Override - public List listTables(String schema) { - return unchecked(() -> { - Connection conn = requireConnected(); - // Try JDBC metadata first — the driver handles version compatibility. - List result = new ArrayList<>(); - DatabaseMetaData meta = conn.getMetaData(); - try (ResultSet rs = meta.getTables(null, blankToNull(schema), "%", new String[]{"TABLE", "VIEW"})) { - while (rs.next()) { - String name = rs.getString("TABLE_NAME"); - String type = rs.getString("TABLE_TYPE"); - String comment = getStringOrNull(rs, "REMARKS"); - if (name != null && !name.isEmpty()) { - result.add(new TableInfo(name, type, comment)); - } - } - } catch (Exception ignored) { - // Fall through to Cypher fallback - } - // Fallback: use Cypher for drivers that don't implement getTables(). - if (result.isEmpty()) { - try (Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("CALL db.labels()")) { - while (rs.next()) { - String label = getStringOrDefault(rs, "label", ""); - if (label.isEmpty()) { - label = rs.getString(1); - } - if (label != null && !label.isEmpty()) { - result.add(new TableInfo(label, "TABLE", null)); - } - } - } catch (Exception ignored) { - // Cypher fallback also failed - } - } - result.sort(Comparator.comparing(TableInfo::getName)); - return result; - }); - } - - @Override - public List getColumns(String schema, String table) { - return unchecked(() -> { - Connection conn = requireConnected(); - List result = new ArrayList<>(); - // Try JDBC metadata first. - DatabaseMetaData meta = conn.getMetaData(); - Set primaryKeys = new LinkedHashSet<>(); - try (ResultSet rs = meta.getPrimaryKeys(null, blankToNull(schema), table)) { - while (rs.next()) { - String pkColumn = rs.getString("COLUMN_NAME"); - if (pkColumn != null) { - primaryKeys.add(pkColumn); - } - } - } catch (Exception ignored) { - // Fall through - } - try (ResultSet rs = meta.getColumns(null, blankToNull(schema), table, "%")) { - while (rs.next()) { - String name = rs.getString("COLUMN_NAME"); - if (name == null || name.isEmpty()) { - continue; - } - String type = rs.getString("TYPE_NAME"); - if (type == null) { - type = "Unknown"; - } - boolean nullable = true; - try { - nullable = "YES".equals(rs.getString("IS_NULLABLE")); - } catch (Exception ignored) { - // keep default - } - String defaultValue = getStringOrNull(rs, "COLUMN_DEF"); - String comment = getStringOrNull(rs, "REMARKS"); - result.add(new ColumnInfo( - name, type, nullable, defaultValue, - primaryKeys.contains(name), - comment, null, null, null, null - )); - } - } catch (Exception ignored) { - // Fall through to Cypher fallback - } - // Fallback: use Cypher for drivers that don't implement getColumns(). - if (result.isEmpty()) { - try (Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("CALL db.schema.nodeTypeProperties()")) { - while (rs.next()) { - String nodeLabels = rs.getString("nodeLabels"); - if (nodeLabels == null) { - nodeLabels = ""; - } - if (!nodeLabels.contains(table)) { - continue; - } - String propertyName = rs.getString("propertyName"); - if (propertyName == null) { - continue; - } - String propertyTypes = rs.getString("propertyTypes"); - if (propertyTypes == null) { - propertyTypes = "Unknown"; - } - boolean isNullable; - try { - isNullable = !rs.getBoolean("mandatory"); - } catch (Exception ignored) { - isNullable = true; - } - result.add(new ColumnInfo( - propertyName, propertyTypes, isNullable, - null, false, null, null, null, null, null - )); - } - } catch (Exception ignored) { - // Cypher fallback also failed - } - } - result.sort(Comparator.comparing(ColumnInfo::getName)); - return result; - }); - } - - private static String blankToNull(String value) { - if (value == null) return null; - String trimmed = value.trim(); - return trimmed.isEmpty() ? null : trimmed; - } - - @Override - public List listIndexes(String schema, String table) { - return unchecked(() -> { - Connection conn = requireConnected(); - List result = new ArrayList<>(); - try (Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SHOW INDEXES")) { - while (rs.next()) { - String labelsOrTypes = getStringOrDefault(rs, "labelsOrTypes", ""); - if (!labelsOrTypes.contains(table)) { - continue; - } - - String properties = getStringOrDefault(rs, "properties", ""); - String uniqueness = getStringOrDefault(rs, "uniqueness", ""); - result.add(new IndexInfo( - getStringOrDefault(rs, "name", ""), - parseProperties(properties), - "UNIQUE".equals(uniqueness.toUpperCase(Locale.ROOT)), - false, - null, - getStringOrNull(rs, "type"), - null, - null - )); - } - } - result.sort(Comparator.comparing(IndexInfo::getName)); - return result; - }); - } - - @Override - public List listForeignKeys(String schema, String table) { - return Collections.emptyList(); - } - - @Override - public List listTriggers(String schema, String table) { - return Collections.emptyList(); - } - - @Override - public QueryResult executeTransaction(List statements, String schema) { - return TransactionExecutor.executeStatements(requireConnected(), statements, schema, this::setSchemaSQL, (stmt, sql) -> { - stmt.execute(sql); - return Math.max(stmt.getUpdateCount(), 0); - }); - } - - public QueryResult executeQuery(String sql, String schema) { - return executeQuery(sql, schema, new ExecuteQueryOptions()); - } - - @Override - public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) { - return JdbcExecutor.current().execute( - requireConnected(), - sql, - schema, - this::setSchemaSQL, - options.getMaxRows(), - options.getFetchSize(), - options.getTimeoutSecs(), - this::resultValue - ); - } - - @Override - protected Object resultValue(ResultSet rs, int index, int sqlType) { - return unchecked(() -> { - Object value = rs.getObject(index); - return rs.wasNull() ? null : value == null ? null : value.toString(); - }); - } - - private static List parseProperties(String properties) { - String trimmed = properties; - if (trimmed.startsWith("[") && trimmed.endsWith("]") && trimmed.length() >= 2) { - trimmed = trimmed.substring(1, trimmed.length() - 1); - } - - List result = new ArrayList<>(); - for (String property : trimmed.split(",")) { - String value = property.trim(); - if (value.startsWith("\"") && value.endsWith("\"") && value.length() >= 2) { - value = value.substring(1, value.length() - 1); - } - if (!value.isEmpty()) { - result.add(value); - } - } - return result; - } - - private static String getStringOrDefault(ResultSet rs, String column, String defaultValue) { - try { - String value = rs.getString(column); - return value == null ? defaultValue : value; - } catch (Exception ignored) { - return defaultValue; - } - } - - private static String getStringOrNull(ResultSet rs, String column) { - try { - return rs.getString(column); - } catch (Exception ignored) { - return null; - } - } - - public static void main(String[] args) { - new MultiSessionJsonRpcServer(Neo4jAgent::new).run(); - } -} diff --git a/agents/drivers/neo4j/src/test/java/com/dbx/agent/neo4j/Neo4jAgentTest.java b/agents/drivers/neo4j/src/test/java/com/dbx/agent/neo4j/Neo4jAgentTest.java deleted file mode 100644 index f9b6d0db46..0000000000 --- a/agents/drivers/neo4j/src/test/java/com/dbx/agent/neo4j/Neo4jAgentTest.java +++ /dev/null @@ -1,276 +0,0 @@ -package com.dbx.agent.neo4j; - -import com.dbx.agent.ConnectParams; -import com.dbx.agent.DatabaseInfo; -import com.dbx.agent.QueryResult; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class Neo4jAgentTest { - @Test - void executesCypherWritesWithReturnThroughStatementExecute() throws Exception { - List calls = new ArrayList<>(); - Neo4jAgent agent = connectedAgent(fakeConnection(calls)); - - QueryResult result = agent.executeQuery("CREATE (n:Person {name: 'Ada'}) RETURN n", null); - - Assertions.assertEquals(Collections.singletonList("n"), result.getColumns()); - Assertions.assertEquals( - Collections.singletonList(Collections.singletonList("(:Person {name: Ada})")), - result.getRows() - ); - Assertions.assertTrue(calls.contains("execute")); - Assertions.assertFalse(calls.contains("executeUpdate")); - } - - @Test - void executesTransactionsThroughStatementExecute() throws Exception { - List calls = new ArrayList<>(); - Neo4jAgent agent = connectedAgent(fakeConnection(calls)); - - QueryResult result = agent.executeTransaction( - Arrays.asList( - "MATCH (n:Employee) WHERE elementId(n) = '4:abc:7' SET n.name = 'Grace'", - "CREATE (n:Employee {name: 'Linus'})" - ), - null - ); - - Assertions.assertEquals(0L, result.getAffected_rows()); - Assertions.assertEquals( - Arrays.asList("setAutoCommit:false", "execute", "execute", "commit", "setAutoCommit:true"), - calls - ); - Assertions.assertFalse(calls.contains("executeUpdate")); - } - - @Test - void listsConfiguredMemgraphDatabaseWhenCatalogDiscoveryRequiresEnterprise() { - SQLException catalogError = new SQLException( - "Your license has an invalid type. To use multi-tenancy you need to have an enterprise license." - ); - Neo4jAgent agent = connectedAgent(fakeCatalogConnection(Collections.emptyList(), catalogError, null), "memgraph"); - - Assertions.assertEquals( - Collections.singletonList(new DatabaseInfo("memgraph")), - agent.listDatabases() - ); - } - - @Test - void keepsDiscoveredNeo4jDatabasesSorted() { - Neo4jAgent agent = connectedAgent( - fakeCatalogConnection(Arrays.asList("system", "neo4j"), null, "neo4j"), - "neo4j" - ); - - Assertions.assertEquals( - Arrays.asList(new DatabaseInfo("neo4j"), new DatabaseInfo("system")), - agent.listDatabases() - ); - } - - private static Neo4jAgent connectedAgent(Connection connection) { - return connectedAgent(connection, ""); - } - - private static Neo4jAgent connectedAgent(Connection connection, String database) { - Neo4jAgent agent = new Neo4jAgent() { - @Override - protected void loadDriver(ConnectParams params) { - } - - @Override - protected Connection openConnection(ConnectParams params) { - return connection; - } - }; - ConnectParams params = new ConnectParams(); - params.setDatabase(database); - agent.connect(params); - return agent; - } - - private static Connection fakeCatalogConnection( - List catalogs, - SQLException catalogError, - String currentCatalog - ) { - DatabaseMetaData metadata = proxy(DatabaseMetaData.class, (unused, method, args) -> { - switch (method.getName()) { - case "getCatalogs": - if (catalogError != null) { - throw catalogError; - } - return fakeCatalogResultSet(catalogs); - case "getIdentifierQuoteString": - return "`"; - default: - return defaultValue(method.getReturnType()); - } - }); - return proxy(Connection.class, (unused, method, args) -> { - switch (method.getName()) { - case "getMetaData": - return metadata; - case "getCatalog": - return currentCatalog; - case "close": - return null; - case "isClosed": - return false; - default: - return defaultValue(method.getReturnType()); - } - }); - } - - private static ResultSet fakeCatalogResultSet(List catalogs) { - int[] index = {-1}; - return proxy(ResultSet.class, (unused, method, args) -> { - switch (method.getName()) { - case "next": - index[0] += 1; - return index[0] < catalogs.size(); - case "getString": - return catalogs.get(index[0]); - case "close": - return null; - default: - return defaultValue(method.getReturnType()); - } - }); - } - - private static Connection fakeConnection(List calls) { - Statement statement = fakeStatement(calls); - return proxy(Connection.class, (unused, method, args) -> { - switch (method.getName()) { - case "createStatement": - return statement; - case "setAutoCommit": - calls.add("setAutoCommit:" + args[0]); - return null; - case "getAutoCommit": - return true; - case "commit": - calls.add("commit"); - return null; - case "rollback": - calls.add("rollback"); - return null; - case "close": - return null; - case "isClosed": - return false; - default: - return defaultValue(method.getReturnType()); - } - }); - } - - private static Statement fakeStatement(List calls) { - ResultSet resultSet = fakeResultSet(); - return proxy(Statement.class, (unused, method, args) -> { - switch (method.getName()) { - case "execute": - calls.add("execute"); - return true; - case "executeUpdate": - calls.add("executeUpdate"); - throw new SQLException("syntax error or access rule violation - invalid syntax"); - case "executeQuery": - calls.add("executeQuery"); - return resultSet; - case "getResultSet": - return resultSet; - case "getUpdateCount": - return 0; - case "close": - return null; - default: - return defaultValue(method.getReturnType()); - } - }); - } - - private static ResultSet fakeResultSet() { - int[] index = {-1}; - ResultSetMetaData metadata = fakeMetadata(); - return proxy(ResultSet.class, (unused, method, args) -> { - switch (method.getName()) { - case "next": - index[0] += 1; - return index[0] == 0; - case "getMetaData": - return metadata; - case "getObject": - return "(:Person {name: Ada})"; - case "wasNull": - return false; - case "close": - return null; - default: - return defaultValue(method.getReturnType()); - } - }); - } - - private static ResultSetMetaData fakeMetadata() { - return proxy(ResultSetMetaData.class, (unused, method, args) -> { - switch (method.getName()) { - case "getColumnCount": - return 1; - case "getColumnLabel": - return "n"; - default: - return defaultValue(method.getReturnType()); - } - }); - } - - @SuppressWarnings("unchecked") - private static T proxy(Class type, InvocationHandler handler) { - return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler); - } - - private static Object defaultValue(Class type) { - if (type == Boolean.TYPE) { - return false; - } - if (type == Byte.TYPE) { - return (byte) 0; - } - if (type == Short.TYPE) { - return (short) 0; - } - if (type == Integer.TYPE) { - return 0; - } - if (type == Long.TYPE) { - return 0L; - } - if (type == Float.TYPE) { - return 0F; - } - if (type == Double.TYPE) { - return 0.0D; - } - if (type == Character.TYPE) { - return '\0'; - } - return null; - } -} diff --git a/agents/metadata-constraint-coverage.tsv b/agents/metadata-constraint-coverage.tsv index b157fb3674..ce4a321ad4 100644 --- a/agents/metadata-constraint-coverage.tsv +++ b/agents/metadata-constraint-coverage.tsv @@ -14,7 +14,7 @@ informix native-pushdown java-sql Uses systables/sysprocedures with type, filter kingbase-go shared-fallback native-go Uses sys_catalog/information_schema for compatibility-aware metadata, then applies stable filtering and paging in the native agent. vastbase-go shared-fallback native-go Uses pg_catalog/information_schema for Vastbase metadata, then applies stable filtering and paging in the native agent. kylin intentional-fallback java-jdbc-metadata Uses JDBC metadata from Kylin driver; no portable server-side paging API, common constraints filter locally. -neo4j intentional-fallback java-graph-metadata Uses JDBC metadata with Cypher fallback; label discovery cannot guarantee relational-style filtered paging, common constraints filter locally. +neo4j-go shared-fallback native-go Uses Neo4j schema procedures with stable local filtering and paging because label discovery does not expose portable server-side metadata pagination. oceanbase-oracle native-pushdown java-sql Uses Oracle-compatible metadata SQL with type, filter, stable order, and ROWNUM paging. oracle-go native-pushdown native-go Uses Oracle-compatible metadata SQL with type, filter, stable order, and ROWNUM paging, with legacy profile guard. snowflake native-pushdown java-sql Uses INFORMATION_SCHEMA with type, filter, stable order, and literal LIMIT/OFFSET. diff --git a/agents/scripts/validate_agents.py b/agents/scripts/validate_agents.py index 4e31c093b1..a70ac1feda 100644 --- a/agents/scripts/validate_agents.py +++ b/agents/scripts/validate_agents.py @@ -17,6 +17,7 @@ NATIVE_ONLY_AGENT_MODULES = { "duckdb": "drivers/duckdb", "oracle": "drivers/oracle-go", "kingbase": "drivers/kingbase-go", + "neo4j": "drivers/neo4j-go", "vastbase": "drivers/vastbase-go", "tdengine": "drivers/tdengine", "xugu": "drivers/xugu", diff --git a/agents/scripts/validate_agents_test.py b/agents/scripts/validate_agents_test.py index 9599911739..1a82f89707 100644 --- a/agents/scripts/validate_agents_test.py +++ b/agents/scripts/validate_agents_test.py @@ -140,6 +140,7 @@ class ValidateAgentsTest(unittest.TestCase): "cassandra-go", "oracle-go", "kingbase-go", + "neo4j-go", "vastbase-go", "xugu", "duckdb", @@ -148,7 +149,7 @@ class ValidateAgentsTest(unittest.TestCase): ): (root / "drivers" / driver).mkdir(parents=True) (root / "versions.json").write_text( - json.dumps({"h2": "0.1.0", "cassandra": "0.1.0", "oracle": "0.1.0", "kingbase": "0.1.0", "vastbase": "0.1.0", "xugu": "0.1.0", "rabbitmq": "0.1.0", "tdengine": "0.1.0"}), + json.dumps({"h2": "0.1.0", "cassandra": "0.1.0", "oracle": "0.1.0", "kingbase": "0.1.0", "neo4j": "0.1.0", "vastbase": "0.1.0", "xugu": "0.1.0", "rabbitmq": "0.1.0", "tdengine": "0.1.0"}), encoding="utf-8", ) diff --git a/agents/settings.gradle b/agents/settings.gradle index bcd9ef8437..2cb648d850 100644 --- a/agents/settings.gradle +++ b/agents/settings.gradle @@ -5,7 +5,7 @@ def driverModules = [ 'access', 'dameng', 'goldendb', 'databend', 'databricks', 'saphana', 'teradata', 'vertica', 'firebird', 'exasol', 'oceanbase-oracle', 'gbase8a', 'gbase8s', 'bigquery', 'kylin', 'sundb', 'h2', 'h2-legacy', 'snowflake', 'trino', 'hive', 'spark', - 'db2', 'informix', 'neo4j', 'mongodb', 'highgo', 'uxdb', 'yashandb', 'oscar', + 'db2', 'informix', 'mongodb', 'highgo', 'uxdb', 'yashandb', 'oscar', 'iris', 'iotdb', 'etcd', 'zookeeper', 'kafka', 'rocketmq', 'sqlserver-legacy' ]