package store import "testing" func TestSQLiteKeepsItsPlaceholders(t *testing.T) { q := "SELECT a FROM t WHERE b = ? AND c = ?" if got := SQLiteDialect.rebind(q); got != q { t.Errorf("sqlite query was rewritten: %s", got) } } func TestPostgresGetsNumberedPlaceholders(t *testing.T) { got := PostgresDialect.rebind("SELECT a FROM t WHERE b = ? AND c = ?") want := "SELECT a FROM t WHERE b = $1 AND c = $2" if got != want { t.Errorf("rebind = %q, want %q", got, want) } } func TestAQuestionMarkInsideALiteralIsLeftAlone(t *testing.T) { // No query here contains one today. This exists so that adding one is a // test failure rather than a query that silently reads the wrong argument // — or, worse, one that still parses and returns the wrong rows. got := PostgresDialect.rebind(`SELECT a FROM t WHERE name = 'why?' AND b = ?`) want := `SELECT a FROM t WHERE name = 'why?' AND b = $1` if got != want { t.Errorf("rebind = %q, want %q", got, want) } } func TestDoubleQuotedIdentifiersAreAlsoSkipped(t *testing.T) { got := PostgresDialect.rebind(`SELECT "odd?name" FROM t WHERE b = ?`) want := `SELECT "odd?name" FROM t WHERE b = $1` if got != want { t.Errorf("rebind = %q, want %q", got, want) } } func TestAQueryWithNoPlaceholdersIsUntouched(t *testing.T) { q := "DELETE FROM t" if got := PostgresDialect.rebind(q); got != q { t.Errorf("rebind = %q", got) } } func TestACommentDoesNotOpenAString(t *testing.T) { // An apostrophe in a comment is not a quote. Read as one, it swallowed // every placeholder after it. got := PostgresDialect.rebind("SELECT a -- the key's owner\nFROM t WHERE x = ? AND y = ?") want := "SELECT a -- the key's owner\nFROM t WHERE x = $1 AND y = $2" if got != want { t.Errorf("got %q", got) } // A comment's own question mark is not a placeholder either. if got := PostgresDialect.rebind("SELECT 1 -- why?\nWHERE x = ?"); got != "SELECT 1 -- why?\nWHERE x = $1" { t.Errorf("got %q", got) } }