I'm finding that create table
statements are not recognizing wildcard characters I want to insert. How would I do something like with a prepared statement in golang?
stmt, err := tx.Prepare(`
CREATE TABLE table_number_$1 (
guid character varying(64) NOT NULL,
number integer,
name character varying(64),
PRIMARY KEY (guid),
CONSTRAINT some_onstraint
CHECK ((number = $2))
)`)
if err != nil {
return err
}
defer stmt.Close()
if _, err := stmt.Exec(
string(table_number),
table_number; err != nil {
tx.Rollback()
return err
}
The error I get when I print it out is:
sql: expected 0 arguments, got 2
EDIT: obviously I know I could build the string like normal, but was wondering if there's a built in way.
It's not possible in PostgreSQL because:
PostgreSQL's bind parameters may only be used for literals, not identifiers. That's because the parser has to know what the identifiers are in order to parse the query correctly, but bind parameters are only sent after parsing. So you can't use parameters for table names etc.
PostgreSQL doesn't support bind parameters in utility statements (anything other than insert/update/delete/select) anyway.
Some drivers support client-side parameter substitution and escaping for identifiers via a different placement parameter syntax, but as far as I can tell Go's does not. So you'll have to use string interpolation, carefully. Remember to always enclose the identifier in "double quotes"
and double any embedded quotes, so a table name my "table"!
becomes "my ""table!"
in SQL.