Back to index

php5  5.3.10
Classes | Typedefs | Functions
table.c File Reference
#include <stdlib.h>
#include <string.h>
#include "sqliteInt.h"

Go to the source code of this file.

Classes

struct  TabResult

Typedefs

typedef struct TabResult TabResult

Functions

static int sqlite_get_table_cb (void *pArg, int nCol, char **argv, char **colv)
int sqlite_get_table (sqlite *db, const char *zSql, char ***pazResult, int *pnRow, int *pnColumn, char **pzErrMsg)
void sqlite_free_table (char **azResult)

Class Documentation

struct TabResult

Definition at line 27 of file table.c.

Class Members
char ** azResult
int nAlloc
int nColumn
long nData
int nData
int nResult
int nRow
int rc
char * zErrMsg

Typedef Documentation

typedef struct TabResult TabResult

Function Documentation

void sqlite_free_table ( char **  azResult)

Definition at line 192 of file table.c.

 {
  if( azResult ){
    int i, n;
    azResult--;
    if( azResult==0 ) return;
    n = (int)(long)azResult[0];
    for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
    free(azResult);
  }
}

Here is the caller graph for this function:

int sqlite_get_table ( sqlite db,
const char *  zSql,
char ***  pazResult,
int pnRow,
int pnColumn,
char **  pzErrMsg 
)

Definition at line 125 of file table.c.

 {
  int rc;
  TabResult res;
  if( pazResult==0 ){ return SQLITE_ERROR; }
  *pazResult = 0;
  if( pnColumn ) *pnColumn = 0;
  if( pnRow ) *pnRow = 0;
  res.zErrMsg = 0;
  res.nResult = 0;
  res.nRow = 0;
  res.nColumn = 0;
  res.nData = 1;
  res.nAlloc = 20;
  res.rc = SQLITE_OK;
  res.azResult = malloc( sizeof(char*)*res.nAlloc );
  if( res.azResult==0 ){
    return SQLITE_NOMEM;
  }
  res.azResult[0] = 0;
  rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
  if( res.azResult ){
    res.azResult[0] = (char*)res.nData;
  }
  if( rc==SQLITE_ABORT ){
    sqlite_free_table(&res.azResult[1]);
    if( res.zErrMsg ){
      if( pzErrMsg ){
        free(*pzErrMsg);
        *pzErrMsg = res.zErrMsg;
        sqliteStrRealloc(pzErrMsg);
      }else{
        sqliteFree(res.zErrMsg);
      }
    }
    return res.rc;
  }
  sqliteFree(res.zErrMsg);
  if( rc!=SQLITE_OK ){
    sqlite_free_table(&res.azResult[1]);
    return rc;
  }
  if( res.nAlloc>res.nData ){
    char **azNew;
    azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
    if( azNew==0 ){
      sqlite_free_table(&res.azResult[1]);
      return SQLITE_NOMEM;
    }
    res.nAlloc = res.nData+1;
    res.azResult = azNew;
  }
  *pazResult = &res.azResult[1];
  if( pnColumn ) *pnColumn = res.nColumn;
  if( pnRow ) *pnRow = res.nRow;
  return rc;
}

Here is the call graph for this function:

static int sqlite_get_table_cb ( void *  pArg,
int  nCol,
char **  argv,
char **  colv 
) [static]

Definition at line 43 of file table.c.

                                                                              {
  TabResult *p = (TabResult*)pArg;
  int need;
  int i;
  char *z;

  /* Make sure there is enough space in p->azResult to hold everything
  ** we need to remember from this invocation of the callback.
  */
  if( p->nRow==0 && argv!=0 ){
    need = nCol*2;
  }else{
    need = nCol;
  }
  if( p->nData + need >= p->nAlloc ){
    char **azNew;
    p->nAlloc = p->nAlloc*2 + need + 1;
    azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
    if( azNew==0 ){
      p->rc = SQLITE_NOMEM;
      return 1;
    }
    p->azResult = azNew;
  }

  /* If this is the first row, then generate an extra row containing
  ** the names of all columns.
  */
  if( p->nRow==0 ){
    p->nColumn = nCol;
    for(i=0; i<nCol; i++){
      if( colv[i]==0 ){
        z = 0;
      }else{
        z = malloc( strlen(colv[i])+1 );
        if( z==0 ){
          p->rc = SQLITE_NOMEM;
          return 1;
        }
        strcpy(z, colv[i]);
      }
      p->azResult[p->nData++] = z;
    }
  }else if( p->nColumn!=nCol ){
    sqliteSetString(&p->zErrMsg,
       "sqlite_get_table() called with two or more incompatible queries",
       (char*)0);
    p->rc = SQLITE_ERROR;
    return 1;
  }

  /* Copy over the row data
  */
  if( argv!=0 ){
    for(i=0; i<nCol; i++){
      if( argv[i]==0 ){
        z = 0;
      }else{
        z = malloc( strlen(argv[i])+1 );
        if( z==0 ){
          p->rc = SQLITE_NOMEM;
          return 1;
        }
        strcpy(z, argv[i]);
      }
      p->azResult[p->nData++] = z;
    }
    p->nRow++;
  }
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: