> -----Original Message-----
> From: Igor Tandetnik [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, November 07, 2006 8:15 AM
> To: SQLite
> Subject: [sqlite] Re: Q about new SQLite API
> 
> Marco Bambini <[EMAIL PROTECTED]> wrote:
> > 2. maybe with the new sqlite3_compile routine there should also be a
> > way to retrieve the rowid of the current row (NULL is no valid rowid
> > is found), a possible API could be sqlite3_rowid to call after each
> > sqlite3_step only if it returns an SQLITE_ROW is returned.
> 
> In general, there is no rowid associated with a row returned 
> by select. 
> A row may be constructed out of data taken from multiple rows of 
> multiple tables, or be manufactured without reference to any table at 
> all. If you want a rowid from a particular table, why not 
> just retrieve 
> it explicitly, as a column in select statement?

I had actually written some add-on funcs to do something similar, but the
code is experimental:


// Exclude btree.c from the project and include this file instead
#include "src/btree.c"
#include "src/vdbeint.h"

// Before using these two API calls, you must do these steps:
// Step 1:  Prepare any SQL statement
// Step 2:  Get the metadata for each column returned in the statement
// Step 3:  For each column, get the database and table (if any) for that
column.
// Step 4:  For each database, record the rowid (database index) of the
database
// Step 5:  For each table referenced, get the root page of that table


// Given a statement, an index of the database and the table's root page,
return (if any) the index of the cursor the
// statement is using on that table
__declspec(dllexport) int WINAPI sqlite3_table_cursor(sqlite3_stmt *pstmt,
int iDb, Pgno tableRootPage)
{
  Vdbe *p = (Vdbe *)pstmt;
  int n;

  for (n = 0; n < p->nCursor && p->apCsr[n] != NULL; n++)
  {
    if (p->apCsr[n]->isTable == FALSE) continue;
    if (p->apCsr[n]->iDb != iDb) continue;
    if (p->apCsr[n]->pCursor->pgnoRoot == tableRootPage)
      return n;
  }
  return -1;
}

// Try and fetch the rowid for a given cursor.  The cursor is obtained from
sqlite3_table_cursor()
__declspec(dllexport) int WINAPI sqlite3_cursor_rowid(sqlite3_stmt *pstmt,
int cursor, sqlite_int64 *prowid)
{
  Vdbe *p = (Vdbe *)pstmt;
  int rc = 0;
  Cursor *pC;

  if (cursor < 0 || cursor >= p->nCursor) return SQLITE_ERROR;
  if (p->apCsr[cursor] == NULL) return SQLITE_ERROR;
  pC = p->apCsr[cursor];

  rc = sqlite3VdbeCursorMoveto(pC);
  if( rc ) return rc;

  if( pC->rowidIsValid )
  {
    *prowid = pC->lastRowid;
  }
  else if(pC->pseudoTable )
  {
    *prowid = keyToInt(pC->iKey);
  }
  else if(pC->nullRow || pC->pCursor==0)
  {
    return SQLITE_ERROR;
  }
  else
  {
    if (pC->pCursor == NULL) return SQLITE_ERROR;
    sqlite3BtreeKeySize(pC->pCursor, prowid);
    *prowid = keyToInt(*prowid);
  }
  return 0;
}



-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to