Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LiteCore/Query/N1QL_Parser/n1ql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ YY_ACTION(void) yy_1_IDENTIFIER(yycontext *yy, char *yytext, int yyleng)
yyprintf((stderr, "do yy_1_IDENTIFIER\n"));
{
#line 397
y_ = string(yytext);;
y_ = warnOnServerReservedWord(yytext);;
}
#undef yythunkpos
#undef yypos
Expand Down
2 changes: 1 addition & 1 deletion LiteCore/Query/N1QL_Parser/n1ql.leg
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ parenExprs =
# In N1QL, unlike SQL, `"` delimits a string, while "`" is used to quote identifiers.
IDENTIFIER =
!reservedWord <[a-zA-Z_] [a-zA-Z0-9_$]*>
_ { $$ = string(yytext);}
_ { $$ = warnOnServerReservedWord(yytext);}
| "`" <( [^`] | "``" )*> "`" _ { $$ = unquote(yytext, '`');}

# Note: the 'i' suffix on strings makes them case-insensitive.
Expand Down
69 changes: 69 additions & 0 deletions LiteCore/Query/N1QL_Parser/n1ql_parser_internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#pragma once
#include "fleece/Mutable.hh"
#include "Any.hh"
#include "Logging.hh"
#include <algorithm>
#include <array>
#include <unordered_set>
#include <sstream>
#include <typeinfo>
#include <utility>
Expand Down Expand Up @@ -204,6 +206,13 @@ namespace litecore::n1ql {
return str;
}

static bool isServerReservedWord(std::string word);

static string warnOnServerReservedWord(const char* input) {
if ( isServerReservedWord(input) ) { Warn(R"("%s" is a reserved word in the Server SQL++)", input); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( isServerReservedWord(input) ) { Warn(R"("%s" is a reserved word in the Server SQL++)", input); }
if ( isServerReservedWord(input) ) { Warn(R"("%s" is a reserved word in Server SQL++)", input); }

return input;
}

// Property-path operations:

static string quoteIdentity(string id) {
Expand Down Expand Up @@ -349,6 +358,66 @@ namespace litecore::n1ql {
return collate;
}

// constexpr const char* UnabridgedServerReservedWords =
// "ADVISE ALL ALTER ANALYZE ARRAY AT BEGIN BINARY BOOLEAN BREAK BUCKET BUILD CACHE CALL CAST CLUSTER "
// "COLLECTION COMMIT COMMITTED CONNECT CONTINUE CORRELATED COVER CREATE CURRENT"
// " CYCLE DATABASE DATASET DATASTORE DECLARE DECREMENT DEFAULT DELETE DERIVED DESCRIBE DO DROP EACH ELEMENT "
// "ESCAPE EXCEPT EXCLUDE EXECUTE EXISTS EXPLAIN FETCH FILTER FIRST FLATTEN FLATTEN_KEYS"
// " FLUSH FOLLOWING FOR FORCE FTS FUNCTION GOLANG GRANT GROUPS GSI HASH IF IGNORE ILIKE INCLUDE INCREMENT "
// "INDEX INFER INLINE INSERT INTERSECT INTO ISOLATION JAVASCRIPT KEY"
// " KEYS KEYSPACE KNOWN LANGUAGE LAST LATERAL LET LETTING LEVEL LSM MAP MAPPING MATCHED MATERIALIZED "
// "MAXVALUE MERGE MINUS MINVALUE NAMESPACE NAMESPACE_ID NEST NEXT NEXTVAL NL NO"
// " NOT_A_TOKEN NTH_VALUE NULLS NUMBER OBJECT OPTION OPTIONS OTHERS OVER PARSE PARTITION PASSWORD PATH POOL "
// "PRECEDING PREPARE PREV PREVIOUS PREVVAL PRIMARY PRIVATE PRIVILEGE PROBE PROCEDURE PUBLIC"
// " RANGE RAW READ REALM RECURSIVE REDUCE RENAME REPLACE RESPECT RESTART RESTRICT RETURN RETURNING REVOKE "
// "ROLE ROLES ROLLBACK ROW ROWS SAVEPOINT SCHEMA SCOPE SELF SEMI SEQUENCE"
// " SET SHOW SOME START STATISTICS STRING SYSTEM TIES TO TRAN TRANSACTION TRIGGER TRUNCATE UNBOUNDED UNDER "
// "UNION UNIQUE UNKNOWN UNSET UPDATE UPSERT USE USER USERS VALIDATE"
// " VALUE VALUES VECTOR VIA VIEW WHILE WINDOW WITH WITHIN WORK XOR";

constexpr const char* kServerReservedWords =
" ALL ARRAY AT BEGIN CAST CORRELATED COVER CURRENT"
" DECREMENT DEFAULT DERIVED DESCRIBE DO EACH ELEMENT ESCAPE"
" EXCEPT EXCLUDE EXECUTE EXISTS EXPLAIN FETCH FILTER FIRST"
" FLATTEN FLATTEN_KEYS FOLLOWING FOR FORCE FUNCTION GRANT GROUPS"
" HASH IF IGNORE ILIKE INCLUDE INCREMENT INDEX INFER"
" INLINE INTERSECT ISOLATION KEY KEYS KEYSPACE KNOWN LAST"
" LATERAL LET LETTING LEVEL LSM MAP MAPPING MATCHED"
" MATERIALIZED MAXVALUE MERGE MINUS MINVALUE NAMESPACE NAMESPACE_ID NEST"
" EXT NEXTVAL NL NO NOT_A_TOKEN NTH_VALUE NULLS NUMBER"
" OBJECT OPTION OPTIONS OTHERS OVER PARSE PARTITION PASSWORD"
" PATH POOL PRECEDING PREPARE PREV PREVIOUS PREVVAL PRIMARY"
" PRIVATE PRIVILEGE PROBE PROCEDURE PUBLIC RANGE RAW READ"
" REALM RECURSIVE REDUCE RENAME REPLACE RESPECT RESTART RESTRICT"
" RETURN RETURNING REVOKE ROLE ROLES ROLLBACK ROW ROWS"
" SAVEPOINT SCHEMA SCOPE SELF SEMI SEQUENCE SHOW SOME"
" START STATISTICS STRING SYSTEM TIES TO TRAN TRIGGER"
" TRUNCATE UNBOUNDED UNDER UNION UNIQUE UNKNOWN UNSET UPDATE"
" UPSERT USE USER USERS VALIDATE VALUE VALUES VECTOR"
" VIA VIEW WHILE WINDOW WITH WITHIN WORK XOR";

bool isServerReservedWord(std::string word) {
static std::unordered_set<std::string_view> serverReserved;
if ( serverReserved.empty() ) {
const char* p = kServerReservedWords;
const char* start = nullptr;
for ( ; *p != '\0'; ++p ) {
if ( *p == ' ' ) {
if ( start != nullptr ) {
//start -> p
serverReserved.emplace(start, p);
start = nullptr;
}
} else if ( start == nullptr ) {
start = p;
}
}
if ( start != nullptr ) { serverReserved.emplace(start, p); }
Comment on lines +402 to +415
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use split() for this instead of nasty C code:

    /** Splits the string at occurrences of `separator` and calls the callback for each piece.
        There may be empty pieces, if the separator occurs at the start or end or twice in a row. */
    void split(std::string_view str, std::string_view separator, fleece::function_ref<void(std::string_view)> callback);

You'll need a consistent delimiter between words though, probably a single space.

}
uppercase(word);
return serverReserved.contains(word);
}

} // namespace litecore::n1ql

// The code generator produces some unreachable code; keep Clang from warning about it:
Expand Down