Skip to content

Commit

Permalink
Partial revert of 7dd23fd
Browse files Browse the repository at this point in the history
Restore Postgres references
  • Loading branch information
jsha committed Mar 27, 2024
1 parent 077e6df commit b0f33e4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ To avoid any potential issues with timezone/DST, consider:

## Running the tests

The included tests may be run against MySQL or sqlite3.
The included tests may be run against MySQL, Postgres, or sqlite3.
You must set two environment variables so the test code knows which
driver to use, and how to connect to your database.

Expand All @@ -686,7 +686,7 @@ go test -bench="Bench" -benchtime 10
```

Valid `GORP_TEST_DIALECT` values are: "mysql"(for mymysql),
"gomysql"(for go-sql-driver), or "sqlite" See the
"gomysql"(for go-sql-driver), "postgres", or "sqlite" See the
`test_all.sh` script for examples of all 3 databases. This is the
script I run locally to test the library.

Expand Down
5 changes: 5 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TestWithCanceledContext(t *testing.T) {
}

switch driver {
case "postgres":
// pq doesn't return standard deadline exceeded error
if err.Error() != "pq: canceling statement due to user request" {
t.Errorf("expected context.DeadlineExceeded, got %v", err)
}
default:
if err != context.DeadlineExceeded {
t.Errorf("expected context.DeadlineExceeded, got %v", err)
Expand Down
3 changes: 3 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (m *DbMap) createIndexImpl(ctx context.Context, dialect reflect.Type,
}
s.WriteString(" index")
s.WriteString(fmt.Sprintf(" %s on %s", index.IndexName, table.TableName))
if dname := dialect.Name(); dname == "PostgresDialect" && index.IndexType != "" {
s.WriteString(fmt.Sprintf(" %s %s", m.Dialect.CreateIndexSuffix(), index.IndexType))
}
s.WriteString(" (")
for x, col := range index.columns {
if x > 0 {
Expand Down
2 changes: 1 addition & 1 deletion dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Dialect interface {
TruncateClause() string

// Bind variable string to use when forming SQL statements
// in many dbs it is "?".
// in many dbs it is "?", but Postgres appears to use $1
//
// i is a zero based index of the bind variable in this statement
//
Expand Down
4 changes: 2 additions & 2 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func fieldByName(val reflect.Value, fieldName string) *reflect.Value {
return &f
}

// try to find by case insensitive match in the case where columns are
// aliased in the sql
// try to find by case insensitive match - only the Postgres driver
// seems to require thi
fieldNameL := strings.ToLower(fieldName)
fieldCount := val.NumField()
t := val.Type()
Expand Down
20 changes: 16 additions & 4 deletions gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

_ "github.com/go-sql-driver/mysql"
"github.com/letsencrypt/borp"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)

Expand Down Expand Up @@ -1427,6 +1428,9 @@ func TestTransaction(t *testing.T) {
}

func TestTransactionExecNamed(t *testing.T) {
if os.Getenv("GORP_TEST_DIALECT") == "postgres" {
return
}
dbmap := initDBMap(t)
defer dropAndClose(dbmap)
trans, err := dbmap.BeginTx(context.Background())
Expand Down Expand Up @@ -2531,10 +2535,18 @@ func BenchmarkNativeCrud(b *testing.B) {
columnPersonId := columnName(dbmap, Invoice{}, "PersonId")
b.StartTimer()

insert := "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values (?, ?, ?, ?)"
sel := "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=?"
update := "update invoice_test set " + columnCreated + "=?, " + columnUpdated + "=?, " + columnMemo + "=?, " + columnPersonId + "=? where " + columnId + "=?"
delete := "delete from invoice_test where " + columnId + "=?"
var insert, sel, update, delete string
if os.Getenv("GORP_TEST_DIALECT") != "postgres" {
insert = "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values (?, ?, ?, ?)"
sel = "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=?"
update = "update invoice_test set " + columnCreated + "=?, " + columnUpdated + "=?, " + columnMemo + "=?, " + columnPersonId + "=? where " + columnId + "=?"
delete = "delete from invoice_test where " + columnId + "=?"
} else {
insert = "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values ($1, $2, $3, $4)"
sel = "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=$1"
update = "update invoice_test set " + columnCreated + "=$1, " + columnUpdated + "=$2, " + columnMemo + "=$3, " + columnPersonId + "=$4 where " + columnId + "=$5"
delete = "delete from invoice_test where " + columnId + "=$1"
}

inv := &Invoice{0, 100, 200, "my memo", 0, false}

Expand Down

0 comments on commit b0f33e4

Please sign in to comment.