Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Jun 29, 2023
1 parent f4a2703 commit b704793
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/sql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def execute(self, query, with_=None):
Executes SQL query on a given connection
"""
query = self._prepare_query(query, with_)
return self.session.execute(query)
return self.engine.execute(query)


atexit.register(Connection.close_all, verbose=True)
Expand Down
4 changes: 4 additions & 0 deletions src/sql/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,15 @@ def __init__(self, table_name, schema=None) -> None:
columns_query_result = sql.run.raw_run(
Connection.current, f"SELECT * FROM {table_name} WHERE 1=0"
)

if Connection.is_custom_connection():
columns = [i[0] for i in columns_query_result.description]
else:
columns = columns_query_result.keys()

# TODO: abstract it internally
columns_query_result.close()

table_stats = dict({})
columns_to_include_in_report = set()
columns_with_styles = []
Expand Down
2 changes: 1 addition & 1 deletion src/sql/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def _first_word(sql):


def raw_run(conn, sql):
return conn.session.execute(sqlalchemy.sql.text(sql))
return conn.engine.execute(sqlalchemy.sql.text(sql))


class PrettyTable(prettytable.PrettyTable):
Expand Down
18 changes: 12 additions & 6 deletions src/tests/test_magic_cmd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sqlite3
import sys
import math
import pytest
from IPython.core.error import UsageError
from pathlib import Path

from sqlalchemy import create_engine
from sql.connection import Connection
from sql.store import store
from sql.inspect import _is_numeric

Expand Down Expand Up @@ -112,8 +111,15 @@ def test_tables(ip):


def test_tables_with_schema(ip, tmp_empty):
conn = Connection(engine=create_engine("sqlite:///my.db"))
conn.execute("CREATE TABLE numbers (some_number FLOAT)")
# TODO: why does this fail?
# ip.run_cell(
# """%%sql sqlite:///my.db
# CREATE TABLE numbers (some_number FLOAT)
# """
# )

with sqlite3.connect("my.db") as conn:
conn.execute("CREATE TABLE numbers (some_number FLOAT)")

ip.run_cell(
"""%%sql
Expand Down Expand Up @@ -150,8 +156,8 @@ def test_columns(ip, cmd, cols):


def test_columns_with_schema(ip, tmp_empty):
conn = Connection(engine=create_engine("sqlite:///my.db"))
conn.execute("CREATE TABLE numbers (some_number FLOAT)")
with sqlite3.connect("my.db") as conn:
conn.execute("CREATE TABLE numbers (some_number FLOAT)")

ip.run_cell(
"""%%sql
Expand Down

0 comments on commit b704793

Please sign in to comment.