xref: /sqlite-3.40.0/src/sqliteInt.h (revision 0bdaf62e)
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*0bdaf62eSdrh ** @(#) $Id: sqliteInt.h,v 1.24 2000/06/11 23:50:13 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 
3856c58161Sdrh /* #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__)
436e142f54Sdrh # define sqliteStrDup(X)    sqliteStrDup_(X,__FILE__,__LINE__)
446e142f54Sdrh # define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__)
45c3c2fc9aSdrh   void sqliteStrRealloc(char**);
46c3c2fc9aSdrh #else
47c3c2fc9aSdrh # define sqliteStrRealloc(X)
48dcc581ccSdrh #endif
49dcc581ccSdrh 
5075897234Sdrh /*
516e142f54Sdrh ** The following global variables are used for testing and debugging
526e142f54Sdrh ** only.  Thy only work if MEMORY_DEBUG is defined.
536e142f54Sdrh */
546e142f54Sdrh #ifdef MEMORY_DEBUG
556e142f54Sdrh int sqlite_nMalloc;         /* Number of sqliteMalloc() calls */
566e142f54Sdrh int sqlite_nFree;           /* Number of sqliteFree() calls */
576e142f54Sdrh int sqlite_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
586e142f54Sdrh #endif
596e142f54Sdrh 
606e142f54Sdrh /*
6175897234Sdrh ** The number of entries in the in-memory hash table holding the
6275897234Sdrh ** schema.
6375897234Sdrh */
6475897234Sdrh #define N_HASH        51
6575897234Sdrh 
6675897234Sdrh /*
6775897234Sdrh ** Name of the master database table.  The master database table
6875897234Sdrh ** is a special table that holds the names and attributes of all
6975897234Sdrh ** user tables and indices.
7075897234Sdrh */
7175897234Sdrh #define MASTER_NAME   "sqlite_master"
7275897234Sdrh 
7375897234Sdrh /*
7475897234Sdrh ** A convenience macro that returns the number of elements in
7575897234Sdrh ** an array.
7675897234Sdrh */
7775897234Sdrh #define ArraySize(X)    (sizeof(X)/sizeof(X[0]))
7875897234Sdrh 
7975897234Sdrh /*
80cce7d176Sdrh ** Integer identifiers for functions.
81cce7d176Sdrh */
82cce7d176Sdrh #define FN_Unknown    0
83cce7d176Sdrh #define FN_Count      1
84cce7d176Sdrh #define FN_Min        2
85cce7d176Sdrh #define FN_Max        3
86cce7d176Sdrh #define FN_Sum        4
87cce7d176Sdrh #define FN_Avg        5
88*0bdaf62eSdrh #define FN_Fcnt       6
89cce7d176Sdrh 
90cce7d176Sdrh /*
9175897234Sdrh ** Forward references to structures
9275897234Sdrh */
937020f651Sdrh typedef struct Column Column;
9475897234Sdrh typedef struct Table Table;
9575897234Sdrh typedef struct Index Index;
9675897234Sdrh typedef struct Instruction Instruction;
9775897234Sdrh typedef struct Expr Expr;
9875897234Sdrh typedef struct ExprList ExprList;
9975897234Sdrh typedef struct Parse Parse;
10075897234Sdrh typedef struct Token Token;
10175897234Sdrh typedef struct IdList IdList;
10275897234Sdrh typedef struct WhereInfo WhereInfo;
1039bb61fe7Sdrh typedef struct Select Select;
1042282792aSdrh typedef struct AggExpr AggExpr;
10575897234Sdrh 
10675897234Sdrh /*
10775897234Sdrh ** Each database is an instance of the following structure
10875897234Sdrh */
10975897234Sdrh struct sqlite {
11075897234Sdrh   Dbbe *pBe;                 /* The backend driver */
11175897234Sdrh   int flags;                 /* Miscellanous flags */
11275897234Sdrh   Table *apTblHash[N_HASH];  /* All tables of the database */
11375897234Sdrh   Index *apIdxHash[N_HASH];  /* All indices of the database */
11475897234Sdrh };
11575897234Sdrh 
11675897234Sdrh /*
11775897234Sdrh ** Possible values for the flags field of sqlite
11875897234Sdrh */
11975897234Sdrh #define SQLITE_VdbeTrace    0x00000001
12058b9576bSdrh #define SQLITE_Initialized  0x00000002
12158b9576bSdrh 
12258b9576bSdrh /*
1237020f651Sdrh ** information about each column of a table is held in an instance
1247020f651Sdrh ** of this structure.
1257020f651Sdrh */
1267020f651Sdrh struct Column {
1277020f651Sdrh   char *zName;        /* Name of this column */
1287020f651Sdrh   char *zDflt;        /* Default value of this column */
1297020f651Sdrh   int notNull;        /* True if there is a NOT NULL constraing */
1307020f651Sdrh };
1317020f651Sdrh 
1327020f651Sdrh /*
13375897234Sdrh ** Each table is represented in memory by
13475897234Sdrh ** an instance of the following structure
13575897234Sdrh */
13675897234Sdrh struct Table {
13775897234Sdrh   char *zName;        /* Name of the table */
13875897234Sdrh   Table *pHash;       /* Next table with same hash on zName */
13975897234Sdrh   int nCol;           /* Number of columns in this table */
1407020f651Sdrh   Column *aCol;       /* Information about each column */
14175897234Sdrh   int readOnly;       /* True if this table should not be written by the user */
14275897234Sdrh   Index *pIndex;      /* List of indices on this table. */
14375897234Sdrh };
14475897234Sdrh 
14575897234Sdrh /*
14675897234Sdrh ** Each index is represented in memory by and
14775897234Sdrh ** instance of the following structure.
14875897234Sdrh */
14975897234Sdrh struct Index {
15075897234Sdrh   char *zName;        /* Name of this index */
15175897234Sdrh   Index *pHash;       /* Next index with the same hash on zName */
15275897234Sdrh   int nField;         /* Number of fields in the table indexed by this index */
15375897234Sdrh   int *aiField;       /* Indices of fields used by this index.  1st is 0 */
15475897234Sdrh   Table *pTable;      /* The table being indexed */
1557020f651Sdrh   int isUnique;       /* True if keys must all be unique */
15675897234Sdrh   Index *pNext;       /* The next index associated with the same table */
15775897234Sdrh };
15875897234Sdrh 
15975897234Sdrh /*
16075897234Sdrh ** Each token coming out of the lexer is an instance of
16175897234Sdrh ** this structure.
16275897234Sdrh */
16375897234Sdrh struct Token {
16475897234Sdrh   char *z;      /* Text of the token */
16575897234Sdrh   int n;        /* Number of characters in this token */
16675897234Sdrh };
16775897234Sdrh 
16875897234Sdrh /*
16975897234Sdrh ** Each node of an expression in the parse tree is an instance
17075897234Sdrh ** of this structure
17175897234Sdrh */
17275897234Sdrh struct Expr {
17375897234Sdrh   int op;                /* Operation performed by this node */
17475897234Sdrh   Expr *pLeft, *pRight;  /* Left and right subnodes */
17575897234Sdrh   ExprList *pList;       /* A list of expressions used as a function argument */
17675897234Sdrh   Token token;           /* An operand token */
17775897234Sdrh   int iTable, iField;    /* When op==TK_FIELD, then this node means the
1782282792aSdrh                          ** iField-th field of the iTable-th table.  When
1792282792aSdrh                          ** op==TK_FUNCTION, iField holds the function id */
1802282792aSdrh   int iAgg;              /* When op==TK_FIELD and pParse->useAgg==TRUE, pull
1812282792aSdrh                          ** value from these element of the aggregator */
18219a775c2Sdrh   Select *pSelect;       /* When the expression is a sub-select */
18375897234Sdrh };
18475897234Sdrh 
18575897234Sdrh /*
18675897234Sdrh ** A list of expressions.  Each expression may optionally have a
18775897234Sdrh ** name.  An expr/name combination can be used in several ways, such
18875897234Sdrh ** as the list of "expr AS ID" fields following a "SELECT" or in the
18975897234Sdrh ** list of "ID = expr" items in an UPDATE.  A list of expressions can
19075897234Sdrh ** also be used as the argument to a function, in which case the azName
19175897234Sdrh ** field is not used.
19275897234Sdrh */
19375897234Sdrh struct ExprList {
19475897234Sdrh   int nExpr;             /* Number of expressions on the list */
19575897234Sdrh   struct {
19675897234Sdrh     Expr *pExpr;           /* The list of expressions */
19775897234Sdrh     char *zName;           /* Token associated with this expression */
198d8bc7086Sdrh     char sortOrder;        /* 1 for DESC or 0 for ASC */
199d8bc7086Sdrh     char isAgg;            /* True if this is an aggregate like count(*) */
200d8bc7086Sdrh     char done;             /* A flag to indicate when processing is finished */
20175897234Sdrh   } *a;                  /* One entry for each expression */
20275897234Sdrh };
20375897234Sdrh 
20475897234Sdrh /*
20575897234Sdrh ** A list of identifiers.
20675897234Sdrh */
20775897234Sdrh struct IdList {
20875897234Sdrh   int nId;         /* Number of identifiers on the list */
20975897234Sdrh   struct {
21075897234Sdrh     char *zName;      /* Text of the identifier. */
21175897234Sdrh     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
21275897234Sdrh     Table *pTab;      /* Table corresponding to zName */
21319a775c2Sdrh     int idx;          /* Index of a field named zName in a table */
21475897234Sdrh   } *a;            /* One entry for each identifier on the list */
21575897234Sdrh };
21675897234Sdrh 
21775897234Sdrh /*
21875897234Sdrh ** The WHERE clause processing routine has two halves.  The
21975897234Sdrh ** first part does the start of the WHERE loop and the second
22075897234Sdrh ** half does the tail of the WHERE loop.  An instance of
22175897234Sdrh ** this structure is returned by the first half and passed
22275897234Sdrh ** into the second half to give some continuity.
22375897234Sdrh */
22475897234Sdrh struct WhereInfo {
22575897234Sdrh   Parse *pParse;
22619a775c2Sdrh   IdList *pTabList;    /* List of tables in the join */
22719a775c2Sdrh   int iContinue;       /* Jump here to continue with next record */
22819a775c2Sdrh   int iBreak;          /* Jump here to break out of the loop */
22919a775c2Sdrh   int base;            /* Index of first Open opcode */
23019a775c2Sdrh   Index *aIdx[32];     /* Indices used for each table */
23175897234Sdrh };
23275897234Sdrh 
23375897234Sdrh /*
2349bb61fe7Sdrh ** An instance of the following structure contains all information
2359bb61fe7Sdrh ** needed to generate code for a single SELECT statement.
2369bb61fe7Sdrh */
2379bb61fe7Sdrh struct Select {
2389bb61fe7Sdrh   int isDistinct;        /* True if the DISTINCT keyword is present */
2399bb61fe7Sdrh   ExprList *pEList;      /* The fields of the result */
2409bb61fe7Sdrh   IdList *pSrc;          /* The FROM clause */
2419bb61fe7Sdrh   Expr *pWhere;          /* The WHERE clause */
2429bb61fe7Sdrh   ExprList *pGroupBy;    /* The GROUP BY clause */
2439bb61fe7Sdrh   Expr *pHaving;         /* The HAVING clause */
2449bb61fe7Sdrh   ExprList *pOrderBy;    /* The ORDER BY clause */
24582c3d636Sdrh   int op;                /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
24682c3d636Sdrh   Select *pPrior;        /* Prior select to which this one joins */
2479bb61fe7Sdrh };
2489bb61fe7Sdrh 
2499bb61fe7Sdrh /*
250fef5208cSdrh ** The results of a select can be distributed in several ways.
251fef5208cSdrh */
252fef5208cSdrh #define SRT_Callback     1  /* Invoke a callback with each row of result */
253fef5208cSdrh #define SRT_Mem          2  /* Store result in a memory cell */
25482c3d636Sdrh #define SRT_Set          3  /* Store result as unique keys in a table */
25582c3d636Sdrh #define SRT_Union        5  /* Store result as keys in a table */
25682c3d636Sdrh #define SRT_Except       6  /* Remove result from a UNION table */
2575974a30fSdrh #define SRT_Table        7  /* Store result as data with a unique key */
258fef5208cSdrh 
259fef5208cSdrh /*
2602282792aSdrh ** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)")
2612282792aSdrh ** we have to do some additional analysis of expressions.  An instance
2622282792aSdrh ** of the following structure holds information about a single subexpression
2632282792aSdrh ** somewhere in the SELECT statement.  An array of these structures holds
2642282792aSdrh ** all the information we need to generate code for aggregate
2652282792aSdrh ** expressions.
2662282792aSdrh **
2672282792aSdrh ** Note that when analyzing a SELECT containing aggregates, both
2682282792aSdrh ** non-aggregate field variables and aggregate functions are stored
2692282792aSdrh ** in the AggExpr array of the Parser structure.
2702282792aSdrh **
2712282792aSdrh ** The pExpr field points to an expression that is part of either the
2722282792aSdrh ** field list, the GROUP BY clause, the HAVING clause or the ORDER BY
2732282792aSdrh ** clause.  The expression will be freed when those clauses are cleaned
2742282792aSdrh ** up.  Do not try to delete the expression attached to AggExpr.pExpr.
2752282792aSdrh **
2762282792aSdrh ** If AggExpr.pExpr==0, that means the expression is "count(*)".
2772282792aSdrh */
2782282792aSdrh struct AggExpr {
2792282792aSdrh   int isAgg;        /* if TRUE contains an aggregate function */
2802282792aSdrh   Expr *pExpr;      /* The expression */
2812282792aSdrh };
2822282792aSdrh 
2832282792aSdrh /*
28475897234Sdrh ** An SQL parser context
28575897234Sdrh */
28675897234Sdrh struct Parse {
28775897234Sdrh   sqlite *db;          /* The main database structure */
28875897234Sdrh   sqlite_callback xCallback;  /* The callback function */
28975897234Sdrh   void *pArg;          /* First argument to the callback function */
29075897234Sdrh   char *zErrMsg;       /* An error message */
29175897234Sdrh   Token sErrToken;     /* The token at which the error occurred */
29275897234Sdrh   Token sFirstToken;   /* The first token parsed */
29375897234Sdrh   Token sLastToken;    /* The last token parsed */
29475897234Sdrh   Table *pNewTable;    /* A table being constructed by CREATE TABLE */
29575897234Sdrh   Vdbe *pVdbe;         /* An engine for executing database bytecode */
296d8bc7086Sdrh   int colNamesSet;     /* TRUE after OP_ColumnCount has been issued to pVdbe */
29775897234Sdrh   int explain;         /* True if the EXPLAIN flag is found on the query */
29875897234Sdrh   int initFlag;        /* True if reparsing CREATE TABLEs */
29975897234Sdrh   int nErr;            /* Number of errors seen */
30019a775c2Sdrh   int nTab;            /* Number of previously allocated cursors */
30119a775c2Sdrh   int nMem;            /* Number of memory cells used so far */
302fef5208cSdrh   int nSet;            /* Number of sets used so far */
3032282792aSdrh   int nAgg;            /* Number of aggregate expressions */
3042282792aSdrh   AggExpr *aAgg;       /* An array of aggregate expressions */
3052282792aSdrh   int iAggCount;       /* Index of the count(*) aggregate in aAgg[] */
3062282792aSdrh   int useAgg;          /* If true, extract field values from the aggregator
3072282792aSdrh                        ** while generating expressions.  Normally false */
30875897234Sdrh };
30975897234Sdrh 
31075897234Sdrh /*
31175897234Sdrh ** Internal function prototypes
31275897234Sdrh */
31375897234Sdrh int sqliteStrICmp(const char *, const char *);
31475897234Sdrh int sqliteStrNICmp(const char *, const char *, int);
31575897234Sdrh int sqliteHashNoCase(const char *, int);
31675897234Sdrh int sqliteCompare(const char *, const char *);
31775897234Sdrh int sqliteSortCompare(const char *, const char *);
318dcc581ccSdrh #ifdef MEMORY_DEBUG
319dcc581ccSdrh   void *sqliteMalloc_(int,char*,int);
320dcc581ccSdrh   void sqliteFree_(void*,char*,int);
321dcc581ccSdrh   void *sqliteRealloc_(void*,int,char*,int);
3226e142f54Sdrh   char *sqliteStrDup_(const char*,char*,int);
3236e142f54Sdrh   char *sqliteStrNDup_(const char*, int,char*,int);
324dcc581ccSdrh #else
32575897234Sdrh   void *sqliteMalloc(int);
32675897234Sdrh   void sqliteFree(void*);
32775897234Sdrh   void *sqliteRealloc(void*,int);
3286e142f54Sdrh   char *sqliteStrDup(const char*);
3296e142f54Sdrh   char *sqliteStrNDup(const char*, int);
330dcc581ccSdrh #endif
33175897234Sdrh int sqliteGetToken(const char*, int *);
33275897234Sdrh void sqliteSetString(char **, const char *, ...);
33375897234Sdrh void sqliteSetNString(char **, ...);
334982cef7eSdrh void sqliteDequote(char*);
33575897234Sdrh int sqliteRunParser(Parse*, char*, char **);
33675897234Sdrh void sqliteExec(Parse*);
33775897234Sdrh Expr *sqliteExpr(int, Expr*, Expr*, Token*);
33875897234Sdrh Expr *sqliteExprFunction(ExprList*, Token*);
33975897234Sdrh void sqliteExprDelete(Expr*);
34075897234Sdrh ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
34175897234Sdrh void sqliteExprListDelete(ExprList*);
34275897234Sdrh void sqliteStartTable(Parse*,Token*,Token*);
34375897234Sdrh void sqliteAddColumn(Parse*,Token*);
3447020f651Sdrh void sqliteAddDefaultValue(Parse*,Token*,int);
34575897234Sdrh void sqliteEndTable(Parse*,Token*);
34675897234Sdrh void sqliteDropTable(Parse*, Token*);
34775897234Sdrh void sqliteDeleteTable(sqlite*, Table*);
3485974a30fSdrh void sqliteInsert(Parse*, Token*, ExprList*, Select*, IdList*);
34975897234Sdrh IdList *sqliteIdListAppend(IdList*, Token*);
35075897234Sdrh void sqliteIdListAddAlias(IdList*, Token*);
35175897234Sdrh void sqliteIdListDelete(IdList*);
35275897234Sdrh void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*);
35375897234Sdrh void sqliteDropIndex(Parse*, Token*);
35419a775c2Sdrh int sqliteSelect(Parse*, Select*, int, int);
3559bb61fe7Sdrh Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*,int);
3569bb61fe7Sdrh void sqliteSelectDelete(Select*);
35775897234Sdrh void sqliteDeleteFrom(Parse*, Token*, Expr*);
35875897234Sdrh void sqliteUpdate(Parse*, Token*, ExprList*, Expr*);
35975897234Sdrh WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
36075897234Sdrh void sqliteWhereEnd(WhereInfo*);
36175897234Sdrh void sqliteExprCode(Parse*, Expr*);
36275897234Sdrh void sqliteExprIfTrue(Parse*, Expr*, int);
36375897234Sdrh void sqliteExprIfFalse(Parse*, Expr*, int);
36475897234Sdrh Table *sqliteFindTable(sqlite*,char*);
365982cef7eSdrh void sqliteCopy(Parse*, Token*, Token*, Token*);
366dce2cbe6Sdrh void sqliteVacuum(Parse*, Token*);
367dce2cbe6Sdrh int sqliteGlobCompare(const char*,const char*);
368dce2cbe6Sdrh int sqliteLikeCompare(const unsigned char*,const unsigned char*);
369cce7d176Sdrh char *sqliteTableNameFromToken(Token*);
370cce7d176Sdrh int sqliteExprCheck(Parse*, Expr*, int, int*);
371d8bc7086Sdrh int sqliteExprCompare(Expr*, Expr*);
372cce7d176Sdrh int sqliteFuncId(Token*);
373bed8690fSdrh int sqliteExprResolveIds(Parse*, IdList*, Expr*);
3744794b980Sdrh void sqliteExprResolveInSelect(Parse*, Expr*);
3752282792aSdrh int sqliteExprAnalyzeAggregates(Parse*, Expr*);
376d8bc7086Sdrh void sqliteParseInfoReset(Parse*);
377d8bc7086Sdrh Vdbe *sqliteGetVdbe(Parse*);
378