kopia lustrzana https://github.com/simonw/datasette
Refactored connection logic to database.connect()
rodzic
27cb29365c
commit
f04deebec4
|
@ -470,20 +470,7 @@ class Datasette:
|
||||||
def in_thread():
|
def in_thread():
|
||||||
conn = getattr(connections, db_name, None)
|
conn = getattr(connections, db_name, None)
|
||||||
if not conn:
|
if not conn:
|
||||||
db = self.databases[db_name]
|
conn = self.databases[db_name].connect()
|
||||||
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,
|
|
||||||
)
|
|
||||||
self.prepare_connection(conn)
|
self.prepare_connection(conn)
|
||||||
setattr(connections, db_name, conn)
|
setattr(connections, db_name, conn)
|
||||||
return fn(conn)
|
return fn(conn)
|
||||||
|
|
|
@ -33,6 +33,18 @@ class Database:
|
||||||
for key, value in self.ds.inspect_data[self.name]["tables"].items()
|
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
|
@property
|
||||||
def size(self):
|
def size(self):
|
||||||
if self.is_memory:
|
if self.is_memory:
|
||||||
|
|
Ładowanie…
Reference in New Issue