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

PostgreSQL-specific SQL dialect. More...

#include <sql_dialect.h>

Inheritance diagram for database::query::postgresql_dialect:
Inheritance graph
Collaboration diagram for database::query::postgresql_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

PostgreSQL-specific SQL dialect.

Definition at line 155 of file sql_dialect.h.

Member Function Documentation

◆ auto_increment()

std::string database::query::postgresql_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 101 of file sql_dialect.cpp.

101 {
102 return "SERIAL";
103}

◆ concat_operator()

std::string database::query::postgresql_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 109 of file sql_dialect.cpp.

109 {
110 return "||";
111}

◆ current_timestamp()

std::string database::query::postgresql_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 105 of file sql_dialect.cpp.

105 {
106 return "CURRENT_TIMESTAMP";
107}

◆ escape_string()

std::string database::query::postgresql_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 43 of file sql_dialect.cpp.

43 {
44 std::string result;
45 result.reserve(str.size());
46 for (char c : str) {
47 if (c == '\'') {
48 result += "''"; // Escape single quotes by doubling
49 } else if (c == '\\') {
50 result += "\\\\"; // Escape backslashes
51 } else {
52 result += c;
53 }
54 }
55 return result;
56}

◆ limit_clause()

std::string database::query::postgresql_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 93 of file sql_dialect.cpp.

93 {
94 std::string clause = "LIMIT " + std::to_string(limit);
95 if (offset > 0) {
96 clause += " OFFSET " + std::to_string(offset);
97 }
98 return clause;
99}

◆ placeholder()

std::string database::query::postgresql_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 24 of file sql_dialect.cpp.

24 {
25 return "$" + std::to_string(index);
26}

◆ quote_identifier()

std::string database::query::postgresql_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 28 of file sql_dialect.cpp.

28 {
29 std::string result;
30 result.reserve(name.size() + 2);
31 result += '"';
32 for (char c : name) {
33 if (c == '"') {
34 result += "\"\""; // Escape double quotes by doubling
35 } else {
36 result += c;
37 }
38 }
39 result += '"';
40 return result;
41}

Referenced by returning_clause(), and upsert_clause().

Here is the caller graph for this function:

◆ returning_clause()

std::string database::query::postgresql_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 58 of file sql_dialect.cpp.

58 {
59 if (column.empty()) {
60 return " RETURNING *";
61 }
62 return " RETURNING " + quote_identifier(column);
63}
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::postgresql_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 113 of file sql_dialect.cpp.

113 {
114 // PostgreSQL supports most SQL features
115 if (feature == "window_functions") return true;
116 if (feature == "cte") return true; // Common Table Expressions
117 if (feature == "recursive_cte") return true;
118 if (feature == "json") return true;
119 if (feature == "full_outer_join") return true;
120 if (feature == "returning") return true; // RETURNING clause
121 if (feature == "array") return true;
122 if (feature == "upsert") return true; // ON CONFLICT
123 return false;
124}

◆ upsert_clause()

std::string database::query::postgresql_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 65 of file sql_dialect.cpp.

67 {
68
69 if (conflict_columns.empty()) {
70 return "";
71 }
72
73 std::string result = "ON CONFLICT (";
74 for (size_t i = 0; i < conflict_columns.size(); ++i) {
75 if (i > 0) result += ", ";
76 result += quote_identifier(conflict_columns[i]);
77 }
78 result += ")";
79
80 if (update_columns.empty()) {
81 result += " DO NOTHING";
82 } else {
83 result += " DO UPDATE SET ";
84 for (size_t i = 0; i < update_columns.size(); ++i) {
85 if (i > 0) result += ", ";
86 std::string quoted = quote_identifier(update_columns[i]);
87 result += quoted + " = EXCLUDED." + quoted;
88 }
89 }
90 return result;
91}

References quote_identifier().

Here is the call graph for this function:

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