Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
insert_data_with_expressions.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 runInsertDataWithExpressions() {
22 std::cout << "EXAMPLE - Push down computations to Hyper during data insertion using expressions" << std::endl;
23 const std::string pathToDatabase = "data/orders.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 "orders.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 // Hyper API's Inserter allows users to transform data during insertion.
40 // To make use of data transformation during insertion, the inserter requires the following inputs
41 // 1. The connection to the Hyper instance containing the table
42 // 2. The table name or table defintion into which data is inserted
43 // 3. List of hyperapi::Inserter::ColumnMapping.
44 // This list informs the inserter how each column in the target table is tranformed.
45 // The list must contain all the columns into which data is inserted.
46 // "hyperapi::Inserter::ColumnMapping" maps a valid SQL expression (if any) to a column in the target table
47 // For example hyperapi::Inserter::ColumnMapping("target_column", hyperapi::escapeName("colA") + "*" + hyperapi::escapeName("colB"))
48 // The column "target_column" contains the product of "colA" and "colB" after successful insertion.
49 // SQL expression string is optional in Inserter.ColumnMapping.
50 // For a column without any transformation only the column name is required.
51 // For example hyperapi::Inserter::ColumnMapping{"no_data_transformation_column"}
52 // 4. Inserter Definition, a list of column definitions for all the input values provided during insertion.
53
54 // Inserter definition contains the column definition for the values that are inserted.
55 std::vector<hyperapi::TableDefinition::Column> inserterDefinition{
60
61 // Column 'Order Id' is inserted into "Extract"."Extract" as-is.
62 // Column 'Ship Timestamp' in "Extract"."Extract" of timestamp type is computed from Column 'Ship Timestamp Text' of text type using 'to_timestamp()'.
63 // Column 'Ship Mode' is inserted into "Extract"."Extract" as-is.
64 // Column 'Ship Priority' is "Extract"."Extract" of integer type is computed from Colum 'Ship Priority Text' of text type using 'CASE' statement.
65 std::string textToTimeStampExpression = "to_timestamp(" + hyperapi::escapeName("Ship Timestamp Text") + ", " + hyperapi::escapeStringLiteral("YYYY-MM-DD HH24:MI:SS") + ")";
66 std::string shipPriorityAsIntCaseExpression = "CASE " + hyperapi::escapeName("Ship Priority Text") +
67 " WHEN " + hyperapi::escapeStringLiteral("Urgent") + " THEN 1 " +
68 " WHEN " + hyperapi::escapeStringLiteral("Medium") + " THEN 2 " +
69 " WHEN " + hyperapi::escapeStringLiteral("Low") + " THEN 3 END";
70
71 std::vector<hyperapi::Inserter::ColumnMapping> columnMappings{
73 hyperapi::Inserter::ColumnMapping{"Ship Timestamp", textToTimeStampExpression},
75 hyperapi::Inserter::ColumnMapping{"Ship Priority", shipPriorityAsIntCaseExpression}};
76
77 // Insert data into the "Extract"."Extract" table using expressions.
78 {
79 hyperapi::Inserter inserter(connection, extractTable, columnMappings, inserterDefinition);
80 inserter.addRow(399, "2012-09-13 10:00:00", "Express Class", "Urgent");
81 inserter.addRow(530, "2012-07-12 14:00:00", "Standard Class", "Low");
82 inserter.execute();
83 }
84
85 // Number of rows in the "Extract"."Extract" table.
86 // `executeScalarQuery` is for executing a query that returns exactly one row with one column.
87 int64_t rowCount = connection.executeScalarQuery<int64_t>("SELECT COUNT(*) FROM " + extractTable.getTableName().toString());
88 std::cout << "The number of rows in table " << extractTable.getTableName() << " is " << rowCount << "." << std::endl;
89 }
90 std::cout << "The connection to the Hyper file has been closed." << std::endl;
91 }
92 std::cout << "The Hyper Process has been shut down." << std::endl;
93}
94
95int main() {
96 try {
97 runInsertDataWithExpressions();
98 } catch (const hyperapi::HyperException& e) {
99 std::cout << e.toString() << std::endl;
100 return 1;
101 }
102 return 0;
103}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
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:27
An inserter.
Definition Inserter.hpp:24
static SqlType text() noexcept
Returns the TEXT SQL type.
Definition SqlType.hpp:143
static SqlType integer() noexcept
Returns the INTEGER SQL type.
Definition SqlType.hpp:111
static SqlType timestamp() noexcept
Returns the TIMESTAMP SQL type.
Definition SqlType.hpp:180
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.