Back off of the extended upsert syntax that allows multiple ON CONFLICT

clauses.  The syntax now is exactly as in PostgreSQL and MySQL.  Add support
for WHERE clauses on the conflict-target phrase, for partial indexes.

FossilOrigin-Name: 2c1b1987d8de1efa8ed7e1f199710e32ff20edf8ceec570514fc63bb1ef264e0
This commit is contained in:
drh
2018-04-13 13:06:45 +00:00
parent 788d55aa77
commit e9c2e772f1
7 changed files with 128 additions and 90 deletions
+2 -2
View File
@@ -805,9 +805,9 @@ void sqlite3Insert(
}
}
#ifndef SQLITE_OMIT_UPSERT
if( pUpsert ){
if( pUpsert && pUpsert->pUpsertTarget ){
pTabList->a[0].iCursor = iDataCur;
sqlite3UpsertAnalyze(pParse, pTabList, pUpsert);
sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
}
#endif
+8 -8
View File
@@ -870,15 +870,15 @@ cmd ::= with insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
%type upsert {Upsert*}
%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
upsert(A) ::= . { A = 0; }
upsert(A) ::= upsert(X) ON CONFLICT LP sortlist(Y) RP
upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
DO UPDATE SET setlist(Z) where_opt(W).
{ A = sqlite3UpsertNew(pParse->db,X,Y,Z,W); /*X-overwrites-A*/ }
upsert(A) ::= upsert(X) ON DUPLICATE KEY UPDATE setlist(Z) where_opt(W).
{ A = sqlite3UpsertNew(pParse->db,X,0,Z,W); /*X-overwrites-A*/ }
upsert(A) ::= upsert(X) ON CONFLICT LP sortlist(Y) RP DO NOTHING.
{ A = sqlite3UpsertNew(pParse->db,X,Y,0,0); /*X-overwrites-A*/ }
upsert(A) ::= upsert(X) ON CONFLICT DO NOTHING.
{ A = sqlite3UpsertNew(pParse->db,X,0,0,0); /*X-overwrites-A*/ }
{ A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
upsert(A) ::= ON DUPLICATE KEY UPDATE setlist(Z) where_opt(W).
{ A = sqlite3UpsertNew(pParse->db,0,0,Z,W); }
upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
{ A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
upsert(A) ::= ON CONFLICT DO NOTHING.
{ A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
%type insert_cmd {int}
insert_cmd(A) ::= INSERT orconf(R). {A = R;}
+7 -13
View File
@@ -2711,30 +2711,24 @@ struct NameContext {
/*
** An instance of the following object describes a single ON CONFLICT
** clause in an upsert. A list of these objects may be attached to
** an INSERT statement in order to form an upsert.
** clause in an upsert.
**
** The pUpsertTarget field is only set if the ON CONFLICT clause includes
** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
** conflict-target clause.)
** conflict-target clause.) The pUpsertTargetWhere is the optional
** WHERE clause used to identify partial unique indexes.
**
** pUpsertSet is the list of column=expr terms of the UPDATE statement.
** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
** WHERE clause is omitted.
**
** The pUpsertIdx is a transient pointer to the unique index described
** by pUpsertTarget. If pUpsertTarget describes the rowid, then pUpsertIdx
** will be NULL. pUpsertIdx does not own the Index object it points to.
** Care must be taken to ensure that the Index object does not expire while
** the pointer is valid.
*/
struct Upsert {
ExprList *pUpsertTarget; /* Optional description of conflicting index */
Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
Index *pUpsertIdx; /* UNIQUE index referenced by pUpsertTarget */
Upsert *pUpsertNext; /* Next ON CONFLICT clause in the list */
};
/*
@@ -4287,10 +4281,10 @@ const char *sqlite3JournalModename(int);
#define sqlite3WithDelete(x,y)
#endif
#ifndef SQLITE_OMIT_UPSERT
Upsert *sqlite3UpsertNew(sqlite3*,Upsert*,ExprList*,ExprList*,Expr*);
Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
void sqlite3UpsertDelete(sqlite3*,Upsert*);
Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
int sqlite3UpsertAnalyze(Parse*,SrcList*,Upsert*);
int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
#else
#define sqlite3UpsertNew(x,y,z,w) ((Upsert*)0)
#define sqlite3UpsertDelete(x,y)
+74 -41
View File
@@ -19,13 +19,12 @@
** Free a list of Upsert objects
*/
void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
while( p ){
Upsert *pNext = p->pUpsertNext;
if( p ){
sqlite3ExprListDelete(db, p->pUpsertTarget);
sqlite3ExprDelete(db, p->pUpsertTargetWhere);
sqlite3ExprListDelete(db, p->pUpsertSet);
sqlite3ExprDelete(db, p->pUpsertWhere);
sqlite3DbFree(db, p);
p = pNext;
}
}
@@ -35,8 +34,8 @@ void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
if( p==0 ) return 0;
return sqlite3UpsertNew(db,
sqlite3UpsertDup(db, p->pUpsertNext),
sqlite3ExprListDup(db, p->pUpsertTarget, 0),
sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
sqlite3ExprListDup(db, p->pUpsertSet, 0),
sqlite3ExprDup(db, p->pUpsertWhere, 0)
);
@@ -47,84 +46,118 @@ Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
*/
Upsert *sqlite3UpsertNew(
sqlite3 *db, /* Determines which memory allocator to use */
Upsert *pPrior, /* Append this upsert to the end of the new one */
ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
Expr *pTargetWhere, /* Optional WHERE clause on the target */
ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
Expr *pWhere /* WHERE clause for the ON CONFLICT UPDATE */
){
Upsert *pNew;
pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
if( pNew==0 ){
sqlite3UpsertDelete(db, pPrior);
sqlite3ExprListDelete(db, pTarget);
sqlite3ExprDelete(db, pTargetWhere);
sqlite3ExprListDelete(db, pSet);
sqlite3ExprDelete(db, pWhere);
return 0;
}else{
pNew->pUpsertTarget = pTarget;
pNew->pUpsertTargetWhere = pTargetWhere;
pNew->pUpsertSet = pSet;
pNew->pUpsertNext = pPrior;
pNew->pUpsertWhere = pWhere;
pNew->pUpsertIdx = 0;
}
return pNew;
}
/*
** Analyze the ON CONFLICT clause(s) described by pUpsert. Resolve all
** symbols in the conflict-target clausees. Fill in the pUpsertIdx pointers.
** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
** symbols in the conflict-target.
**
** Return non-zero if there are errors.
** Return SQLITE_OK if everything works, or an error code is something
** is wrong.
*/
int sqlite3UpsertAnalyze(
int sqlite3UpsertAnalyzeTarget(
Parse *pParse, /* The parsing context */
SrcList *pTabList, /* Table into which we are inserting */
Upsert *pUpsert /* The list of ON CONFLICT clauses */
Upsert *pUpsert /* The ON CONFLICT clauses */
){
NameContext sNC;
Upsert *p;
Table *pTab;
Index *pIdx;
int rc = SQLITE_OK;
int nDoNothing = 0;
ExprList *pTarget;
Expr *pTerm;
int rc;
assert( pTabList->nSrc==1 );
assert( pTabList->a[0].pTab!=0 );
assert( pUpsert!=0 );
assert( pUpsert->pUpsertTarget!=0 );
/* Resolve all symbolic names in the conflict-target clause, which
** includes both the list of columns and the optional partial-index
** WHERE clause.
*/
memset(&sNC, 0, sizeof(sNC));
sNC.pParse = pParse;
sNC.pSrcList = pTabList;
rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
if( rc ) return rc;
rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
if( rc ) return rc;
/* Check to see if the conflict target matches the rowid. */
pTab = pTabList->a[0].pTab;
for(p=pUpsert; p; p=p->pUpsertNext){
if( p->pUpsertTarget==0 ){
if( p->pUpsertSet ){
/* This is a MySQL-style ON DUPLICATE KEY clause. The ON DUPLICATE
** KEY clause can only be used if there is exactly one uniqueness
** constraint and/or PRIMARY KEY */
int nUnique = 0;
for(pIdx = pTab->pIndex; pIdx; pIdx=pIdx->pNext){
if( IsUniqueIndex(pIdx) ){
p->pUpsertIdx = pIdx;
nUnique++;
}
}
if( pTab->iPKey>=0 ) nUnique++;
if( nUnique!=0 ){
sqlite3ErrorMsg(pParse, "ON DUPLICATE KEY may only be used if there "
"is exactly one UNIQUE or PRIMARY KEY constraint");
return SQLITE_ERROR;
pTarget = pUpsert->pUpsertTarget;
if( HasRowid(pTab)
&& pTarget->nExpr==1
&& (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
&& (pTerm->iColumn==XN_ROWID || pTerm->iColumn==pTab->iPKey)
){
/* The conflict-target is the rowid of the primary table */
assert( pUpsert->pUpsertIdx==0 );
return SQLITE_OK;
}
/* Check for matches against other indexes */
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
int ii, jj, nn;
if( !IsUniqueIndex(pIdx) ) continue;
if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
if( pIdx->pPartIdxWhere ){
if( pUpsert->pUpsertTargetWhere==0 ) continue;
if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
pIdx->pPartIdxWhere, pTabList->a[0].iCursor)!=0 ){
continue;
}
}
nn = pIdx->nKeyCol;
for(ii=0; ii<nn; ii++){
if( pIdx->aiColumn[ii]!=XN_EXPR ){
for(jj=0; jj<nn; jj++){
if( pTarget->a[jj].pExpr->op!=TK_COLUMN ) continue;
if( pTarget->a[jj].pExpr->iColumn!=pIdx->aiColumn[ii] ) continue;
break;
}
}else{
nDoNothing++;
if( nDoNothing>1 ){
sqlite3ErrorMsg(pParse, "multiple unconstrained DO NOTHING clauses");
return SQLITE_ERROR;
Expr *pExpr;
assert( pIdx->aColExpr!=0 );
assert( pIdx->aColExpr->nExpr>ii );
pExpr = pIdx->aColExpr->a[ii].pExpr;
for(jj=0; jj<nn; jj++){
if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr, -1)==0 ){
break;
}
}
}
continue;
if( jj<nn ) break;
}
rc = sqlite3ResolveExprListNames(&sNC, p->pUpsertTarget);
if( rc ) return rc;
if( ii>=nn ) continue;
pUpsert->pUpsertIdx = pIdx;
return SQLITE_OK;
}
return rc;
sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
"PRIMARY KEY or UNIQUE constraint");
return SQLITE_ERROR;
}
#endif /* SQLITE_OMIT_UPSERT */