def pending_function(): QueryBuilder.table(self._table_name).where( self._other_name, '=', self_id).update({self._other_name: None}) QueryBuilder.table(self._table_name).where( other_model.id_key, '=', other_id).update({self._other_name: self_id})
def __getattr__(cls, name): if not cls._set_up: cls._set_up_model() if getattr(QueryBuilder, name, None): return getattr(QueryBuilder.table(cls.table_name, cls), name) else: return None
def save(self): """ Commit the current changes to the database. Return: self """ values_for_db = {} for field_name, field_instance in self._fields.items(): values_for_db[field_name] = field_instance.db_set( self, self._field_values[field_name]) # Make sure that the model + the relationships are saved in a transaction with O.db.transaction(): if self._creating_new: O.db.create(self.table_name, **values_for_db) else: if self._dumb: # If the model has no primary key, then do a "where_many" with all fields and hope for the best O.warn( f"Updating model {self.table_name} without primary key is error prone." ) self.__class__.where_many( self._convert_orig_values_to_conditions()).update( values_for_db) else: self.__class__.where(self.id_key, '=', self.get_id()).update(values_for_db) # Deal with all simple relations for rel_function in self._rel_queue: rel_function() if not self._dumb: id_of_row = O.db.last_id( ) if self._creating_new else self.get_id() # Deal with ManyToMany middle table saving for pivot in self._pivots.values(): if pivot is None: continue pivot.set_self_id(id_of_row) pivot.save() # When updating, update the values with the one gotten from the database row = QueryBuilder.table(self.table_name).where( self.id_key, '=', id_of_row).first() self._update_values(dict(zip(row.keys(), tuple(row)))) self._creating_new = False return self
def eager_load_builder(self): """ Used when a class is to be eager loaded. Allows to get every one of the related model. Returns: A partial method that allows to get all the related models. """ if not self._setted_up: self._set_up() builder = QueryBuilder(self._model.table_name, self._model).where(self._other_name, 'IS NOT', None) return partial(builder.where_in, self._other_name)
def eager_load_builder(self): if not self._setted_up: self._set_up() middle_table = self._middle_table target_model = self._model builder = QueryBuilder.table( self._model.table_name + ' oxygent', self._model).select('oxygent.*', middle_table + '.' + self._self_name).cross_join(middle_table).on( 'oxygent.' + target_model.id_key, '=', middle_table + '.' + self._other_name) return partial(builder.where_in, middle_table + '.' + self._self_name)
def pending_function(): QueryBuilder.table(self._table_name).where( self._other_name, '=', self_id).update({self._other_name: None})
def pending_function(): QueryBuilder.table(self._middle_table).where_many([ (self._parting_model.id_key, '=', self_id), (other_model.id_key, '=', other_id) ]).delete()
def pending_function(): QueryBuilder.table(self._middle_table).where( self._self_name, '=', self_id).delete()
def pending_function(): QueryBuilder.table(self._middle_table).where_many([ (self._self_name, '=', self_id), (self._other_name, '=', other_id) ]).update({self._other_name: None})
def pending_function(): QueryBuilder.table( self._table_name).or_where_many(conditions).update( {self._other_name: self_id})