Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -1046,10 +1046,22 @@ /* If an error occurs, we jump here */ begin_table_error: sqlite3DbFree(db, zName); return; } + +/* Set properties of a table column based on the (magical) +** name of the column. +*/ +void sqlite3ColumnPropertiesFromName(Column *pCol){ +#if SQLITE_ENABLE_HIDDEN_COLUMNS + if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ + pCol->colFlags |= COLFLAG_HIDDEN; + } +#endif +} + /* ** This macro is used to compare two strings in a case-insensitive manner. ** It is slightly faster than calling sqlite3StrICmp() directly, but ** produces larger code. @@ -1102,10 +1114,11 @@ p->aCol = aNew; } pCol = &p->aCol[p->nCol]; memset(pCol, 0, sizeof(p->aCol[0])); pCol->zName = z; + sqlite3ColumnPropertiesFromName(pCol); /* If there is no type specified, columns have the default affinity ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will ** be called next to set pCol->affinity correctly. */ Index: src/insert.c ================================================================== --- src/insert.c +++ src/insert.c @@ -734,14 +734,12 @@ } /* Make sure the number of columns in the source data matches the number ** of columns to be inserted into the table. */ - if( IsVirtual(pTab) ){ - for(i=0; inCol; i++){ - nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); - } + for(i=0; inCol; i++){ + nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); } if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ sqlite3ErrorMsg(pParse, "table %S has %d columns but %d values were supplied", pTabList, 0, pTab->nCol-nHidden, nColumn); @@ -833,26 +831,26 @@ */ assert( !IsVirtual(pTab) ); /* Create the new column data */ - for(i=0; inCol; i++){ - if( pColumn==0 ){ - j = i; - }else{ + for(i=j=0; inCol; i++){ + if( pColumn ){ for(j=0; jnId; j++){ if( pColumn->a[j].idx==i ) break; } } - if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ + if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) + || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){ sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); }else if( useTempTable ){ sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); }else{ assert( pSelect==0 ); /* Otherwise useTempTable is true */ sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); } + if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++; } /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, ** do not attempt any conversions before assembling the record. ** If this is a real table, attempt conversions as required by the @@ -932,11 +930,10 @@ sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); continue; } if( pColumn==0 ){ if( IsHiddenColumn(&pTab->aCol[i]) ){ - assert( IsVirtual(pTab) ); j = -1; nHidden++; }else{ j = i - nHidden; } Index: src/select.c ================================================================== --- src/select.c +++ src/select.c @@ -1657,10 +1657,11 @@ } zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt); if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); } pCol->zName = zName; + sqlite3ColumnPropertiesFromName(pCol); if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){ db->mallocFailed = 1; } } sqlite3HashClear(&ht); @@ -4358,16 +4359,14 @@ && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0 ){ continue; } - /* If a column is marked as 'hidden' (currently only possible - ** for virtual tables), do not include it in the expanded - ** result-set list. + /* If a column is marked as 'hidden', do not include it in + ** the expanded result-set list. */ if( IsHiddenColumn(&pTab->aCol[j]) ){ - assert(IsVirtual(pTab)); continue; } tableSeen = 1; if( i>0 && zTName==0 ){ Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -1694,15 +1694,32 @@ ** done as a macro so that it will be optimized out when virtual ** table support is omitted from the build. */ #ifndef SQLITE_OMIT_VIRTUALTABLE # define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0) -# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) #else # define IsVirtual(X) 0 # define IsHiddenColumn(X) 0 #endif + +/* +** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn() +** only works for non-virtual tables (ordinary tables and views) and is +** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The +** IsHiddenColumn() macro is general purpose. +*/ +#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS) +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) +# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) +#elif !defined(SQLITE_OMIT_VIRTUAL) +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) +# define IsOrdinaryHiddenColumn(X) 0 +#else +# define IsHiddenColumn(X) 0 +# define IsOrdinaryHiddenColumn(X) 0 +#endif + /* Does the table have a rowid */ #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0) #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0) @@ -3312,10 +3329,11 @@ Table *sqlite3ResultSetOfSelect(Parse*,Select*); void sqlite3OpenMasterTable(Parse *, int); Index *sqlite3PrimaryKeyIndex(Table*); i16 sqlite3ColumnOfIndex(Index*, i16); void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int); +void sqlite3ColumnPropertiesFromName(Column*); void sqlite3AddColumn(Parse*,Token*); void sqlite3AddNotNull(Parse*, int); void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int); void sqlite3AddCheckConstraint(Parse*, Expr*); void sqlite3AddColumnType(Parse*,Token*); Index: src/test_config.c ================================================================== --- src/test_config.c +++ src/test_config.c @@ -122,10 +122,16 @@ #ifdef SQLITE_ENABLE_CURSOR_HINTS Tcl_SetVar2(interp, "sqlite_options", "cursorhints", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "cursorhints", "0", TCL_GLOBAL_ONLY); #endif + +#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS + Tcl_SetVar2(interp, "sqlite_options", "hiddencolumns", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "hiddencolumns", "0", TCL_GLOBAL_ONLY); +#endif #ifdef SQLITE_ENABLE_MEMSYS3 Tcl_SetVar2(interp, "sqlite_options", "mem3", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "mem3", "0", TCL_GLOBAL_ONLY); ADDED test/hidden.test Index: test/hidden.test ================================================================== --- /dev/null +++ test/hidden.test @@ -0,0 +1,74 @@ +# 2015 November 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test the __hidden__ hack. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix hidden + +ifcapable !hiddencolumns { + finish_test + return +} + +do_execsql_test 1.1 { + CREATE TABLE t1(__hidden__a, b); + INSERT INTO t1 VALUES('1'); + INSERT INTO t1(__hidden__a, b) VALUES('x', 'y'); +} {} + +do_execsql_test 1.2 { + SELECT * FROM t1; +} {1 y} + +do_execsql_test 1.3 { + SELECT __hidden__a, * FROM t1; +} {{} 1 x y} + +foreach {tn view} { + 1 { CREATE VIEW v1(a, b, __hidden__c) AS SELECT a, b, c FROM x1 } + 2 { CREATE VIEW v1 AS SELECT a, b, c AS __hidden__c FROM x1 } +} { + do_execsql_test 2.$tn.1 { + DROP TABLE IF EXISTS x1; + CREATE TABLE x1(a, b, c); + INSERT INTO x1 VALUES(1, 2, 3); + } + + catchsql { DROP VIEW v1 } + execsql $view + + do_execsql_test 2.$tn.2 { + SELECT a, b, __hidden__c FROM v1; + } {1 2 3} + + do_execsql_test 2.$tn.3 { + SELECT * FROM v1; + } {1 2} + + do_execsql_test 2.$tn.4 { + CREATE TRIGGER tr1 INSTEAD OF INSERT ON v1 BEGIN + INSERT INTO x1 VALUES(new.a, new.b, new.__hidden__c); + END; + + INSERT INTO v1 VALUES(4, 5); + SELECT * FROM x1; + } {1 2 3 4 5 {}} + + do_execsql_test 2.$tn.5 { + INSERT INTO v1(a, b, __hidden__c) VALUES(7, 8, 9); + SELECT * FROM x1; + } {1 2 3 4 5 {} 7 8 9} +} + +finish_test Index: test/releasetest.tcl ================================================================== --- test/releasetest.tcl +++ test/releasetest.tcl @@ -117,10 +117,11 @@ -DSQLITE_ENABLE_RTREE=1 -DSQLITE_ENABLE_MEMSYS5=1 -DSQLITE_ENABLE_MEMSYS3=1 -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_ENABLE_STAT4 + -DSQLITE_ENABLE_HIDDEN_COLUMNS -DSQLITE_MAX_ATTACHED=125 } "Fast-One" { -O6 -DSQLITE_ENABLE_FTS4=1 @@ -143,10 +144,11 @@ -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 -DSQLITE_MAX_PAGE_SIZE=4096 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_OMIT_PROGRESS_CALLBACK=1 -DSQLITE_OMIT_VIRTUALTABLE=1 + -DSQLITE_ENABLE_HIDDEN_COLUMNS -DSQLITE_TEMP_STORE=3 --enable-json1 } "Device-Two" { -DSQLITE_4_BYTE_ALIGNED_MALLOC=1 @@ -211,10 +213,11 @@ } "Valgrind" { -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE + -DSQLITE_ENABLE_HIDDEN_COLUMNS --enable-json1 } # The next group of configurations are used only by the # Failure-Detection platform. They are all the same, but we need