MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / idxProcessOneTrigger

Function idxProcessOneTrigger

modules/dasSQLITE/sqlite/shell.c:11494–11575  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11492}
11493
11494static int idxProcessOneTrigger(
11495 sqlite3expert *p,
11496 IdxWrite *pWrite,
11497 char **pzErr
11498){
11499 static const char *zInt = UNIQUE_TABLE_NAME;
11500 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
11501 IdxTable *pTab = pWrite->pTab;
11502 const char *zTab = pTab->zName;
11503 const char *zSql =
11504 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
11505 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
11506 "ORDER BY type;";
11507 sqlite3_stmt *pSelect = 0;
11508 int rc = SQLITE_OK;
11509 char *zWrite = 0;
11510
11511 /* Create the table and its triggers in the temp schema */
11512 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
11513 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
11514 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
11515 if( zCreate==0 ) continue;
11516 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
11517 }
11518 idxFinalize(&rc, pSelect);
11519
11520 /* Rename the table in the temp schema to zInt */
11521 if( rc==SQLITE_OK ){
11522 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
11523 if( z==0 ){
11524 rc = SQLITE_NOMEM;
11525 }else{
11526 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
11527 sqlite3_free(z);
11528 }
11529 }
11530
11531 switch( pWrite->eOp ){
11532 case SQLITE_INSERT: {
11533 int i;
11534 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
11535 for(i=0; i<pTab->nCol; i++){
11536 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
11537 }
11538 zWrite = idxAppendText(&rc, zWrite, ")");
11539 break;
11540 }
11541 case SQLITE_UPDATE: {
11542 int i;
11543 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
11544 for(i=0; i<pTab->nCol; i++){
11545 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
11546 pTab->aCol[i].zName
11547 );
11548 }
11549 break;
11550 }
11551 default: {

Callers 1

idxProcessTriggersFunction · 0.85

Calls 6

idxPrintfPrepareStmtFunction · 0.85
sqlite3_execFunction · 0.85
idxFinalizeFunction · 0.85
idxAppendTextFunction · 0.85
assertFunction · 0.85
idxDatabaseErrorFunction · 0.85

Tested by

no test coverage detected