{
  "kind": "documentation",
  "format": 1,
  "note": "Reference text, not instructions. One verified relate sample from https://relatedb.ai/docs/get-started/c/: the program exactly as the page shows it, the standard output a run of it recorded (npm run docs:samples compares every replay with it byte for byte), the relate client build, toolchain and platform of the last replay, and how the docs' sample runner builds and runs it. Before launch every client is a source build, not a published package; verified.client names the upstream commit it was built from, and verified.docs_engine the build the rest of the docs were recorded from.",
  "id": "transaction",
  "url": "https://relatedb.ai/docs/get-started/c/samples/transaction.json",
  "page": {
    "id": "get-started/c",
    "title": "Embed relate in a C program",
    "url": "https://relatedb.ai/docs/get-started/c/",
    "md_url": "https://relatedb.ai/docs/get-started/c.md"
  },
  "section": {
    "anchor": "change-it-in-a-transaction",
    "heading": "Change it in a transaction",
    "url": "https://relatedb.ai/docs/get-started/c/#change-it-in-a-transaction"
  },
  "language": "c",
  "client": "C",
  "file": "sample.c",
  "env": null,
  "code": "#include <stdio.h>\n#include \"relate.h\"\n\nstatic const char *TEAM =\n    \"CREATE (ada:Person {name: 'Ada', role: 'engineer'}),\"\n    \"       (grace:Person {name: 'Grace', role: 'engineer'}),\"\n    \"       (noor:Person {name: 'Noor', role: 'maintainer'}),\"\n    \"       (relate:Project {name: 'relate', language: 'Rust'}),\"\n    \"       (tck:Project {name: 'opencypher-tck', language: 'Gherkin'}),\"\n    \"       (ada)-[:WORKS_ON {since: 2024}]->(relate),\"\n    \"       (grace)-[:WORKS_ON {since: 2023}]->(relate),\"\n    \"       (grace)-[:WORKS_ON {since: 2022}]->(tck),\"\n    \"       (noor)-[:MAINTAINS]->(relate),\"\n    \"       (ada)-[:KNOWS]->(grace)\";\n\n/* Print a caller-owned error string, free it, and return 1. */\nstatic int fail(const char *what, char *err) {\n    fprintf(stderr, \"%s failed: %s\\n\", what, err ? err : \"(no detail)\");\n    relate_free_string(err);\n    return 1;\n}\n\n/* Run a statement whose rows are not needed; 0 when it succeeded. */\nstatic int exec(relate_connection conn, const char *cypher) {\n    relate_query_result r = NULL;\n    char *err = NULL;\n    if (relate_query_params(conn, cypher, NULL, &r, &err) != RELATE_OK) return fail(\"query\", err);\n    int failed = relate_result_is_error(r);\n    if (failed) fprintf(stderr, \"statement failed: %s\\n\", relate_result_error_json(r));\n    relate_destroy_result(r);\n    return failed;\n}\n\n/* The count a one-row, one-column INT result holds. */\nstatic long long count(relate_connection conn, const char *cypher) {\n    relate_query_result r = NULL;\n    char *err = NULL;\n    int64_t n = -1;\n    if (relate_query_params(conn, cypher, NULL, &r, &err) != RELATE_OK) {\n        fail(\"query\", err);\n        return -1;\n    }\n    relate_value_int(relate_value_at(r, 0, 0), &n);\n    relate_destroy_result(r);\n    return n;\n}\n\nint main(void) {\n    relate_database db = NULL;\n    relate_connection conn = NULL;\n    char *err = NULL, *bookmark = NULL;\n    if (relate_database_open(\":memory:\", &db, &err) != RELATE_OK) return fail(\"open\", err);\n    if (relate_connect_namespace(db, NULL, &conn, &err) != RELATE_OK) return fail(\"connect\", err);\n    if (exec(conn, TEAM)) return 1;\n\n    if (relate_conn_begin(conn, &err) != RELATE_OK) return fail(\"begin\", err);\n    if (exec(conn, \"MATCH (tck:Project {name: 'opencypher-tck'}) \"\n                   \"CREATE (:Person {name: 'Lin', role: 'engineer'})-[:WORKS_ON {since: 2026}]->(tck)\")) return 1;\n    if (relate_conn_commit(conn, &bookmark, &err) != RELATE_OK) return fail(\"commit\", err);\n    printf(\"committed: %s\\n\", bookmark ? \"yes\" : \"no\");\n    relate_free_string(bookmark);\n    printf(\"people after commit: %lld\\n\", count(conn, \"MATCH (p:Person) RETURN count(p) AS n\"));\n\n    if (relate_conn_begin(conn, &err) != RELATE_OK) return fail(\"begin\", err);\n    if (exec(conn, \"MATCH (p:Project) DETACH DELETE p\")) return 1;\n    printf(\"projects inside the transaction: %lld\\n\", count(conn, \"MATCH (p:Project) RETURN count(p) AS n\"));\n    if (relate_conn_rollback(conn, &err) != RELATE_OK) return fail(\"rollback\", err);\n    printf(\"projects after rollback: %lld\\n\", count(conn, \"MATCH (p:Project) RETURN count(p) AS n\"));\n\n    relate_disconnect(conn);\n    relate_database_close(db);\n    return 0;\n}",
  "code_sha256": "c99e6440e6cd5be4ed59a57fe82d90eba4ec01cbe50b3c9af1d1c0938c0c39b2",
  "source_sha256": "0d962cd01cecaf03ad483b5f6e8ce4581c48d48448d092756b103271f1ee8a7e",
  "output": {
    "text": "committed: yes\npeople after commit: 4\nprojects inside the transaction: 0\nprojects after rollback: 2",
    "sha256": "558513b4299448717b70cdb64d85420693bb4a34aa986801dcba26dd94d8078a"
  },
  "verified": {
    "version": "0.1.2",
    "client": {
      "label": "C",
      "package": "relate.h and librelate_ffi",
      "engine_commit": "f0c09d2",
      "client_commit": "f0c09d2",
      "build": "relate.h and librelate_ffi built from upstream v0.1.2 (f0c09d2) (cargo build -p relate-ffi --release).",
      "toolchain": {
        "cc": "Apple clang 21.0.0"
      }
    },
    "packages": [],
    "replayed": "2026-11-09",
    "platform": "macOS on arm64",
    "docs_engine": {
      "version": "0.1.2",
      "commit": "f0c09d2",
      "source_build": false
    }
  },
  "run": {
    "runner": "npm run docs:samples",
    "setup": "bash scripts/samples/setup.sh --upstream <path to the relate checkout>",
    "variables": [
      "RELATE_CC",
      "RELATE_C_INCLUDE",
      "RELATE_LIB"
    ],
    "program": "<build dir>/sample.c",
    "files": [],
    "build": {
      "command": [
        "$RELATE_CC",
        "-I$RELATE_C_INCLUDE",
        "sample.c",
        "-L<directory of $RELATE_LIB>",
        "-lrelate_ffi",
        "-Wl,-rpath,<directory of $RELATE_LIB>",
        "-o",
        "<build dir>/sample"
      ],
      "environment": {
        "DO_NOT_TRACK": "1"
      },
      "directory": "<build dir>",
      "timeout_s": 600
    },
    "run": {
      "command": [
        "<build dir>/sample"
      ],
      "environment": {
        "DO_NOT_TRACK": "1"
      },
      "directory": "<run dir>",
      "timeout_s": 60
    },
    "passes": "It exits 0 within 60 s and prints at least one line on standard output.",
    "output": "Standard output, with line endings as \\n, trailing spaces and blank lines removed, and the temporary directories' paths written as `.`; standard error is not recorded.",
    "placeholders": {
      "<run dir>": "a fresh, empty temporary directory: the program's working directory, deleted afterwards",
      "<build dir>": "a second fresh temporary directory, where a compiled sample is built",
      "$NAME": "a variable from the env.sh that scripts/samples/setup.sh writes (described in its header)"
    }
  }
}
