Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
insert_data_into_single_table.cpp
1
8#include <iostream>
9#include <string>
10#include <unordered_set>
11
12// The table is called "Extract" and will be created in the "Extract" schema.
13// This has historically been the default table name and schema for extracts created by Tableau.
14static const hyperapi::TableDefinition extractTable{
15 {"Extract", "Extract"},
20
21static void runInsertDataIntoSingleTable() {
22 std::cout << "EXAMPLE - Insert data into a single table within a new Hyper file" << std::endl;
23 const std::string pathToDatabase = "data/customer.hyper";
24
25 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
26 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
27 {
29 // Creates new Hyper file "customer.hyper".
30 // Replaces existing file with hyperapi::CreateMode::CreateAndReplace if it already exists.
31 {
32 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase, hyperapi::CreateMode::CreateAndReplace);
33 const hyperapi::Catalog& catalog = connection.getCatalog();
34
35 // Create the schema and the table.
36 catalog.createSchema("Extract");
37 catalog.createTable(extractTable);
38
39 // Insert data into the "Extract"."Extract" table.
40 {
41 hyperapi::Inserter inserter(connection, extractTable);
42 inserter.addRow("DK-13375", "Dennis Kane", 518, "Consumer");
43 inserter.addRow("EB-13705", "Ed Braxton", 815, "Corporate");
44 inserter.execute();
45 }
46
47 // Print the table names in the "Extract" schema.
48 std::unordered_set<hyperapi::TableName> tableNames = catalog.getTableNames("Extract");
49 std::cout << "Tables available in " << pathToDatabase << " in the Extract schema are: ";
50 for (auto& tableName : tableNames)
51 std::cout << tableName.toString() << "\t";
52 std::cout << std::endl;
53
54 // Number of rows in the "Extract"."Extract" table.
55 // `executeScalarQuery` is for executing a query that returns exactly one row with one column.
56 int64_t rowCount = connection.executeScalarQuery<int64_t>("SELECT COUNT(*) FROM " + extractTable.getTableName().toString());
57 std::cout << "The number of rows in table " << extractTable.getTableName() << " is " << rowCount << "." << std::endl;
58 }
59 std::cout << "The connection to the Hyper file has been closed." << std::endl;
60 }
61 std::cout << "The Hyper Process has been shut down." << std::endl;
62}
63
64int main() {
65 try {
66 runInsertDataIntoSingleTable();
67 } catch (const hyperapi::HyperException& e) {
68 std::cout << e.toString() << std::endl;
69 return 1;
70 }
71 return 0;
72}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
std::unordered_set< TableName > getTableNames(const SchemaName &schema) const
Gets the names of all tables in the given schema.
void createSchema(const SchemaName &schemaName) const
Creates a SQL schema with the given name.
void createTable(const hyperapi::TableDefinition &table_definition) const
Creates a SQL table with the given table definition.
Defines a Hyper connection.
Defines an exception object that is thrown on failure by the functions in the Hyper API C++ library.
std::string toString() const
Returns a formatted string containing the message and hint of the error and all causes.
Defines a Hyper process.
An inserter.
Definition Inserter.hpp:24
static SqlType bigInt() noexcept
Returns the BIG INTEGER SQL type.
Definition SqlType.hpp:101
static SqlType text() noexcept
Returns the TEXT SQL type.
Definition SqlType.hpp:143
A Column of a table definition.
const TableName & getTableName() const noexcept
Returns the name of the table.
std::string toString() const
The main header of the Hyper API for C++.
@ NotNullable
The column cannot contain NULL values.
@ SendUsageDataToTableau
Telemetry data will be sent to tableau to help improve the Hyper API.
@ CreateAndReplace
Create the database. If it already exists, drop the old one first.