|
|
Home » U++ Library support » U++ SQL » SqlArray Limit and Offset
SqlArray Limit and Offset [message #4466] |
Fri, 11 August 2006 01:03  |
zsolt
Messages: 702 Registered: December 2005 Location: Budapest, Hungary
|
Contributor |
|
|
I have implemented it. I don't know if this is a good interface or not, but I hope so.
SqlArray declaration private section (SqlCtrl.h):
unsigned int limit;
bool limit_set;
int64 offset;
bool offset_set;
SqlArray declaration public section:
SqlArray& SetLimit(unsigned int _limit) { limit = _limit; limit_set = true; return *this; }
SqlArray& SetOffset(int64 _offset) { offset = _offset; offset_set = true; return *this; }
In SqlArray::SqlArray() and SqlArray::Clear()
limit_set = false;
offset_set = false;
In void SqlArray::AppendQuery(SqlBool where)
I have changed
sql * ::Select(cols).From(table).Where(wh).OrderBy(orderby);
to
SqlSet set = ::Select(cols).From(table).Where(wh).OrderBy(orderby);
if(limit_set)
if(offset_set) set.Limit(offset, limit);
else set.Limit(limit);
else
if(offset_set) set.Offset(offset);
sql * set;
|
|
|
|
|
|
|
Re: SqlArray Limit and Offset [message #4471 is a reply to message #4470] |
Fri, 11 August 2006 18:36   |
|
zsolt wrote on Fri, 11 August 2006 03:50 | An other idea:
I think Limit(offset, limit) and Limit(limit) methods would be better than Rows() methods, because Limit() shows more cleanly what it's purpose is.
|
I agree that this nomenclature is better. I know what it means to limit an sql query, but I would have no clue what Rows() would do.
|
|
|
|
|
|
|
|
|
Re: SqlArray Limit and Offset [message #7309 is a reply to message #7297] |
Thu, 21 December 2006 15:17   |
fabio
Messages: 9 Registered: November 2006
|
Promising Member |
|
|
The 2 level query work only for first time....
the solution is (from http://www.oracle.com/technology/oramag/oracle/06-sep/o56ask tom.html)
... 3 level select.....
select * from ( select a.*, ROWNUM as row_num from (
select .......... from .... where ...... order by ......
) a where rownum <= offset+limit) where row_num > offset
Upp working solution is ...
1) Change in file: upp\uppsrc\sql\SqlCtrl\SqlArray.cpp
SqlArray::AppendQuery() method:
sql * ::Select(cols).From(table).Where(wh).OrderBy(orderby).Limit(count).Offset(offset);
in:
sql * ::Select(cols).From(table).Where(wh).OrderBy(orderby).Limit(offset,count);
2) Change in file: upp\uppsrc\sql\Sql\SqlStatement.cpp
SqlSelect& SqlSelect::Limit(int64 offset, int limit) {
text = SqlCase(ORACLE,"select * from ( select rownum_a.*, ROWNUM as row_num from ( ")() + text +
SqlCase(ORACLE,Format(") rownum_a where rownum <= %d ) where row_num > %d", offset+limit,offset))
(Format(" limit %d, %d",offset,limit));
return *this;
}
this query use rownum reserved word, row_num, rownum_a user word and generate an extra column row_num in the result.
3) optional ?
Remove SqlSelect::Limit(count)
Remove SqlSelect::Offset(offset)
for complete portability
Tested in oracle and sqlite, my be work in MySql (same syntax of sqlite).
Fabio
-
Attachment: limit.zip
(Size: 3.91KB, Downloaded 379 times)
|
|
|
|
|
|
Re: SqlArray Limit and Offset [message #7408 is a reply to message #7358] |
Wed, 27 December 2006 17:09   |
fabio
Messages: 9 Registered: November 2006
|
Promising Member |
|
|
You can implement "limit and offset" at fetch level.
Many objects (Java, VB...) fetch and discard rows, simulating (one-way) limit and offset.....
In oracle 10g (and 9) is present a new version of OCIStmtFetch for make a clean limit solution:
OCIStmtFetch2(stmthp, errhp, 1, OCI_FETCH_RELATIVE, fetchOffset, OCI_DEFAULT);
This fetches a row from the (scrollable) result set.
But, this oci function, and scrollable cursor feacture, is not present in oci8.
Fabio
|
|
|
|
Goto Forum:
Current Time: Mon Apr 28 13:20:24 CEST 2025
Total time taken to generate the page: 0.01221 seconds
|
|
|