From 39fb33ded1dc955fa6075d6659fc5122f6f83ba3 Mon Sep 17 00:00:00 2001 From: Hiroshi Inoue Date: Tue, 26 Apr 2016 12:52:33 +0900 Subject: [PATCH] Fix the use and the implementation of getNthValid(). --- qresult.c | 2 +- results.c | 36 +++++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/qresult.c b/qresult.c index e0bea4f..524a65d 100644 --- a/qresult.c +++ b/qresult.c @@ -996,7 +996,7 @@ inolog("moved=%d ? " FORMAT_ULEN "\n", moved, movement); SQLULEN bmovement, mback; SQLLEN rowset_start = self->cursTuple + 1, back_offset, backpt; inolog("FETCH LAST case\n"); - if (getNthValid(self, QR_get_num_total_tuples(self) - 1, SQL_FETCH_PRIOR, self->move_offset, &backpt) < 0) + if (getNthValid(self, QR_get_num_total_tuples(self) - 1, SQL_FETCH_PRIOR, self->move_offset, &backpt) <= 0) { /* the rowset_start is on BOF */ self->tupleField = NULL; diff --git a/results.c b/results.c index 2e9ac7d..8c544e2 100644 --- a/results.c +++ b/results.c @@ -1219,38 +1219,50 @@ inolog("get %dth Valid data from %d to %s [dlt=%d]", nth, sta, orientation == SQ if (QR_get_cursor(res)) { SQLLEN *deleted = res->deleted; + SQLLEN delsta; - *nearest = sta - 1 + nth; if (SQL_FETCH_PRIOR == orientation) { + *nearest = sta + 1 - nth; + delsta = (-1); for (i = res->dl_count - 1; i >=0 && *nearest <= deleted[i]; i--) { inolog("deleted[%d]=%d\n", i, deleted[i]); if (sta >= deleted[i]) + { (*nearest)--; + if (i > delsta) + delsta = i; + } } inolog("nearest=%d\n", *nearest); if (*nearest < 0) { *nearest = -1; - count = sta + 1; + count = sta - delsta; } else return nth; } else { + *nearest = sta - 1 + nth; + delsta = res->dl_count; if (!QR_once_reached_eof(res)) num_tuples = INT_MAX; for (i = 0; i < res->dl_count && *nearest >= deleted[i]; i++) { if (sta <= deleted[i]) + { (*nearest)++; + if (i < delsta) + delsta = i; + } } if (*nearest >= num_tuples) { *nearest = num_tuples; - count = *nearest - sta; + count = *nearest - sta + delsta - res->dl_count; } else return nth; @@ -1515,23 +1527,21 @@ inolog("num_tuples=%d\n", num_tuples); * RESULT SET, then this should be equivalent to * SQL_FETCH_LAST. */ - if (SC_get_rowset_start(stmt) <= 0) + if (rowset_start <= 0) { EXTFETCH_RETURN_BOF(stmt, res) } - if (SC_get_rowset_start(stmt) >= num_tuples) + if (rowset_start >= num_tuples && + rowsetSize > num_tuples) { - if (rowsetSize > num_tuples) - { - SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beginning", func); - } - SC_set_rowset_start(stmt, num_tuples <= 0 ? 0 : (num_tuples - rowsetSize), TRUE); + SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beginning", func); + SC_set_rowset_start(stmt, 0, TRUE); } else if (QR_haskeyset(res)) { - if (i = getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, rowsetSize, &rowset_start), i < 0) + if (i = getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, rowsetSize, &rowset_start), i <= 0) { - if (i = getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, 1, &rowset_start), i < 0) + if (i = getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, 1, &rowset_start), i <= 0) { EXTFETCH_RETURN_BOF(stmt, res) } @@ -1544,7 +1554,7 @@ inolog("num_tuples=%d\n", num_tuples); else should_set_rowset_start = TRUE; } - else if (SC_get_rowset_start(stmt) < rowsetSize) + else if (rowset_start < rowsetSize) { SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beggining", func); SC_set_rowset_start(stmt, 0, TRUE); -- 2.39.5