Skip to content

Commit

Permalink
Allow using pq.CopyIn in table associated with triggers that use rais…
Browse files Browse the repository at this point in the history
…e statement
  • Loading branch information
yaismel authored and johto committed Apr 11, 2015
1 parent a33d605 commit 35aad20
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ func (ci *copyin) resploop() {
switch t {
case 'C':
// complete
case 'N':
// NoticeResponse
case 'Z':
ci.cn.processReadyForQuery(&r)
ci.done <- true
Expand Down
80 changes: 80 additions & 0 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,86 @@ func TestCopyInMultipleValues(t *testing.T) {
}
}

func TestCopyInRaiseStmtTrigger(t *testing.T) {
db := openTestConn(t)
defer db.Close()

if getServerVersion(t, db) < 90000 {
var exists int
err := db.QueryRow("SELECT 1 FROM pg_language WHERE lanname = 'plpgsql'").Scan(&exists)
if err == sql.ErrNoRows {
t.Skip("language PL/PgSQL does not exist; skipping TestCopyInRaiseStmtTrigger")
} else if err != nil {
t.Fatal(err)
}
}

txn, err := db.Begin()
if err != nil {
t.Fatal(err)
}
defer txn.Rollback()

_, err = txn.Exec("CREATE TEMP TABLE temp (a int, b varchar)")
if err != nil {
t.Fatal(err)
}

_, err = txn.Exec(`
CREATE OR REPLACE FUNCTION pg_temp.temptest()
RETURNS trigger AS
$BODY$ begin
raise notice 'Hello world';
return new;
end $BODY$
LANGUAGE plpgsql`)
if err != nil {
t.Fatal(err)
}

_, err = txn.Exec(`
CREATE TRIGGER temptest_trigger
BEFORE INSERT
ON temp
FOR EACH ROW
EXECUTE PROCEDURE pg_temp.temptest()`)
if err != nil {
t.Fatal(err)
}

stmt, err := txn.Prepare(CopyIn("temp", "a", "b"))
if err != nil {
t.Fatal(err)
}

longString := strings.Repeat("#", 500)

_, err = stmt.Exec(int64(1), longString)
if err != nil {
t.Fatal(err)
}

_, err = stmt.Exec()
if err != nil {
t.Fatal(err)
}

err = stmt.Close()
if err != nil {
t.Fatal(err)
}

var num int
err = txn.QueryRow("SELECT COUNT(*) FROM temp").Scan(&num)
if err != nil {
t.Fatal(err)
}

if num != 1 {
t.Fatalf("expected 1 items, not %d", num)
}
}

func TestCopyInTypes(t *testing.T) {
db := openTestConn(t)
defer db.Close()
Expand Down

0 comments on commit 35aad20

Please sign in to comment.