-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
847c993
commit bcbaa7f
Showing
7 changed files
with
157 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from uuid import uuid4 | ||
|
||
|
||
TEMP_TABLE_PREFIX = "chronify" | ||
|
||
|
||
def make_temp_view_name() -> str: | ||
"""Make a random name to be used as a temporary view or table.""" | ||
return f"{TEMP_TABLE_PREFIX}_{uuid4().hex}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copied this code from https://github.com/sqlalchemy/sqlalchemy/wiki/Views | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import Engine, MetaData, Selectable, TableClause | ||
from sqlalchemy.ext import compiler | ||
from sqlalchemy.schema import DDLElement | ||
from sqlalchemy.sql import table | ||
|
||
|
||
class CreateView(DDLElement): | ||
def __init__(self, name, selectable): | ||
self.name = name | ||
self.selectable = selectable | ||
|
||
|
||
class DropView(DDLElement): | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
|
||
@compiler.compiles(CreateView) | ||
def _create_view(element, compiler, **kw): | ||
return "CREATE VIEW %s AS %s" % ( | ||
element.name, | ||
compiler.sql_compiler.process(element.selectable, literal_binds=True), | ||
) | ||
|
||
|
||
@compiler.compiles(DropView) | ||
def _drop_view(element, compiler, **kw): | ||
return "DROP VIEW %s" % (element.name) | ||
|
||
|
||
def _view_exists(ddl, target, connection, **kw): | ||
return ddl.name in sa.inspect(connection).get_view_names() | ||
|
||
|
||
def _view_doesnt_exist(ddl, target, connection, **kw): | ||
return not _view_exists(ddl, target, connection, **kw) | ||
|
||
|
||
def create_view( | ||
name: str, selectable: Selectable, engine: Engine, metadata: MetaData | ||
) -> TableClause: | ||
"""Create a view from a selectable.""" | ||
view = table(name) | ||
view._columns._populate_separate_keys( | ||
col._make_proxy(view) for col in selectable.selected_columns | ||
) | ||
sa.event.listen( | ||
metadata, | ||
"after_create", | ||
CreateView(name, selectable).execute_if(callable_=_view_doesnt_exist), | ||
) | ||
sa.event.listen(metadata, "before_drop", DropView(name).execute_if(callable_=_view_exists)) | ||
metadata.create_all(engine) | ||
return view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters