Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix sessions module handling of tables with generated columns. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
437fb316389bc3c24c5cdb4d01edfc81 |
User & Date: | dan 2025-01-28 19:03:37.907 |
Context
2025-01-28
| ||
20:32 | Enhance the if() and iif() SQL functions so that they support any number of arguments greater than or equal to two. Suggested by forum post 40f7867f75f80. (check-in: fb76d184ee user: drh tags: trunk) | |
19:03 | Fix sessions module handling of tables with generated columns. (check-in: 437fb31638 user: dan tags: trunk) | |
18:03 | Fix a copy/paste typo in the output of vfstrace for xDlClose(). (check-in: 1d57b57c85 user: drh tags: trunk) | |
Changes
Changes to ext/session/session_common.tcl.
︙ | ︙ | |||
197 198 199 200 201 202 203 204 205 | puts $lot2 error "databases contain different tables" } foreach tbl $lot1 { set col1 [list] set col2 [list] $db1 eval "PRAGMA table_info = $tbl" { lappend col1 $name } | > | > > > | | 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | puts $lot2 error "databases contain different tables" } foreach tbl $lot1 { set col1 [list] set col2 [list] set quoted [list] $db1 eval "PRAGMA table_info = $tbl" { lappend col1 $name } $db2 eval "PRAGMA table_info = $tbl" { lappend col2 $name lappend quoted "\"[string map {\" \"\"} $name]\"" } if {$col1 != $col2} { error "table $tbl schema mismatch" } set sql "SELECT * FROM $tbl ORDER BY [join $quoted ,]" set data1 [$db1 eval $sql] set data2 [$db2 eval $sql] if {$data1 != $data2} { puts "$db1: $data1" puts "$db2: $data2" error "table $tbl data mismatch" } |
︙ | ︙ |
Added ext/session/session_gen.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | # 2025 Jan 28 # # 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. # #*********************************************************************** # This file implements regression tests for SQLite library. # if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join [file dirname [info script]] session_common.tcl] source $testdir/tester.tcl ifcapable !session {finish_test; return} set testprefix session_gen foreach {otn sct} { 1 VIRTUAL 2 STORED } { eval [string map [list %TYPE% $sct] { reset_db set testprefix $testprefix-$otn do_execsql_test 1.0 { CREATE TABLE t1(a INTEGER PRIMARY KEY, b); CREATE TABLE t2(a INTEGER PRIMARY KEY, b AS (c+1) %TYPE%, c); CREATE TABLE t3( a, b AS (a+10) %TYPE%, c, d AS (c+1) %TYPE%, e, PRIMARY KEY(c, e) ) WITHOUT ROWID; CREATE TABLE t4(a AS (c*100) %TYPE%, b INTEGER PRIMARY KEY, c); CREATE TABLE t5(x, y); } foreach {tn sql changeset} { 0.1 { INSERT INTO t5 VALUES('abc', 'def'); } { {INSERT t5 0 X.. {} {i 1 t abc t def}} } 0.2 { UPDATE t5 SET y='xyz' WHERE rowid=1; } { {UPDATE t5 0 X.. {i 1 {} {} t def} {{} {} {} {} t xyz}} } 0.3 { DELETE FROM t5; } { {DELETE t5 0 X.. {i 1 t abc t xyz} {}} } 1.1 { INSERT INTO t2 VALUES(1, 2); INSERT INTO t2 VALUES(2, 123); } { {INSERT t2 0 X. {} {i 1 i 2}} {INSERT t2 0 X. {} {i 2 i 123}} } 1.2 { UPDATE t2 SET c=456 WHERE a=1 } { {UPDATE t2 0 X. {i 1 i 2} {{} {} i 456}} } 1.3 { DELETE FROM t2 WHERE a=2 } { {DELETE t2 0 X. {i 2 i 123} {}} } 1.4 { UPDATE t2 SET a=15 } { {INSERT t2 0 X. {} {i 15 i 456}} {DELETE t2 0 X. {i 1 i 456} {}} } 2.1 { INSERT INTO t3 VALUES(5, 6, 7); INSERT INTO t3 VALUES(8, 9, 10); } { {INSERT t3 0 .XX {} {i 8 i 9 i 10}} {INSERT t3 0 .XX {} {i 5 i 6 i 7}} } 2.2 { UPDATE t3 SET a = 505 WHERE (c, e) = (6, 7); } { {UPDATE t3 0 .XX {i 5 i 6 i 7} {i 505 {} {} {} {}}} } 2.3 { DELETE FROM t3 WHERE (c, e) = (9, 10); } { {DELETE t3 0 .XX {i 8 i 9 i 10} {}} } 2.4 { UPDATE t3 SET c=1000 } { {DELETE t3 0 .XX {i 505 i 6 i 7} {}} {INSERT t3 0 .XX {} {i 505 i 1000 i 7}} } 3.1 { INSERT INTO t4 VALUES(100, 100); } { {INSERT t4 0 X. {} {i 100 i 100}} } } { do_test 1.$tn.1 { sqlite3session S db main S object_config rowid 1 S attach * execsql $sql } {} do_changeset_test 1.$tn.2 S $changeset S delete } #------------------------------------------------------------------------- reset_db forcedelete test.db2 sqlite3 db2 test.db2 do_common_sql { CREATE TABLE t0(x INTEGER PRIMARY KEY, y); INSERT INTO t0 VALUES(1, 'one'); INSERT INTO t0 VALUES(2, 'two'); CREATE TABLE t1(a AS (c*10) %TYPE%, b INTEGER PRIMARY KEY, c); INSERT INTO t1 VALUES(1, 5); INSERT INTO t1 VALUES(2, 10); INSERT INTO t1 VALUES(3, 5); CREATE TABLE t2( a, b, c AS (a*b) %TYPE%, 'k 1', 'k 2', PRIMARY KEY('k 1', 'k 2') ) WITHOUT ROWID; INSERT INTO t2 VALUES('a', 'b', 1, 11); INSERT INTO t2 VALUES('A', 'B', 2, 22); INSERT INTO t2 VALUES('Aa', 'Bb', 3, 33); } foreach {tn sql} { 1.1 { INSERT INTO t0 VALUES(4, 15) } 1.2 { INSERT INTO t1 VALUES(4, 15) } 1.3 { INSERT INTO t2 VALUES(1, 2, 3, 4) } 2.1 { UPDATE t1 SET c=100 WHERE b=2 } 2.2 { UPDATE t2 SET a=11 } 3.1 { DELETE FROM t2 WHERE (t2.'k 1') = 2 } 3.2 { DELETE FROM t1 } } { do_test 2.$tn.1 { # execsql { PRAGMA vdbe_listing = 1 } db2 do_then_apply_sql $sql } {} do_test 2.$tn.2 { compare_db db db2 } {} } }]} finish_test |
Changes to ext/session/sqlite3session.c.
︙ | ︙ | |||
135 136 137 138 139 140 141 | ** then this variable is the compiled version of: ** ** SELECT 1, NULL, 'abc' */ struct SessionTable { SessionTable *pNext; char *zName; /* Local name of table */ | | > > | 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | ** then this variable is the compiled version of: ** ** SELECT 1, NULL, 'abc' */ struct SessionTable { SessionTable *pNext; char *zName; /* Local name of table */ int nCol; /* Number of non-hidden columns */ int nTotalCol; /* Number of columns including hidden */ int bStat1; /* True if this is sqlite_stat1 */ int bRowid; /* True if this table uses rowid for PK */ const char **azCol; /* Column names */ const char **azDflt; /* Default value expressions */ int *aiIdx; /* Index to pass to xNew/xOld */ u8 *abPK; /* Array of primary key flags */ int nEntry; /* Total number of entries in hash table */ int nChange; /* Size of apChange[] array */ SessionChange **apChange; /* Hash table buckets */ sqlite3_stmt *pDfltStmt; }; |
︙ | ︙ | |||
542 543 544 545 546 547 548 549 | int bNew, /* True to hash the new.* PK */ int *piHash, /* OUT: Hash value */ int *pbNullPK /* OUT: True if there are NULL values in PK */ ){ unsigned int h = 0; /* Hash value to return */ int i; /* Used to iterate through columns */ if( pTab->bRowid ){ | > < < > | | | 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | int bNew, /* True to hash the new.* PK */ int *piHash, /* OUT: Hash value */ int *pbNullPK /* OUT: True if there are NULL values in PK */ ){ unsigned int h = 0; /* Hash value to return */ int i; /* Used to iterate through columns */ assert( pTab->nTotalCol==pSession->hook.xCount(pSession->hook.pCtx) ); if( pTab->bRowid ){ h = sessionHashAppendI64(h, iRowid); }else{ assert( *pbNullPK==0 ); for(i=0; i<pTab->nCol; i++){ if( pTab->abPK[i] ){ int rc; int eType; sqlite3_value *pVal; int iIdx = pTab->aiIdx[i]; if( bNew ){ rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal); }else{ rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal); } if( rc!=SQLITE_OK ) return rc; eType = sqlite3_value_type(pVal); h = sessionHashAppendType(h, eType); if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ i64 iVal; |
︙ | ︙ | |||
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | for(iCol=0; iCol<pTab->nCol; iCol++){ if( !pTab->abPK[iCol] ){ a += sessionSerialLen(a); }else{ sqlite3_value *pVal; /* Value returned by preupdate_new/old */ int rc; /* Error code from preupdate_new/old */ int eType = *a++; /* Type of value from change record */ /* The following calls to preupdate_new() and preupdate_old() can not ** fail. This is because they cache their return values, and by the ** time control flows to here they have already been called once from ** within sessionPreupdateHash(). The first two asserts below verify ** this (that the method has already been called). */ if( op==SQLITE_INSERT ){ /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */ | > | | | 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | for(iCol=0; iCol<pTab->nCol; iCol++){ if( !pTab->abPK[iCol] ){ a += sessionSerialLen(a); }else{ sqlite3_value *pVal; /* Value returned by preupdate_new/old */ int rc; /* Error code from preupdate_new/old */ int eType = *a++; /* Type of value from change record */ int iIdx = pTab->aiIdx[iCol]; /* The following calls to preupdate_new() and preupdate_old() can not ** fail. This is because they cache their return values, and by the ** time control flows to here they have already been called once from ** within sessionPreupdateHash(). The first two asserts below verify ** this (that the method has already been called). */ if( op==SQLITE_INSERT ){ /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */ rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal); }else{ /* assert( db->pPreUpdate->pUnpacked ); */ rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal); } assert( rc==SQLITE_OK ); (void)rc; /* Suppress warning about unused variable */ if( sqlite3_value_type(pVal)!=eType ) return 0; /* A SessionChange object never has a NULL value in a PK column */ assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT |
︙ | ︙ | |||
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 | */ static int sessionTableInfo( sqlite3_session *pSession, /* For memory accounting. May be NULL */ sqlite3 *db, /* Database connection */ const char *zDb, /* Name of attached database (e.g. "main") */ const char *zThis, /* Table name */ int *pnCol, /* OUT: number of columns */ const char **pzTab, /* OUT: Copy of zThis */ const char ***pazCol, /* OUT: Array of column names for table */ const char ***pazDflt, /* OUT: Array of default value expressions */ u8 **pabPK, /* OUT: Array of booleans - true for PK col */ int *pbRowid /* OUT: True if only PK is a rowid */ ){ char *zPragma; sqlite3_stmt *pStmt; int rc; sqlite3_int64 nByte; int nDbCol = 0; int nThis; int i; u8 *pAlloc = 0; char **azCol = 0; char **azDflt = 0; u8 *abPK = 0; int bRowid = 0; /* Set to true to use rowid as PK */ assert( pazCol && pabPK ); *pazCol = 0; *pabPK = 0; *pnCol = 0; if( pzTab ) *pzTab = 0; if( pazDflt ) *pazDflt = 0; nThis = sqlite3Strlen30(zThis); if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){ rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0); if( rc==SQLITE_OK ){ /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */ zPragma = sqlite3_mprintf( | > > > > > | | | | > | > | | | > > | | | | | | | | | | | | | | | | > | > > > | 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 | */ static int sessionTableInfo( sqlite3_session *pSession, /* For memory accounting. May be NULL */ sqlite3 *db, /* Database connection */ const char *zDb, /* Name of attached database (e.g. "main") */ const char *zThis, /* Table name */ int *pnCol, /* OUT: number of columns */ int *pnTotalCol, /* OUT: number of hidden columns */ const char **pzTab, /* OUT: Copy of zThis */ const char ***pazCol, /* OUT: Array of column names for table */ const char ***pazDflt, /* OUT: Array of default value expressions */ int **paiIdx, /* OUT: Array of xNew/xOld indexes */ u8 **pabPK, /* OUT: Array of booleans - true for PK col */ int *pbRowid /* OUT: True if only PK is a rowid */ ){ char *zPragma; sqlite3_stmt *pStmt; int rc; sqlite3_int64 nByte; int nDbCol = 0; int nThis; int i; u8 *pAlloc = 0; char **azCol = 0; char **azDflt = 0; u8 *abPK = 0; int *aiIdx = 0; int bRowid = 0; /* Set to true to use rowid as PK */ assert( pazCol && pabPK ); *pazCol = 0; *pabPK = 0; *pnCol = 0; if( pnTotalCol ) *pnTotalCol = 0; if( paiIdx ) *paiIdx = 0; if( pzTab ) *pzTab = 0; if( pazDflt ) *pazDflt = 0; nThis = sqlite3Strlen30(zThis); if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){ rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0); if( rc==SQLITE_OK ){ /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */ zPragma = sqlite3_mprintf( "SELECT 0, 'tbl', '', 0, '', 1, 0 UNION ALL " "SELECT 1, 'idx', '', 0, '', 2, 0 UNION ALL " "SELECT 2, 'stat', '', 0, '', 0, 0" ); }else if( rc==SQLITE_ERROR ){ zPragma = sqlite3_mprintf(""); }else{ return rc; } }else{ zPragma = sqlite3_mprintf("PRAGMA '%q'.table_xinfo('%q')", zDb, zThis); } if( !zPragma ){ return SQLITE_NOMEM; } rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0); sqlite3_free(zPragma); if( rc!=SQLITE_OK ){ return rc; } nByte = nThis + 1; bRowid = (pbRowid!=0); while( SQLITE_ROW==sqlite3_step(pStmt) ){ nByte += sqlite3_column_bytes(pStmt, 1); /* name */ nByte += sqlite3_column_bytes(pStmt, 4); /* dflt_value */ if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */ nDbCol++; } if( sqlite3_column_int(pStmt, 5) ) bRowid = 0; /* pk */ } if( nDbCol==0 ) bRowid = 0; nDbCol += bRowid; nByte += strlen(SESSIONS_ROWID); rc = sqlite3_reset(pStmt); if( rc==SQLITE_OK ){ nByte += nDbCol * (sizeof(const char *)*2 +sizeof(int)+sizeof(u8) + 1 + 1); pAlloc = sessionMalloc64(pSession, nByte); if( pAlloc==0 ){ rc = SQLITE_NOMEM; }else{ memset(pAlloc, 0, nByte); } } if( rc==SQLITE_OK ){ azCol = (char **)pAlloc; azDflt = (char**)&azCol[nDbCol]; aiIdx = (int*)&azDflt[nDbCol]; abPK = (u8 *)&aiIdx[nDbCol]; pAlloc = &abPK[nDbCol]; if( pzTab ){ memcpy(pAlloc, zThis, nThis+1); *pzTab = (char *)pAlloc; pAlloc += nThis+1; } i = 0; if( bRowid ){ size_t nName = strlen(SESSIONS_ROWID); memcpy(pAlloc, SESSIONS_ROWID, nName+1); azCol[i] = (char*)pAlloc; pAlloc += nName+1; abPK[i] = 1; aiIdx[i] = -1; i++; } while( SQLITE_ROW==sqlite3_step(pStmt) ){ if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */ int nName = sqlite3_column_bytes(pStmt, 1); int nDflt = sqlite3_column_bytes(pStmt, 4); const unsigned char *zName = sqlite3_column_text(pStmt, 1); const unsigned char *zDflt = sqlite3_column_text(pStmt, 4); if( zName==0 ) break; memcpy(pAlloc, zName, nName+1); azCol[i] = (char *)pAlloc; pAlloc += nName+1; if( zDflt ){ memcpy(pAlloc, zDflt, nDflt+1); azDflt[i] = (char *)pAlloc; pAlloc += nDflt+1; }else{ azDflt[i] = 0; } abPK[i] = sqlite3_column_int(pStmt, 5); aiIdx[i] = sqlite3_column_int(pStmt, 0); i++; } if( pnTotalCol ) (*pnTotalCol)++; } rc = sqlite3_reset(pStmt); } /* If successful, populate the output variables. Otherwise, zero them and ** free any allocation made. An error code will be returned in this case. */ if( rc==SQLITE_OK ){ *pazCol = (const char**)azCol; if( pazDflt ) *pazDflt = (const char**)azDflt; *pabPK = abPK; *pnCol = nDbCol; if( paiIdx ) *paiIdx = aiIdx; }else{ sessionFree(pSession, azCol); } if( pbRowid ) *pbRowid = bRowid; sqlite3_finalize(pStmt); return rc; } |
︙ | ︙ | |||
1190 1191 1192 1193 1194 1195 1196 | ){ int rc = SQLITE_OK; if( pTab->nCol==0 ){ u8 *abPK; assert( pTab->azCol==0 || pTab->abPK==0 ); rc = sessionTableInfo(pSession, db, zDb, | | > | 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 | ){ int rc = SQLITE_OK; if( pTab->nCol==0 ){ u8 *abPK; assert( pTab->azCol==0 || pTab->abPK==0 ); rc = sessionTableInfo(pSession, db, zDb, pTab->zName, &pTab->nCol, &pTab->nTotalCol, 0, &pTab->azCol, &pTab->azDflt, &pTab->aiIdx, &abPK, ((pSession==0 || pSession->bImplicitPK) ? &pTab->bRowid : 0) ); if( rc==SQLITE_OK ){ int i; for(i=0; i<pTab->nCol; i++){ if( abPK[i] ){ pTab->abPK = abPK; |
︙ | ︙ | |||
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 | } /* ** Re-initialize table object pTab. */ static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){ int nCol = 0; const char **azCol = 0; const char **azDflt = 0; u8 *abPK = 0; int bRowid = 0; assert( pSession->rc==SQLITE_OK ); pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb, | > > | | 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 | } /* ** Re-initialize table object pTab. */ static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){ int nCol = 0; int nTotalCol = 0; const char **azCol = 0; const char **azDflt = 0; int *aiIdx = 0; u8 *abPK = 0; int bRowid = 0; assert( pSession->rc==SQLITE_OK ); pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb, pTab->zName, &nCol, &nTotalCol, 0, &azCol, &azDflt, &aiIdx, &abPK, (pSession->bImplicitPK ? &bRowid : 0) ); if( pSession->rc==SQLITE_OK ){ if( pTab->nCol>nCol || pTab->bRowid!=bRowid ){ pSession->rc = SQLITE_SCHEMA; }else{ int ii; |
︙ | ︙ | |||
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 | } } if( pSession->rc==SQLITE_OK ){ const char **a = pTab->azCol; pTab->azCol = azCol; pTab->nCol = nCol; pTab->azDflt = azDflt; pTab->abPK = abPK; azCol = a; } if( pSession->bEnableSize ){ pSession->nMaxChangesetSize += (nCol - nOldCol); pSession->nMaxChangesetSize += sessionVarintLen(nCol); pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol); } | > > | 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 | } } if( pSession->rc==SQLITE_OK ){ const char **a = pTab->azCol; pTab->azCol = azCol; pTab->nCol = nCol; pTab->nTotalCol = nTotalCol; pTab->azDflt = azDflt; pTab->abPK = abPK; pTab->aiIdx = aiIdx; azCol = a; } if( pSession->bEnableSize ){ pSession->nMaxChangesetSize += (nCol - nOldCol); pSession->nMaxChangesetSize += sessionVarintLen(nCol); pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol); } |
︙ | ︙ | |||
1575 1576 1577 1578 1579 1580 1581 | i64 nNew = 2; if( pC->op==SQLITE_INSERT ){ if( pTab->bRowid ) nNew += 9; if( op!=SQLITE_DELETE ){ int ii; for(ii=0; ii<pTab->nCol; ii++){ sqlite3_value *p = 0; | | > | | 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 | i64 nNew = 2; if( pC->op==SQLITE_INSERT ){ if( pTab->bRowid ) nNew += 9; if( op!=SQLITE_DELETE ){ int ii; for(ii=0; ii<pTab->nCol; ii++){ sqlite3_value *p = 0; pSession->hook.xNew(pSession->hook.pCtx, pTab->aiIdx[ii], &p); sessionSerializeValue(0, p, &nNew); } } }else if( op==SQLITE_DELETE ){ nNew += pC->nRecord; if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){ nNew += pC->nRecord; } }else{ int ii; u8 *pCsr = pC->aRecord; if( pTab->bRowid ){ nNew += 9 + 1; pCsr += 9; } for(ii=pTab->bRowid; ii<pTab->nCol; ii++){ int bChanged = 1; int nOld = 0; int eType; int iIdx = pTab->aiIdx[ii]; sqlite3_value *p = 0; pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p); if( p==0 ){ return SQLITE_NOMEM; } eType = *pCsr++; switch( eType ){ case SQLITE_NULL: |
︙ | ︙ | |||
1693 1694 1695 1696 1697 1698 1699 | /* Load table details if required */ if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return; /* Check the number of columns in this xPreUpdate call matches the ** number of columns in the table. */ nExpect = pSession->hook.xCount(pSession->hook.pCtx); | | | | 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 | /* Load table details if required */ if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return; /* Check the number of columns in this xPreUpdate call matches the ** number of columns in the table. */ nExpect = pSession->hook.xCount(pSession->hook.pCtx); if( pTab->nTotalCol<nExpect ){ if( sessionReinitTable(pSession, pTab) ) return; if( sessionUpdateChanges(pSession, pTab) ) return; } if( pTab->nTotalCol!=nExpect ){ pSession->rc = SQLITE_SCHEMA; return; } /* Grow the hash table if required */ if( sessionGrowHash(pSession, 0, pTab) ){ pSession->rc = SQLITE_NOMEM; |
︙ | ︙ | |||
1754 1755 1756 1757 1758 1759 1760 | int i; /* Used to iterate through columns */ assert( rc==SQLITE_OK ); pTab->nEntry++; /* Figure out how large an allocation is required */ nByte = sizeof(SessionChange); | | > | | | 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 | int i; /* Used to iterate through columns */ assert( rc==SQLITE_OK ); pTab->nEntry++; /* Figure out how large an allocation is required */ nByte = sizeof(SessionChange); for(i=pTab->bRowid; i<pTab->nCol; i++){ int iIdx = pTab->aiIdx[i]; sqlite3_value *p = 0; if( op!=SQLITE_INSERT ){ /* This may fail if the column has a non-NULL default and was added ** using ALTER TABLE ADD COLUMN after this record was created. */ rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p); }else if( pTab->abPK[i] ){ TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx,iIdx,&p); assert( trc==SQLITE_OK ); } if( rc==SQLITE_OK ){ /* This may fail if SQLite value p contains a utf-16 string that must ** be converted to utf-8 and an OOM error occurs while doing so. */ rc = sessionSerializeValue(0, p, &nByte); |
︙ | ︙ | |||
1796 1797 1798 1799 1800 1801 1802 | ** It is not possible for an OOM to occur in this block. */ nByte = 0; if( pTab->bRowid ){ pC->aRecord[0] = SQLITE_INTEGER; sessionPutI64(&pC->aRecord[1], iRowid); nByte = 9; } | | > | | | 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 | ** It is not possible for an OOM to occur in this block. */ nByte = 0; if( pTab->bRowid ){ pC->aRecord[0] = SQLITE_INTEGER; sessionPutI64(&pC->aRecord[1], iRowid); nByte = 9; } for(i=pTab->bRowid; i<pTab->nCol; i++){ sqlite3_value *p = 0; int iIdx = pTab->aiIdx[i]; if( op!=SQLITE_INSERT ){ pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p); }else if( pTab->abPK[i] ){ pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p); } sessionSerializeValue(&pC->aRecord[nByte], p, &nByte); } /* Add the change to the hash-table */ if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){ pC->bIndirect = 1; |
︙ | ︙ | |||
2203 2204 2205 2206 2207 2208 2209 | if( rc==SQLITE_OK ){ int bHasPk = 0; int bMismatch = 0; int nCol; /* Columns in zFrom.zTbl */ int bRowid = 0; u8 *abPK; const char **azCol = 0; | | > | 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 | if( rc==SQLITE_OK ){ int bHasPk = 0; int bMismatch = 0; int nCol; /* Columns in zFrom.zTbl */ int bRowid = 0; u8 *abPK; const char **azCol = 0; rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, 0, &azCol, 0, 0, &abPK, pSession->bImplicitPK ? &bRowid : 0 ); if( rc==SQLITE_OK ){ if( pTo->nCol!=nCol ){ bMismatch = 1; }else{ int i; |
︙ | ︙ | |||
2780 2781 2782 2783 2784 2785 2786 | const char **azCol, /* Names of table columns */ u8 *abPK, /* PRIMARY KEY array */ sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */ ){ int rc = SQLITE_OK; char *zSql = 0; const char *zSep = ""; | < > | > > > > > > > | | 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 | const char **azCol, /* Names of table columns */ u8 *abPK, /* PRIMARY KEY array */ sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */ ){ int rc = SQLITE_OK; char *zSql = 0; const char *zSep = ""; int nSql = -1; int i; SessionBuffer cols = {0, 0, 0}; SessionBuffer nooptest = {0, 0, 0}; SessionBuffer pkfield = {0, 0, 0}; SessionBuffer pkvar = {0, 0, 0}; sessionAppendStr(&nooptest, ", 1", &rc); if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){ sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc); sessionAppendStr(&pkfield, "tbl, idx", &rc); sessionAppendStr(&pkvar, "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc ); sessionAppendStr(&cols, "tbl, ?2, stat", &rc); }else{ #if 0 if( bRowid ){ sessionAppendStr(&cols, SESSIONS_ROWID, &rc); } #endif for(i=0; i<nCol; i++){ if( cols.nBuf ) sessionAppendStr(&cols, ", ", &rc); sessionAppendIdent(&cols, azCol[i], &rc); if( abPK[i] ){ sessionAppendStr(&pkfield, zSep, &rc); sessionAppendStr(&pkvar, zSep, &rc); zSep = ", "; sessionAppendIdent(&pkfield, azCol[i], &rc); sessionAppendPrintf(&pkvar, &rc, "?%d", i+1); }else{ sessionAppendPrintf(&nooptest, &rc, " AND (?%d OR ?%d IS %w.%w)", i+1+nCol, i+1, zTab, azCol[i] ); } } } if( rc==SQLITE_OK ){ zSql = sqlite3_mprintf( "SELECT %s%s FROM %Q.%Q WHERE (%s) IS (%s)", (char*)cols.aBuf, (bIgnoreNoop ? (char*)nooptest.aBuf : ""), zDb, zTab, (char*)pkfield.aBuf, (char*)pkvar.aBuf ); if( zSql==0 ) rc = SQLITE_NOMEM; } #if 0 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){ |
︙ | ︙ | |||
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 | if( rc==SQLITE_OK ){ rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0); } sqlite3_free(zSql); sqlite3_free(nooptest.aBuf); sqlite3_free(pkfield.aBuf); sqlite3_free(pkvar.aBuf); return rc; } /* ** Bind the PRIMARY KEY values from the change passed in argument pChange ** to the SELECT statement passed as the first argument. The SELECT statement ** is as prepared by function sessionSelectStmt(). | > | 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 | if( rc==SQLITE_OK ){ rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0); } sqlite3_free(zSql); sqlite3_free(nooptest.aBuf); sqlite3_free(pkfield.aBuf); sqlite3_free(pkvar.aBuf); sqlite3_free(cols.aBuf); return rc; } /* ** Bind the PRIMARY KEY values from the change passed in argument pChange ** to the SELECT statement passed as the first argument. The SELECT statement ** is as prepared by function sessionSelectStmt(). |
︙ | ︙ | |||
5199 5200 5201 5202 5203 5204 5205 | sApply.azCol = (const char **)zTab; }else{ int nMinCol = 0; int i; sqlite3changeset_pk(pIter, &abPK, 0); rc = sessionTableInfo(0, db, "main", zNew, | > | | 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 | sApply.azCol = (const char **)zTab; }else{ int nMinCol = 0; int i; sqlite3changeset_pk(pIter, &abPK, 0); rc = sessionTableInfo(0, db, "main", zNew, &sApply.nCol, 0, &zTab, &sApply.azCol, 0, 0, &sApply.abPK, &sApply.bRowid ); if( rc!=SQLITE_OK ) break; for(i=0; i<sApply.nCol; i++){ if( sApply.abPK[i] ) nMinCol = i+1; } if( sApply.nCol==0 ){ |
︙ | ︙ |
Changes to src/vdbeapi.c.
︙ | ︙ | |||
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 | ** This function is called from within a pre-update callback to retrieve ** a field of the row currently being updated or deleted. */ int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ PreUpdate *p; Mem *pMem; int rc = SQLITE_OK; #ifdef SQLITE_ENABLE_API_ARMOR if( db==0 || ppValue==0 ){ return SQLITE_MISUSE_BKPT; } #endif p = db->pPreUpdate; /* Test that this call is being made from within an SQLITE_DELETE or ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ if( !p || p->op==SQLITE_INSERT ){ rc = SQLITE_MISUSE_BKPT; goto preupdate_old_out; } if( p->pPk ){ | > | > > | | 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 | ** This function is called from within a pre-update callback to retrieve ** a field of the row currently being updated or deleted. */ int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ PreUpdate *p; Mem *pMem; int rc = SQLITE_OK; int iStore = 0; #ifdef SQLITE_ENABLE_API_ARMOR if( db==0 || ppValue==0 ){ return SQLITE_MISUSE_BKPT; } #endif p = db->pPreUpdate; /* Test that this call is being made from within an SQLITE_DELETE or ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ if( !p || p->op==SQLITE_INSERT ){ rc = SQLITE_MISUSE_BKPT; goto preupdate_old_out; } if( p->pPk ){ iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); }else{ iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); } if( iStore>=p->pCsr->nField || iStore<0 ){ rc = SQLITE_RANGE; goto preupdate_old_out; } if( iIdx==p->pTab->iPKey ){ *ppValue = pMem = &p->oldipk; sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
︙ | ︙ | |||
2220 2221 2222 2223 2224 2225 2226 | if( rc!=SQLITE_OK ){ sqlite3DbFree(db, aRec); goto preupdate_old_out; } p->aRecord = aRec; } | | | | 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 | if( rc!=SQLITE_OK ){ sqlite3DbFree(db, aRec); goto preupdate_old_out; } p->aRecord = aRec; } pMem = *ppValue = &p->pUnpacked->aMem[iStore]; if( iStore>=p->pUnpacked->nField ){ /* This occurs when the table has been extended using ALTER TABLE ** ADD COLUMN. The value to return is the default value of the column. */ Column *pCol = &p->pTab->aCol[iIdx]; if( pCol->iDflt>0 ){ if( p->apDflt==0 ){ int nByte = sizeof(sqlite3_value*)*p->pTab->nCol; p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); |
︙ | ︙ | |||
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 | ** This function is called from within a pre-update callback to retrieve ** a field of the row currently being updated or inserted. */ int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ PreUpdate *p; int rc = SQLITE_OK; Mem *pMem; #ifdef SQLITE_ENABLE_API_ARMOR if( db==0 || ppValue==0 ){ return SQLITE_MISUSE_BKPT; } #endif p = db->pPreUpdate; if( !p || p->op==SQLITE_DELETE ){ rc = SQLITE_MISUSE_BKPT; goto preupdate_new_out; } if( p->pPk && p->op!=SQLITE_UPDATE ){ | > | > > > | | | | | | | | 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 | ** This function is called from within a pre-update callback to retrieve ** a field of the row currently being updated or inserted. */ int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ PreUpdate *p; int rc = SQLITE_OK; Mem *pMem; int iStore = 0; #ifdef SQLITE_ENABLE_API_ARMOR if( db==0 || ppValue==0 ){ return SQLITE_MISUSE_BKPT; } #endif p = db->pPreUpdate; if( !p || p->op==SQLITE_DELETE ){ rc = SQLITE_MISUSE_BKPT; goto preupdate_new_out; } if( p->pPk && p->op!=SQLITE_UPDATE ){ iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); }else{ iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); } if( iStore>=p->pCsr->nField || iStore<0 ){ rc = SQLITE_RANGE; goto preupdate_new_out; } if( p->op==SQLITE_INSERT ){ /* For an INSERT, memory cell p->iNewReg contains the serialized record ** that is being inserted. Deserialize it. */ UnpackedRecord *pUnpack = p->pNewUnpacked; if( !pUnpack ){ Mem *pData = &p->v->aMem[p->iNewReg]; rc = ExpandBlob(pData); if( rc!=SQLITE_OK ) goto preupdate_new_out; pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); if( !pUnpack ){ rc = SQLITE_NOMEM; goto preupdate_new_out; } p->pNewUnpacked = pUnpack; } pMem = &pUnpack->aMem[iStore]; if( iIdx==p->pTab->iPKey ){ sqlite3VdbeMemSetInt64(pMem, p->iKey2); }else if( iStore>=pUnpack->nField ){ pMem = (sqlite3_value *)columnNullValue(); } }else{ /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required ** value. Make a copy of the cell contents and return a pointer to it. ** It is not safe to return a pointer to the memory cell itself as the ** caller may modify the value text encoding. */ assert( p->op==SQLITE_UPDATE ); if( !p->aNew ){ p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); if( !p->aNew ){ rc = SQLITE_NOMEM; goto preupdate_new_out; } } assert( iStore>=0 && iStore<p->pCsr->nField ); pMem = &p->aNew[iStore]; if( pMem->flags==0 ){ if( iIdx==p->pTab->iPKey ){ sqlite3VdbeMemSetInt64(pMem, p->iKey2); }else{ rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]); if( rc!=SQLITE_OK ) goto preupdate_new_out; } } } *ppValue = pMem; preupdate_new_out: |
︙ | ︙ |