Exemplo n.º 1
0
    def create_index(self, columns, name=None, **kw):
        """Create an index to speed up queries on a table.

        If no ``name`` is given a random name is created.
        ::

            table.create_index(['name', 'country'])
        """
        columns = [normalize_column_name(c) for c in ensure_tuple(columns)]
        with self.db.lock:
            if not self.exists:
                raise DatasetException("Table has not been created yet.")

            if not self.has_index(columns):
                self._threading_warn()
                name = name or index_name(self.name, columns)
                columns = [self.table.c[c] for c in columns]
                idx = Index(name, *columns, **kw)
                idx.create(self.db.executable)
Exemplo n.º 2
0
    def drop_column(self, name):
        """Drop the column ``name``.
        ::
            table.drop_column('created_at')
        """
        if self.db.engine.dialect.name == 'sqlite':
            raise RuntimeError("SQLite does not support dropping columns.")
        name = normalize_column_name(name)
        with self.db.lock:
            if not self.exists or not self.has_column(name):
                log.debug("Column does not exist: %s", name)
                return

            self._threading_warn()
            self.db.op.drop_column(
                self.table.name,
                name,
                self.table.schema
            )
            self._reflect_table()
Exemplo n.º 3
0
    def create_column(self, name, type, **kwargs):
        """Create a new column ``name`` of a specified type.
        ::

            table.create_column('created_at', db.types.datetime)

        `type` corresponds to an SQLAlchemy type as described by
        `dataset.db.Types`. Additional keyword arguments are passed
        to the constructor of `Column`, so that default values, and
        options like `nullable` and `unique` can be set.
        ::

            table.create_column('key', unique=True, nullable=False)
            table.create_column('food', default='banana')
        """
        name = normalize_column_name(name)
        if self.has_column(name):
            log.debug("Column exists: %s" % name)
            return
        self._sync_table((Column(name, type, **kwargs),))
Exemplo n.º 4
0
 def has_column(self, column):
     """Check if a column with the given name exists on this table."""
     return normalize_column_name(column) in self.columns
Exemplo n.º 5
0
 def _get_column_name(self, name):
     """Find the best column name with case-insensitive matching."""
     name = normalize_column_name(name)
     key = normalize_column_key(name)
     return self._column_keys.get(key, name)
Exemplo n.º 6
0
 def has_column(self, column):
     """Check if a column with the given name exists on this table."""
     key = normalize_column_key(normalize_column_name(column))
     return key in self._column_keys
Exemplo n.º 7
0
 def has_column(self, column):
     """Check if a column with the given name exists on this table."""
     columns = [c.lower() for c in self.columns]
     return normalize_column_name(column).lower() in columns
Exemplo n.º 8
0
 def _keys_to_args(self, row, keys):
     keys = ensure_tuple(keys)
     keys = [normalize_column_name(k) for k in keys]
     row = row.copy()
     args = {k: row.pop(k, None) for k in keys}
     return args, row