-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite SQL and DBML rendering (#39)
- Loading branch information
1 parent
6e0af33
commit 90c63c8
Showing
95 changed files
with
2,895 additions
and
2,553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
from . import classes | ||
from . import _classes | ||
from .parser import PyDBML | ||
from .database import Database | ||
from pydbml.constants import MANY_TO_ONE | ||
from pydbml.constants import ONE_TO_MANY | ||
from pydbml.constants import ONE_TO_ONE |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from typing import List | ||
from typing import Optional | ||
from typing import TYPE_CHECKING | ||
from typing import Union | ||
|
||
from pydbml.exceptions import TableNotFoundError | ||
from .base import SQLObject, DBMLObject | ||
from .enum import Enum | ||
from .expression import Expression | ||
from .note import Note | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from .table import Table | ||
from .reference import Reference | ||
|
||
|
||
class Column(SQLObject, DBMLObject): | ||
'''Class representing table column.''' | ||
|
||
required_attributes = ('name', 'type') | ||
dont_compare_fields = ('table',) | ||
|
||
def __init__(self, | ||
name: str, | ||
type: Union[str, Enum], | ||
unique: bool = False, | ||
not_null: bool = False, | ||
pk: bool = False, | ||
autoinc: bool = False, | ||
default: Optional[Union[str, int, bool, float, Expression]] = None, | ||
note: Optional[Union[Note, str]] = None, | ||
# ref_blueprints: Optional[List[ReferenceBlueprint]] = None, | ||
comment: Optional[str] = None): | ||
self.name = name | ||
self.type = type | ||
self.unique = unique | ||
self.not_null = not_null | ||
self.pk = pk | ||
self.autoinc = autoinc | ||
self.comment = comment | ||
self.note = Note(note) | ||
|
||
self.default = default | ||
self.table: Optional['Table'] = None | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if other is self: | ||
return True | ||
if not isinstance(other, self.__class__): | ||
return False | ||
self_table = self.table.full_name if self.table else None | ||
other_table = other.table.full_name if other.table else None | ||
if self_table != other_table: | ||
return False | ||
return super().__eq__(other) | ||
|
||
@property | ||
def note(self): | ||
return self._note | ||
|
||
@note.setter | ||
def note(self, val: Note) -> None: | ||
self._note = val | ||
val.parent = self | ||
|
||
def get_refs(self) -> List['Reference']: | ||
''' | ||
get all references related to this column (where this col is col1 in) | ||
''' | ||
if not self.table: | ||
raise TableNotFoundError('Table for the column is not set') | ||
return [ref for ref in self.table.get_refs() if self in ref.col1] | ||
|
||
@property | ||
def database(self): | ||
return self.table.database if self.table else None | ||
|
||
def __repr__(self): | ||
''' | ||
>>> Column('name', 'VARCHAR2') | ||
<Column 'name', 'VARCHAR2'> | ||
''' | ||
type_name = self.type if isinstance(self.type, str) else self.type.name | ||
return f'<Column {self.name!r}, {type_name!r}>' | ||
|
||
def __str__(self): | ||
''' | ||
>>> print(Column('name', 'VARCHAR2')) | ||
name[VARCHAR2] | ||
''' | ||
|
||
return f'{self.name}[{self.type}]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from typing import List | ||
from typing import Literal | ||
from typing import Optional | ||
from typing import TYPE_CHECKING | ||
from typing import Union | ||
|
||
from .base import SQLObject, DBMLObject | ||
from .column import Column | ||
from .expression import Expression | ||
from .note import Note | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from .table import Table | ||
|
||
|
||
class Index(SQLObject, DBMLObject): | ||
'''Class representing index.''' | ||
required_attributes = ('subjects', 'table') | ||
dont_compare_fields = ('table',) | ||
|
||
def __init__(self, | ||
subjects: List[Union[str, Column, Expression]], | ||
name: Optional[str] = None, | ||
unique: bool = False, | ||
type: Optional[Literal['hash', 'btree']] = None, | ||
pk: bool = False, | ||
note: Optional[Union[Note, str]] = None, | ||
comment: Optional[str] = None): | ||
self.subjects = subjects | ||
self.table: Optional[Table] = None | ||
|
||
self.name = name if name else None | ||
self.unique = unique | ||
self.type = type | ||
self.pk = pk | ||
self.note = Note(note) | ||
self.comment = comment | ||
|
||
@property | ||
def note(self): | ||
return self._note | ||
|
||
@note.setter | ||
def note(self, val: Note) -> None: | ||
self._note = val | ||
val.parent = self | ||
|
||
@property | ||
def subject_names(self): | ||
''' | ||
Returns updated list of subject names. | ||
''' | ||
return [s.name if isinstance(s, Column) else str(s) for s in self.subjects] | ||
|
||
def __repr__(self): | ||
''' | ||
<Index 'test', ['col', '(c*2)']> | ||
''' | ||
|
||
table_name = self.table.name if self.table else None | ||
return f"<Index {table_name!r}, {self.subject_names!r}>" | ||
|
||
def __str__(self): | ||
''' | ||
Index(test[col, (c*2)]) | ||
''' | ||
|
||
table_name = self.table.name if self.table else '' | ||
subjects = ', '.join(self.subject_names) | ||
return f"Index({table_name}[{subjects}])" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Any | ||
|
||
from .base import SQLObject, DBMLObject | ||
|
||
|
||
class Note(SQLObject, DBMLObject): | ||
dont_compare_fields = ('parent',) | ||
|
||
def __init__(self, text: Any) -> None: | ||
self.text: str | ||
self.text = str(text) if text is not None else '' | ||
self.parent: Any = None | ||
|
||
def __str__(self): | ||
'''Note text''' | ||
return self.text | ||
|
||
def __bool__(self): | ||
return bool(self.text) | ||
|
||
def __repr__(self): | ||
'''Note('Note text')''' | ||
return f'Note({repr(self.text)})' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Dict | ||
from typing import Optional | ||
from typing import Union | ||
|
||
from pydbml._classes.base import DBMLObject | ||
from pydbml._classes.note import Note | ||
|
||
|
||
class Project(DBMLObject): | ||
dont_compare_fields = ('database',) | ||
|
||
def __init__(self, | ||
name: str, | ||
items: Optional[Dict[str, str]] = None, | ||
note: Optional[Union[Note, str]] = None, | ||
comment: Optional[str] = None): | ||
self.database = None | ||
self.name = name | ||
self.items = items | ||
self.note = Note(note) | ||
self.comment = comment | ||
|
||
def __repr__(self): | ||
"""<Project 'myproject'>""" | ||
return f'<Project {self.name!r}>' | ||
|
||
@property | ||
def note(self): | ||
return self._note | ||
|
||
@note.setter | ||
def note(self, val: Note) -> None: | ||
self._note = val | ||
val.parent = self |
Oops, something went wrong.