Skip to content

Commit

Permalink
Move setup_connection to connection.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gvx committed Jan 18, 2021
1 parent 1a5ca61 commit fd8ad77
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions wurm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
__version__ = '0.1.0'

from .typemaps import register_type, Unique, Primary
from .tables import WithoutRowid, Table, setup_connection
from .connection import WurmError
from .tables import WithoutRowid, Table
from .connection import WurmError, setup_connection
from .queries import lt, gt, le, ge, ne, eq, Query

__all__ = ['register_type', 'Unique', 'Primary', 'WurmError',
Expand Down
11 changes: 9 additions & 2 deletions wurm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class WurmError(Exception):
:class:`sqlite3.Error` when that exists."""

def execute(*args, conn=None):
with open('.sql.log', 'a') as f:
print(*args, file=f)
if conn is None:
try:
conn = connection.get()
Expand All @@ -23,3 +21,12 @@ def execute(*args, conn=None):
return conn.execute(*args)
except sqlite3.Error as e:
raise WurmError from e

def setup_connection(conn):
"""Call this once in each OS thread with a
:class:`sqlite3.Connection`, before accessing the database via wurm.
This records the connection and ensures all tables are created."""
connection.set(conn)
from .tables import BaseTable, create_tables
create_tables(BaseTable, conn)
10 changes: 1 addition & 9 deletions wurm/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .typemaps import to_stored, Primary, is_primary
from .queries import Query
from .connection import execute, connection
from .connection import execute
from . import sql

def table_fields(table):
Expand Down Expand Up @@ -166,11 +166,3 @@ def create_tables(tbl, conn):
if not table.__abstract__:
execute(sql.create(table), conn=conn)
create_tables(table, conn)

def setup_connection(conn):
"""Call this once in each OS thread with a
:class:`sqlite3.Connection`, before accessing the database via wurm.
This records the connection and ensures all tables are created."""
connection.set(conn)
create_tables(BaseTable, conn)

0 comments on commit fd8ad77

Please sign in to comment.