* Replace :param with $n throughout the command's SQL text, which * is a modifiable string in cmd->lines. */
| 4666 | * is a modifiable string in cmd->lines. |
| 4667 | */ |
| 4668 | static bool |
| 4669 | parseQuery(Command *cmd) |
| 4670 | { |
| 4671 | char *sql, |
| 4672 | *p; |
| 4673 | |
| 4674 | cmd->argc = 1; |
| 4675 | |
| 4676 | p = sql = pg_strdup(cmd->lines.data); |
| 4677 | while ((p = strchr(p, ':')) != NULL) |
| 4678 | { |
| 4679 | char var[13]; |
| 4680 | char *name; |
| 4681 | int eaten; |
| 4682 | |
| 4683 | name = parseVariable(p, &eaten); |
| 4684 | if (name == NULL) |
| 4685 | { |
| 4686 | while (*p == ':') |
| 4687 | { |
| 4688 | p++; |
| 4689 | } |
| 4690 | continue; |
| 4691 | } |
| 4692 | |
| 4693 | /* |
| 4694 | * cmd->argv[0] is the SQL statement itself, so the max number of |
| 4695 | * arguments is one less than MAX_ARGS |
| 4696 | */ |
| 4697 | if (cmd->argc >= MAX_ARGS) |
| 4698 | { |
| 4699 | pg_log_error("statement has too many arguments (maximum is %d): %s", |
| 4700 | MAX_ARGS - 1, cmd->lines.data); |
| 4701 | pg_free(name); |
| 4702 | return false; |
| 4703 | } |
| 4704 | |
| 4705 | sprintf(var, "$%d", cmd->argc); |
| 4706 | p = replaceVariable(&sql, p, eaten, var); |
| 4707 | |
| 4708 | cmd->argv[cmd->argc] = name; |
| 4709 | cmd->argc++; |
| 4710 | } |
| 4711 | |
| 4712 | Assert(cmd->argv[0] == NULL); |
| 4713 | cmd->argv[0] = sql; |
| 4714 | return true; |
| 4715 | } |
| 4716 | |
| 4717 | /* |
| 4718 | * syntax error while parsing a script (in practice, while parsing a |
no test coverage detected