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

SQLite-specific SQL dialect. More...

#include <sql_dialect.h>

Inheritance diagram for database::query::sqlite_dialect:
Inheritance graph
Collaboration diagram for database::query::sqlite_dialect:
Collaboration graph

Public Member Functions

std::string placeholder (int index) const override
 Generate parameter placeholder.
 
std::string quote_identifier (std::string_view name) const override
 Quote an identifier (table or column name)
 
std::string escape_string (std::string_view str) const override
 Escape a string value for safe SQL inclusion.
 
std::string returning_clause (std::string_view column="") const override
 Generate RETURNING clause for INSERT/UPDATE.
 
std::string upsert_clause (const std::vector< std::string > &conflict_columns, const std::vector< std::string > &update_columns) const override
 Generate UPSERT (INSERT OR UPDATE) clause.
 
std::string limit_clause (size_t limit, size_t offset) const override
 Generate LIMIT clause.
 
std::string auto_increment () const override
 Get auto-increment column definition.
 
std::string current_timestamp () const override
 Get current timestamp function.
 
std::string concat_operator () const override
 Get string concatenation operator.
 
bool supports_feature (const std::string &feature) const override
 Check if database supports specific feature.
 
- Public Member Functions inherited from database::query::sql_dialect
virtual ~sql_dialect ()=default
 

Additional Inherited Members

- Static Public Member Functions inherited from database::query::sql_dialect
static std::unique_ptr< sql_dialectcreate (database_types type)
 Factory method to create appropriate dialect.
 

Detailed Description

SQLite-specific SQL dialect.

Definition at line 175 of file sql_dialect.h.

Member Function Documentation

◆ auto_increment()

std::string database::query::sqlite_dialect::auto_increment ( ) const
overridevirtual

Get auto-increment column definition.

Returns
SQL fragment for auto-increment column

Examples:

  • PostgreSQL: "SERIAL"
  • SQLite: "AUTOINCREMENT"

Implements database::query::sql_dialect.

Definition at line 204 of file sql_dialect.cpp.

204 {
205 return "AUTOINCREMENT";
206}

◆ concat_operator()

std::string database::query::sqlite_dialect::concat_operator ( ) const
overridevirtual

Get string concatenation operator.

Returns
Operator or function for concatenation

Examples:

  • PostgreSQL: "||"
  • SQLite: "||"

Implements database::query::sql_dialect.

Definition at line 212 of file sql_dialect.cpp.

212 {
213 return "||";
214}

◆ current_timestamp()

std::string database::query::sqlite_dialect::current_timestamp ( ) const
overridevirtual

Get current timestamp function.

Returns
SQL function for current timestamp

Examples:

  • PostgreSQL: "CURRENT_TIMESTAMP"
  • SQLite: "CURRENT_TIMESTAMP"

Implements database::query::sql_dialect.

Definition at line 208 of file sql_dialect.cpp.

208 {
209 return "CURRENT_TIMESTAMP";
210}

◆ escape_string()

std::string database::query::sqlite_dialect::escape_string ( std::string_view str) const
overridevirtual

Escape a string value for safe SQL inclusion.

Parameters
strThe string to escape
Returns
Escaped string (without surrounding quotes)

Implements database::query::sql_dialect.

Definition at line 147 of file sql_dialect.cpp.

147 {
148 std::string result;
149 result.reserve(str.size());
150 for (char c : str) {
151 if (c == '\'') {
152 result += "''"; // Escape single quotes by doubling
153 } else {
154 result += c;
155 }
156 }
157 return result;
158}

◆ limit_clause()

std::string database::query::sqlite_dialect::limit_clause ( size_t limit,
size_t offset ) const
overridevirtual

Generate LIMIT clause.

Parameters
limitMaximum number of rows
offsetNumber of rows to skip
Returns
Database-specific LIMIT clause

Examples:

  • PostgreSQL: "LIMIT 10 OFFSET 5"
  • SQLite: "LIMIT 10 OFFSET 5"

Implements database::query::sql_dialect.

Definition at line 196 of file sql_dialect.cpp.

196 {
197 std::string clause = "LIMIT " + std::to_string(limit);
198 if (offset > 0) {
199 clause += " OFFSET " + std::to_string(offset);
200 }
201 return clause;
202}

◆ placeholder()

std::string database::query::sqlite_dialect::placeholder ( int index) const
overridevirtual

Generate parameter placeholder.

Parameters
index1-based parameter index
Returns
Database-specific placeholder string

Examples:

  • PostgreSQL: "$1", "$2", "$3"
  • SQLite: "?1", "?2", "?3"

Implements database::query::sql_dialect.

Definition at line 128 of file sql_dialect.cpp.

128 {
129 return "?" + std::to_string(index);
130}

◆ quote_identifier()

std::string database::query::sqlite_dialect::quote_identifier ( std::string_view name) const
overridevirtual

Quote an identifier (table or column name)

Parameters
nameThe identifier to quote
Returns
Quoted identifier

Examples:

  • PostgreSQL: "\"column_name\""
  • SQLite: "\"column_name\""

Implements database::query::sql_dialect.

Definition at line 132 of file sql_dialect.cpp.

132 {
133 std::string result;
134 result.reserve(name.size() + 2);
135 result += '"';
136 for (char c : name) {
137 if (c == '"') {
138 result += "\"\""; // Escape double quotes by doubling
139 } else {
140 result += c;
141 }
142 }
143 result += '"';
144 return result;
145}

Referenced by returning_clause(), and upsert_clause().

Here is the caller graph for this function:

◆ returning_clause()

std::string database::query::sqlite_dialect::returning_clause ( std::string_view column = "") const
overridevirtual

Generate RETURNING clause for INSERT/UPDATE.

Parameters
columnThe column to return (empty for all)
Returns
Database-specific RETURNING clause or empty if unsupported

Examples:

  • PostgreSQL: " RETURNING id"
  • SQLite: " RETURNING id" (3.35+)

Implements database::query::sql_dialect.

Definition at line 160 of file sql_dialect.cpp.

160 {
161 // SQLite 3.35+ supports RETURNING
162 if (column.empty()) {
163 return " RETURNING *";
164 }
165 return " RETURNING " + quote_identifier(column);
166}
std::string quote_identifier(std::string_view name) const override
Quote an identifier (table or column name)

References quote_identifier().

Here is the call graph for this function:

◆ supports_feature()

bool database::query::sqlite_dialect::supports_feature ( const std::string & feature) const
overridevirtual

Check if database supports specific feature.

Parameters
featureFeature name
Returns
true if supported

Implements database::query::sql_dialect.

Definition at line 216 of file sql_dialect.cpp.

216 {
217 if (feature == "window_functions") return true; // SQLite 3.25+
218 if (feature == "cte") return true;
219 if (feature == "recursive_cte") return true;
220 if (feature == "json") return true; // SQLite 3.38+
221 if (feature == "full_outer_join") return false; // Not supported
222 if (feature == "returning") return true; // SQLite 3.35+
223 if (feature == "array") return false; // Not supported
224 if (feature == "upsert") return true; // ON CONFLICT
225 return false;
226}

◆ upsert_clause()

std::string database::query::sqlite_dialect::upsert_clause ( const std::vector< std::string > & conflict_columns,
const std::vector< std::string > & update_columns ) const
overridevirtual

Generate UPSERT (INSERT OR UPDATE) clause.

Parameters
conflict_columnsColumns that define conflict
update_columnsColumns to update on conflict
Returns
Database-specific upsert clause

Examples:

  • PostgreSQL: "ON CONFLICT (id) DO UPDATE SET ..."
  • SQLite: "ON CONFLICT (id) DO UPDATE SET ..."

Implements database::query::sql_dialect.

Definition at line 168 of file sql_dialect.cpp.

170 {
171
172 if (conflict_columns.empty()) {
173 return "";
174 }
175
176 std::string result = "ON CONFLICT (";
177 for (size_t i = 0; i < conflict_columns.size(); ++i) {
178 if (i > 0) result += ", ";
179 result += quote_identifier(conflict_columns[i]);
180 }
181 result += ")";
182
183 if (update_columns.empty()) {
184 result += " DO NOTHING";
185 } else {
186 result += " DO UPDATE SET ";
187 for (size_t i = 0; i < update_columns.size(); ++i) {
188 if (i > 0) result += ", ";
189 std::string quoted = quote_identifier(update_columns[i]);
190 result += quoted + " = excluded." + quoted;
191 }
192 }
193 return result;
194}

References quote_identifier().

Here is the call graph for this function:

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