Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
update_data_in_existing_hyper_file.cpp
1#include <fstream>
3#include <iostream>
4#include <string>
5
15static void copy(const std::string& sourcePath, const std::string& destinationPath) {
16 std::ifstream source(sourcePath, std::ios::binary);
17 std::ofstream destination(destinationPath, std::ios::binary);
18 destination << source.rdbuf();
19 source.close();
20 destination.close();
21}
22
23static void runUpdateDataInExistingHyperFile() {
24 std::cout << "EXAMPLE - Update existing data in a Hyper file" << std::endl;
25
26 // Path to a Hyper file containing all data inserted into Customer, Product, Orders and LineItems table
27 // See "insert_data_into_multiple_tables.cpp" for an example that works with the complete schema.
28 const std::string pathToSourceDatabase = "data/superstore_sample.hyper";
29
30 // Make a copy of the superstore example Hyper file.
31 const std::string pathToDatabase = "data/superstore_sample_update.hyper";
32 copy(pathToSourceDatabase, pathToDatabase);
33
34 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
35 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
36 {
38
39 // Connect to existing Hyper file "superstore_sample_update.hyper".
40 {
41 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase);
42
43 hyperapi::Result rowsPreUpdate = connection.executeQuery(
44 "SELECT " + hyperapi::escapeName("Loyalty Reward Points") + ", " + hyperapi::escapeName("Segment") + " FROM " + hyperapi::escapeName("Customer"));
45 std::cout << "Pre-Update: Individual rows showing 'Loyalty Reward Points' and 'Segment' columns: " << std::endl;
46 for (const hyperapi::Row& row : rowsPreUpdate) {
47 for (const hyperapi::Value& value : row) {
48 std::cout << value << '\t';
49 }
50 std::cout << '\n';
51 }
52 std::cout << std::endl;
53
54 std::cout << "Update 'Customers' table by adding 50 Loyalty Reward Points to all Corporate Customers." << std::endl;
55 int64_t rowCount = connection.executeCommand(
56 "UPDATE " + hyperapi::escapeName("Customer") + " SET " + hyperapi::escapeName("Loyalty Reward Points") + " = " +
57 hyperapi::escapeName("Loyalty Reward Points") + " + 50 " + "WHERE " + hyperapi::escapeName("Segment") + " = " +
59
60 std::cout << "The number of updated rows in table " << hyperapi::escapeName("Customer") << " is " << rowCount << "." << std::endl;
61
62 hyperapi::Result rowsPostUpdate = connection.executeQuery(
63 "SELECT " + hyperapi::escapeName("Loyalty Reward Points") + ", " + hyperapi::escapeName("Segment") + " FROM " + hyperapi::escapeName("Customer"));
64 for (const hyperapi::Row& row : rowsPostUpdate) {
65 for (const hyperapi::Value& value : row) {
66 std::cout << value << '\t';
67 }
68 std::cout << '\n';
69 }
70 }
71 std::cout << "The connection to the Hyper file has been closed." << std::endl;
72 }
73 std::cout << "The Hyper Process has been shut down." << std::endl;
74}
75
76int main() {
77 try {
78 runUpdateDataInExistingHyperFile();
79 } catch (const hyperapi::HyperException& e) {
80 std::cout << e.toString() << std::endl;
81 return 1;
82 }
83 return 0;
84}
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.
Base class for a result of a query.
Definition Result.hpp:326
A Row inside a chunk.
Definition Result.hpp:210
A value inside a row.
Definition Result.hpp:44
The main header of the Hyper API for C++.
@ SendUsageDataToTableau
Telemetry data will be sent to tableau to help improve the Hyper API.
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.