Break out the upsert code into a separate source file.
[sqlite.git] / src / upsert.c
blobb42ef06a14b62e945fa2a873676e2cdc0a90b218
1 /*
2 ** 2018-04-12
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code to implement various aspects of UPSERT
13 ** processing and handling of the Upsert object.
15 #include "sqliteInt.h"
17 #ifndef SQLITE_OMIT_UPSERT
19 ** Free a list of Upsert objects
21 void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
22 while( p ){
23 Upsert *pNext = p->pUpsertNext;
24 sqlite3ExprListDelete(db, p->pUpsertTarget);
25 sqlite3ExprListDelete(db, p->pUpsertSet);
26 sqlite3ExprDelete(db, p->pUpsertWhere);
27 sqlite3DbFree(db, p);
28 p = pNext;
33 ** Duplicate an Upsert object.
35 Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
36 if( p==0 ) return 0;
37 return sqlite3UpsertNew(db,
38 sqlite3UpsertDup(db, p->pUpsertNext),
39 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
40 sqlite3ExprListDup(db, p->pUpsertSet, 0),
41 sqlite3ExprDup(db, p->pUpsertWhere, 0)
46 ** Create a new Upsert object.
48 Upsert *sqlite3UpsertNew(
49 sqlite3 *db, /* Determines which memory allocator to use */
50 Upsert *pPrior, /* Append this upsert to the end of the new one */
51 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
52 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
53 Expr *pWhere /* WHERE clause for the ON CONFLICT UPDATE */
55 Upsert *pNew;
56 pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
57 if( pNew==0 ){
58 sqlite3UpsertDelete(db, pPrior);
59 sqlite3ExprListDelete(db, pTarget);
60 sqlite3ExprListDelete(db, pSet);
61 sqlite3ExprDelete(db, pWhere);
62 return 0;
63 }else{
64 pNew->pUpsertTarget = pTarget;
65 pNew->pUpsertSet = pSet;
66 pNew->pUpsertNext = pPrior;
67 pNew->pUpsertWhere = pWhere;
69 return pNew;
72 #endif /* SQLITE_OMIT_UPSERT */