| 227 | |
| 228 | |
| 229 | def suggest_special(text): |
| 230 | text = text.lstrip() |
| 231 | cmd, _, arg = parse_special_command(text) |
| 232 | |
| 233 | if cmd == text: |
| 234 | # Trying to complete the special command itself |
| 235 | return (Special(),) |
| 236 | |
| 237 | if cmd in ("\\c", "\\connect"): |
| 238 | return (Database(),) |
| 239 | |
| 240 | if cmd == "\\T": |
| 241 | return (TableFormat(),) |
| 242 | |
| 243 | if cmd == "\\dn": |
| 244 | return (Schema(),) |
| 245 | |
| 246 | if arg: |
| 247 | # Try to distinguish "\d name" from "\d schema.name" |
| 248 | # Note that this will fail to obtain a schema name if wildcards are |
| 249 | # used, e.g. "\d schema???.name" |
| 250 | parsed = sqlparse.parse(arg)[0].tokens[0] |
| 251 | try: |
| 252 | schema = parsed.get_parent_name() |
| 253 | except AttributeError: |
| 254 | schema = None |
| 255 | else: |
| 256 | schema = None |
| 257 | |
| 258 | if cmd[1:] == "d": |
| 259 | # \d can describe tables or views |
| 260 | if schema: |
| 261 | return (Table(schema=schema), View(schema=schema)) |
| 262 | else: |
| 263 | return (Schema(), Table(schema=None), View(schema=None)) |
| 264 | elif cmd[1:] in SPECIALS_SUGGESTION: |
| 265 | rel_type = SPECIALS_SUGGESTION[cmd[1:]] |
| 266 | if schema: |
| 267 | if rel_type == Function: |
| 268 | return (Function(schema=schema, usage="special"),) |
| 269 | return (rel_type(schema=schema),) |
| 270 | else: |
| 271 | if rel_type == Function: |
| 272 | return (Schema(), Function(schema=None, usage="special")) |
| 273 | return (Schema(), rel_type(schema=None)) |
| 274 | |
| 275 | if cmd in ["\\n", "\\ns", "\\nd", "\\nq"]: |
| 276 | return (NamedQuery(),) |
| 277 | |
| 278 | return (Keyword(), Special()) |
| 279 | |
| 280 | |
| 281 | def suggest_based_on_last_token(token, stmt): |