str, int, bytes aliases for column types, closes #606

This commit is contained in:
Simon Willison 2023-12-06 10:49:21 -08:00
commit 88bd372205
5 changed files with 32 additions and 3 deletions

View file

@ -1157,7 +1157,7 @@ See :ref:`cli_add_column`.
::
Usage: sqlite-utils add-column [OPTIONS] PATH TABLE COL_NAME
[[integer|float|blob|text|INTEGER|FLOAT|BLOB|TEXT]]
[[integer|int|float|text|str|blob|bytes]]
Add a column to the specified table

View file

@ -2310,7 +2310,14 @@ You can add a column using the ``add-column`` command:
sqlite-utils add-column mydb.db mytable nameofcolumn text
The last argument here is the type of the column to be created. You can use one of ``text``, ``integer``, ``float`` or ``blob``. If you leave it off, ``text`` will be used.
The last argument here is the type of the column to be created. This can be one of:
- ``text`` or ``str``
- ``integer`` or ``int``
- ``float``
- ``blob`` or ``bytes``
This argument is optional and defaults to ``text``.
You can add a column that is a foreign key reference to another table using the ``--fk`` option:

View file

@ -60,6 +60,14 @@ It's often worth trying: --encoding=latin-1
maximize_csv_field_size_limit()
class CaseInsensitiveChoice(click.Choice):
def __init__(self, choices):
super().__init__([choice.lower() for choice in choices])
def convert(self, value, param, ctx):
return super().convert(value.lower(), param, ctx)
def output_options(fn):
for decorator in reversed(
(
@ -412,7 +420,8 @@ def dump(path, load_extension):
@click.argument(
"col_type",
type=click.Choice(
["integer", "float", "blob", "text", "INTEGER", "FLOAT", "BLOB", "TEXT"]
["integer", "int", "float", "text", "str", "blob", "bytes"],
case_sensitive=False,
),
required=False,
)

View file

@ -197,9 +197,12 @@ COLUMN_TYPE_MAPPING = {
"FLOAT": "FLOAT",
"BLOB": "BLOB",
"text": "TEXT",
"str": "TEXT",
"integer": "INTEGER",
"int": "INTEGER",
"float": "FLOAT",
"blob": "BLOB",
"bytes": "BLOB",
}
# If numpy is available, add more types
if np:

View file

@ -272,13 +272,23 @@ def test_create_index_desc(db_path):
"col_name,col_type,expected_schema",
(
("text", "TEXT", "CREATE TABLE [dogs] (\n [name] TEXT\n, [text] TEXT)"),
("text", "str", "CREATE TABLE [dogs] (\n [name] TEXT\n, [text] TEXT)"),
("text", "STR", "CREATE TABLE [dogs] (\n [name] TEXT\n, [text] TEXT)"),
(
"integer",
"INTEGER",
"CREATE TABLE [dogs] (\n [name] TEXT\n, [integer] INTEGER)",
),
(
"integer",
"int",
"CREATE TABLE [dogs] (\n [name] TEXT\n, [integer] INTEGER)",
),
("float", "FLOAT", "CREATE TABLE [dogs] (\n [name] TEXT\n, [float] FLOAT)"),
("blob", "blob", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"),
("blob", "BLOB", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"),
("blob", "bytes", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"),
("blob", "BYTES", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"),
("default", None, "CREATE TABLE [dogs] (\n [name] TEXT\n, [default] TEXT)"),
),
)