| 9590 | } |
| 9591 | |
| 9592 | static int idxProcessOneTrigger( |
| 9593 | sqlite3expert *p, |
| 9594 | IdxWrite *pWrite, |
| 9595 | char **pzErr |
| 9596 | ){ |
| 9597 | static const char *zInt = UNIQUE_TABLE_NAME; |
| 9598 | static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME; |
| 9599 | IdxTable *pTab = pWrite->pTab; |
| 9600 | const char *zTab = pTab->zName; |
| 9601 | const char *zSql = |
| 9602 | "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema " |
| 9603 | "WHERE tbl_name = %Q AND type IN ('table', 'trigger') " |
| 9604 | "ORDER BY type;"; |
| 9605 | sqlite3_stmt *pSelect = 0; |
| 9606 | int rc = SQLITE_OK; |
| 9607 | char *zWrite = 0; |
| 9608 | |
| 9609 | /* Create the table and its triggers in the temp schema */ |
| 9610 | rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); |
| 9611 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){ |
| 9612 | const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0); |
| 9613 | rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); |
| 9614 | } |
| 9615 | idxFinalize(&rc, pSelect); |
| 9616 | |
| 9617 | /* Rename the table in the temp schema to zInt */ |
| 9618 | if( rc==SQLITE_OK ){ |
| 9619 | char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt); |
| 9620 | if( z==0 ){ |
| 9621 | rc = SQLITE_NOMEM; |
| 9622 | }else{ |
| 9623 | rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); |
| 9624 | sqlite3_free(z); |
| 9625 | } |
| 9626 | } |
| 9627 | |
| 9628 | switch( pWrite->eOp ){ |
| 9629 | case SQLITE_INSERT: { |
| 9630 | int i; |
| 9631 | zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt); |
| 9632 | for(i=0; i<pTab->nCol; i++){ |
| 9633 | zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", "); |
| 9634 | } |
| 9635 | zWrite = idxAppendText(&rc, zWrite, ")"); |
| 9636 | break; |
| 9637 | } |
| 9638 | case SQLITE_UPDATE: { |
| 9639 | int i; |
| 9640 | zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt); |
| 9641 | for(i=0; i<pTab->nCol; i++){ |
| 9642 | zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", |
| 9643 | pTab->aCol[i].zName |
| 9644 | ); |
| 9645 | } |
| 9646 | break; |
| 9647 | } |
| 9648 | default: { |
| 9649 | assert( pWrite->eOp==SQLITE_DELETE ); |
no test coverage detected