/* * This function is called to drop a trigger from the database schema. * * This may be called directly from the parser and therefore identifies * the trigger by name. The sqliteDropTriggerPtr() routine does the * same job as this routine except it take a spointer to the trigger * instead of the trigger name. * * Note that this function does not delete the trigger entirely. Instead it * removes it from the internal schema and places it in the trigDrop hash * table. This is so that the trigger can be restored into the database schema * if the transaction is rolled back. */ void sqliteDropTrigger(Parse *pParse, SrcList *pName){ Trigger *pTrigger; int i; const char *zDb; const char *zName; int nName; sqlite *db = pParse->db; if( sqlite_malloc_failed ) goto drop_trigger_cleanup; assert( pName->nSrc==1 ); zDb = pName->a[0].zDatabase; zName = pName->a[0].zName; nName = strlen(zName); for(i=0; i<db->nDb; i++){ int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ if( zDb && sqliteStrICmp(db->aDb[j].zName, zDb) ) continue; pTrigger = sqliteHashFind(&(db->aDb[j].trigHash), zName, nName+1); if( pTrigger ) break; } if( !pTrigger ){ sqliteErrorMsg(pParse, "no such trigger: %S", pName, 0); goto drop_trigger_cleanup; } sqliteDropTriggerPtr(pParse, pTrigger, 0); drop_trigger_cleanup: sqliteSrcListDelete(pName); }
/* ** Locate a user function given a name and a number of arguments. ** Return a pointer to the FuncDef structure that defines that ** function, or return NULL if the function does not exist. ** ** If the createFlag argument is true, then a new (blank) FuncDef ** structure is created and liked into the "db" structure if a ** no matching function previously existed. When createFlag is true ** and the nArg parameter is -1, then only a function that accepts ** any number of arguments will be returned. ** ** If createFlag is false and nArg is -1, then the first valid ** function found is returned. A function is valid if either xFunc ** or xStep is non-zero. */ FuncDef *sqliteFindFunction( sqlite *db, /* An open database */ const char *zName, /* Name of the function. Not null-terminated */ int nName, /* Number of characters in the name */ int nArg, /* Number of arguments. -1 means any number */ int createFlag /* Create new entry if true and does not otherwise exist */ ){ FuncDef *pFirst, *p, *pMaybe; pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName); if( p && !createFlag && nArg<0 ){ while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } return p; } pMaybe = 0; while( p && p->nArg!=nArg ){ if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; p = p->pNext; } if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ return 0; } if( p==0 && pMaybe ){ assert( createFlag==0 ); return pMaybe; } if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ p->nArg = nArg; p->pNext = pFirst; p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC; sqliteHashInsert(&db->aFunc, zName, nName, (void*)p); } return p; }
/* ** Change the datatype for all functions with a given name. See the ** header comment for the prototype of this function in sqlite.h for ** additional information. */ int sqlite_function_type(sqlite *db, const char *zName, int dataType){ FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName)); while( p ){ p->dataType = dataType; p = p->pNext; } return SQLITE_OK; }
/* * Execute and delete the supplied rollback-list on pRbtree. */ static void execute_rollback_list(Rbtree *pRbtree, BtRollbackOp *pList) { BtRollbackOp *pTmp; RbtCursor cur; int res; cur.pRbtree = pRbtree; cur.wrFlag = 1; while( pList ){ switch( pList->eOp ){ case ROLLBACK_INSERT: cur.pTree = sqliteHashFind( &pRbtree->tblHash, 0, pList->iTab ); assert(cur.pTree); cur.iTree = pList->iTab; cur.eSkip = SKIP_NONE; memRbtreeInsert( &cur, pList->pKey, pList->nKey, pList->pData, pList->nData ); break; case ROLLBACK_DELETE: cur.pTree = sqliteHashFind( &pRbtree->tblHash, 0, pList->iTab ); assert(cur.pTree); cur.iTree = pList->iTab; cur.eSkip = SKIP_NONE; memRbtreeMoveto(&cur, pList->pKey, pList->nKey, &res); assert(res == 0); memRbtreeDelete( &cur ); break; case ROLLBACK_CREATE: btreeCreateTable(pRbtree, pList->iTab); break; case ROLLBACK_DROP: memRbtreeDropTable(pRbtree, pList->iTab); break; default: assert(0); } sqliteFree(pList->pKey); sqliteFree(pList->pData); pTmp = pList->pNext; sqliteFree(pList); pList = pTmp; } }
void sqliteDropProc(Parse *pParse, Token *pName){ Object *pObj; char *zName; Vdbe *v = sqliteGetVdbe(pParse); sqlite *db = pParse->db; zName = sqliteStrNDup(pName->z, pName->n); sqliteDequote(zName); pObj = sqliteHashFind(&(db->aDb[0].objectHash), zName, pName->n+1); if( !pParse->explain && !pObj ){ sqliteErrorMsg(pParse, "no such object: %T", pName); goto dropobject_cleanup; } /* Generate code to destroy the database record of the trigger. */ if( v ){ int base; static VdbeOpList dropObject[] = { { OP_Rewind, 0, ADDR(9), 0}, { OP_String, 0, 0, 0}, /* 1 */ { OP_Column, 0, 1, 0}, { OP_Ne, 0, ADDR(8), 0}, { OP_String, 0, 0, "procedure"}, { OP_Column, 0, 0, 0}, { OP_Ne, 0, ADDR(8), 0}, { OP_Delete, 0, 0, 0}, { OP_Next, 0, ADDR(1), 0}, /* 8 */ }; sqliteBeginWriteOperation(pParse, 0, 0); sqliteOpenMasterTable(v, 0); base = sqliteVdbeAddOpList(v, ArraySize(dropObject), dropObject); sqliteVdbeChangeP3(v, base+1, zName, 0); if( pObj && pObj->iDb==0 ){ sqliteChangeCookie(db, v); } sqliteVdbeAddOp(v, OP_Close, 0, 0); sqliteEndWriteOperation(pParse); } /* * If this is not an "explain", then delete the trigger structure. */ if( !pParse->explain ){ sqliteHashInsert(&(db->aDb[pObj->iDb].objectHash), zName, pName->n+1, 0); sqliteDeleteObject(pObj); } dropobject_cleanup: sqliteFree(zName); }
static int sqliteCompileCall( Parse *pParse, Token *pName, ExprList *pEList ) { char *zName = 0; Vdbe *v = sqliteGetVdbe(pParse); Block *b = pParse->pCurrentBlock; Object * pObj = 0; sqlite *db = pParse->db; int i, nActual = 0; /* Check that the object exist & get its Object pointer*/ zName = sqliteStrNDup(pName->z, pName->n); sqliteDequote(zName); pObj = sqliteHashFind(&(db->aDb[0].objectHash), zName,pName->n+1); if( !pObj ){ sqliteErrorMsg(pParse, "object %T not found", pName); goto proc_cleanup; } if( pEList ) { nActual = pEList->nExpr; } if( pObj->nParam!=nActual ) { sqliteErrorMsg(pParse, "bad parameter count for object %T", pName); goto proc_cleanup; } for(i=0; i<nActual; i++) { Expr *pExpr = pEList->a[i].pExpr; if( sqliteExprProcResolve(pParse, b, pExpr) ){ goto proc_cleanup; } if( sqliteExprCheck(pParse, pExpr, 0, 0) ){ goto proc_cleanup; } sqliteExprCode(pParse, pExpr); } sqliteVdbeOp3(v, OP_Exec, nActual, 0, zName, P3_DYNAMIC); return 0; proc_cleanup: sqliteFree(zName); return 1; }
/* * Empty table n of the Rbtree. */ static int memRbtreeClearTable(Rbtree* tree, int n) { BtRbTree *pTree; BtRbNode *pNode; pTree = sqliteHashFind(&tree->tblHash, 0, n); assert(pTree); pNode = pTree->pHead; while( pNode ){ if( pNode->pLeft ){ pNode = pNode->pLeft; } else if( pNode->pRight ){ pNode = pNode->pRight; } else { BtRbNode *pTmp = pNode->pParent; if( tree->eTransState == TRANS_ROLLBACK ){ sqliteFree( pNode->pKey ); sqliteFree( pNode->pData ); }else{ BtRollbackOp *pRollbackOp = sqliteMallocRaw(sizeof(BtRollbackOp)); if( pRollbackOp==0 ) return SQLITE_NOMEM; pRollbackOp->eOp = ROLLBACK_INSERT; pRollbackOp->iTab = n; pRollbackOp->nKey = pNode->nKey; pRollbackOp->pKey = pNode->pKey; pRollbackOp->nData = pNode->nData; pRollbackOp->pData = pNode->pData; btreeLogRollbackOp(tree, pRollbackOp); } sqliteFree( pNode ); if( pTmp ){ if( pTmp->pLeft == pNode ) pTmp->pLeft = 0; else if( pTmp->pRight == pNode ) pTmp->pRight = 0; } pNode = pTmp; } } pTree->pHead = 0; return SQLITE_OK; }
/* * Get a new cursor for table iTable of the supplied Rbtree. The wrFlag * parameter indicates that the cursor is open for writing. * * Note that RbtCursor.eSkip and RbtCursor.pNode both initialize to 0. */ static int memRbtreeCursor( Rbtree* tree, int iTable, int wrFlag, RbtCursor **ppCur ){ RbtCursor *pCur; assert(tree); pCur = *ppCur = sqliteMalloc(sizeof(RbtCursor)); if( sqlite_malloc_failed ) return SQLITE_NOMEM; pCur->pTree = sqliteHashFind(&tree->tblHash, 0, iTable); assert( pCur->pTree ); pCur->pRbtree = tree; pCur->iTree = iTable; pCur->pOps = &sqliteRbtreeCursorOps; pCur->wrFlag = wrFlag; pCur->pShared = pCur->pTree->pCursors; pCur->pTree->pCursors = pCur; assert( (*ppCur)->pTree ); return SQLITE_OK; }
void sqliteBeginProc( Parse *pParse, /* The parse context of the statement */ int what, /* One of TK_PROCEDURE or TK_FUNCTION */ Token *pName /* The name of the object */ ){ Object *no; Block *pBlock = pParse->pCurrentBlock; char *zName = 0; /* Name of the object */ sqlite *db = pParse->db; /* Check that the object name does not already exist */ zName = sqliteStrNDup(pName->z, pName->n); sqliteDequote(zName); if( !pParse->explain && sqliteHashFind(&(db->aDb[0].objectHash), zName,pName->n+1) ){ sqliteErrorMsg(pParse, "object %T already exists", pName); goto object_cleanup; } /* Build the object */ no = (Object*)sqliteMalloc(sizeof(Object)); if( no==0 ) goto object_cleanup; no->name = zName; zName = 0; no->what = what; no->iDb = 0; no->nParam = pBlock->nVar; /* add param checks here */ pBlock->pObj = no; pBlock->params = 0; assert( pParse->pNewTrigger==0 ); pParse->pNewObject = no; return; object_cleanup: sqliteFree(zName); }
/* ** This function registered all of the above C functions as SQL ** functions. This should be the only routine in this file with ** external linkage. */ void sqliteRegisterBuiltinFunctions(sqlite *db){ static struct { char *zName; signed char nArg; signed char dataType; u8 argType; /* 0: none. 1: db 2: (-1) */ void (*xFunc)(sqlite_func*,int,const char**); } aFuncs[] = { { "min", -1, SQLITE_ARGS, 0, minmaxFunc }, { "min", 0, 0, 0, 0 }, { "max", -1, SQLITE_ARGS, 2, minmaxFunc }, { "max", 0, 0, 2, 0 }, { "typeof", 1, SQLITE_TEXT, 0, typeofFunc }, { "length", 1, SQLITE_NUMERIC, 0, lengthFunc }, { "substr", 3, SQLITE_TEXT, 0, substrFunc }, { "abs", 1, SQLITE_NUMERIC, 0, absFunc }, { "round", 1, SQLITE_NUMERIC, 0, roundFunc }, { "round", 2, SQLITE_NUMERIC, 0, roundFunc }, { "upper", 1, SQLITE_TEXT, 0, upperFunc }, { "lower", 1, SQLITE_TEXT, 0, lowerFunc }, { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc }, { "coalesce", 0, 0, 0, 0 }, { "coalesce", 1, 0, 0, 0 }, { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc }, { "random", -1, SQLITE_NUMERIC, 0, randomFunc }, { "like", 2, SQLITE_NUMERIC, 0, likeFunc }, { "glob", 2, SQLITE_NUMERIC, 0, globFunc }, { "nullif", 2, SQLITE_ARGS, 0, nullifFunc }, { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc}, { "quote", 1, SQLITE_ARGS, 0, quoteFunc }, { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid }, { "change_count", 0, SQLITE_NUMERIC, 1, change_count }, { "last_statement_change_count", 0, SQLITE_NUMERIC, 1, last_statement_change_count }, #ifdef SQLITE_SOUNDEX { "soundex", 1, SQLITE_TEXT, 0, soundexFunc}, #endif #ifdef SQLITE_TEST { "randstr", 2, SQLITE_TEXT, 0, randStr }, #endif }; static struct { char *zName; signed char nArg; signed char dataType; u8 argType; void (*xStep)(sqlite_func*,int,const char**); void (*xFinalize)(sqlite_func*); } aAggs[] = { { "min", 1, 0, 0, minmaxStep, minMaxFinalize }, { "max", 1, 0, 2, minmaxStep, minMaxFinalize }, { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize }, { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize }, { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize }, { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize }, #if 0 { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize }, #endif }; static const char *azTypeFuncs[] = { "min", "max", "typeof" }; int i; for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ void *pArg; switch( aFuncs[i].argType ){ case 0: pArg = 0; break; case 1: pArg = db; break; case 2: pArg = (void*)(-1); break; } sqlite_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, aFuncs[i].xFunc, pArg); if( aFuncs[i].xFunc ){ sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType); } } for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ void *pArg; switch( aAggs[i].argType ){ case 0: pArg = 0; break; case 1: pArg = db; break; case 2: pArg = (void*)(-1); break; } sqlite_create_aggregate(db, aAggs[i].zName, aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg); sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType); } for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){ int n = strlen(azTypeFuncs[i]); FuncDef *p = sqliteHashFind(&db->aFunc, azTypeFuncs[i], n); while( p ){ p->includeTypes = 1; p = p->pNext; } } sqliteRegisterDateTimeFunctions(db); }
/* ** This is called by the parser when it sees a CREATE TRIGGER statement ** up to the point of the BEGIN before the trigger actions. A Trigger ** structure is generated based on the information available and stored ** in pParse->pNewTrigger. After the trigger actions have been parsed, the ** sqliteFinishTrigger() function is called to complete the trigger ** construction process. */ void sqliteBeginTrigger( Parse *pParse, /* The parse context of the CREATE TRIGGER statement */ Token *pName, /* The name of the trigger */ int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */ IdList *pColumns, /* column list if this is an UPDATE OF trigger */ SrcList *pTableName,/* The name of the table/view the trigger applies to */ int foreach, /* One of TK_ROW or TK_STATEMENT */ Expr *pWhen, /* WHEN clause */ int isTemp /* True if the TEMPORARY keyword is present */ ){ Trigger *nt; Table *tab; char *zName = 0; /* Name of the trigger */ sqlite *db = pParse->db; int iDb; /* When database to store the trigger in */ DbFixer sFix; /* Check that: ** 1. the trigger name does not already exist. ** 2. the table (or view) does exist in the same database as the trigger. ** 3. that we are not trying to create a trigger on the sqlite_master table ** 4. That we are not trying to create an INSTEAD OF trigger on a table. ** 5. That we are not trying to create a BEFORE or AFTER trigger on a view. */ if( sqlite_malloc_failed ) goto trigger_cleanup; assert( pTableName->nSrc==1 ); if( db->init.busy && sqliteFixInit(&sFix, pParse, db->init.iDb, "trigger", pName) && sqliteFixSrcList(&sFix, pTableName) ){ goto trigger_cleanup; } tab = sqliteSrcListLookup(pParse, pTableName); if( !tab ){ goto trigger_cleanup; } iDb = isTemp ? 1 : tab->iDb; if( iDb>=2 && !db->init.busy ){ sqliteErrorMsg(pParse, "triggers may not be added to auxiliary " "database %s", db->aDb[tab->iDb].zName); goto trigger_cleanup; } zName = sqliteStrNDup(pName->z, pName->n); sqliteDequote(zName); if( sqliteHashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){ sqliteErrorMsg(pParse, "trigger %T already exists", pName); goto trigger_cleanup; } if( sqliteStrNICmp(tab->zName, "sqlite_", 7)==0 ){ sqliteErrorMsg(pParse, "cannot create trigger on system table"); pParse->nErr++; goto trigger_cleanup; } if( tab->pSelect && tr_tm != TK_INSTEAD ){ sqliteErrorMsg(pParse, "cannot create %s trigger on view: %S", (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0); goto trigger_cleanup; } if( !tab->pSelect && tr_tm == TK_INSTEAD ){ sqliteErrorMsg(pParse, "cannot create INSTEAD OF" " trigger on table: %S", pTableName, 0); goto trigger_cleanup; } #ifndef SQLITE_OMIT_AUTHORIZATION { int code = SQLITE_CREATE_TRIGGER; const char *zDb = db->aDb[tab->iDb].zName; const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb; if( tab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; if( sqliteAuthCheck(pParse, code, zName, tab->zName, zDbTrig) ){ goto trigger_cleanup; } if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(tab->iDb), 0, zDb)){ goto trigger_cleanup; } } #endif /* INSTEAD OF triggers can only appear on views and BEGIN triggers ** cannot appear on views. So we might as well translate every ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code ** elsewhere. */ if (tr_tm == TK_INSTEAD){ tr_tm = TK_BEFORE; } /* Build the Trigger object */ nt = (Trigger*)sqliteMalloc(sizeof(Trigger)); if( nt==0 ) goto trigger_cleanup; nt->name = zName; zName = 0; nt->table = sqliteStrDup(pTableName->a[0].zName); if( sqlite_malloc_failed ) goto trigger_cleanup; nt->iDb = iDb; nt->iTabDb = tab->iDb; nt->op = op; nt->tr_tm = tr_tm; nt->pWhen = sqliteExprDup(pWhen); nt->pColumns = sqliteIdListDup(pColumns); nt->foreach = foreach; sqliteTokenCopy(&nt->nameToken,pName); assert( pParse->pNewTrigger==0 ); pParse->pNewTrigger = nt; trigger_cleanup: sqliteFree(zName); sqliteSrcListDelete(pTableName); sqliteIdListDelete(pColumns); sqliteExprDelete(pWhen); }