Alfred Zhong
Alfred Zhong

Reputation: 7081

Confusion on sqlite3 struct in sqlite3 source code

In the function

static int sqlite3Prepare(
    sqlite3 *db,              /* Database handle. */
    const char *zSql,         /* UTF-8 encoded SQL statement. */
    int nBytes,               /* Length of zSql in bytes. */
    int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
    Vdbe *pReprepare,         /* VM being reprepared */
    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    const char **pzTail       /* OUT: End of parsed string */
    ) {
     ...
     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
     ...
     assert( !db->mallocFailed );
     ...
}

I know sqlite3 is just a fake struct declared as

 typedef struct sqlite3 sqlite3;

without any body. I know sqlite3 * is usually is cast to a Vdbe*.

But here, db is of the type of sqlite3*, how can db->malloFailed exist? Why doesn't the compiler complain?

There is similar situation with sqlite3_stmt:

typedef struct sqlite3_stmt sqlite3_stmt;

with no body. I guess sqlite3_stmt is a syntax tree of parsed SQL statements. I would like to see the structure of it. However, the type is hidden so deeply using this weird pattern that I can't see what it is.

Even Vdbe is the same situation...

typedef struct Vdbe Vdbe;

Where on earth is the real struct?

Upvotes: 1

Views: 2274

Answers (1)

CL.
CL.

Reputation: 180080

sqlite3 is not a fake struct; the sqlite.h file just does not define its body.

Its definition is in the sqliteInt.h file (which is also part of the sqlite3.c amalgamation):

/*
** Each database connection is an instance of the following structure.
*/
struct sqlite3 {
  sqlite3_vfs *pVfs;            /* OS Interface */
  struct Vdbe *pVdbe;           /* List of active virtual machines */
  CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
  ...
  u8 mallocFailed;              /* True if we have seen a malloc failure */
  ...

Upvotes: 5

Related Questions