mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 17:34:32 +02:00
Added sqlite-utils create-view command, closes #107
This commit is contained in:
parent
78264b738c
commit
d16097231c
3 changed files with 113 additions and 0 deletions
|
|
@ -594,6 +594,38 @@ def create_table(path, table, columns, pk, not_null, default, fk, ignore, replac
|
|||
)
|
||||
|
||||
|
||||
@cli.command(name="create-view")
|
||||
@click.argument(
|
||||
"path",
|
||||
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
|
||||
required=True,
|
||||
)
|
||||
@click.argument("view")
|
||||
@click.argument("select")
|
||||
@click.option(
|
||||
"--ignore", is_flag=True, help="If view already exists, do nothing",
|
||||
)
|
||||
@click.option(
|
||||
"--replace", is_flag=True, help="If view already exists, replace it",
|
||||
)
|
||||
def create_view(path, view, select, ignore, replace):
|
||||
"Create a view for the provided SELECT query"
|
||||
db = sqlite_utils.Database(path)
|
||||
# Does view already exist?
|
||||
if view in db.view_names():
|
||||
if ignore:
|
||||
return
|
||||
elif replace:
|
||||
db[view].drop()
|
||||
else:
|
||||
raise click.ClickException(
|
||||
'View "{}" already exists. Use --replace to delete and replace it.'.format(
|
||||
view
|
||||
)
|
||||
)
|
||||
db.create_view(view, select)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument(
|
||||
"path",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue