Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WrJPV-0001nj-6e for pgsql-odbc@arkaria.postgresql.org; Mon, 02 Jun 2014 03:59:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WrJPU-0001UX-1D for pgsql-odbc@arkaria.postgresql.org; Mon, 02 Jun 2014 03:59:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WrJPS-0001UO-5D for pgsql-odbc@postgresql.org; Mon, 02 Jun 2014 03:59:42 +0000 Received: from sd.tpf.co.jp ([210.161.239.34]) by magus.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WrJPM-0007F4-7m for pgsql-odbc@postgresql.org; Mon, 02 Jun 2014 03:59:41 +0000 Received: (qmail 9352 invoked by uid 512); 2 Jun 2014 03:59:31 -0000 X-Authentication: inoue was authenticated by sd.tpf.co.jp at 2 Jun 2014 03:59:31 -0000 Received: from unknown (HELO ?100.0.33.224?) (114.160.219.229) by sd.tpf.co.jp with SMTP; 2 Jun 2014 03:59:31 -0000 Message-ID: <538BF6A0.1020404@tpf.co.jp> Date: Mon, 02 Jun 2014 12:59:28 +0900 From: Hiroshi Inoue User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: lev.bukovski@teliasonera.com, pgsql-odbc@postgresql.org Subject: Re: Prepared statement error with UseServerSidePrepare=1 References: <619ABD714EEDD84E8E582E993559DB36142ADA8C@EXMB12TSTRZ2.tcad.telia.se> In-Reply-To: <619ABD714EEDD84E8E582E993559DB36142ADA8C@EXMB12TSTRZ2.tcad.telia.se> Content-Type: multipart/mixed; boundary="------------000503090104050205000302" X-Pg-Spam-Score: -1.8 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------000503090104050205000302 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Hi Lev, Could you please try the attahced patch? regards, Hiroshi Inoue (2014/05/28 17:52), lev.bukovski@teliasonera.com wrote: > Hi, > > I found some strangeness in psqlodbc’s behavior. May be you can explain > to me, do I do something wrong or there is a bug. > > Tested on psqlodbc 9.3.3 and postgresql 9.3.4. > > I have department tree table named KEY11 with 2 indexes on that: > > CREATE TABLE Key11 ( > > ID INTEGER not null, > > Disp VARCHAR (64) not null, > > Search VARCHAR (64) not null, > > CustomerID INTEGER not null, > > ParentID INTEGER not null, > > primary KEY (Search,Disp,CustomerID,ParentID) > > ); > > create unique index key11_idx on Key11 (ID); > > create unique index key11_disp_idx on Key11(Disp,CustomerID,ParentID); > > So I can’t insert same named department for same customer and on same > organization level (ParentID). > > I do login to the database and prepare all SQL statements only once. > Afterward prepared SQL statements are reused with different parameters. > > So I do insert new department to KEY11 table: > > SQLPrepare - INSERT INTO KEY11 (ID, DISP, SEARCH, CUSTOMERID, PARENTID) > VALUES (?,?,?,?,?) > > SQLExecute with values for example (6022, ‘New department’, ‘NEW > DEPARTMENT’, 2, 6021) > > Everything fine. > > Now I make another insert with same parameters: > > SQLExecute with values for example (6023, ‘New department’, ‘NEW > DEPARTMENT’, 2, 6021) > > Fine, I get an error: duplicate key value violates unique constraint > "key11_disp_idx" > > Now I rename department 6022 “New Department” -> “New Department 2”: > > SQLPrepare - UPDATE KEY11 SET DISP = ?, SEARCH = ? WHERE ID = ? AND > CUSTOMERID = ? > > SQLExecute with values for example (‘New department 2’, ‘NEW > DEPARTMENT’, 6022, 2) > > Fine again. Department’s name is renamed, so I can try to insert new > department again. Error while executing the query > > Third insert with new department in parameters: > > SQLExecute with values for example (6024, ‘New department’, ‘NEW > DEPARTMENT’, 2, 6021) > > Still an error, but only: “Error while executing the query” ? > > But if I query now the KEY11 table, I see that New department was added > despite of error: > > select * from key11 where parentid = 6021; > > id | disp | search | > customerid | parentid > > ------+-----------------------------+-----------------------------+------------+---------- > > 6022 | New department 2 | NEW DEPARTMENT 2 > | 2 | 6021 > > 6024 | New department | NEW DEPARTMENT > | 2 | 6021 > > (3 rows) > > I noticed the behavior on psqlodbc 9.2.1. In that version third insert > returned same error, but didn’t really insert the data. > > If I disable UseServerSidePrepare=0, then third insert executes > successfully. > > Thanks in advance, > > Lev Bukovski --------------000503090104050205000302 Content-Type: text/x-patch; name="prepared_statement_error.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="prepared_statement_error.patch" diff --git a/execute.c b/execute.c index 1cc4f04..8dcc3ef 100644 --- a/execute.c +++ b/execute.c @@ -988,16 +988,13 @@ PGAPI_Execute(HSTMT hstmt, UWORD flag) else if (PREPARED_PERMANENTLY == stmt->prepared || PREPARED_TEMPORARILY == stmt->prepared) { - QResultClass *res; - /* * re-executing an prepared statement. * Don't recycle the statement but * discard the old result. */ recycle = FALSE; - if (res = SC_get_Result(stmt), res) - QR_close_result(res, FALSE); + SC_reset_result_for_rerun(stmt); } /* * If SQLExecute is being called again, recycle the statement. Note diff --git a/odbcapi.c b/odbcapi.c index b5b5aac..bfbbae1 100644 --- a/odbcapi.c +++ b/odbcapi.c @@ -386,6 +386,7 @@ SQLExecute(HSTMT StatementHandle) else { StartRollbackState(stmt); + stmt->exec_current_row = -1; ret = PGAPI_Execute(StatementHandle, flag); ret = DiscardStatementSvp(stmt, ret, FALSE); } diff --git a/qresult.c b/qresult.c index 361be56..1060bfa 100644 --- a/qresult.c +++ b/qresult.c @@ -196,7 +196,6 @@ QR_Constructor(void) rv->notice = NULL; rv->conn = NULL; rv->next = NULL; - rv->pstatus = 0; rv->count_backend_allocated = 0; rv->count_keyset_allocated = 0; rv->num_total_read = 0; @@ -320,6 +319,25 @@ QR_close_result(QResultClass *self, BOOL destroy) } void +QR_reset_for_re_execute(QResultClass *self) +{ + mylog("QResult: enter %s for %x\n", __FUNCTION__, self); + if (!self) return; + QR_close_result(self, FALSE); + /* reset flags etc */ + self->flags = 0; + QR_set_rowstart_in_cache(self, -1); + self->recent_processed_row_count = -1; + /* clear error info etc */ + self->rstatus = PORES_EMPTY_QUERY; + self->aborted = FALSE; + self->sqlstate[0] = '\0'; + self->messageref = NULL; + + mylog("QResult: exit %s\n", __FUNCTION__); +} + +void QR_Destructor(QResultClass *self) { mylog("QResult: enter DESTRUCTOR\n"); @@ -464,6 +482,7 @@ QR_free_memory(QResultClass *self) self->count_backend_allocated = 0; self->backend_tuples = NULL; self->dataFilled = FALSE; + self->tupleField = NULL; } if (self->keyset) { diff --git a/qresult.h b/qresult.h index dfae6d8..8a9315e 100644 --- a/qresult.h +++ b/qresult.h @@ -226,6 +226,7 @@ int QR_next_tuple(QResultClass *self, StatementClass *, int *LastMessageType); int QR_close(QResultClass *self); void QR_on_close_cursor(QResultClass *self); void QR_close_result(QResultClass *self, BOOL destroy); +void QR_reset_for_re_execute(QResultClass *self); char QR_fetch_tuples(QResultClass *self, ConnectionClass *conn, const char *cursor, int *LastMessageType); void QR_free_memory(QResultClass *self); void QR_set_command(QResultClass *self, const char *msg); diff --git a/statement.c b/statement.c index 0aa31ce..d6bf81a 100644 --- a/statement.c +++ b/statement.c @@ -805,6 +805,27 @@ SC_initialize_and_recycle(StatementClass *self) return SQL_SUCCESS; } +void +SC_reset_result_for_rerun(StatementClass *self) +{ + QResultClass *res; + ColumnInfoClass *flds; + + if (!self) return; + if (res = SC_get_Result(self), NULL == res) + return; + flds = QR_get_fields(res); + if (NULL == flds || + 0 == CI_get_num_fields(flds)) + SC_set_Result(self, NULL); + else + { + QR_reset_for_re_execute(res); + self->curr_param_result = 1; + SC_set_Curres(self, NULL); + } +} + /* * Called from SQLPrepare if STMT_PREMATURE, or * from SQLExecute if STMT_FINISHED, or @@ -880,11 +901,10 @@ inolog("SC_clear_parse_status\n"); { case PREPARED_PERMANENTLY: case PREPARED_TEMPORARILY: - QR_close_result(res, FALSE); + SC_reset_result_for_rerun(self); break; default: - QR_Destructor(res); - SC_init_Result(self); + SC_set_Result(self, NULL); break; } } @@ -2533,7 +2553,8 @@ ReflectColumnsInfo(StatementClass *self, QResultClass *res) if (res->num_fields > 0) return FALSE; pres = SC_get_Result(self); - if (pres != res && + if (pres != NULL && + pres != res && pres->num_fields > 0) { QR_set_fields(res, QR_get_fields(pres)); @@ -2627,6 +2648,8 @@ inolog(" response_length=%d\n", response_length); res->recent_processed_row_count = ret1; } } + else if (QR_command_successful(res)) + QR_set_rstatus(res, PORES_COMMAND_OK); break; case 'E': /* ErrorMessage */ handle_error_message(conn, msgbuffer, sizeof(msgbuffer), res->sqlstate, comment, res); diff --git a/statement.h b/statement.h index 6a138be..25f8c32 100644 --- a/statement.h +++ b/statement.h @@ -466,6 +466,7 @@ char SC_Destructor(StatementClass *self); BOOL SC_opencheck(StatementClass *self, const char *func); RETCODE SC_initialize_and_recycle(StatementClass *self); void SC_initialize_cols_info(StatementClass *self, BOOL DCdestroy, BOOL parseReset); +void SC_reset_result_for_rerun(StatementClass *self); int statement_type(const char *statement); char parse_statement(StatementClass *stmt, BOOL); char parse_sqlsvr(StatementClass *stmt); --------------000503090104050205000302 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------000503090104050205000302--