Skip to content

Commit

Permalink
improve widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Apr 28, 2023
1 parent 6fdea7f commit fe55ce4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ type Connection
if all_fields then renamed else
renamed.select_columns ["Database", "Schema", "Name", "Type", "Description"]

## PRIVATE
all_known_table_names : Vector Text
all_known_table_names self =
tables = self.tables name_like=Nothing database=self.database schema=Nothing types=default_table_types all_fields=False
tables.at "Name" . to_vector

## PRIVATE
Set up a query returning a Table object, which can be used to work with
data within the database or load it into memory.
Expand All @@ -153,11 +147,12 @@ type Connection
raised.
- If provided with a `Table_Name` or a text short-hand and the table is
not found, a `Table_Not_Found` error is raised.
@query make_table_name_selector
query : Text | SQL_Query -> Text -> Table ! Table_Not_Found | SQL_Error
query self query alias="" = case query of
_ : Text ->
result = self.query alias=alias <|
if self.all_known_table_names.contains query then (SQL_Query.Table_Name query) else
if (all_known_table_names self).contains query then (SQL_Query.Table_Name query) else
SQL_Query.Raw_SQL query
result.catch SQL_Error sql_error->
case self.dialect.is_probably_a_query query of
Expand Down Expand Up @@ -187,6 +182,7 @@ type Connection
- query: name of the table or sql statement to query.
If supplied as `Text`, the name is checked against the `tables` list to determine if it is a table or a query.
- limit: the maximum number of rows to return.
@query make_table_name_selector
read : Text | SQL_Query -> Integer | Nothing -> Materialized_Table
read self query limit=Nothing =
self.query query . read max_rows=limit
Expand Down Expand Up @@ -252,3 +248,12 @@ make_table_types_selector connection =
make_schema_selector connection =
schemas_without_nothing = connection.schemas.filter Filter_Condition.Not_Nothing
Single_Choice values=(schemas_without_nothing.map t-> Option t t.pretty)+[Option "any schema" "Nothing"]

## PRIVATE
all_known_table_names connection =
tables = connection.tables name_like=Nothing database=connection.database schema=Nothing types=default_table_types all_fields=False
tables.at "Name" . to_vector

## PRIVATE
make_table_name_selector connection =
Single_Choice display=Display.Always values=((all_known_table_names connection).map t-> Option t t.pretty)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import project.Internal.IR.Query.Query
import project.Internal.JDBC_Connection
import project.Internal.SQL_Type_Reference.SQL_Type_Reference

from project.Connection.Connection import default_table_types, make_table_types_selector, make_schema_selector
from project.Connection.Connection import default_table_types, make_table_types_selector, make_schema_selector, make_table_name_selector
from project.Errors import SQL_Error
from project.Internal.Result_Set import read_column

Expand Down Expand Up @@ -111,7 +111,7 @@ type Postgres_Connection
- query: name of the table or sql statement to query.
If supplied as `Text`, the name is checked against the `tables` list to determine if it is a table or a query.
- alias: optionally specify a friendly alias for the query.
@query (self-> Single_Choice display=Display.Always values=(self.tables.at "Name" . to_vector . map t-> Option t t.pretty))
@query make_table_name_selector
query : Text | SQL_Query -> Text -> Database_Table
query self query alias="" = self.connection.query query alias

Expand All @@ -121,7 +121,7 @@ type Postgres_Connection
- query: name of the table or sql statement to query.
If supplied as `Text`, the name is checked against the `tables` list to determine if it is a table or a query.
- limit: the maximum number of rows to return.
@query (self-> Single_Choice display=Display.Always values=(self.tables.at "Name" . to_vector . map t-> Option t t.pretty))
@query make_table_name_selector
read : Text | SQL_Query -> Integer | Nothing -> Materialized_Table
read self query limit=Nothing = self.connection.read query limit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import project.Internal.IR.Query.Query
import project.Internal.JDBC_Connection
import project.Internal.SQL_Type_Reference.SQL_Type_Reference

from project.Connection.Connection import default_table_types, make_table_types_selector, make_schema_selector
from project.Connection.Connection import default_table_types, make_table_types_selector, make_schema_selector, make_table_name_selector
from project.Errors import SQL_Error

type SQLite_Connection
Expand Down Expand Up @@ -105,7 +105,7 @@ type SQLite_Connection
- query: name of the table or sql statement to query.
If supplied as `Text`, the name is checked against the `tables` list to determine if it is a table or a query.
- alias: optionally specify a friendly alias for the query.
@query (self-> Single_Choice display=Display.Always values=(self.tables.at "Name" . to_vector . map t-> Option t t.pretty))
@query make_table_name_selector
query : Text | SQL_Query -> Text -> Database_Table
query self query alias="" = self.connection.query query alias

Expand All @@ -115,7 +115,7 @@ type SQLite_Connection
- query: name of the table or sql statement to query.
If supplied as `Text`, the name is checked against the `tables` list to determine if it is a table or a query.
- limit: the maximum number of rows to return.
@query (self-> Single_Choice display=Display.Always values=(self.tables.at "Name" . to_vector . map t-> Option t t.pretty))
@query make_table_name_selector
read : Text | SQL_Query -> Integer | Nothing -> Materialized_Table
read self query limit=Nothing = self.connection.read query limit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec =

Test.group "Widgets for In-Database Connection with table name sets" <|
Test.specify "works for `query` and `read`" <|
choices = ['sqlite_schema', 'a_table', 'another', 'mock_table'] . map n-> Choice.Option n n.pretty
choices = ['a_table', 'another', 'mock_table'] . map n-> Choice.Option n n.pretty
expect = [["query", Widget.Single_Choice choices Nothing Display.Always]] . to_json
Widgets.get_widget_json connection "query" ["query"] . should_equal expect
Widgets.get_widget_json connection "read" ["query"] . should_equal expect
Expand Down

0 comments on commit fe55ce4

Please sign in to comment.