/ Check-in [b9ae9a0a]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Rename internal function sqlite3RenameToken() to sqlite3RenameTokenMap() and sqlite3MoveRenameToken() to sqlite3RenameTokenRemap().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256: b9ae9a0a188b82d7066f6ef7d21592889df0a61559e9a0e01819e9d5a92c3f36
User & Date: dan 2018-08-21 16:32:53
Context
2018-08-23
18:22
Make sure the Expr.iTable field is initialized by tokenExpr(). check-in: 02a05a3b user: drh tags: alter-table-rename-column
2018-08-21
16:32
Rename internal function sqlite3RenameToken() to sqlite3RenameTokenMap() and sqlite3MoveRenameToken() to sqlite3RenameTokenRemap(). check-in: b9ae9a0a user: dan tags: alter-table-rename-column
15:06
Rationalize some duplicated code in alter.c. Also improve error messages for ALTER TABLE RENAME COLUMN in some cases. check-in: 5d2163c7 user: dan tags: alter-table-rename-column
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   904    904   ** the token that generated that element.  The parse tree element
   905    905   ** might be one of:
   906    906   **
   907    907   **     *  A pointer to an Expr that represents an ID
   908    908   **     *  The name of a table column in Column.zName
   909    909   **
   910    910   ** A list of RenameToken objects can be constructed during parsing.
   911         -** Each new object is created by sqlite3RenameToken().
   912         -** As the parse tree is transformed, the sqlite3MoveRenameToken()
          911  +** Each new object is created by sqlite3RenameTokenMap().
          912  +** As the parse tree is transformed, the sqlite3RenameTokenRemap()
   913    913   ** routine is used to keep the mapping current.
   914    914   **
   915    915   ** After the parse finishes, renameTokenFind() routine can be used
   916    916   ** to look up the actual token value that created some element in
   917    917   ** the parse tree.
   918    918   */
   919    919   struct RenameToken {
................................................................................
   934    934     Table *pTab;                    /* Table being ALTERed */ 
   935    935     const char *zOld;               /* Old column name */
   936    936   };
   937    937   
   938    938   /*
   939    939   ** Add a new RenameToken object mapping parse tree element pPtr into
   940    940   ** token *pToken to the Parse object currently under construction.
          941  +**
          942  +** Return a copy of pPtr.
   941    943   */
   942         -void *sqlite3RenameToken(Parse *pParse, void *pPtr, Token *pToken){
          944  +void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
   943    945     RenameToken *pNew;
   944    946     pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
   945    947     if( pNew ){
   946    948       pNew->p = pPtr;
   947    949       pNew->t = *pToken;
   948    950       pNew->pNext = pParse->pRename;
   949    951       pParse->pRename = pNew;
   950    952     }
   951    953     return pPtr;
   952    954   }
   953    955   
   954    956   /*
   955         -** If there is a RenameToken object associated with parse tree element
   956         -** pFrom, then remap that object over to pTo due to a transformation
   957         -** in the parse tree.
          957  +** It is assumed that there is already a RenameToken object associated
          958  +** with parse tree element pFrom. This function remaps the associated token
          959  +** to parse tree element pTo.
   958    960   */
   959         -void sqlite3MoveRenameToken(Parse *pParse, void *pTo, void *pFrom){
          961  +void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
   960    962     RenameToken *p;
   961    963     for(p=pParse->pRename; ALWAYS(p); p=p->pNext){
   962    964       if( p->p==pFrom ){
   963    965         p->p = pTo;
   964    966         break;
   965    967       }
   966    968     }

Changes to src/build.c.

  1068   1068     if( (p = pParse->pNewTable)==0 ) return;
  1069   1069     if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1070   1070       sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
  1071   1071       return;
  1072   1072     }
  1073   1073     z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
  1074   1074     if( z==0 ) return;
  1075         -  if( IN_RENAME_COLUMN ) sqlite3RenameToken(pParse, (void*)z, pName);
         1075  +  if( IN_RENAME_COLUMN ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
  1076   1076     memcpy(z, pName->z, pName->n);
  1077   1077     z[pName->n] = 0;
  1078   1078     sqlite3Dequote(z);
  1079   1079     for(i=0; i<p->nCol; i++){
  1080   1080       if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
  1081   1081         sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
  1082   1082         sqlite3DbFree(db, z);
................................................................................
  1367   1367     }
  1368   1368     if( nTerm==1
  1369   1369      && pCol
  1370   1370      && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
  1371   1371      && sortOrder!=SQLITE_SO_DESC
  1372   1372     ){
  1373   1373       if( IN_RENAME_COLUMN && pList ){
  1374         -      sqlite3MoveRenameToken(pParse, &pTab->iPKey, pList->a[0].pExpr);
         1374  +      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
  1375   1375       }
  1376   1376       pTab->iPKey = iCol;
  1377   1377       pTab->keyConf = (u8)onError;
  1378   1378       assert( autoInc==0 || autoInc==1 );
  1379   1379       pTab->tabFlags |= autoInc*TF_Autoincrement;
  1380   1380       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
  1381   1381     }else if( autoInc ){
................................................................................
  2767   2767         if( j>=p->nCol ){
  2768   2768           sqlite3ErrorMsg(pParse, 
  2769   2769             "unknown column \"%s\" in foreign key definition", 
  2770   2770             pFromCol->a[i].zName);
  2771   2771           goto fk_end;
  2772   2772         }
  2773   2773         if( IN_RENAME_COLUMN ){
  2774         -        sqlite3MoveRenameToken(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
         2774  +        sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
  2775   2775         }
  2776   2776       }
  2777   2777     }
  2778   2778     if( pToCol ){
  2779   2779       for(i=0; i<nCol; i++){
  2780   2780         int n = sqlite3Strlen30(pToCol->a[i].zName);
  2781   2781         pFKey->aCol[i].zCol = z;
  2782   2782         if( IN_RENAME_COLUMN ){
  2783         -        sqlite3MoveRenameToken(pParse, z, pToCol->a[i].zName);
         2783  +        sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
  2784   2784         }
  2785   2785         memcpy(z, pToCol->a[i].zName, n);
  2786   2786         z[n] = 0;
  2787   2787         z += n+1;
  2788   2788       }
  2789   2789     }
  2790   2790     pFKey->isDeferred = 0;
................................................................................
  3710   3710     );
  3711   3711     if( i<0 ){
  3712   3712       sqlite3IdListDelete(db, pList);
  3713   3713       return 0;
  3714   3714     }
  3715   3715     pList->a[i].zName = sqlite3NameFromToken(db, pToken);
  3716   3716     if( IN_RENAME_COLUMN && pList->a[i].zName ){
  3717         -    sqlite3RenameToken(pParse, (void*)pList->a[i].zName, pToken);
         3717  +    sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
  3718   3718     }
  3719   3719     return pList;
  3720   3720   }
  3721   3721   
  3722   3722   /*
  3723   3723   ** Delete an IdList.
  3724   3724   */

Changes to src/expr.c.

  1663   1663       struct ExprList_item *pItem;
  1664   1664       assert( pList->nExpr>0 );
  1665   1665       pItem = &pList->a[pList->nExpr-1];
  1666   1666       assert( pItem->zName==0 );
  1667   1667       pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
  1668   1668       if( dequote ) sqlite3Dequote(pItem->zName);
  1669   1669       if( IN_RENAME_COLUMN ){
  1670         -      sqlite3RenameToken(pParse, (void*)pItem->zName, pName);
         1670  +      sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
  1671   1671       }
  1672   1672     }
  1673   1673   }
  1674   1674   
  1675   1675   /*
  1676   1676   ** Set the ExprList.a[].zSpan element of the most recently added item
  1677   1677   ** on the expression list.

Changes to src/parse.y.

   949    949           if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
   950    950           sqlite3Dequote(p->u.zToken);
   951    951         }
   952    952   #if SQLITE_MAX_EXPR_DEPTH>0
   953    953         p->nHeight = 1;
   954    954   #endif  
   955    955         if( IN_RENAME_COLUMN ){
   956         -        return (Expr*)sqlite3RenameToken(pParse, (void*)p, &t);
          956  +        return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
   957    957         }
   958    958       }
   959    959       return p;
   960    960     }
   961    961   
   962    962   }
   963    963   
................................................................................
   964    964   expr(A) ::= term(A).
   965    965   expr(A) ::= LP expr(X) RP. {A = X;}
   966    966   expr(A) ::= id(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
   967    967   expr(A) ::= JOIN_KW(X).     {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
   968    968   expr(A) ::= nm(X) DOT nm(Y). {
   969    969     Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   970    970     Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
   971         -  if( IN_RENAME_COLUMN ) sqlite3RenameToken(pParse, (void*)temp2, &Y);
          971  +  if( IN_RENAME_COLUMN ) sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
   972    972     A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
   973    973   }
   974    974   expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
   975    975     Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   976    976     Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
   977    977     Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
   978    978     Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
   979         -  if( IN_RENAME_COLUMN ) sqlite3RenameToken(pParse, (void*)temp3, &Z);
          979  +  if( IN_RENAME_COLUMN ) sqlite3RenameTokenMap(pParse, (void*)temp3, &Z);
   980    980     A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
   981    981   }
   982    982   term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
   983    983   term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
   984    984   term(A) ::= INTEGER(X). {
   985    985     A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
   986    986   }
................................................................................
  1320   1320           && pParse->db->init.busy==0
  1321   1321       ){
  1322   1322         sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
  1323   1323                            pIdToken->n, pIdToken->z);
  1324   1324       }
  1325   1325       sqlite3ExprListSetName(pParse, p, pIdToken, 1);
  1326   1326       if( IN_RENAME_COLUMN && p ){
  1327         -      sqlite3RenameToken(pParse, (void*)(p->a[p->nExpr-1].zName), pIdToken);
         1327  +      sqlite3RenameTokenMap(pParse, (void*)(p->a[p->nExpr-1].zName), pIdToken);
  1328   1328       }
  1329   1329       return p;
  1330   1330     }
  1331   1331   } // end %include
  1332   1332   
  1333   1333   eidlist_opt(A) ::= .                         {A = 0;}
  1334   1334   eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}

Changes to src/resolve.c.

   672    672             assert( pRight->op==TK_DOT );
   673    673             zDb = pExpr->pLeft->u.zToken;
   674    674             zTable = pRight->pLeft->u.zToken;
   675    675             pRight = pRight->pRight;
   676    676           }
   677    677           zColumn = pRight->u.zToken;
   678    678           if( IN_RENAME_COLUMN ){
   679         -          sqlite3MoveRenameToken(pParse, (void*)pExpr, (void*)pRight);
          679  +          sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
   680    680           }
   681    681         }
   682    682         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
   683    683       }
   684    684   
   685    685       /* Resolve function names
   686    686       */

Changes to src/sqliteInt.h.

  4232   4232   int sqlite3ResolveExprListNames(NameContext*, ExprList*);
  4233   4233   void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
  4234   4234   void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
  4235   4235   int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
  4236   4236   void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
  4237   4237   void sqlite3AlterFinishAddColumn(Parse *, Token *);
  4238   4238   void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  4239         -void *sqlite3RenameToken(Parse*, void*, Token*);
  4240         -void sqlite3MoveRenameToken(Parse*, void *pTo, void *pFrom);
         4239  +void *sqlite3RenameTokenMap(Parse*, void*, Token*);
         4240  +void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
  4241   4241   CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
  4242   4242   char sqlite3AffinityType(const char*, Column*);
  4243   4243   void sqlite3Analyze(Parse*, Token*, Token*);
  4244   4244   int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
  4245   4245   int sqlite3FindDb(sqlite3*, Token*);
  4246   4246   int sqlite3FindDbName(sqlite3 *, const char *);
  4247   4247   int sqlite3AnalysisLoad(sqlite3*,int iDB);