Hyper API for C++ 0.0.24081
Hyper client library for C++ applications
Loading...
Searching...
No Matches
insert_data_with_expressions.cpp
1
8#include <iostream>
9#include <string>
10
11// The table is called "Extract" and will be created in the "Extract" schema.
12// This has historically been the default table name and schema for extracts created by Tableau.
13static const hyperapi::TableDefinition extractTable{
14 {"Extract", "Extract"},
19
20static void runInsertDataWithExpressions() {
21 std::cout << "EXAMPLE - Push down computations to Hyper during data insertion using expressions" << std::endl;
22 const std::string pathToDatabase = "data/orders.hyper";
23
24 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
25 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
26 {
28 // Creates new Hyper file "orders.hyper".
29 // Replaces existing file with hyperapi::CreateMode::CreateAndReplace if it already exists.
30 {
31 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase, hyperapi::CreateMode::CreateAndReplace);
32 const hyperapi::Catalog& catalog = connection.getCatalog();
33
34 // Create the schema and the table.
35 catalog.createSchema("Extract");
36 catalog.createTable(extractTable);
37
38 // Hyper API's Inserter allows users to transform data during insertion.
39 // To make use of data transformation during insertion, the inserter requires the following inputs
40 // 1. The connection to the Hyper instance containing the table
41 // 2. The table name or table defintion into which data is inserted
42 // 3. List of hyperapi::Inserter::ColumnMapping.
43 // This list informs the inserter how each column in the target table is tranformed.
44 // The list must contain all the columns into which data is inserted.
45 // "hyperapi::Inserter::ColumnMapping" maps a valid SQL expression (if any) to a column in the target table
46 // For example hyperapi::Inserter::ColumnMapping("target_column", hyperapi::escapeName("colA") + "*" + hyperapi::escapeName("colB"))
47 // The column "target_column" contains the product of "colA" and "colB" after successful insertion.
48 // SQL expression string is optional in Inserter.ColumnMapping.
49 // For a column without any transformation only the column name is required.
50 // For example hyperapi::Inserter::ColumnMapping{"no_data_transformation_column"}
51 // 4. Inserter Definition, a list of column definitions for all the input values provided during insertion.
52
53 // Inserter definition contains the column definition for the values that are inserted.
54 std::vector<hyperapi::TableDefinition::Column> inserterDefinition{
59
60 // Column 'Order Id' is inserted into "Extract"."Extract" as-is.
61 // Column 'Ship Timestamp' in "Extract"."Extract" of timestamp type is computed from Column 'Ship Timestamp Text' of text type using 'to_timestamp()'.
62 // Column 'Ship Mode' is inserted into "Extract"."Extract" as-is.
63 // Column 'Ship Priority' is "Extract"."Extract" of integer type is computed from Colum 'Ship Priority Text' of text type using 'CASE' statement.
64 std::string textToTimeStampExpression = "to_timestamp(" + hyperapi::escapeName("Ship Timestamp Text") + ", " + hyperapi::escapeStringLiteral("YYYY-MM-DD HH24:MI:SS") + ")";
65 std::string shipPriorityAsIntCaseExpression = "CASE " + hyperapi::escapeName("Ship Priority Text") +
66 " WHEN " + hyperapi::escapeStringLiteral("Urgent") + " THEN 1 " +
67 " WHEN " + hyperapi::escapeStringLiteral("Medium") + " THEN 2 " +
68 " WHEN " + hyperapi::escapeStringLiteral("Low") + " THEN 3 END";
69
70 std::vector<hyperapi::Inserter::ColumnMapping> columnMappings{
72 hyperapi::Inserter::ColumnMapping{"Ship Timestamp", textToTimeStampExpression},
74 hyperapi::Inserter::ColumnMapping{"Ship Priority", shipPriorityAsIntCaseExpression}};
75
76 // Insert data into the "Extract"."Extract" table using expressions.
77 {
78 hyperapi::Inserter inserter(connection, extractTable, columnMappings, inserterDefinition);
79 inserter.addRow(399, "2012-09-13 10:00:00", "Express Class", "Urgent");
80 inserter.addRow(530, "2012-07-12 14:00:00", "Standard Class", "Low");
81 inserter.execute();
82 }
83
84 // Number of rows in the "Extract"."Extract" table.
85 // `executeScalarQuery` is for executing a query that returns exactly one row with one column.
86 int64_t rowCount = connection.executeScalarQuery<int64_t>("SELECT COUNT(*) FROM " + extractTable.getTableName().toString());
87 std::cout << "The number of rows in table " << extractTable.getTableName() << " is " << rowCount << "." << std::endl;
88 }
89 std::cout << "The connection to the Hyper file has been closed." << std::endl;
90 }
91 std::cout << "The Hyper Process has been shut down." << std::endl;
92}
93
94int main() {
95 try {
96 runInsertDataWithExpressions();
97 } catch (const hyperapi::HyperException& e) {
98 std::cout << e.toString() << std::endl;
99 return 1;
100 }
101 return 0;
102}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:29
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.
Maps an expression to a column
Definition Inserter.hpp:26
An inserter.
Definition Inserter.hpp:23
static SqlType text() noexcept
Returns the TEXT SQL type.
Definition SqlType.hpp:145
static SqlType integer() noexcept
Returns the INTEGER SQL type.
Definition SqlType.hpp:113
static SqlType timestamp() noexcept
Returns the TIMESTAMP SQL type.
Definition SqlType.hpp:182
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.
std::string escapeName(string_view input)
Escapes the given string for safe usage in SQL query or command strings as an identifier.
std::string escapeStringLiteral(string_view input)
Escapes the given string for safe usage in SQL query or command strings as a string literal.