Skip to content

Commit 35624f3

Browse files
author
Pierre-Luc Gagné
committed
Rename and apply formatting
1 parent 17bebfc commit 35624f3

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

examples/basic_query.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
// ===================================================================
2626

2727
namespace {
28-
constexpr auto k_host = "127.0.0.1";
29-
constexpr auto k_database = "ds_mysql_example";
30-
constexpr auto k_user = "root";
31-
constexpr auto k_password = "";
32-
constexpr unsigned int k_port = 3306;
33-
}
28+
constexpr auto host = "127.0.0.1";
29+
constexpr auto database = "ds_mysql_example";
30+
constexpr auto user = "root";
31+
constexpr auto password = "";
32+
constexpr unsigned int port = 3306;
33+
} // namespace
3434

3535
// ===================================================================
3636
// Define a typed table struct.
@@ -58,13 +58,13 @@ struct product {
5858
using description = ds_mysql::column_field<description_tag, std::optional<ds_mysql::varchar_field<512>>>;
5959
using created_at = ds_mysql::column_field<created_at_tag, ds_mysql::sql_datetime>;
6060

61-
id id_;
62-
sku sku_;
63-
name name_;
64-
price price_;
65-
stock stock_;
61+
id id_;
62+
sku sku_;
63+
name name_;
64+
price price_;
65+
stock stock_;
6666
description description_;
67-
created_at created_at_;
67+
created_at created_at_;
6868
};
6969

7070
// ===================================================================
@@ -74,17 +74,17 @@ struct product {
7474
int main() {
7575
// --- Connect ---
7676
auto db_result = ds_mysql::mysql_database::connect(ds_mysql::mysql_config{
77-
ds_mysql::host_name{k_host},
78-
ds_mysql::database_name{k_database},
79-
ds_mysql::auth_credentials{ds_mysql::user_name{k_user}, ds_mysql::user_password{k_password}},
80-
ds_mysql::port_number{k_port},
77+
ds_mysql::host_name{host},
78+
ds_mysql::database_name{database},
79+
ds_mysql::auth_credentials{ds_mysql::user_name{user}, ds_mysql::user_password{password}},
80+
ds_mysql::port_number{port},
8181
});
8282
if (!db_result) {
8383
std::println(stderr, "Connection failed: {}", db_result.error());
8484
return 1;
8585
}
8686
auto& db = *db_result;
87-
std::println("Connected to {}:{}", k_host, k_port);
87+
std::println("Connected to {}:{}", host, port);
8888

8989
// --- Create table ---
9090
if (auto r = db.execute(ds_mysql::create_table<product>().if_not_exists()); !r) {
@@ -102,13 +102,13 @@ int main() {
102102

103103
// --- Insert a row ---
104104
product row;
105-
row.id_ = product::id{0}; // AUTO_INCREMENT — value ignored on INSERT
106-
row.sku_ = product::sku{"WIDGET-001"};
107-
row.name_ = product::name{"Blue Widget"};
108-
row.price_ = product::price{9.99};
109-
row.stock_ = product::stock{42};
105+
row.id_ = product::id{0}; // AUTO_INCREMENT — value ignored on INSERT
106+
row.sku_ = product::sku{"WIDGET-001"};
107+
row.name_ = product::name{"Blue Widget"};
108+
row.price_ = product::price{9.99};
109+
row.stock_ = product::stock{42};
110110
row.description_ = product::description{std::nullopt};
111-
row.created_at_ = product::created_at{ds_mysql::sql_now};
111+
row.created_at_ = product::created_at{ds_mysql::sql_now};
112112

113113
if (auto r = db.execute(ds_mysql::insert_into<product>().values(row)); !r) {
114114
std::println(stderr, "INSERT failed: {}", r.error());
@@ -118,18 +118,15 @@ int main() {
118118

119119
// --- Query all products ---
120120
auto rows = db.query(
121-
ds_mysql::select<product::id, product::sku, product::name, product::price, product::stock>()
122-
.from<product>()
123-
);
121+
ds_mysql::select<product::id, product::sku, product::name, product::price, product::stock>().from<product>());
124122
if (!rows) {
125123
std::println(stderr, "SELECT failed: {}", rows.error());
126124
return 1;
127125
}
128126

129127
std::println("\nProducts ({} row(s)):", rows->size());
130128
for (auto const& [id, sku, name, price, stock] : *rows) {
131-
std::println(" [{:>3}] {:<20} {:>8.2f} stock={}",
132-
id, std::string_view{name}, price, stock);
129+
std::println(" [{:>3}] {:<20} {:>8.2f} stock={}", id, std::string_view{name}, price, stock);
133130
}
134131

135132
// --- Count rows ---
@@ -140,4 +137,3 @@ int main() {
140137

141138
return 0;
142139
}
143-

0 commit comments

Comments
 (0)