MCPcopy Index your code
hub / github.com/clips/pattern / Table

Class Table

pattern/db/__init__.py:801–1004  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

799 pass
800
801class Table(object):
802
803 class Fields(list):
804 # Table.fields.append() alters the table.
805 # New field() with optional=False must have a default value (can not be NOW).
806 # New field() can have index=True, but not PRIMARY or UNIQUE.
807 def __init__(self, table, *args, **kwargs):
808 list.__init__(self, *args, **kwargs); self.table=table
809 def append(self, field):
810 name, (field, index) = field[0], self.table.db._field_SQL(self.table.name, field)
811 self.table.db.execute("alter table `%s` add column %s;" % (self.table.name, field))
812 self.table.db.execute(index, commit=True)
813 self.table._update()
814 def extend(self, fields):
815 [self.append(f) for f in fields]
816 def __setitem__(self, *args, **kwargs):
817 raise NotImplementedError, "Table.fields only supports append()"
818 insert = remove = pop = __setitem__
819
820 def __init__(self, name, database):
821 """ A collection of rows consisting of one or more fields (i.e., table columns)
822 of a certain type (i.e., strings, numbers).
823 """
824 self.database = database
825 self._name = name
826 self.fields = [] # List of field names (i.e., column names).
827 self.schema = {} # Dictionary of (field, Schema)-items.
828 self.default = {} # Default values for Table.insert().
829 self.primary_key = None
830 self._update()
831
832 def _update(self):
833 # Retrieve table column names.
834 # Table column names are available in the Table.fields list.
835 # Table column names should not contain unicode because they can also be function parameters.
836 # Table column names should avoid " ", ".", "(" and ")".
837 # The primary key column is stored in Table.primary_key.
838 self.fields = Table.Fields(self)
839 if self.name not in self.database.tables:
840 raise TableError, "table '%s' does not exist" % (self.database.name + "." + self.name)
841 if self.db.type == MYSQL:
842 q = "show columns from `%s`;" % self.name
843 if self.db.type == SQLITE:
844 q = "pragma table_info(`%s`);" % self.name
845 i = self.db.execute("pragma index_list(`%s`)" % self.name) # look up indices
846 i = dict(((v[1].replace(self.name+"_", "", 1), v[2]) for v in i))
847 for f in self.db.execute(q):
848 # [name, type, default, index, optional, extra]
849 if self.db.type == MYSQL:
850 f = [f[0], f[1], f[4], f[3], f[2], f[5]]
851 if self.db.type == SQLITE:
852 f = [f[1], f[2], f[4], f[5], f[3], ""]
853 f[3] = f[3] == 1 and "pri" or (f[0] in i and ("1","uni")[int(i[f[0]])] or "")
854 list.append(self.fields, f[0])
855 self.schema[f[0]] = Schema(*f)
856 if self.schema[f[0]].index == PRIMARY:
857 self.primary_key = f[0]
858

Callers 1

__getitem__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…