Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Run Query Bar with Limit Checkbox #99

Merged
merged 3 commits into from
Jun 16, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.db
*.db.wal
!tests/data/*/*.db
*.sql
Pipfile

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- Adds checkbox for Limit with a configurable input ([#35](https://github.com/tconbeer/harlequin/issues/35)).
- Adds more obvious Run Query button ([#76](https://github.com/tconbeer/harlequin/issues/76)).
- Removes the Header for more working space.

## [0.0.14] - 2023-06-15

### Features
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry.dependencies]
python = "^3.8"
textual = ">=0.22.3,<1.0.0"
textual = ">=0.27.0,<1.0.0"
textual-textarea = "==0.2.2"
click = "^8.1.3"
duckdb = "==0.8.0"
Expand Down
104 changes: 104 additions & 0 deletions src/harlequin/duck_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from pathlib import Path
from typing import List, Tuple

import duckdb

from harlequin.exception import HarlequinExit

COLS = List[Tuple[str, str]]
TABLES = List[Tuple[str, str, COLS]]
SCHEMAS = List[Tuple[str, TABLES]]
Catalog = List[Tuple[str, SCHEMAS]]


def connect(db_path: List[Path], read_only: bool = False) -> duckdb.DuckDBPyConnection:
if not db_path:
db_path = [Path(":memory:")]
primary_db, *other_dbs = db_path
try:
connection = duckdb.connect(database=str(primary_db), read_only=read_only)
for db in other_dbs:
connection.execute(f"attach '{db}'{' (READ_ONLY)' if read_only else ''}")
except (duckdb.CatalogException, duckdb.IOException) as e:
from rich import print
from rich.panel import Panel

print(
Panel.fit(
str(e),
title="DuckDB couldn't connect to your database.",
title_align="left",
border_style="red",
subtitle="Try again?",
subtitle_align="right",
)
)

raise HarlequinExit()
else:
return connection


def get_databases(conn: duckdb.DuckDBPyConnection) -> List[Tuple[str]]:
return conn.execute("pragma show_databases").fetchall()


def get_schemas(conn: duckdb.DuckDBPyConnection, database: str) -> List[Tuple[str]]:
schemas = conn.execute(
"select schema_name "
"from information_schema.schemata "
"where "
" catalog_name = ? "
" and schema_name not in ('pg_catalog', 'information_schema') "
"order by 1",
[database],
).fetchall()
return schemas


def get_tables(
conn: duckdb.DuckDBPyConnection, database: str, schema: str
) -> List[Tuple[str, str]]:
tables = conn.execute(
"select table_name, table_type "
"from information_schema.tables "
"where "
" table_catalog = ? "
" and table_schema = ? "
"order by 1",
[database, schema],
).fetchall()
return tables


def get_columns(
conn: duckdb.DuckDBPyConnection, database: str, schema: str, table: str
) -> List[Tuple[str, str]]:
columns = conn.execute(
"select column_name, data_type "
"from information_schema.columns "
"where "
" table_catalog = ? "
" and table_schema = ? "
" and table_name = ? "
"order by 1",
[database, schema, table],
).fetchall()
return columns


def get_catalog(conn: duckdb.DuckDBPyConnection) -> Catalog:
data: Catalog = []
databases = get_databases(conn)
for (database,) in databases:
schemas = get_schemas(conn, database)
schemas_data: SCHEMAS = []
for (schema,) in schemas:
tables = get_tables(conn, database, schema)
tables_data: TABLES = []
for table, type in tables:
columns = get_columns(conn, database, schema, table)
tables_data.append((table, type, columns))
schemas_data.append((schema, tables_data))
data.append((database, schemas_data))
return data
2 changes: 2 additions & 0 deletions src/harlequin/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class HarlequinExit(Exception):
pass
46 changes: 42 additions & 4 deletions src/harlequin/tui/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ $border-title-color-focus: $accent;
/* Main Screen */
#sql_client {
layout: grid;
grid-size: 2 2;
grid-size: 2 3;
grid-columns: 1fr 3fr;
grid-rows: 1fr 1 1fr;
}
/* ALL WIDGETS */
SchemaViewer, CodeEditor, ResultsViewer {
Expand All @@ -32,12 +33,10 @@ SchemaViewer, TextContainer, ResultsTable {
scrollbar-color-active: $accent;
}

/* QUERY EDITOR */

/* SCHEMA VIEWER */
SchemaViewer {
padding: 0 1;
row-span: 2;
row-span: 3;
}
SchemaViewer .tree--guides {
color: $primary;
Expand All @@ -49,6 +48,45 @@ SchemaViewer .tree--guides-selected {
color: $secondary;
}

/* RUN QUERY BAR */
RunQueryBar {
height: 100%;
background: $panel;
align-horizontal: right;
padding: 0 2 0 0;
}
RunQueryBar Input {
border: none;
padding: 0;
width: 6;
}
RunQueryBar Input.-invalid {
border: none;
color: red
}
Tooltip {
border: round $panel-lighten-1;
}
RunQueryBar Button {
background: $accent;
border: none;
height: 1;
margin: 0 0 0 4;
}
RunQueryBar Button:hover {
background: $secondary;
}
RunQueryBar Button:focus {
text-style: bold;
}
RunQueryBar:disabled {
text-style: italic;
background: $panel;
}
RunQueryBar Button:disabled {
background: $panel;
}

/* RESULTS VIEWER */

ResultsViewer.non-responsive {
Expand Down
Loading