Skip to content

Commit b1aab48

Browse files
committed
fix: sql generation of varchar columns with defaults
1 parent 6a82cd7 commit b1aab48

4 files changed

Lines changed: 52 additions & 5 deletions

File tree

pydbml/renderer/sql/default/column.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
from typing import Union
2+
13
from pydbml.classes import Column, Enum, Expression
24
from pydbml.renderer.sql.default.renderer import DefaultSQLRenderer
35
from .utils import comment_to_sql
46
from .enum import get_full_name_for_sql as get_full_name_for_sql_enum
57

8+
def default_to_str(val: Union[Expression, str, int, float, bool]) -> str:
9+
if isinstance(val, Expression):
10+
return DefaultSQLRenderer.render(val)
11+
elif isinstance(val, str):
12+
val = val.replace("'", "''")
13+
return f"'{val}'"
14+
else:
15+
return str(val)
616

717
@DefaultSQLRenderer.renderer_for(Column)
818
def render_column(model: Column) -> str:
@@ -28,11 +38,7 @@ def render_column(model: Column) -> str:
2838
if model.not_null:
2939
components.append('NOT NULL')
3040
if model.default is not None:
31-
if isinstance(model.default, Expression):
32-
default = DefaultSQLRenderer.render(model.default)
33-
else:
34-
default = model.default # type: ignore
35-
components.append(f'DEFAULT {default}')
41+
components.append(f'DEFAULT {default_to_str(model.default)}')
3642

3743
result = comment_to_sql(model.comment) if model.comment else ''
3844
result += ' '.join(components)

test/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ def complex_column_with_table(db: Database, table1: Table, complex_column: Colum
6565
db.add(table1)
6666
return complex_column
6767

68+
@pytest.fixture
69+
def string_column() -> Column:
70+
return Column(
71+
name='name',
72+
type='varchar(255)',
73+
pk=False,
74+
autoinc=False,
75+
unique=True,
76+
not_null=True,
77+
default='value\'s',
78+
comment='This is a defaulted string column',
79+
note=Note('This is a note for the column'),
80+
properties={'foo': 'bar', 'baz': "qux\nqux"}
81+
)
82+
83+
84+
@pytest.fixture
85+
def string_column_with_table(db: Database, table1: Table, string_column: Column) -> Column:
86+
table1.add_column(string_column)
87+
db.add(table1)
88+
return string_column
89+
6890

6991
@pytest.fixture
7092
def table1() -> Table:

test/test_renderer/test_dbml/test_column.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,13 @@ def test_complex(complex_column_with_table: Column) -> None:
137137
"a note for the column']"
138138
)
139139
assert render_column(complex_column_with_table) == expected
140+
141+
@staticmethod
142+
def test_string(string_column_with_table: Column) -> None:
143+
expected = (
144+
"// This is a defaulted string column\n"
145+
'"name" varchar(255) [default: \'value\\\'s\', unique, not null, note: \'This is '
146+
"a note for the column']"
147+
)
148+
assert render_column(string_column_with_table) == expected
149+

test/test_renderer/test_sql/test_default/test_column.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ def test_complex(complex_column: Column) -> None:
1717
"0"
1818
)
1919
assert render_column(complex_column) == expected
20+
21+
@staticmethod
22+
def test_string(string_column: Column) -> None:
23+
expected = (
24+
"-- This is a defaulted string column\n"
25+
'"name" varchar(255) UNIQUE NOT NULL DEFAULT '
26+
"'value''s'"
27+
)
28+
assert render_column(string_column) == expected

0 commit comments

Comments
 (0)