Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
insert_data_into_multiple_tables.cpp
1
8#include <iostream>
9#include <string>
10
13static const hyperapi::TableDefinition ordersTable{"Orders", // Since the table name is not prefixed with an explicit schema name, the table will reside in the default "public" namespace.
14 {
21 }};
22static const hyperapi::TableDefinition customerTable{"Customer", // Since the table name is not prefixed with an explicit schema name, the table will reside in the default "public" namespace.
27static const hyperapi::TableDefinition productTable{"Products", // Since the table name is not prefixed with an explicit schema name, the table will reside in the default "public" namespace.
28 {
33 }};
34static const hyperapi::TableDefinition lineItemsTable{"Line Items", // Since the table name is not prefixed with an explicit schema name, the table will reside in the default "public" namespace.
35 {
43 }};
44
45static void runInsertDataIntoMultipleTables() {
46 std::cout << "EXAMPLE - Insert data into multiple tables within a new Hyper file" << std::endl;
47 const std::string pathToDatabase = "data/superstore.hyper";
48
49 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
50 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
51 {
53 // Creates new Hyper file "superstore.hyper"
54 // Replaces existing file with hyperapi::CreateMode::CreateAndReplace if it already exists.
55 {
56 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase, hyperapi::CreateMode::CreateAndReplace);
57 const hyperapi::Catalog& catalog = connection.getCatalog();
58
59 // Create multiple tables.
60 catalog.createTable(ordersTable);
61 catalog.createTable(customerTable);
62 catalog.createTable(productTable);
63 catalog.createTable(lineItemsTable);
64
65 // Insert data into Orders table.
66 {
67 hyperapi::Inserter inserter(connection, ordersTable);
68 inserter.addRow(static_cast<int16_t>(399), "DK-13375", hyperapi::Date{2012, 9, 7}, "CA-2011-100006", hyperapi::Date{2012, 9, 13}, "Standard Class");
69 inserter.addRow(static_cast<int16_t>(530), "EB-13705", hyperapi::Date{2012, 7, 8}, "CA-2011-100090", hyperapi::Date{2012, 7, 12}, "Standard Class");
70 inserter.execute();
71 }
72
73 // Insert data into Customer table.
74 {
75 hyperapi::Inserter inserter(connection, customerTable);
76 inserter.addRow("DK-13375", "Dennis Kane", 518, "Consumer");
77 inserter.addRow("EB-13705", "Ed Braxton", 815, "Corporate");
78 inserter.execute();
79 }
80
81 // Insert individual row into Product table.
82 {
83 hyperapi::Inserter inserter(connection, productTable);
84 inserter.addRow("TEC-PH-10002075", "Technology", "Phones", "AT&T EL51110 DECT");
85 inserter.execute();
86 }
87
88 // Insert data into Line Items table.
89 {
90 hyperapi::Inserter inserter(connection, lineItemsTable);
91 inserter.addRow(2718, "CA-2011-100006", "TEC-PH-10002075", 377.97, int16_t{3}, 0.0, 109.6113);
92 inserter.addRow(2719, "CA-2011-100090", "TEC-PH-10002075", 377.97, int16_t{3}, hyperapi::null, 109.6113);
93 inserter.execute();
94 }
95
96 for (auto& tableName : {ordersTable.getTableName(), customerTable.getTableName(), productTable.getTableName(), lineItemsTable.getTableName()}) {
97 // `executeScalarQuery` is for executing a query that returns exactly one row with one column.
98 int64_t rowCount = connection.executeScalarQuery<int64_t>("SELECT COUNT(*) FROM " + tableName.toString());
99 std::cout << "The number of rows in table " << tableName << " is " << rowCount << "." << std::endl;
100 }
101 }
102 std::cout << "The connection to the Hyper file has been closed." << std::endl;
103 }
104 std::cout << "The Hyper Process has been shut down." << std::endl;
105}
106
107int main() {
108 try {
109 runInsertDataIntoMultipleTables();
110 } catch (const hyperapi::HyperException& e) {
111 std::cout << e.toString() << std::endl;
112 return 1;
113 }
114 return 0;
115}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
void createTable(const hyperapi::TableDefinition &table_definition) const
Creates a SQL table with the given table definition.
Defines a Hyper connection.
A date data value.
Definition Date.hpp:18
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 smallInt() noexcept
Returns the SMALL INTEGER SQL type.
Definition SqlType.hpp:106
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
static SqlType date() noexcept
Returns the DATE SQL type.
Definition SqlType.hpp:165
static SqlType doublePrecision() noexcept
Returns the DOUBLE PRECISION SQL type.
Definition SqlType.hpp:128
A Column of a table definition.
const TableName & getTableName() const noexcept
Returns the name of the table.
The main header of the Hyper API for C++.
@ NotNullable
The column cannot contain NULL values.
@ Nullable
The column can 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.