using Strata.SqlTools.SqlBreakdown.Expressions.Conditional; using SqlServerCommandVisitor = Strata.SqlTools.Visitors.SqlServer.CommandVisitor; namespace Strata.SqlTools.Visitors.PostgreSql; /// /// Implements the visitor pattern to convert SQL expression objects into PostgreSQL-compatible SQL command strings. /// Inherits from SqlServer.CommandVisitor and overrides only the dialect-specific formatting methods. /// public class CommandVisitor : SqlServerCommandVisitor { private int _parameterIndex = 1; /// /// Formats an identifier for PostgreSQL using double-quote quoting. /// /// The identifier to format. /// The quoted identifier. protected override string FormatIdentifier(string identifier) => $"\"{identifier}\""; /// /// Formats a parameter name for PostgreSQL using positional parameter syntax. /// Parameters in PostgreSQL are referenced as $1, $2, $3, etc. /// /// The parameter name to format. /// A SQL string in the format "$position" where position is a number. protected override string FormatParameterName(string parameterName) { // PostgreSQL uses positional parameters: $1, $2, $3, etc. return $"${_parameterIndex++}"; } /// /// Formats a boolean literal for PostgreSQL using TRUE/FALSE keywords. /// /// The boolean value to format. /// The string "true" or "false" in lowercase. protected override string FormatBooleanLiteral(bool value) => value ? "true" : "false"; /// /// Formats a string literal for PostgreSQL with proper escaping of single quotes. /// /// The string value to format. /// A SQL string literal enclosed in single quotes with escaped quotes. protected override string FormatStringLiteral(string value) { // PostgreSQL: escape single quotes by doubling them var escaped = value.Replace("'", "''"); return $"'{escaped}'"; } /// /// Formats a case-insensitive LIKE expression for PostgreSQL using ILIKE keyword. /// /// The LIKE expression to format. /// A SQL string in the format "expression ILIKE pattern". protected override string FormatCaseInsensitiveLike(LikeExpression likeExpression) { return $"{likeExpression.Subject.Accept(this)} ILIKE {likeExpression.Pattern.Accept(this)}"; } }