* processSQLNamePattern * * Scan a wildcard-pattern string and generate appropriate WHERE clauses * to limit the set of objects returned. The WHERE clauses are appended * to the already-partially-constructed query in buf. Returns whether * any clause was added. * * conn: connection query will be sent to (consulted for escaping rules). * buf: output parameter. * pattern: user-specified p
| 1010 | * The appended text, if any, will end with one too. |
| 1011 | */ |
| 1012 | bool |
| 1013 | processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, |
| 1014 | bool have_where, bool force_escape, |
| 1015 | const char *schemavar, const char *namevar, |
| 1016 | const char *altnamevar, const char *visibilityrule, |
| 1017 | PQExpBuffer dbnamebuf, int *dotcnt) |
| 1018 | { |
| 1019 | PQExpBufferData schemabuf; |
| 1020 | PQExpBufferData namebuf; |
| 1021 | bool added_clause = false; |
| 1022 | int dcnt; |
| 1023 | |
| 1024 | #define WHEREAND() \ |
| 1025 | (appendPQExpBufferStr(buf, have_where ? " AND " : "WHERE "), \ |
| 1026 | have_where = true, added_clause = true) |
| 1027 | |
| 1028 | if (dotcnt == NULL) |
| 1029 | dotcnt = &dcnt; |
| 1030 | *dotcnt = 0; |
| 1031 | if (pattern == NULL) |
| 1032 | { |
| 1033 | /* Default: select all visible objects */ |
| 1034 | if (visibilityrule) |
| 1035 | { |
| 1036 | WHEREAND(); |
| 1037 | appendPQExpBuffer(buf, "%s\n", visibilityrule); |
| 1038 | } |
| 1039 | return added_clause; |
| 1040 | } |
| 1041 | |
| 1042 | initPQExpBuffer(&schemabuf); |
| 1043 | initPQExpBuffer(&namebuf); |
| 1044 | |
| 1045 | /* |
| 1046 | * Convert shell-style 'pattern' into the regular expression(s) we want to |
| 1047 | * execute. Quoting/escaping into SQL literal format will be done below |
| 1048 | * using appendStringLiteralConn(). |
| 1049 | */ |
| 1050 | patternToSQLRegex(PQclientEncoding(conn), |
| 1051 | (schemavar ? dbnamebuf : NULL), |
| 1052 | (schemavar ? &schemabuf: NULL), |
| 1053 | &namebuf, pattern, force_escape, true, dotcnt); |
| 1054 | |
| 1055 | /* |
| 1056 | * Now decide what we need to emit. We may run under a hostile |
| 1057 | * search_path, so qualify EVERY name. Note there will be a leading "^(" |
| 1058 | * in the patterns in any case. |
| 1059 | * |
| 1060 | * We want the regex matches to use the database's default collation where |
| 1061 | * collation-sensitive behavior is required (for example, which characters |
| 1062 | * match '\w'). That happened by default before PG v12, but if the server |
| 1063 | * is >= v12 then we need to force it through explicit COLLATE clauses, |
| 1064 | * otherwise the "C" collation attached to "name" catalog columns wins. |
| 1065 | */ |
| 1066 | if (namevar && namebuf.len > 2) |
| 1067 | { |
| 1068 | /* We have a name pattern, so constrain the namevar(s) */ |
| 1069 |
no test coverage detected