Skip to content

Commit

Permalink
Merge pull request #235 from megh-khaire/dev
Browse files Browse the repository at this point in the history
Support autoincrement clause for sqlite
  • Loading branch information
klahnakoski authored May 13, 2024
2 parents 60b92d6 + 01ae5a3 commit a750df1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions mo_sql_parsing/sql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ def mult(tokens):
assign("engine", EQ + identifier)
| assign("collate", EQ + identifier)
| assign("auto_increment", EQ + int_num)
| assign("autoincrement", EQ + int_num)
| assign("comment", EQ + literal_string)
| assign("default character set", EQ + identifier)
| assign("default charset", EQ + identifier)
Expand Down
1 change: 1 addition & 0 deletions mo_sql_parsing/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def get_column_type(expr, identifier, literal_string):
| (NULL / True)("nullable")
| flag("unique")
| flag("auto_increment")
| flag("autoincrement")
| assign("comment", literal_string)
| assign("character set", identifier)
| assign("collate", Optional(EQ) + identifier)
Expand Down
37 changes: 37 additions & 0 deletions tests/test_sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# encoding: utf-8
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#


from unittest import TestCase
from mo_sql_parsing import parse


class TestSqlite(TestCase):
def test_autoincrement_in_create(self):
sql = """
CREATE TABLE Products (
product_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL NOT NULL,
description TEXT
);
"""
result = parse(sql)

self.assertEqual(
result,
{
'create table': {
'name': 'Products',
'columns': [
{'name': 'product_id', 'type': {'integer': {}}, 'primary_key': True, 'autoincrement': True},
{'name': 'name', 'type': {'text': {}}, 'nullable': False},
{'name': 'price', 'type': {'real': {}}, 'nullable': False},
{'name': 'description', 'type': {'text': {}}}
]
}
},
)

0 comments on commit a750df1

Please sign in to comment.