xref: /sqlite-3.40.0/src/sqliteInt.h (revision 9bb61fe7)
175897234Sdrh /*
275897234Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp
375897234Sdrh **
475897234Sdrh ** This program is free software; you can redistribute it and/or
575897234Sdrh ** modify it under the terms of the GNU General Public
675897234Sdrh ** License as published by the Free Software Foundation; either
775897234Sdrh ** version 2 of the License, or (at your option) any later version.
875897234Sdrh **
975897234Sdrh ** This program is distributed in the hope that it will be useful,
1075897234Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of
1175897234Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1275897234Sdrh ** General Public License for more details.
1375897234Sdrh **
1475897234Sdrh ** You should have received a copy of the GNU General Public
1575897234Sdrh ** License along with this library; if not, write to the
1675897234Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1775897234Sdrh ** Boston, MA  02111-1307, USA.
1875897234Sdrh **
1975897234Sdrh ** Author contact information:
2075897234Sdrh **   [email protected]
2175897234Sdrh **   http://www.hwaci.com/drh/
2275897234Sdrh **
2375897234Sdrh *************************************************************************
2475897234Sdrh ** Internal interface definitions for SQLite.
2575897234Sdrh **
26*9bb61fe7Sdrh ** @(#) $Id: sqliteInt.h,v 1.14 2000/06/05 16:01:39 drh Exp $
2775897234Sdrh */
2875897234Sdrh #include "sqlite.h"
2975897234Sdrh #include "dbbe.h"
3075897234Sdrh #include "vdbe.h"
3175897234Sdrh #include "parse.h"
3275897234Sdrh #include <gdbm.h>
3375897234Sdrh #include <stdio.h>
3475897234Sdrh #include <stdlib.h>
3575897234Sdrh #include <string.h>
3675897234Sdrh #include <assert.h>
3775897234Sdrh 
38*9bb61fe7Sdrh #define MEMORY_DEBUG 1
39dcc581ccSdrh #ifdef MEMORY_DEBUG
40dcc581ccSdrh # define sqliteMalloc(X)    sqliteMalloc_(X,__FILE__,__LINE__)
41dcc581ccSdrh # define sqliteFree(X)      sqliteFree_(X,__FILE__,__LINE__)
42dcc581ccSdrh # define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__)
43c3c2fc9aSdrh   void sqliteStrRealloc(char**);
44c3c2fc9aSdrh #else
45c3c2fc9aSdrh # define sqliteStrRealloc(X)
46dcc581ccSdrh #endif
47dcc581ccSdrh 
4875897234Sdrh /*
4975897234Sdrh ** The number of entries in the in-memory hash table holding the
5075897234Sdrh ** schema.
5175897234Sdrh */
5275897234Sdrh #define N_HASH        51
5375897234Sdrh 
5475897234Sdrh /*
5575897234Sdrh ** Name of the master database table.  The master database table
5675897234Sdrh ** is a special table that holds the names and attributes of all
5775897234Sdrh ** user tables and indices.
5875897234Sdrh */
5975897234Sdrh #define MASTER_NAME   "sqlite_master"
6075897234Sdrh 
6175897234Sdrh /*
6275897234Sdrh ** A convenience macro that returns the number of elements in
6375897234Sdrh ** an array.
6475897234Sdrh */
6575897234Sdrh #define ArraySize(X)    (sizeof(X)/sizeof(X[0]))
6675897234Sdrh 
6775897234Sdrh /*
68cce7d176Sdrh ** Integer identifiers for functions.
69cce7d176Sdrh */
70cce7d176Sdrh #define FN_Unknown    0
71cce7d176Sdrh #define FN_Count      1
72cce7d176Sdrh #define FN_Min        2
73cce7d176Sdrh #define FN_Max        3
74cce7d176Sdrh #define FN_Sum        4
75cce7d176Sdrh #define FN_Avg        5
76cce7d176Sdrh 
77cce7d176Sdrh /*
7875897234Sdrh ** Forward references to structures
7975897234Sdrh */
807020f651Sdrh typedef struct Column Column;
8175897234Sdrh typedef struct Table Table;
8275897234Sdrh typedef struct Index Index;
8375897234Sdrh typedef struct Instruction Instruction;
8475897234Sdrh typedef struct Expr Expr;
8575897234Sdrh typedef struct ExprList ExprList;
8675897234Sdrh typedef struct Parse Parse;
8775897234Sdrh typedef struct Token Token;
8875897234Sdrh typedef struct IdList IdList;
8975897234Sdrh typedef struct WhereInfo WhereInfo;
90*9bb61fe7Sdrh typedef struct Select Select;
9175897234Sdrh 
9275897234Sdrh /*
9375897234Sdrh ** Each database is an instance of the following structure
9475897234Sdrh */
9575897234Sdrh struct sqlite {
9675897234Sdrh   Dbbe *pBe;                 /* The backend driver */
9775897234Sdrh   int flags;                 /* Miscellanous flags */
9875897234Sdrh   Table *apTblHash[N_HASH];  /* All tables of the database */
9975897234Sdrh   Index *apIdxHash[N_HASH];  /* All indices of the database */
10075897234Sdrh };
10175897234Sdrh 
10275897234Sdrh /*
10375897234Sdrh ** Possible values for the flags field of sqlite
10475897234Sdrh */
10575897234Sdrh #define SQLITE_VdbeTrace    0x00000001
10658b9576bSdrh #define SQLITE_Initialized  0x00000002
10758b9576bSdrh 
10858b9576bSdrh /*
1097020f651Sdrh ** information about each column of a table is held in an instance
1107020f651Sdrh ** of this structure.
1117020f651Sdrh */
1127020f651Sdrh struct Column {
1137020f651Sdrh   char *zName;        /* Name of this column */
1147020f651Sdrh   char *zDflt;        /* Default value of this column */
1157020f651Sdrh   int notNull;        /* True if there is a NOT NULL constraing */
1167020f651Sdrh };
1177020f651Sdrh 
1187020f651Sdrh /*
11975897234Sdrh ** Each table is represented in memory by
12075897234Sdrh ** an instance of the following structure
12175897234Sdrh */
12275897234Sdrh struct Table {
12375897234Sdrh   char *zName;        /* Name of the table */
12475897234Sdrh   Table *pHash;       /* Next table with same hash on zName */
12575897234Sdrh   int nCol;           /* Number of columns in this table */
1267020f651Sdrh   Column *aCol;       /* Information about each column */
12775897234Sdrh   int readOnly;       /* True if this table should not be written by the user */
12875897234Sdrh   Index *pIndex;      /* List of indices on this table. */
12975897234Sdrh };
13075897234Sdrh 
13175897234Sdrh /*
13275897234Sdrh ** Each index is represented in memory by and
13375897234Sdrh ** instance of the following structure.
13475897234Sdrh */
13575897234Sdrh struct Index {
13675897234Sdrh   char *zName;        /* Name of this index */
13775897234Sdrh   Index *pHash;       /* Next index with the same hash on zName */
13875897234Sdrh   int nField;         /* Number of fields in the table indexed by this index */
13975897234Sdrh   int *aiField;       /* Indices of fields used by this index.  1st is 0 */
14075897234Sdrh   Table *pTable;      /* The table being indexed */
1417020f651Sdrh   int isUnique;       /* True if keys must all be unique */
14275897234Sdrh   Index *pNext;       /* The next index associated with the same table */
14375897234Sdrh };
14475897234Sdrh 
14575897234Sdrh /*
14675897234Sdrh ** Each token coming out of the lexer is an instance of
14775897234Sdrh ** this structure.
14875897234Sdrh */
14975897234Sdrh struct Token {
15075897234Sdrh   char *z;      /* Text of the token */
15175897234Sdrh   int n;        /* Number of characters in this token */
15275897234Sdrh };
15375897234Sdrh 
15475897234Sdrh /*
15575897234Sdrh ** Each node of an expression in the parse tree is an instance
15675897234Sdrh ** of this structure
15775897234Sdrh */
15875897234Sdrh struct Expr {
15975897234Sdrh   int op;                /* Operation performed by this node */
16075897234Sdrh   Expr *pLeft, *pRight;  /* Left and right subnodes */
16175897234Sdrh   ExprList *pList;       /* A list of expressions used as a function argument */
16275897234Sdrh   Token token;           /* An operand token */
16375897234Sdrh   int iTable, iField;    /* When op==TK_FIELD, then this node means the
16475897234Sdrh                          ** iField-th field of the iTable-th table */
16575897234Sdrh };
16675897234Sdrh 
16775897234Sdrh /*
16875897234Sdrh ** A list of expressions.  Each expression may optionally have a
16975897234Sdrh ** name.  An expr/name combination can be used in several ways, such
17075897234Sdrh ** as the list of "expr AS ID" fields following a "SELECT" or in the
17175897234Sdrh ** list of "ID = expr" items in an UPDATE.  A list of expressions can
17275897234Sdrh ** also be used as the argument to a function, in which case the azName
17375897234Sdrh ** field is not used.
17475897234Sdrh */
17575897234Sdrh struct ExprList {
17675897234Sdrh   int nExpr;             /* Number of expressions on the list */
17775897234Sdrh   struct {
17875897234Sdrh     Expr *pExpr;           /* The list of expressions */
17975897234Sdrh     char *zName;           /* Token associated with this expression */
18075897234Sdrh     int idx;               /* ... */
181cce7d176Sdrh     int isAgg;             /* True if this is an aggregate like count(*) */
18275897234Sdrh   } *a;                  /* One entry for each expression */
18375897234Sdrh };
18475897234Sdrh 
18575897234Sdrh /*
18675897234Sdrh ** A list of identifiers.
18775897234Sdrh */
18875897234Sdrh struct IdList {
18975897234Sdrh   int nId;         /* Number of identifiers on the list */
19075897234Sdrh   struct {
19175897234Sdrh     char *zName;      /* Text of the identifier. */
19275897234Sdrh     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
19375897234Sdrh     Table *pTab;      /* Table corresponding to zName */
19475897234Sdrh     int idx;          /* Index of a field name in the table */
19575897234Sdrh   } *a;            /* One entry for each identifier on the list */
19675897234Sdrh };
19775897234Sdrh 
19875897234Sdrh /*
19975897234Sdrh ** The WHERE clause processing routine has two halves.  The
20075897234Sdrh ** first part does the start of the WHERE loop and the second
20175897234Sdrh ** half does the tail of the WHERE loop.  An instance of
20275897234Sdrh ** this structure is returned by the first half and passed
20375897234Sdrh ** into the second half to give some continuity.
20475897234Sdrh */
20575897234Sdrh struct WhereInfo {
20675897234Sdrh   Parse *pParse;
20775897234Sdrh   IdList *pTabList;
20875897234Sdrh   int iContinue;
20975897234Sdrh   int iBreak;
21075897234Sdrh };
21175897234Sdrh 
21275897234Sdrh /*
213*9bb61fe7Sdrh ** An instance of the following structure contains all information
214*9bb61fe7Sdrh ** needed to generate code for a single SELECT statement.
215*9bb61fe7Sdrh */
216*9bb61fe7Sdrh struct Select {
217*9bb61fe7Sdrh   int isDistinct;        /* True if the DISTINCT keyword is present */
218*9bb61fe7Sdrh   ExprList *pEList;      /* The fields of the result */
219*9bb61fe7Sdrh   IdList *pSrc;          /* The FROM clause */
220*9bb61fe7Sdrh   Expr *pWhere;          /* The WHERE clause */
221*9bb61fe7Sdrh   ExprList *pGroupBy;    /* The GROUP BY clause */
222*9bb61fe7Sdrh   Expr *pHaving;         /* The HAVING clause */
223*9bb61fe7Sdrh   ExprList *pOrderBy;    /* The ORDER BY clause */
224*9bb61fe7Sdrh };
225*9bb61fe7Sdrh 
226*9bb61fe7Sdrh /*
22775897234Sdrh ** An SQL parser context
22875897234Sdrh */
22975897234Sdrh struct Parse {
23075897234Sdrh   sqlite *db;          /* The main database structure */
23175897234Sdrh   sqlite_callback xCallback;  /* The callback function */
23275897234Sdrh   void *pArg;          /* First argument to the callback function */
23375897234Sdrh   char *zErrMsg;       /* An error message */
23475897234Sdrh   Token sErrToken;     /* The token at which the error occurred */
23575897234Sdrh   Token sFirstToken;   /* The first token parsed */
23675897234Sdrh   Token sLastToken;    /* The last token parsed */
23775897234Sdrh   Table *pNewTable;    /* A table being constructed by CREATE TABLE */
23875897234Sdrh   Vdbe *pVdbe;         /* An engine for executing database bytecode */
23975897234Sdrh   int explain;         /* True if the EXPLAIN flag is found on the query */
24075897234Sdrh   int initFlag;        /* True if reparsing CREATE TABLEs */
24175897234Sdrh   int nErr;            /* Number of errors seen */
24275897234Sdrh };
24375897234Sdrh 
24475897234Sdrh /*
24575897234Sdrh ** Internal function prototypes
24675897234Sdrh */
24775897234Sdrh int sqliteStrICmp(const char *, const char *);
24875897234Sdrh int sqliteStrNICmp(const char *, const char *, int);
24975897234Sdrh int sqliteHashNoCase(const char *, int);
25075897234Sdrh int sqliteCompare(const char *, const char *);
25175897234Sdrh int sqliteSortCompare(const char *, const char *);
252dcc581ccSdrh #ifdef MEMORY_DEBUG
253dcc581ccSdrh   void *sqliteMalloc_(int,char*,int);
254dcc581ccSdrh   void sqliteFree_(void*,char*,int);
255dcc581ccSdrh   void *sqliteRealloc_(void*,int,char*,int);
256dcc581ccSdrh #else
25775897234Sdrh   void *sqliteMalloc(int);
25875897234Sdrh   void sqliteFree(void*);
25975897234Sdrh   void *sqliteRealloc(void*,int);
260dcc581ccSdrh #endif
26175897234Sdrh int sqliteGetToken(const char*, int *);
26275897234Sdrh void sqliteSetString(char **, const char *, ...);
26375897234Sdrh void sqliteSetNString(char **, ...);
264982cef7eSdrh void sqliteDequote(char*);
26575897234Sdrh int sqliteRunParser(Parse*, char*, char **);
26675897234Sdrh void sqliteExec(Parse*);
26775897234Sdrh Expr *sqliteExpr(int, Expr*, Expr*, Token*);
26875897234Sdrh Expr *sqliteExprFunction(ExprList*, Token*);
26975897234Sdrh void sqliteExprDelete(Expr*);
27075897234Sdrh ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
27175897234Sdrh void sqliteExprListDelete(ExprList*);
27275897234Sdrh void sqliteStartTable(Parse*,Token*,Token*);
27375897234Sdrh void sqliteAddColumn(Parse*,Token*);
2747020f651Sdrh void sqliteAddDefaultValue(Parse*,Token*,int);
27575897234Sdrh void sqliteEndTable(Parse*,Token*);
27675897234Sdrh void sqliteDropTable(Parse*, Token*);
27775897234Sdrh void sqliteDeleteTable(sqlite*, Table*);
27875897234Sdrh void sqliteInsert(Parse*, Token*, ExprList*, IdList*);
27975897234Sdrh IdList *sqliteIdListAppend(IdList*, Token*);
28075897234Sdrh void sqliteIdListAddAlias(IdList*, Token*);
28175897234Sdrh void sqliteIdListDelete(IdList*);
28275897234Sdrh void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*);
28375897234Sdrh void sqliteDropIndex(Parse*, Token*);
284*9bb61fe7Sdrh int sqliteSelect(Parse*, Select*, Table*, int);
285*9bb61fe7Sdrh Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*,int);
286*9bb61fe7Sdrh void sqliteSelectDelete(Select*);
28775897234Sdrh void sqliteDeleteFrom(Parse*, Token*, Expr*);
28875897234Sdrh void sqliteUpdate(Parse*, Token*, ExprList*, Expr*);
28975897234Sdrh WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
29075897234Sdrh void sqliteWhereEnd(WhereInfo*);
29175897234Sdrh void sqliteExprCode(Parse*, Expr*);
29275897234Sdrh void sqliteExprIfTrue(Parse*, Expr*, int);
29375897234Sdrh void sqliteExprIfFalse(Parse*, Expr*, int);
29475897234Sdrh Table *sqliteFindTable(sqlite*,char*);
295982cef7eSdrh void sqliteCopy(Parse*, Token*, Token*, Token*);
296dce2cbe6Sdrh void sqliteVacuum(Parse*, Token*);
297dce2cbe6Sdrh int sqliteGlobCompare(const char*,const char*);
298dce2cbe6Sdrh int sqliteLikeCompare(const unsigned char*,const unsigned char*);
299cce7d176Sdrh char *sqliteTableNameFromToken(Token*);
300cce7d176Sdrh int sqliteExprCheck(Parse*, Expr*, int, int*);
301cce7d176Sdrh int sqliteFuncId(Token*);
302bed8690fSdrh int sqliteExprResolveIds(Parse*, IdList*, Expr*);
303