mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
--not-null-default and not_null_default=, refs #24
This commit is contained in:
parent
7a3041b1ad
commit
2fed87da6e
7 changed files with 87 additions and 16 deletions
|
|
@ -292,7 +292,9 @@ class Table:
|
|||
self.db.conn.execute(sql)
|
||||
return self
|
||||
|
||||
def add_column(self, col_name, col_type=None, fk=None, fk_col=None):
|
||||
def add_column(
|
||||
self, col_name, col_type=None, fk=None, fk_col=None, not_null_default=None
|
||||
):
|
||||
fk_col_type = None
|
||||
if fk is not None:
|
||||
# fk must be a valid table
|
||||
|
|
@ -313,10 +315,19 @@ class Table:
|
|||
fk_col_type = "INTEGER"
|
||||
if col_type is None:
|
||||
col_type = str
|
||||
sql = "ALTER TABLE [{table}] ADD COLUMN [{col_name}] {col_type};".format(
|
||||
not_null_sql = None
|
||||
if not_null_default is not None:
|
||||
not_null_sql = "NOT NULL DEFAULT {}".format(
|
||||
# Use SQLite itself to correctly escape this string:
|
||||
self.db.conn.execute(
|
||||
"SELECT quote(:default)", {"default": not_null_default}
|
||||
).fetchone()[0]
|
||||
)
|
||||
sql = "ALTER TABLE [{table}] ADD COLUMN [{col_name}] {col_type}{not_null_default};".format(
|
||||
table=self.name,
|
||||
col_name=col_name,
|
||||
col_type=fk_col_type or COLUMN_TYPE_MAPPING[col_type],
|
||||
not_null_default=(" " + not_null_sql) if not_null_sql else "",
|
||||
)
|
||||
self.db.conn.execute(sql)
|
||||
if fk is not None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue