Ejemplo n.º 1
0
 def truncate(cls):
     """
     This method removes all records in this table from the database.
     """
     # TODO: dry_run
     __query__ = Query(Query.QTYPE_TRUNCATE, {}, table=cls.__name__.lower())
     ret = __query__.run()
     if ret:
         statement, values = ret
         db.execute(statement, values)
         connection.commit()
Ejemplo n.º 2
0
 def drop(cls):
     """
     This method drops this table from the database.
     """
     # TODO: dry_run
     __query__ = Query(Query.QTYPE_DROP, {}, table=cls.__name__.lower())
     ret = __query__.run()
     if ret:
         statement, values = ret
         db.execute(statement, values)
         connection.commit()
Ejemplo n.º 3
0
    def delete(self, commit=True):
        """
        Calling this method will remove the object from the database. However,
        any variable that is referring an instance still access it.

        :param commit: If true, each change will direct affect the database
        :type commit: Boolean
        """
        if self.pk and self.__meta__['fields'][self.pk].value:
            __query__ = Query(Query.QTYPE_DELETE, self.__meta__['fields'],
                            table=self.__meta__['name'],
                            pk=self.pk)
            ret = __query__.run()
            if ret:
                statement, values = ret
                db.execute(statement, values)
                if commit:
                    connection.commit()
Ejemplo n.º 4
0
    def save(self, commit=True):
        """
        Calling the save methode will store the object in the databse.

        :param commit: If `True` (default), each change will direct affect the
            database. If `commit` is `False`
        :type commit: Boolean
        """
        if self.pk and self.__meta__['fields'][self.pk].value:
            __query__ = Query(Query.QTYPE_UPDATE, self.__meta__['fields'],
                            table=self.__meta__['name'],
                            pk=self.pk)
            ret = __query__.run()
            if ret:
                statement, values = ret
                db.execute(statement, values)
                if commit:
                    connection.commit()

        else:
            __query__ = Query(Query.QTYPE_INSERT, self.__meta__['fields'],
                            table=self.__meta__['name'],
                            pk=self.pk)
            ret = __query__.run()
            if ret:
                statement, values = ret
                db.execute(statement, values)
                if commit:
                    connection.commit()
                if self.pk:
                    self.__meta__['fields'][self.pk].value = db.lastrowid
Ejemplo n.º 5
0
    def query(cls, dry_run=False):
        """
        This function initializes and constructs the prepared database query
        statement and is used to run functions like :py:func:`all`,
        :py:func:`save`, :py:func:`delete`, :py:func:`create`

        :param dry_run: `True` or `False`. If dry_run is `True`, the
            database query is not executed.
        :type dry_run: boolean
        """
        fields = {}
        for f, t in cls.__dict__.items():
            if isinstance(t, Field):
                fields[f] = t

        kwargs = {
            'table': cls.__name__.lower(),
            'dry_run': dry_run,
        }
        cls.__query__ = Query(Query.QTYPE_SELECT, fields, **kwargs)
        return cls
Ejemplo n.º 6
0
    def create(cls, dry_run=False):
        """
        This method creates the table in the database.

        :param dry_run: `True` or `False`. If dry_run is `True`, the
            database query is not executed.
        :type dry_run: boolean
        """
        fields = {}
        for f, t in cls.__dict__.items():
            if isinstance(t, Field):
                fields[f] = t

        kwargs = {
            'table': cls.__name__.lower(),
            'dry_run': dry_run,
        }
        cls.__query__ = Query(Query.QTYPE_CREATE, fields, **kwargs)
        ret = cls.__query__.run()
        if ret:
            statement, values = ret
            db.execute(statement, values)