1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

Code simplification in sqlite3ColumnsFromExprList(). Update the %z format

code so that it works with buffers obtained from sqlite3DbMalloc().  Add a
testcase for the slow column name uniquifier.

FossilOrigin-Name: 9272426057b6cb2d913519ff4c97aa6e211f7d51
This commit is contained in:
drh
2015-11-14 22:04:22 +00:00
parent 0315e3cc14
commit 96ceaf8680
5 changed files with 24 additions and 20 deletions

View File

@@ -1622,7 +1622,6 @@ int sqlite3ColumnsFromExprList(
p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
if( (zName = pEList->a[i].zName)!=0 ){
/* If the column contains an "AS <name>" phrase, use <name> as the name */
zName = sqlite3DbStrDup(db, zName);
}else{
Expr *pColExpr = p; /* The expression that is the result column name */
Table *pTab; /* Table associated with this expression */
@@ -1635,30 +1634,26 @@ int sqlite3ColumnsFromExprList(
int iCol = pColExpr->iColumn;
pTab = pColExpr->pTab;
if( iCol<0 ) iCol = pTab->iPKey;
zName = sqlite3MPrintf(db, "%s",
iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
}else if( pColExpr->op==TK_ID ){
assert( !ExprHasProperty(pColExpr, EP_IntValue) );
zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
zName = pColExpr->u.zToken;
}else{
/* Use the original text of the column expression as its name */
zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
zName = pEList->a[i].zSpan;
}
}
zName = sqlite3MPrintf(db, "%s", zName);
/* Make sure the column name is unique. If the name is not unique,
** append an integer to the name so that it becomes unique.
*/
cnt = 0;
while( zName && sqlite3HashFind(&ht, zName)!=0 ){
char *zNewName;
nName = sqlite3Strlen30(zName);
for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
if( zName[j]==':' ) nName = j;
zName[nName] = 0;
zNewName = sqlite3MPrintf(db, "%s:%u", zName, ++cnt);
sqlite3DbFree(db, zName);
zName = zNewName;
zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
}
pCol->zName = zName;