| 733 | #--- FIELD SCHEMA ---------------------------------------------------------------------------------- |
| 734 | |
| 735 | class Schema(object): |
| 736 | |
| 737 | def __init__(self, name, type, default=None, index=False, optional=True, extra=None): |
| 738 | """ Field info returned from a "show columns from table"-query. |
| 739 | Each table object has a Table.schema{} dictionary describing the fields' structure. |
| 740 | """ |
| 741 | # Determine field type (NUMBER, STRING, TEXT, BLOB or DATE). |
| 742 | type, length = type.lower(), None |
| 743 | if type.startswith(("varchar", "char")): |
| 744 | length = type.split("(")[-1].strip(")") |
| 745 | length = int(length) |
| 746 | type = STRING |
| 747 | if type.startswith("int"): |
| 748 | type = INTEGER |
| 749 | if type.startswith(("real", "double")): |
| 750 | type = FLOAT |
| 751 | if type.startswith("time"): |
| 752 | type = DATE |
| 753 | if type.startswith("text"): |
| 754 | type = TEXT |
| 755 | if type.startswith("blob"): |
| 756 | type = BLOB |
| 757 | if type.startswith("tinyint(1)"): |
| 758 | type = BOOLEAN |
| 759 | # Determine index type (PRIMARY, UNIQUE, True or False). |
| 760 | if isinstance(index, basestring): |
| 761 | if index.lower().startswith("pri"): |
| 762 | index = PRIMARY |
| 763 | if index.lower().startswith("uni"): |
| 764 | index = UNIQUE |
| 765 | if index.lower() in ("0", "1", "", "yes", "mul"): |
| 766 | index = index.lower() in ("1", "yes", "mul") |
| 767 | # SQLite dumps the date string with quotes around it: |
| 768 | if isinstance(default, basestring) and type == DATE: |
| 769 | default = default.strip("'") |
| 770 | default = default.replace("current_timestamp", NOW) |
| 771 | default = default.replace("CURRENT_TIMESTAMP", NOW) |
| 772 | if default is not None and type == INTEGER: |
| 773 | default = int(default) |
| 774 | if default is not None and type == FLOAT: |
| 775 | default = float(default) |
| 776 | if not default and default != 0: |
| 777 | default = None |
| 778 | self.name = name # Field name. |
| 779 | self.type = type # Field type: INTEGER | FLOAT | STRING | TEXT | BLOB | DATE. |
| 780 | self.length = length # Field length for STRING. |
| 781 | self.default = default # Default value. |
| 782 | self.index = index # PRIMARY | UNIQUE | True | False. |
| 783 | self.optional = str(optional) in ("0", "True", "YES") |
| 784 | self.extra = extra or None |
| 785 | |
| 786 | def __repr__(self): |
| 787 | return "Schema(name=%s, type=%s, default=%s, index=%s, optional=%s)" % ( |
| 788 | repr(self.name), |
| 789 | repr(self.type), |
| 790 | repr(self.default), |
| 791 | repr(self.index), |
| 792 | repr(self.optional)) |
no outgoing calls
no test coverage detected
searching dependent graphs…