From: drh <> Date: Wed, 19 Nov 2025 15:25:18 +0000 (+0000) Subject: Improved comments in code. Omit unneeded wrapper functions. X-Git-Tag: artiphishell~211^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=57b61bf78f57b946838433aa77c39188a145ef9b;p=thirdparty%2Fsqlite.git Improved comments in code. Omit unneeded wrapper functions. FossilOrigin-Name: 3485c7c81bee8e98ea6b403438bcdb2b6fa058afeafea150ce1c859345e1529b --- diff --git a/manifest b/manifest index 3f7837e793..00d1443be5 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Strip\sunterminated\s"--"\scomments\sfrom\sthe\send\sof\sconstraints\sadded\susing\sALTER\sTABLE\sADD\sCONSTRAINT\sor\sADD\sNOT\sNULL. -D 2025-11-19T11:31:59.076 +C Improved\scomments\sin\scode.\s\sOmit\sunneeded\swrapper\sfunctions. +D 2025-11-19T15:25:18.821 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -666,7 +666,7 @@ F mptest/multiwrite01.test dab5c5f8f9534971efce679152c5146da265222d F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b F sqlite3.1 1b9c24374a85dfc7eb8fa7c4266ee0db4f9609cceecfc5481cd8307e5af04366 F sqlite3.pc.in e6dee284fba59ef500092fdc1843df3be8433323a3733c91da96690a50a5b398 -F src/alter.c e7ee3350daae58a1f44822fbddae9c27190d1398142c4e0dd1077fd5bbb6bbde +F src/alter.c f3bb458fb014e1a101eae5989dd580b6108443316a012855a6b25046693bae71 F src/analyze.c 03bcfc083fc0cccaa9ded93604e1d4244ea245c17285d463ef6a60425fcb247d F src/attach.c 9af61b63b10ee702b1594ecd24fb8cea0839cfdb6addee52fba26fa879f5db9d F src/auth.c 54ab9c6c5803b47c0d45b76ce27eff22a03b4b1f767c5945a3a4eb13aa4c78dc @@ -723,7 +723,7 @@ F src/os_win.c a89b501fc195085c7d6c9eec7f5bd782625e94bb2a96b000f4d009703df1083f F src/os_win.h 4c247cdb6d407c75186c94a1e84d5a22cbae4adcec93fcae8d2bc1f956fd1f19 F src/pager.c a81461de271ac4886ad75b7ca2cca8157a48635820c4646cd2714acdc2c17e5f F src/pager.h 6137149346e6c8a3ddc1eeb40aee46381e9bc8b0fcc6dda8a1efde993c2275b8 -F src/parse.y ad5b245575d2fea6ec82c227cc1a3e40183583dfcb9e2d15e611751fe4cf22bc +F src/parse.y 424d195ea70f4656a3f6440e0b79ca8f712ae4da9431033a19ec8c9816469287 F src/pcache.c 588cc3c5ccaaadde689ed35ce5c5c891a1f7b1f4d1f56f6cf0143b74d8ee6484 F src/pcache.h 1497ce1b823cf00094bb0cf3bac37b345937e6f910890c626b16512316d3abf5 F src/pcache1.c 131ca0daf4e66b4608d2945ae76d6ed90de3f60539afbd5ef9ec65667a5f2fcd @@ -738,7 +738,7 @@ F src/shell.c.in 4fdceb103ba8761bf3d68108c3a6e6499be913a2490a7c384ee79ee547a9517 F src/sqlite.h.in f1363321ca55cc2feaa289e9fe6dfb08102a28c54edf005564711a2348b06eef F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479 F src/sqlite3ext.h 5d5330f5f8461f5ce74960436ddcfa53ecd09c2b8b23901e22ae38aec3243998 -F src/sqliteInt.h 4a41a6f8c40efd8ae0a5d7d6b5372cefd81e325e8313ca6216fa17a3285c5fb9 +F src/sqliteInt.h a89c3a9296928dffcb4c287df176a739f9cf620c7c9d33aec59e8efb9b39cbbd F src/sqliteLimit.h 0a5516b4ec192a205c541e05f67009028a9451dc6678aae4cf8e68596903c246 F src/status.c 7565d63a79aa2f326339a24a0461a60096d0bd2bce711fefb50b5c89335f3592 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 @@ -2177,8 +2177,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 4bd53fbb20d78de7bbff1154e7ee1321b93b0ede435e150ad4ee992a4108ad5a -R e4fa4c468d8c8146033fef14ba9e155a -U dan -Z 282dcbcbadb63a9b3049c653389a681d +P c73ab47b0250c01f1ffa6e506793769744ebeb8ca7edbb78de9a08db2037d6de +R d0abf20fb116fd4bf9aa47b01bee1c1e +U drh +Z 9a73bafcc919cb505e80bf7380ff4a1d # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 1dfa7c8eee..50d7d82b94 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c73ab47b0250c01f1ffa6e506793769744ebeb8ca7edbb78de9a08db2037d6de +3485c7c81bee8e98ea6b403438bcdb2b6fa058afeafea150ce1c859345e1529b diff --git a/src/alter.c b/src/alter.c index 64c07162da..e026a81db4 100644 --- a/src/alter.c +++ b/src/alter.c @@ -2336,7 +2336,7 @@ exit_drop_column: } /* -** Return the number of bytes of leading whitespace in string z. +** Return the number of bytes of leading whitespace/comments in string z[]. */ static int getWhitespace(const u8 *z){ int nRet = 0; @@ -2351,10 +2351,26 @@ static int getWhitespace(const u8 *z){ /* -** Skip over any leading whitespace, then read the first token from the -** string passed as the first argument. Set *piToken to the type of the -** token before returning the total number of bytes consumed (including -** any whitespace). +** Return the number of bytes until the end of the next non-whitespace and +** non-comment token. For the purpose of this function, a "(" token includes +** all of the bytes through and including the matching ")", or until the +** first illegal token, whichever comes first. +** +** Write the token type into *piToken. +** +** The value returned is the number of bytes in the token itself plus +** the number of bytes of leading whitespace and comments skipped plus +** all bytes through the next matching ")" if the token is TK_LP. +** +** Example: (Note: '.' used in place of '*' in the example z[] text) +** +** ,--------- *piToken := TK_RP +** v +** z[] = " /.comment./ --comment\n (two three four) five" +** | | +** |<-------------------------------------->| +** | +** `--- return value */ static int getConstraintToken(const u8 *z, int *piToken){ int iOff = 0; @@ -2386,8 +2402,12 @@ static int getConstraintToken(const u8 *z, int *piToken){ /* ** Argument z points into the body of a constraint - specifically the -** second token of the constraint definition. Return the number of bytes -** until the end of the constraint. +** second token of the constraint definition. For a named constraint, +** z points to the first token past the CONSTRAINT keyword. For an +** unnamed NOT NULL constraint, z points to the first byte past the NOT +** keyword. +** +** Return the number of bytes until the end of the constraint. */ static int getConstraint(const u8 *z){ int iOff = 0; @@ -2416,14 +2436,25 @@ static int getConstraint(const u8 *z){ return iOff; } +/* +** Compare two constraint names. +** +** Summary: *pRes := zQuote != zCmp +** +** Details: +** Compare the (possibly quoted) constraint name zQuote[0..nQuote-1] +** against zCmp[]. Write zero into *pRes if they are the same and +** non-zero if they differ. Normally return SQLITE_OK, except if there +** is an OOM, set the OOM error condition on ctx and return SQLITE_NOMEM. +*/ static int quotedCompare( - sqlite3_context *ctx, - const u8 *zQuote, - int nQuote, - const u8 *zCmp, - int *pRes + sqlite3_context *ctx, /* Function context on which to report errors */ + const u8 *zQuote, /* Possibly quoted text. Not zero-terminated. */ + int nQuote, /* Length of zQuote in bytes */ + const u8 *zCmp, /* Zero-terminated, unquoted name to compare against */ + int *pRes /* OUT: Set to 0 if equal, non-zero if unequal */ ){ - char *zCopy = 0; + char *zCopy = 0; /* De-quoted, zero-terminated copy of zQuote[] */ zCopy = sqlite3MallocZero(nQuote+1); if( zCopy==0 ){ @@ -2438,12 +2469,10 @@ static int quotedCompare( } /* -** The second argument is passed a CREATE TABLE statement. This function -** attempts to find the offset of the first token of the first column -** definition in the table. If successful, it sets (*piOff) to the offset -** and return SQLITE_OK. Otherwise, if an error occurs, it leaves an -** error code and message in the context handle passed as the first argument -** and returns SQLITE_ERROR. +** zSql[] is a CREATE TABLE statement, supposedly. Find the offset +** into zSql[] of the first character past the first "(" and write +** that offset into *piOff and return SQLITE_OK. Or, if not found, +** set the SQLITE_CORRUPT error code and return SQLITE_ERROR. */ static int skipCreateTable(sqlite3_context *ctx, const u8 *zSql, int *piOff){ int iOff = 0; @@ -2469,8 +2498,8 @@ static int skipCreateTable(sqlite3_context *ctx, const u8 *zSql, int *piOff){ ** with a constraint removed. Two forms, depending on the datatype ** of argv[2]: ** -** sqlite3_drop_constraint(SQL, INT) -- Omit NOT NULL from the INT-th column -** sqlite3_drop_constraint(SQL, TEXT) -- OMIT constraint with name TEXT +** sqlite_drop_constraint(SQL, INT) -- Omit NOT NULL from the INT-th column +** sqlite_drop_constraint(SQL, TEXT) -- OMIT constraint with name TEXT ** ** In the first case, the left-most column is 0. */ @@ -2602,9 +2631,13 @@ static void dropConstraintFunc( } /* -** Implementation of internal SQL function: +** Internal SQL function: ** ** sqlite_add_constraint(SQL, CONSTRAINT-TEXT, ICOL) +** +** SQL is a CREATE TABLE statement. Return a modified version of +** SQL that adds CONSTRAINT-TEXT at the end of the ICOL-th column +** definition. (The left-most column defintion is 0.) */ static void addConstraintFunc( sqlite3_context *ctx, @@ -2699,11 +2732,11 @@ static int alterFindCol(Parse *pParse, Table *pTab, Token *pCol, int *piCol){ ** parameters is undefined in this case. */ static Table *alterFindTable( - Parse *pParse, - SrcList *pSrc, - int *piDb, - const char **pzDb, - int bAuth + Parse *pParse, /* Parsing context */ + SrcList *pSrc, /* Name of the table to look for */ + int *piDb, /* OUT: write the iDb here */ + const char **pzDb, /* OUT: write name of schema here */ + int bAuth /* Do ALTER TABLE authorization checks if true */ ){ sqlite3 *db = pParse->db; Table *pTab = 0; @@ -2731,11 +2764,19 @@ static Table *alterFindTable( return pTab; } -void alterDropConstraint( - Parse *pParse, - SrcList *pSrc, - Token *pCons, - Token *pCol +/* +** Generate bytecode for one of: +** +** (1) ALTER TABLE pSrc DROP CONSTRAINT pCons +** (2) ALTER TABLE pSrc ALTER pCol DROP NOT NULL +** +** One of pCons and pCol must be NULL and the other non-null. +*/ +void sqlite3AlterDropConstraint( + Parse *pParse, /* Parsing context */ + SrcList *pSrc, /* The table being altered */ + Token *pCons, /* Name of the constraint to drop */ + Token *pCol /* Name of the column from which to remove the NOT NULL */ ){ sqlite3 *db = pParse->db; Table *pTab = 0; @@ -2743,6 +2784,8 @@ void alterDropConstraint( const char *zDb = 0; char *zArg = 0; + assert( (pCol==0)!=(pCons==0) ); + assert( pSrc->nSrc==1 ); pTab = alterFindTable(pParse, pSrc, &iDb, &zDb, pCons!=0); if( !pTab ) return; @@ -2767,25 +2810,6 @@ void alterDropConstraint( renameReloadSchema(pParse, iDb, INITFLAG_AlterDropCons); } - -/* -** Prepare a statement of the form: -** -** ALTER TABLE pSrc DROP CONSTRAINT pCons -*/ -void sqlite3AlterDropConstraint(Parse *pParse, SrcList *pSrc, Token *pCons){ - alterDropConstraint(pParse, pSrc, pCons, 0); -} - -/* -** Prepare a statement of the form: -** -** ALTER TABLE pSrc ALTER pCol DROP NOT NULL -*/ -void sqlite3AlterDropNotNull(Parse *pParse, SrcList *pSrc, Token *pCol){ - alterDropConstraint(pParse, pSrc, 0, pCol); -} - /* ** The implementation of SQL function sqlite_fail(MSG). This takes a single ** argument, and returns it as an error message with the error code set to @@ -2809,6 +2833,12 @@ static void failConstraintFunc( ** statement. If successful, this function returns the size of the buffer in ** bytes not including any trailing whitespace or "--" style comments. Or, ** if an OOM occurs, it returns 0 and sets db->mallocFailed to true. +** +** C-style comments at the end are preserved. "--" style comments are +** removed because the comment terminator might be \000, and we are about +** to insert the pCons[] text into the middle of a larger string, and that +** will have the effect of removing the comment terminator and messing up +** the syntax. */ static int alterRtrimConstraint( sqlite3 *db, /* used to record OOM error */ @@ -2844,10 +2874,10 @@ static int alterRtrimConstraint( ** ALTER TABLE pSrc ALTER pCol SET NOT NULL */ void sqlite3AlterSetNotNull( - Parse *pParse, - SrcList *pSrc, - Token *pCol, - Token *pFirst + Parse *pParse, /* Parsing context */ + SrcList *pSrc, /* Name of the table being altered */ + Token *pCol, /* Name of the column to add a NOT NULL constraint to */ + Token *pFirst /* The NOT token of the NOT NULL constraint text */ ){ Table *pTab = 0; int iCol = 0; @@ -2857,6 +2887,7 @@ void sqlite3AlterSetNotNull( int nCons = 0; /* Look up the table being altered. */ + assert( pSrc->nSrc==1 ); pTab = alterFindTable(pParse, pSrc, &iDb, &zDb, 0); if( !pTab ) return; @@ -2935,21 +2966,30 @@ static void findConstraintFunc( sqlite3_result_int(ctx, 0); } +/* +** Generate bytecode to implement: +** +** ALTER TABLE pSrc ADD [CONSTRAINT pName] CHECK(pExpr) +** +** Any "ON CONFLICT" text that occurs after the "CHECK(...)", up +** until pParse->sLastToken, is included as part of the new constraint. +*/ void sqlite3AlterAddConstraint( - Parse *pParse, /* Parse context */ - SrcList *pSrc, /* Table to add constraint to */ - Token *pFirst, /* First token of new constraint */ - Token *pName, /* Name of new constraint */ - const char *pExpr, /* Text of CHECK expression */ - int nExpr /* Size of pExpr in bytes */ + Parse *pParse, /* Parse context */ + SrcList *pSrc, /* Table to add constraint to */ + Token *pFirst, /* First token of new constraint */ + Token *pName, /* Name of new constraint. NULL if name omitted. */ + const char *pExpr, /* Text of CHECK expression */ + int nExpr /* Size of pExpr in bytes */ ){ - Table *pTab = 0; - int iDb = 0; - const char *zDb = 0; - int nCons; - const char *pCons = 0; + Table *pTab = 0; /* Table identified by pSrc */ + int iDb = 0; /* Which schema does pTab live in */ + const char *zDb = 0; /* Name of the schema in which pTab lives */ + const char *pCons = 0; /* Text of the constraint */ + int nCons; /* Bytes of text to use from pCons[] */ /* Look up the table being altered. */ + assert( pSrc->nSrc==1 ); pTab = alterFindTable(pParse, pSrc, &iDb, &zDb, 1); if( !pTab ) return; diff --git a/src/parse.y b/src/parse.y index 93bd016ba4..ff66ccc42b 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1870,10 +1870,10 @@ cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { sqlite3AlterRenameColumn(pParse, X, &Y, &Z); } cmd ::= ALTER TABLE fullname(X) DROP CONSTRAINT nm(Y). { - sqlite3AlterDropConstraint(pParse, X, &Y); + sqlite3AlterDropConstraint(pParse, X, &Y, 0); } cmd ::= ALTER TABLE fullname(X) ALTER kwcolumn_opt nm(Y) DROP NOT NULL. { - sqlite3AlterDropNotNull(pParse, X, &Y); + sqlite3AlterDropConstraint(pParse, X, 0, &Y); } cmd ::= ALTER TABLE fullname(X) ALTER kwcolumn_opt nm(Y) SET NOT(Z) NULL onconf. { sqlite3AlterSetNotNull(pParse, X, &Y, &Z); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 711221944d..9ec7de2aa1 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -5453,8 +5453,7 @@ void sqlite3Reindex(Parse*, Token*, Token*); void sqlite3AlterFunctions(void); void sqlite3AlterRenameTable(Parse*, SrcList*, Token*); void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*); -void sqlite3AlterDropConstraint(Parse*, SrcList*, Token*); -void sqlite3AlterDropNotNull(Parse*, SrcList*, Token*); +void sqlite3AlterDropConstraint(Parse*,SrcList*,Token*,Token*); void sqlite3AlterAddConstraint(Parse*,SrcList*,Token*,Token*,const char*,int); void sqlite3AlterSetNotNull(Parse*, SrcList*, Token*, Token*); i64 sqlite3GetToken(const unsigned char *, int *);