Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::query::value_formatter Class Reference

Formats database values for different backends. More...

#include <value_formatter.h>

Collaboration diagram for database::query::value_formatter:
Collaboration graph

Public Member Functions

 value_formatter (database_types db_type)
 Construct formatter for specific database type.
 
std::string format (const core::database_value &value) const
 Format a database value.
 
std::string escape_string (const std::string &str) const
 Escape a string value.
 
std::string escape_identifier (const std::string &identifier) const
 Quote and escape an identifier (table/column name)
 
std::string null_literal () const
 Get NULL literal for this database.
 
std::string bool_literal (bool val) const
 Get boolean literal.
 

Private Member Functions

std::string format_string (const std::string &str) const
 
std::string format_int (int64_t num) const
 
std::string format_double (double num) const
 
std::string format_bool (bool val) const
 
std::string format_blob (const std::vector< uint8_t > &data) const
 
std::string escape_postgresql_string (const std::string &str) const
 
std::string escape_sqlite_string (const std::string &str) const
 

Private Attributes

database_types db_type_
 

Detailed Description

Formats database values for different backends.

Responsibilities:

  • Format different data types (string, int, double, bool, blob, null)
  • Database-specific escaping (PostgreSQL, SQLite)
  • Identifier quoting
  • NULL literal formatting

Single Responsibility Principle:

  • This class only handles value formatting and escaping
  • Does not handle query building or condition logic

Thread Safety:

  • Thread-safe after construction (immutable state)
  • Can be shared across threads

Definition at line 32 of file value_formatter.h.

Constructor & Destructor Documentation

◆ value_formatter()

database::query::value_formatter::value_formatter ( database_types db_type)
explicit

Construct formatter for specific database type.

Parameters
db_typeTarget database type

Definition at line 13 of file value_formatter.cpp.

14 : db_type_(db_type) {}

Member Function Documentation

◆ bool_literal()

std::string database::query::value_formatter::bool_literal ( bool val) const

Get boolean literal.

Parameters
valBoolean value
Returns
Database-specific boolean representation

Definition at line 80 of file value_formatter.cpp.

80 {
81 switch (db_type_) {
84 return val ? "TRUE" : "FALSE";
86 return val ? "true" : "false";
88 return val ? "1" : "0";
89 default:
90 return val ? "1" : "0";
91 }
92}
@ mongodb
Indicates a MongoDB database (future implementation).
@ sqlite
Indicates a SQLite database.
@ redis
Indicates a Redis database (future implementation).
@ postgres
Indicates a PostgreSQL database.

References db_type_, database::mongodb, database::postgres, database::redis, and database::sqlite.

Referenced by format_bool(), and TEST_F().

Here is the caller graph for this function:

◆ escape_identifier()

std::string database::query::value_formatter::escape_identifier ( const std::string & identifier) const

Quote and escape an identifier (table/column name)

Parameters
identifierIdentifier to escape
Returns
Quoted and escaped identifier

Definition at line 62 of file value_formatter.cpp.

62 {
63 switch (db_type_) {
65 return "\"" + identifier + "\"";
67 return "\"" + identifier + "\"";
70 return identifier; // NoSQL doesn't quote identifiers
71 default:
72 return "\"" + identifier + "\"";
73 }
74}

References db_type_, database::mongodb, database::postgres, database::redis, and database::sqlite.

Referenced by database::query::join_builder::build(), database::query::condition_builder::format_condition(), and TEST_F().

Here is the caller graph for this function:

◆ escape_postgresql_string()

std::string database::query::value_formatter::escape_postgresql_string ( const std::string & str) const
private

Definition at line 154 of file value_formatter.cpp.

154 {
155 std::string result;
156 result.reserve(str.length() + 10);
157
158 for (char c : str) {
159 switch (c) {
160 case '\'':
161 result += "''"; // PostgreSQL escapes single quotes by doubling
162 break;
163 case '\\':
164 result += "\\\\"; // Escape backslashes
165 break;
166 case '\0':
167 result += "\\0"; // NULL byte
168 break;
169 case '\n':
170 result += "\\n"; // Newline
171 break;
172 case '\r':
173 result += "\\r"; // Carriage return
174 break;
175 case '\t':
176 result += "\\t"; // Tab
177 break;
178 default:
179 result += c;
180 }
181 }
182
183 return result;
184}

Referenced by escape_string().

Here is the caller graph for this function:

◆ escape_sqlite_string()

std::string database::query::value_formatter::escape_sqlite_string ( const std::string & str) const
private

Definition at line 186 of file value_formatter.cpp.

186 {
187 std::string result;
188 result.reserve(str.length() + 10);
189
190 for (char c : str) {
191 if (c == '\'') {
192 result += "''"; // SQLite escapes single quotes by doubling
193 } else {
194 result += c;
195 }
196 }
197
198 return result;
199}

Referenced by escape_string().

Here is the caller graph for this function:

◆ escape_string()

std::string database::query::value_formatter::escape_string ( const std::string & str) const

Escape a string value.

Parameters
strString to escape
Returns
Escaped string (without quotes)

Definition at line 47 of file value_formatter.cpp.

47 {
48 switch (db_type_) {
50 return escape_postgresql_string(str);
52 return escape_sqlite_string(str);
55 // NoSQL databases handle escaping differently
56 return str;
57 default:
58 return escape_sqlite_string(str); // Safe default
59 }
60}
std::string escape_sqlite_string(const std::string &str) const
std::string escape_postgresql_string(const std::string &str) const

References db_type_, escape_postgresql_string(), escape_sqlite_string(), database::mongodb, database::postgres, database::redis, and database::sqlite.

Referenced by format_string(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format()

std::string database::query::value_formatter::format ( const core::database_value & value) const

Format a database value.

Parameters
valueValue to format
Returns
Formatted string representation

Definition at line 16 of file value_formatter.cpp.

16 {
17 return std::visit([this](auto&& arg) -> std::string {
18 using T = std::decay_t<decltype(arg)>;
19
20 if constexpr (std::is_same_v<T, std::monostate>) {
21 return null_literal();
22 }
23 else if constexpr (std::is_same_v<T, std::string>) {
24 return format_string(arg);
25 }
26 else if constexpr (std::is_same_v<T, int>) {
27 return format_int(arg);
28 }
29 else if constexpr (std::is_same_v<T, int64_t>) {
30 return format_int(arg);
31 }
32 else if constexpr (std::is_same_v<T, double>) {
33 return format_double(arg);
34 }
35 else if constexpr (std::is_same_v<T, bool>) {
36 return format_bool(arg);
37 }
38 else if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
39 return format_blob(arg);
40 }
41 else {
42 return null_literal();
43 }
44 }, value);
45}
std::string format_bool(bool val) const
std::string format_int(int64_t num) const
std::string format_blob(const std::vector< uint8_t > &data) const
std::string format_double(double num) const
std::string format_string(const std::string &str) const
std::string null_literal() const
Get NULL literal for this database.

References format_blob(), format_bool(), format_double(), format_int(), format_string(), and null_literal().

Referenced by database::query::condition_builder::format_condition(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_blob()

std::string database::query::value_formatter::format_blob ( const std::vector< uint8_t > & data) const
private

Definition at line 123 of file value_formatter.cpp.

123 {
124 switch (db_type_) {
126 // PostgreSQL hex format: '\x...'
127 std::ostringstream oss;
128 oss << "'\\x";
129 for (uint8_t byte : data) {
130 oss << std::hex << std::setw(2) << std::setfill('0')
131 << static_cast<int>(byte);
132 }
133 oss << "'";
134 return oss.str();
135 }
137 // SQLite hex format: X'...'
138 std::ostringstream oss;
139 oss << "X'";
140 for (uint8_t byte : data) {
141 oss << std::hex << std::setw(2) << std::setfill('0')
142 << static_cast<int>(byte);
143 }
144 oss << "'";
145 return oss.str();
146 }
147 default:
148 return "NULL"; // Unsupported for this database
149 }
150}

References db_type_, database::postgres, and database::sqlite.

Referenced by format().

Here is the caller graph for this function:

◆ format_bool()

std::string database::query::value_formatter::format_bool ( bool val) const
private

Definition at line 119 of file value_formatter.cpp.

119 {
120 return bool_literal(val);
121}
std::string bool_literal(bool val) const
Get boolean literal.

References bool_literal().

Referenced by format().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_double()

std::string database::query::value_formatter::format_double ( double num) const
private

Definition at line 104 of file value_formatter.cpp.

104 {
105 // Handle special values
106 if (std::isnan(num)) {
107 return "'NaN'";
108 }
109 if (std::isinf(num)) {
110 return num > 0 ? "'Infinity'" : "'-Infinity'";
111 }
112
113 // Format with appropriate precision
114 std::ostringstream oss;
115 oss << std::setprecision(15) << num;
116 return oss.str();
117}

Referenced by format().

Here is the caller graph for this function:

◆ format_int()

std::string database::query::value_formatter::format_int ( int64_t num) const
private

Definition at line 100 of file value_formatter.cpp.

100 {
101 return std::to_string(num);
102}

Referenced by format().

Here is the caller graph for this function:

◆ format_string()

std::string database::query::value_formatter::format_string ( const std::string & str) const
private

Definition at line 96 of file value_formatter.cpp.

96 {
97 return "'" + escape_string(str) + "'";
98}
std::string escape_string(const std::string &str) const
Escape a string value.

References escape_string().

Referenced by format().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ null_literal()

std::string database::query::value_formatter::null_literal ( ) const

Get NULL literal for this database.

Returns
"NULL" for most databases

Definition at line 76 of file value_formatter.cpp.

76 {
77 return "NULL";
78}

Referenced by format(), and TEST_F().

Here is the caller graph for this function:

Member Data Documentation

◆ db_type_

database_types database::query::value_formatter::db_type_
private

Definition at line 75 of file value_formatter.h.

Referenced by bool_literal(), escape_identifier(), escape_string(), and format_blob().


The documentation for this class was generated from the following files: