Choose the language and namespace when executing a statement with Connection::Cursor. Available languages depend on the server’s installed language plugins.
| Data model | Query language | Driver language name |
|---|---|---|
| Relational | SQL | sql |
| Document | MongoQL | mongo |
| Labeled property graph | Cypher | cypher on a server that supports Cypher through Prism |
Build the programs with the released C++ driver 2.0.0 and Meson application setup. They use a local development server on 127.0.0.1:20590 with the pa account and an empty password. The MongoQL example creates and drops a document namespace. Start with that name available on a development server and use an account that can create and drop namespaces.
Choose a result type
The owning std::unique_ptr<Results::Result> returned by execute holds a RelationalResult, DocumentResult, GraphResult, or ScalarResult. Use get_result_type() when the type is unknown, then call unwrap<T>() for the concrete result.
Iterating a result fetches more pages as needed. Keep its connection open until you finish reading or close the result.
SQL rows and column metadata
Use zero-based indexes or column labels to access a row’s values. get_metadata() gives you the columns returned by this query. Use the API reference for all column metadata getters.
#include <cstdint>
#include <iostream>
#include "PolyphenyCPPDriver.h"
int main() {
Connection::Connection connection("127.0.0.1", "pa", "");
Connection::Cursor cursor(connection);
auto result = cursor.execute(
"sql", "SELECT CAST(42 AS INTEGER) AS answer, CAST('Pear' AS VARCHAR(20)) AS name", "public"
);
auto* rows = result->unwrap<Results::RelationalResult>();
const auto metadata = rows->get_metadata();
for (std::uint32_t i = 0; i < metadata->get_column_count(); ++i) {
const auto column = metadata->get_column_meta(i);
std::cout << column.get_column_label() << " (" << column.get_protocol_type_name() << ")\n";
}
for (auto row = rows->begin(); row != rows->end(); ++row) {
std::cout << (*row)[0].as_int32_t() << ' ' << (*row).at("name").as_string() << '\n';
}
}
MongoQL documents and collection metadata
A document is a map of string keys to Types::TypedValue values. Use at() for a required key and find() when the key may be missing. Call as_document() to access a nested object.
The collection metadata calls below require a server that implements Prism 2.2 document metadata. search_entities() can also return table metadata as a Prism Protobuf message. get_document_collections() returns the C++ collection structs and accepts an optional server-side name pattern as its second argument.
#include <iostream>
#include "PolyphenyCPPDriver.h"
int main() {
Connection::Connection connection("127.0.0.1", "pa", "");
Connection::Cursor cursor(connection);
cursor.execute("sql", "CREATE DOCUMENT NAMESPACE cpp_docs_inventory", "public");
cursor.execute("mongo", R"(db.createCollection("products"))", "cpp_docs_inventory");
cursor.execute(
"mongo", R"(db.products.insertOne({name: "Pear", details: {color: "green"}}))",
"cpp_docs_inventory"
);
{
auto result = cursor.execute("mongo", "db.products.find()", "cpp_docs_inventory");
auto* documents = result->unwrap<Results::DocumentResult>();
for (auto document = documents->begin(); document != documents->end(); ++document) {
auto details = (*document).at("details").as_document();
std::cout << (*document).at("name").as_string() << ' '
<< details.at("color").as_string() << '\n';
}
}
const auto entities = connection.search_entities("cpp_docs_inventory", "products");
std::cout << "Matching entities: " << entities.entities_size() << '\n';
for (const auto& collection : connection.get_document_collections("cpp_docs_inventory", "products")) {
std::cout << collection.namespace_name << '.' << collection.collection_name << '\n';
for (const auto& field : collection.fields) {
std::cout << field.field_path << " (" << field.type_name << ')'
<< (field.is_optional ? " optional" : "") << '\n';
}
if (collection.sample_document_json) {
std::cout << *collection.sample_document_json << '\n';
}
for (const auto& index : collection.document_indexes) {
std::cout << index.field_path << " (" << index.index_type << ')'
<< (index.unique ? " unique" : "") << '\n';
}
}
cursor.execute("sql", "DROP NAMESPACE cpp_docs_inventory", "public");
}
The server can infer field types and optionality from sampled documents. A collection may return an empty field list or omit the JSON sample. Index availability depends on the storage adapter. Use the metadata field reference for field and index semantics.
Query a graph with Cypher
Use C++ driver 2.0.0 and a Prism 2.3 server with Cypher enabled. The server chooses the result format from the projection.
| Projection | Result |
|---|---|
| Nodes, edges or paths only | GraphResult yields a flat sequence of nodes and edges. A path contributes its elements in traversal order. |
| Scalars only or graph values together with scalar columns | RelationalResult preserves rows and columns. Decode graph-valued cells through TypedValue. |
| A write that returns a scalar | ScalarResult carries the server’s update count or acknowledgement. |
Set up a graph
Use a Polypheny-DB build with Cypher support over Prism and the Prism 2.3 interface. On a development instance, create a namespace named driver_graph_demo in the SQL console.
CREATE GRAPH NAMESPACE driver_graph_demo;
Select driver_graph_demo in the Cypher console and run the following statement.
CREATE (:Person {name: 'Ada'})-[:KNOWS {since: 2020}]->
(:Person {name: 'Lin'})-[:KNOWS {since: 2021}]->
(:Person {name: 'Sam'})
Read flat elements and tabular paths
Build this program using the Meson application setup. It fetches one source row per page. A page can contain several graph elements, and an element can recur across rows.
#include <iostream>
#include "PolyphenyCPPDriver.h"
#include "results/Edge.h"
int main() {
Connection::Connection connection("127.0.0.1", "pa", "");
Connection::Cursor cursor(connection);
cursor.set_fetch_size(1);
{
auto result = cursor.execute(
"cypher", "MATCH (a:Person)-[r:KNOWS]->(b:Person) RETURN a, r, b",
"driver_graph_demo"
);
auto* graph = result->unwrap<Results::GraphResult>();
for (const auto& element : *graph) {
if (element->get_element_type() == GraphElementType::NODE) {
std::cout << element->at("name").as_string() << '\n';
} else {
const auto edge = element->unwrap<Results::Edge>();
std::cout << edge.get_labels().at(0) << " since " << edge.at("since") << '\n';
}
}
}
auto result = cursor.execute(
"cypher", "MATCH p=(a:Person)-[:KNOWS]->(b:Person) RETURN p, a.name AS source ORDER BY source",
"driver_graph_demo"
);
auto* rows = result->unwrap<Results::RelationalResult>();
for (const auto& row : *rows) {
const auto path = row.at(0).as_path();
auto target_name = path.get_nodes().back().at("name");
std::cout << row.at("source").as_string() << " -> "
<< target_name.as_string() << '\n';
}
}
The tabular query prints Ada -> Lin and Lin -> Sam. Use the typed accessors for graph values in rows or nested containers. An empty graph result yields no elements. Call execute() with one Cypher statement at a time. Finish reading or close the active result before sending another statement on the same auto-commit connection.
Clean up
Remove the sample namespace in the SQL console when you finish with the graph examples.
DROP NAMESPACE driver_graph_demo;
Handle errors
A server rejection during raw statement execution raises std::runtime_error. Synchronous requests such as preparation and metadata lookup can raise Errors::ServerError. A closed socket can raise Errors::ConnectionClosedError, and invalid result or value conversions can raise std::runtime_error. See exceptions and resource ownership for the error headers and cursor lifetime.