C++ Prepared Statements and Batches

Use prepared statements to bind values independently of the statement text. A cursor keeps the prepared statement for subsequent executions. Running the prepared statement again closes its previous result. In auto-commit mode, finish or close an open Cypher result before starting another statement on the connection.

Operation Call Input and result
SQL statement batch execute(language, statements[, namespace]) Pass a std::vector<std::string> of data manipulation statements. The result is a vector of update counts.
Indexed preparation prepare_indexed(language, statement[, namespace]) Place a ? at each parameter position.
Indexed execution execute_prepared(parameters) Pass a mutable std::vector<Types::TypedValue> in placeholder order. The result is a regular Result.
Indexed batch execution execute_prepared(parameter_sets) Pass a mutable std::vector<std::vector<Types::TypedValue>>. Each inner vector supplies one execution of the prepared data manipulation statement. The result contains the update counts reported by the server.
Named preparation prepare_named(language, statement[, namespace]) Use :name in SQL and $name in Cypher.
Named execution execute_prepared(parameters) Pass a mutable std::unordered_map<std::string, Types::TypedValue> with keys such as "name". The result is a regular Result.

Use an explicit namespace or omit it to use the default from the connection. Bind values using the appropriate TypedValue constructors. For SQL insert, update or delete statements, unwrap the result as Results::ScalarResult to read the statement’s scalar value. Polypheny-DB can return one aggregate update count for an indexed batch.

Insert with batches and parameters

On a development server, use an account that can create and drop tables in public. This example creates and drops the table cpp_docs_customers. Start with the name available. Build the program as described in the integration guide.

#include <cstdint>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>

#include "PolyphenyCPPDriver.h"

int main() {
    Connection::Connection connection("127.0.0.1", "pa", "");
    Connection::Cursor setup(connection);
    setup.execute(
        "sql",
        "CREATE TABLE cpp_docs_customers (id INTEGER NOT NULL, name VARCHAR(50), PRIMARY KEY (id))",
        "public"
    );

    {
        Connection::Cursor batch(connection);
        const std::vector<std::string> statements = {
            "INSERT INTO cpp_docs_customers (id, name) VALUES (1, 'Maria')",
            "INSERT INTO cpp_docs_customers (id, name) VALUES (2, 'Daniel')"
        };
        const auto counts = batch.execute("sql", statements, "public");
        std::cout << "Batch statements: " << counts.size() << '\n';
    }

    {
        Connection::Cursor indexed(connection);
        indexed.prepare_indexed("sql", "INSERT INTO cpp_docs_customers VALUES (?, ?)", "public");
        std::vector<Types::TypedValue> parameters = {
            Types::TypedValue(std::int32_t{3}), Types::TypedValue("Anna")
        };
        auto result = indexed.execute_prepared(parameters);
        std::cout << "Indexed insert: " << result->unwrap<Results::ScalarResult>()->get_scalar() << '\n';

        result.reset();

        std::vector<std::vector<Types::TypedValue>> parameter_sets = {
            {Types::TypedValue(std::int32_t{4}), Types::TypedValue("Thomas")},
            {Types::TypedValue(std::int32_t{5}), Types::TypedValue("Andreas")}
        };
        const auto counts = indexed.execute_prepared(parameter_sets);
        std::cout << "Indexed batch rows: " << std::accumulate(counts.begin(), counts.end(), std::uint64_t{0}) << '\n';
    }

    {
        Connection::Cursor named(connection);
        named.prepare_named("sql", "INSERT INTO cpp_docs_customers VALUES (:id, :name)", "public");
        std::unordered_map<std::string, Types::TypedValue> parameters = {
            {"id", Types::TypedValue(std::int32_t{6})}, {"name", Types::TypedValue("Michael")}
        };
        auto result = named.execute_prepared(parameters);
        std::cout << "Named insert: " << result->unwrap<Results::ScalarResult>()->get_scalar() << '\n';
    }

    {
        auto result = setup.execute("sql", "SELECT name FROM cpp_docs_customers ORDER BY id", "public");
        auto* rows = result->unwrap<Results::RelationalResult>();
        for (auto row = rows->begin(); row != rows->end(); ++row) {
            std::cout << (*row)[0].as_string() << '\n';
        }
    }

    setup.execute("sql", "DROP TABLE cpp_docs_customers", "public");
}

The program prints the batch counts and six names in ID order.

Bind a named Cypher value

On a server build with Cypher over Prism, prepare a single Cypher statement with prepare_named. The namespace in this example comes from the graph fixture.

Connection::Cursor cursor(connection);
cursor.prepare_named(
    "cypher",
    "MATCH (n:Person) WHERE n.name = $name RETURN n AS person, n.name AS name",
    "driver_graph_demo"
);
std::unordered_map<std::string, Types::TypedValue> parameters = {
    {"name", Types::TypedValue("Ada")}
};
auto result = cursor.execute_prepared(parameters);
auto* rows = result->unwrap<Results::RelationalResult>();

Read the two projected columns from rows as a typed node and a string. C++ typed values · Prism named parameter rules

© Polypheny GmbH. All Rights Reserved.