mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-13 03:54:23 +02:00
Add duplicate table feature
This commit is contained in:
parent
42440d6345
commit
1eb89beda3
2 changed files with 53 additions and 15 deletions
|
|
@ -305,11 +305,7 @@ class Database:
|
|||
), "Either specify a filename_or_conn or pass memory=True"
|
||||
if memory_name:
|
||||
uri = "file:{}?mode=memory&cache=shared".format(memory_name)
|
||||
self.conn = sqlite3.connect(
|
||||
uri,
|
||||
uri=True,
|
||||
check_same_thread=False,
|
||||
)
|
||||
self.conn = sqlite3.connect(uri, uri=True, check_same_thread=False,)
|
||||
elif memory or filename_or_conn == ":memory:":
|
||||
self.conn = sqlite3.connect(":memory:")
|
||||
elif isinstance(filename_or_conn, (str, pathlib.Path)):
|
||||
|
|
@ -1104,9 +1100,7 @@ class Queryable:
|
|||
self.name = name
|
||||
|
||||
def count_where(
|
||||
self,
|
||||
where: str = None,
|
||||
where_args: Optional[Union[Iterable, dict]] = None,
|
||||
self, where: str = None, where_args: Optional[Union[Iterable, dict]] = None,
|
||||
) -> int:
|
||||
"""
|
||||
Executes ``SELECT count(*) FROM table WHERE ...`` and returns a count.
|
||||
|
|
@ -1476,6 +1470,20 @@ class Table(Queryable):
|
|||
)
|
||||
return self
|
||||
|
||||
def duplicate(self, name_new: str) -> "Table":
|
||||
"""
|
||||
Duplicate this table in this database.
|
||||
|
||||
:param name_new: Name of new table.
|
||||
"""
|
||||
assert self.exists()
|
||||
with self.db.conn:
|
||||
sql = "CREATE TABLE [{new_table}] AS SELECT * FROM [{table}];".format(
|
||||
new_table=name_new, table=self.name,
|
||||
)
|
||||
self.db.execute(sql)
|
||||
return self.db[name_new]
|
||||
|
||||
def transform(
|
||||
self,
|
||||
*,
|
||||
|
|
@ -1711,13 +1719,7 @@ class Table(Queryable):
|
|||
)
|
||||
else:
|
||||
lookup_table.create(
|
||||
{
|
||||
**{
|
||||
"id": int,
|
||||
},
|
||||
**lookup_columns_definition,
|
||||
},
|
||||
pk="id",
|
||||
{**{"id": int,}, **lookup_columns_definition,}, pk="id",
|
||||
)
|
||||
lookup_columns = [(rename.get(col) or col) for col in columns]
|
||||
lookup_table.create_index(lookup_columns, unique=True, if_not_exists=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue