Documentation
¶
Index ¶
- func IsValidIdentifier(s string) bool
- func ParameterizedSQLQueryNamedBasedToIndexBased(dbType base.DXDatabaseType, query string, namedArgs utils.JSON) (string, []any, error)
- func QuoteFieldWithPrefixByDbType(dbType base.DXDatabaseType, field string) string
- func QuoteIdentifierByDbType(dbType base.DXDatabaseType, identifier string) string
- func SQLBuildWhereInClauseInt64(fieldName string, values []int64) stringdeprecated
- func SQLBuildWhereInClauseInt64For(dbType base.DXDatabaseType, fieldName string, values []int64, argPrefix string) (string, utils.JSON, error)
- func SQLBuildWhereInClauseStrings(fieldName string, values []string) stringdeprecated
- func SQLBuildWhereInClauseStringsFor(dbType base.DXDatabaseType, fieldName string, values []string, ...) (string, utils.JSON, error)
- func SQLInClauseParameterLimit(dbType base.DXDatabaseType) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsValidIdentifier ¶
IsValidIdentifier checks if a string is a valid SQL identifier (alphanumeric + underscore, optionally with dot for table.field)
func ParameterizedSQLQueryNamedBasedToIndexBased ¶
func ParameterizedSQLQueryNamedBasedToIndexBased(dbType base.DXDatabaseType, query string, namedArgs utils.JSON) (string, []any, error)
ParameterizedSQLQueryNamedBasedToIndexBased converts a SQL query with named parameters (:param_name) to index-based positional parameters ($1, $2 for PostgreSQL, ? for MariaDB, etc.) and returns the converted query string along with an ordered slice of argument values.
Example:
query: "SELECT * FROM users WHERE name = :name AND age > :age"
namedArgs: {"name": "John", "age": 25}
For PostgreSQL returns:
query: "SELECT * FROM users WHERE name = $1 AND age > $2"
args: ["John", 25]
For MariaDB returns:
query: "SELECT * FROM users WHERE name = ? AND age > ?"
args: ["John", 25]
func QuoteFieldWithPrefixByDbType ¶
func QuoteFieldWithPrefixByDbType(dbType base.DXDatabaseType, field string) string
QuoteFieldWithPrefixByDbType quotes a field that may have table prefix (e.g., "t.field_name" -> "t"."field_name")
func QuoteIdentifierByDbType ¶
func QuoteIdentifierByDbType(dbType base.DXDatabaseType, identifier string) string
QuoteIdentifierByDbType quotes a SQL identifier based on database type to prevent SQL injection
func SQLBuildWhereInClauseInt64
deprecated
SQLBuildWhereInClauseInt64 builds a WHERE IN clause for int64 values.
Deprecated: it returns an empty string for an empty list, which drops the filter, and has no database type to quote the field or split a long list for. Use SQLBuildWhereInClauseInt64For, or SelectQueryBuilder.BuildInClause and TableSelectQueryBuilder.In.
func SQLBuildWhereInClauseInt64For ¶ added in v1.137.0
func SQLBuildWhereInClauseInt64For(dbType base.DXDatabaseType, fieldName string, values []int64, argPrefix string) (string, utils.JSON, error)
SQLBuildWhereInClauseInt64For is SQLBuildWhereInClauseStringsFor for int64 values, which are bound the same way.
func SQLBuildWhereInClauseStrings
deprecated
SQLBuildWhereInClauseStrings builds a WHERE IN clause for string values.
Deprecated: it writes each value into the statement between quotes, which a backslash defeats on MariaDB and MySQL (BUG-024). Use SQLBuildWhereInClauseStringsFor, which binds the values, or SelectQueryBuilder.BuildInClause and TableSelectQueryBuilder.In.
func SQLBuildWhereInClauseStringsFor ¶ added in v1.137.0
func SQLBuildWhereInClauseStringsFor(dbType base.DXDatabaseType, fieldName string, values []string, argPrefix string) (string, utils.JSON, error)
SQLBuildWhereInClauseStringsFor builds "fieldName IN (...)" for dbType with every value bound, and returns the clause and its arguments.
Each value is bound as the named argument <argPrefix>_<n> (n from 0), written ":<argPrefix>_<n>" in the clause; argPrefix must be an identifier (letters, digits, underscore, not starting with a digit). fieldName is a column or alias.column, quoted for the engine; anything else is refused.
- One value: fieldName = :<argPrefix>_0.
- No values: 1=0, which matches no row, and no arguments. Never an empty string, which would drop the filter.
- Oracle, more than 1000 values: (f IN (...) OR f IN (...)), 1000 per list (ORA-01795).
- More values than SQLInClauseParameterLimit gives for dbType: an error naming the count and the limit.
The clause goes into a statement whose arguments include the returned ones: a builder's And with the arguments added to its Args, or a where map as a db.SQLExpression carrying them:
clause, args, err := SQLBuildWhereInClauseStringsFor(dbType, "code", codes, "code_in")
where := utils.JSON{"code": db.SQLExpression{Expression: clause, Arguments: args}}
func SQLInClauseParameterLimit ¶ added in v1.137.0
func SQLInClauseParameterLimit(dbType base.DXDatabaseType) (int, error)
SQLInClauseParameterLimit returns how many bound values an IN clause built by SQLBuildWhereInClauseStringsFor or SQLBuildWhereInClauseInt64For may hold on dbType, the most parameters one statement takes on that engine:
- PostgreSQL: 65535, the 16-bit parameter count of the extended query protocol's Bind message.
- MariaDB and MySQL: 65535, error 1390 ER_PS_MANY_PARAM ("Prepared statement contains too many placeholders"); the binary protocol counts them in 16 bits.
- Oracle: 65535; a statement with 65536 binds is refused (ORA-01745).
- SQL Server: 2098. The server takes 2100 parameters per request (error 8003), and go-mssqldb sends a statement with arguments as an sp_executesql call whose first two parameters are the statement text (@stmt) and its declarations (@params), so 2098 are left for values.
The limit is the statement's, and the clause's values are only part of it: the statement's other arguments count too, so the engine can still refuse a statement whose clause alone is under the limit.
Types ¶
This section is empty.