Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
delete_data_in_existing_hyper_file.cpp
1
7#include <fstream>
9#include <iostream>
10#include <string>
11
16static void copy(const std::string& sourcePath, const std::string& destinationPath) {
17 std::ifstream source(sourcePath, std::ios::binary);
18 std::ofstream destination(destinationPath, std::ios::binary);
19 destination << source.rdbuf();
20 source.close();
21 destination.close();
22}
23
24static void runDeleteDataInExistingHyperFile() {
25 std::cout << "EXAMPLE - Delete data from an existing Hyper file" << std::endl;
26
27 // Path to a Hyper file containing all data inserted into Customer, Product, Orders and LineItems table
28 // See "insert_data_into_multiple_tables.cpp" for an example that works with the complete schema.
29 const std::string pathToSourceDatabase = "data/superstore_sample.hyper";
30
31 // Make a copy of the superstore example Hyper file.
32 const std::string pathToDatabase = "data/superstore_sample_delete.hyper";
33 copy(pathToSourceDatabase, pathToDatabase);
34
35 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
36 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
37 {
39
40 // Connect to existing Hyper file "superstore_sample_delete.hyper".
41 {
42 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase);
43
44 std::cout << "Delete all rows from customer with the name 'Dennis Kane' from table " << hyperapi::escapeName("Orders") << "." << std::endl;
45 // `executeCommand` executes a SQL statement and returns the impacted row count.
46 int64_t rowCount = connection.executeCommand(
47 "DELETE FROM " + hyperapi::escapeName("Orders") + " WHERE " + hyperapi::escapeName("Customer ID") + " = ANY(SELECT " +
48 hyperapi::escapeName("Customer ID") + " FROM " + hyperapi::escapeName("Customer") + " WHERE " + hyperapi::escapeName("Customer Name") + " = " +
49 hyperapi::escapeStringLiteral("Dennis Kane") + ")");
50 std::cout << "The number of deleted rows in table " << hyperapi::escapeName("Orders") << " is " << rowCount << "." << std::endl
51 << std::endl;
52
53 std::cout << "Delete all rows from customer with the name 'Dennis Kane' from table " << hyperapi::escapeName("Customer") << "." << std::endl;
54 rowCount = connection.executeCommand(
55 "DELETE FROM " + hyperapi::escapeName("Customer") + " WHERE " + hyperapi::escapeName("Customer Name") + " =" +
56 hyperapi::escapeStringLiteral("Dennis Kane"));
57
58 std::cout << "The number of deleted rows in table Customer is " << rowCount << "." << std::endl;
59 }
60 std::cout << "The connection to the Hyper file has been closed." << std::endl;
61 }
62 std::cout << "The Hyper Process has been shut down." << std::endl;
63}
64
65int main() {
66 try {
67 runDeleteDataInExistingHyperFile();
68 } catch (const hyperapi::HyperException& e) {
69 std::cout << e.toString() << std::endl;
70 return 1;
71 }
72 return 0;
73}
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.
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.