Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.
Merged
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
20 changes: 15 additions & 5 deletions spannercli/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABCMeta, abstractmethod
from collections import OrderedDict
from typing import List, Optional
import re
import webbrowser

from google.api_core import exceptions as googleExceptions
Expand Down Expand Up @@ -205,11 +206,16 @@ def alias(cls) -> (str, bool):
def handler(self, cli, **kwargs) -> Optional[ResultContainer]:
query = clean(kwargs.get("text"))
table = find_last_word(query)
sql = "SELECT TABLE_NAME, INDEX_NAME, INDEX_TYPE, PARENT_TABLE_NAME, IS_UNIQUE, IS_NULL_FILTERED, INDEX_STATE"\
" FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_CATALOG='' AND TABLE_SCHEMA =''"

if table != "INDEX":
sql = sql + " AND TABLE_NAME='{0}';".format(table)
# https://cloud.google.com/spanner/docs/information-schema
sql = "SELECT c.TABLE_NAME, c.INDEX_NAME, c.INDEX_TYPE, c.COLUMN_NAME, c.SPANNER_TYPE, c.IS_NULLABLE,"\
" c.COLUMN_ORDERING, i.PARENT_TABLE_NAME, i.IS_UNIQUE, i.IS_NULL_FILTERED, i.INDEX_STATE"\
" FROM INFORMATION_SCHEMA.INDEX_COLUMNS c"\
" LEFT JOIN INFORMATION_SCHEMA.INDEXES i ON c.TABLE_NAME = i.TABLE_NAME AND i.INDEX_NAME = c.INDEX_NAME"\
" WHERE c.TABLE_SCHEMA='' AND c.TABLE_SCHEMA=''"

if table.upper() != "INDEX":
sql = sql + " AND c.TABLE_NAME='{0}' AND i.TABLE_NAME = '{0}'"
sql = sql + " ORDER BY c.TABLE_NAME, c.INDEX_NAME ASC, c.ORDINAL_POSITION ASC;"
return cli.query(sql.format(table))

def help_message(self) -> List[str]:
Expand Down Expand Up @@ -300,11 +306,15 @@ def execute(cli, text: str) -> ResultContainer:
return command.handler(cli, **kwargs)


spaces = re.compile(r"\s+")


def find(text: str) -> Command:
if not text:
raise CommandNotFound

# 1st, search by raw input
text = re.sub(spaces, " ", text)
text = clean(text)
found = find_command(text)
if found is not None:
Expand Down
1 change: 1 addition & 0 deletions spannercli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def change_database(self, dbname):
self.prompt_message = self.get_prompt_message()

def query(self, sql) -> structures.ResultContainer:
self.logger.debug("QUERY: %s", sql)
if queryutils.is_write_query(sql):
return self.write_query(sql)
if queryutils.is_ddl_query(sql):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def test_find_command():
cmd = commands.find("browse")
assert type(cmd) is commands.BrowserCommand

# double words
cmd = commands.find("show index")
assert type(cmd) is commands.ShowIndexCommand

# double words with mixed case
cmd = commands.find("show INDEX")
assert type(cmd) is commands.ShowIndexCommand

# double words with extra space
cmd = commands.find(" show index ")
assert type(cmd) is commands.ShowIndexCommand

# BrowserCommand is case sensitive
with pytest.raises(commands.CommandNotFound):
commands.find("BrOwSE")
Expand Down