Skip to content
Permalink
Browse files

Refactored connection logic to database.connect()

  • Loading branch information...
simonw committed Jul 25, 2019
1 parent 27cb293 commit f04deebec4f3842f7bd610cd5859de529f77d50e
Showing with 13 additions and 14 deletions.
  1. +1 −14 datasette/app.py
  2. +12 −0 datasette/database.py
@@ -470,20 +470,7 @@ def table_metadata(self, database, table):
def in_thread():
conn = getattr(connections, db_name, None)
if not conn:
db = self.databases[db_name]
if db.is_memory:
conn = sqlite3.connect(":memory:")
else:
# mode=ro or immutable=1?
if db.is_mutable:
qs = "mode=ro"
else:
qs = "immutable=1"
conn = sqlite3.connect(
"file:{}?{}".format(db.path, qs),
uri=True,
check_same_thread=False,
)
conn = self.databases[db_name].connect()
self.prepare_connection(conn)
setattr(connections, db_name, conn)
return fn(conn)
@@ -33,6 +33,18 @@ def __init__(self, ds, path=None, is_mutable=False, is_memory=False):
for key, value in self.ds.inspect_data[self.name]["tables"].items()
}

def connect(self):
if self.is_memory:
return sqlite3.connect(":memory:")
# mode=ro or immutable=1?
if self.is_mutable:
qs = "mode=ro"
else:
qs = "immutable=1"
return sqlite3.connect(
"file:{}?{}".format(self.path, qs), uri=True, check_same_thread=False
)

@property
def size(self):
if self.is_memory:

0 comments on commit f04deeb

Please sign in to comment.
You can’t perform that action at this time.