summaryrefslogtreecommitdiff
path: root/databases/p5-DBD-postgresql/patches/patch-aa
blob: 2c8907c797b19fbec974f808d547f793e5179358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
$NetBSD: patch-aa,v 1.2 2002/08/21 23:10:39 mjl Exp $

--- dbdimp.c.orig	Sat Apr 27 22:35:52 2002
+++ dbdimp.c	Thu Aug 22 01:01:06 2002
@@ -40,6 +40,30 @@
 
 
 int
+_dbd_begin(imp_dbh_t *imp_dbh)
+{
+	PGresult *result = NULL;
+	ExecStatusType status;
+
+	if (DBIc_has(imp_dbh, DBIcf_AutoCommit) != FALSE)
+		return 1;
+
+	if (imp_dbh->need_begin == 0)
+		return 1;
+
+	imp_dbh->need_begin = 0;
+
+	result = PQexec(imp_dbh->conn, "begin");
+	status = result ? PQresultStatus(result) : -1;
+	PQclear(result);
+	if (status != PGRES_COMMAND_OK) {
+		return 0;
+	}
+
+	return 1;
+}
+
+int
 dbd_discon_all (drh, imp_drh)
     SV *drh;
     imp_drh_t *imp_drh;
@@ -191,6 +215,7 @@
 
     imp_dbh->init_commit = 1;			/* initialize AutoCommit */
     imp_dbh->pg_auto_escape = 1;		/* initialize pg_auto_escape */
+    imp_dbh->need_begin = 1;
 
     DBIc_IMPSET_on(imp_dbh);			/* imp_dbh set up now */
     DBIc_ACTIVE_on(imp_dbh);			/* call disconnect before freeing */
@@ -241,6 +266,9 @@
         PGresult* result = 0;
         ExecStatusType status;
 
+	if (imp_dbh->need_begin)
+		return 1;
+
         /* execute commit */
         result = PQexec(imp_dbh->conn, "commit");
         status = result ? PQresultStatus(result) : -1;
@@ -252,15 +280,8 @@
             return 0;
         }
 
-        /* start new transaction.  AutoCommit must be FALSE, ref. 20 lines up */
-        result = PQexec(imp_dbh->conn, "begin");
-        status = result ? PQresultStatus(result) : -1;
-        PQclear(result);
-        if (status != PGRES_COMMAND_OK) {
-            pg_error(dbh, status, "begin failed\n");
-            return 0;
-        }
-        
+        imp_dbh->need_begin = 1;
+
         return 1;
     }
     
@@ -284,6 +305,9 @@
         PGresult* result = 0;
         ExecStatusType status;
         
+	if (imp_dbh->need_begin)
+		return 1;
+
         /* execute rollback */
         result = PQexec(imp_dbh->conn, "rollback");
         status = result ? PQresultStatus(result) : -1;
@@ -295,15 +319,8 @@
             return 0;
         }
 
-        /* start new transaction.  AutoCommit must be FALSE, ref. 20 lines up */
-        result = PQexec(imp_dbh->conn, "begin");
-        status = result ? PQresultStatus(result) : -1;
-        PQclear(result);
-        if (status != PGRES_COMMAND_OK) {
-            pg_error(dbh, status, "begin failed\n");
-            return 0;
-        }
-        
+        imp_dbh->need_begin = 1;
+
         return 1;
     }
 
@@ -326,7 +343,8 @@
 
     if (NULL != imp_dbh->conn) {
         /* rollback if AutoCommit = off */
-        if (DBIc_has(imp_dbh, DBIcf_AutoCommit) == FALSE) {
+        if ((imp_dbh->need_begin == 0)
+	    && (DBIc_has(imp_dbh, DBIcf_AutoCommit) == FALSE)) {
             PGresult* result = 0;
             ExecStatusType status;
             result = PQexec(imp_dbh->conn, "rollback");
@@ -402,16 +420,7 @@
             if (dbis->debug >= 2) { PerlIO_printf(DBILOGFP, "dbd_db_STORE: switch AutoCommit to on: commit\n"); }
         } else if ((oldval != FALSE && newval == FALSE) || (oldval == FALSE && newval == FALSE && imp_dbh->init_commit)) {
             if (NULL != imp_dbh->conn) {
-                /* start new transaction */
-                PGresult* result = 0;
-                ExecStatusType status;
-                result = PQexec(imp_dbh->conn, "begin");
-                status = result ? PQresultStatus(result) : -1;
-                PQclear(result);
-                if (status != PGRES_COMMAND_OK) {
-                    pg_error(dbh, status, "begin failed\n");
-                    return 0;
-                }
+		    imp_dbh->need_begin = 1;
             }
             if (dbis->debug >= 2) { PerlIO_printf(DBILOGFP, "dbd_db_STORE: switch AutoCommit to off: begin\n"); }
         }
@@ -1080,6 +1089,11 @@
         return -2;
     }   
     
+    if (_dbd_begin(imp_dbh) == 0) {
+		 pg_error(sth, -1, "executing begin failed\n");
+	    return -2;
+	 }
+
     statement = imp_sth->statement;
     if (! statement) {
         /* are we prepared ? */