Beispiel #1
0
 def __init__(self, database, table_name, primary_id=None,
              primary_type=None, auto_create=False):
     """Initialise the table from database schema."""
     self.db = database
     self.name = normalize_table_name(table_name)
     self._table = None
     self._indexes = []
     self._primary_id = primary_id
     self._primary_type = primary_type
     self._auto_create = auto_create
Beispiel #2
0
 def __contains__(self, table_name):
     """Check if the given table name exists in the database."""
     try:
         table_name = normalize_table_name(table_name)
         if table_name in self.tables:
             return True
         if table_name in self.views:
             return True
         return False
     except ValueError:
         return False
Beispiel #3
0
 def __init__(self, database, table_name, primary_id=None,
              primary_type=None, auto_create=False):
     """Initialise the table from database schema."""
     self.db = database
     self.name = normalize_table_name(table_name)
     self._table = None
     self._indexes = []
     self._primary_id = primary_id if primary_id is not None \
         else self.PRIMARY_DEFAULT
     self._primary_type = primary_type if primary_type is not None \
         else Types.integer
     self._auto_create = auto_create
    def create_table(self,
                     table_name,
                     primary_id=None,
                     primary_type=None,
                     primary_increment=None):
        """Create a new table.

        Either loads a table or creates it if it doesn't exist yet. You can
        define the name and type of the primary key field, if a new table is to
        be created. The default is to create an auto-incrementing integer,
        ``id``. You can also set the primary key to be a string or big integer.
        The caller will be responsible for the uniqueness of ``primary_id`` if
        it is defined as a text type. You can disable auto-increment behaviour
        for numeric primary keys by setting `primary_increment` to `False`.

        Returns a :py:class:`Table <dataset.Table>` instance.
        ::

            table = db.create_table('population')

            # custom id and type
            table2 = db.create_table('population2', 'age')
            table3 = db.create_table('population3',
                                     primary_id='city',
                                     primary_type=db.types.text)
            # custom length of String
            table4 = db.create_table('population4',
                                     primary_id='city',
                                     primary_type=db.types.string(25))
            # no primary key
            table5 = db.create_table('population5',
                                     primary_id=False)
        """
        assert not isinstance(
            primary_type,
            str), "Text-based primary_type support is dropped, use db.types."
        table_name = normalize_table_name(table_name)
        with self.lock:
            if table_name not in self._tables:
                self._tables[table_name] = Table(
                    self,
                    table_name,
                    primary_id=primary_id,
                    primary_type=primary_type,
                    primary_increment=primary_increment,
                    auto_create=True,
                )
            return self._tables.get(table_name)
Beispiel #5
0
    def load_table(self, table_name):
        """Load a table.

        This will fail if the tables does not already exist in the database. If
        the table exists, its columns will be reflected and are available on
        the :py:class:`Table <dataset.Table>` object.

        Returns a :py:class:`Table <dataset.Table>` instance.
        ::

            table = db.load_table('population')
        """
        table_name = normalize_table_name(table_name)
        with self.lock:
            if table_name not in self._tables:
                self._tables[table_name] = Table(self, table_name)
            return self._tables.get(table_name)
Beispiel #6
0
    def load_table(self, table_name):
        """Load a table.

        This will fail if the tables does not already exist in the database. If
        the table exists, its columns will be reflected and are available on
        the :py:class:`Table <dataset.Table>` object.

        Returns a :py:class:`Table <dataset.Table>` instance.
        ::

            table = db.load_table('population')
        """
        table_name = normalize_table_name(table_name)
        with self.lock:
            if table_name not in self._tables:
                self._tables[table_name] = Table(self, table_name)
            return self._tables.get(table_name)
Beispiel #7
0
    def create_table(self, table_name, primary_id=None, primary_type=None):
        """Create a new table.

        Either loads a table or creates it if it doesn't exist yet. You can
        define the name and type of the primary key field, if a new table is to
        be created. The default is to create an auto-incrementing integer,
        ``id``. You can also set the primary key to be a string or big integer.
        The caller will be responsible for the uniqueness of ``primary_id`` if
        it is defined as a text type.

        Returns a :py:class:`Table <dataset.Table>` instance.
        ::

            table = db.create_table('population')

            # custom id and type
            table2 = db.create_table('population2', 'age')
            table3 = db.create_table('population3',
                                     primary_id='city',
                                     primary_type=db.types.text)
            # custom length of String
            table4 = db.create_table('population4',
                                     primary_id='city',
                                     primary_type=db.types.string(25))
            # no primary key
            table5 = db.create_table('population5',
                                     primary_id=False)
        """
        assert not isinstance(primary_type, six.string_types), \
            'Text-based primary_type support is dropped, use db.types.'
        table_name = normalize_table_name(table_name)
        with self.lock:
            if table_name not in self._tables:
                self._tables[table_name] = Table(self, table_name,
                                                 primary_id=primary_id,
                                                 primary_type=primary_type,
                                                 auto_create=True)
            return self._tables.get(table_name)
Beispiel #8
0
 def __init__(
     self,
     database,
     table_name,
     primary_id=None,
     primary_type=None,
     primary_increment=None,
     auto_create=False,
 ):
     """Initialise the table from database schema."""
     self.db = database
     self.name = normalize_table_name(table_name)
     self._table = None
     self._columns = None
     self._indexes = []
     self._primary_id = (primary_id if primary_id is not None else
                         self.PRIMARY_DEFAULT)
     self._primary_type = primary_type if primary_type is not None else Types.integer
     if primary_increment is None:
         primary_increment = self._primary_type in (Types.integer,
                                                    Types.bigint)
     self._primary_increment = primary_increment
     self._auto_create = auto_create
 def __contains__(self, table_name):
     """Check if the given table name exists in the database."""
     try:
         return normalize_table_name(table_name) in self.tables
     except ValueError:
         return False
Beispiel #10
0
 def __contains__(self, table_name):
     """Check if the given table name exists in the database."""
     try:
         return normalize_table_name(table_name) in self.tables
     except ValueError:
         return False