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
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()
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()
def get(cls, **kwargs): """ :return: returns a single instances of the model or None if there is no object matching the pattern. :rtype: either an instance of the current model class or None """ cls.__query__ = cls.__query__.filter(**kwargs).limit(1) ret = cls.__query__.run() if ret: statement, values = ret db.execute(statement, values) for row in db: instance = cls(**row) return instance return None
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()
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)
def all(cls, **kwargs): """ This function returns all objects that match all performed (:py:func:`filter`) queries. If there are no results, :py:func:`all` returns an empty list. :param kwargs: Any type of filter query (see :py:func:`filter`) :return: A list of instances or an empty list if there are no matching :rtype: list """ cls.__query__ = cls.__query__.filter(**kwargs) fields = [f for f, t in cls.__dict__.items() if isinstance(t, Field)] fields.sort() ret = cls.__query__.run() if ret: statement, values = ret db.execute(statement, values) result = [] for row in db: instance = cls(**row) result.append(copy.copy(instance)) return result if len(result) > 0 else [] return []