def insert(self, data, return_id=False): gae_data = {} opts = self.query.get_meta() indexes = get_indexes().get(self.query.model, {}) unindexed_fields = indexes.get('unindexed', ()) unindexed_cols = [ opts.get_field(name).column for name in unindexed_fields ] kwds = {'unindexed_properties': unindexed_cols} for column, value in data.items(): if column == opts.pk.column: if isinstance(value, basestring): kwds['name'] = value else: kwds['id'] = value elif isinstance(value, (tuple, list)) and not len(value): # gae does not store emty lists (and even does not allow passing empty # lists to Entity.update) so skip them continue else: gae_data[column] = value entity = Entity(self.query.get_meta().db_table, **kwds) entity.update(gae_data) key = Put(entity) return key.id_or_name()
def insert(self, data, return_id=False): gae_data = {} opts = self.query.get_meta() indexes = get_indexes().get(self.query.model, {}) unindexed_fields = indexes.get('unindexed', ()) unindexed_cols = [opts.get_field(name).column for name in unindexed_fields] kwds = {'unindexed_properties': unindexed_cols} for column, value in data.items(): if column == opts.pk.column: if isinstance(value, basestring): kwds['name'] = value else: kwds['id'] = value elif isinstance(value, (tuple, list)) and not len(value): # gae does not store emty lists (and even does not allow passing empty # lists to Entity.update) so skip them continue else: gae_data[column] = value entity = Entity(self.query.get_meta().db_table, **kwds) entity.update(gae_data) key = Put(entity) return key.id_or_name()
def update_entity(self, pk, pk_field): gae_query = self.build_query() entity = Get(self.ops.value_for_db(pk, pk_field)) if not gae_query.matches_filters(entity): return for field, _, value in self.query.values: if hasattr(value, 'prepare_database_save'): value = value.prepare_database_save(field) else: value = field.get_db_prep_save(value, connection=self.connection) if hasattr(value, 'evaluate'): assert not value.negated value = ExpressionEvaluator(value, self.query, entity, allow_joins=False) if hasattr(value, 'as_sql'): value = value.as_sql(lambda n: n, self.connection) entity[field.column] = self.ops.value_for_db(value, field) Put(entity)
def insert(self, data_list, return_id=False): opts = self.query.get_meta() unindexed_fields = get_model_indexes(self.query.model)['unindexed'] unindexed_cols = [ opts.get_field(name).column for name in unindexed_fields ] entity_list = [] for data in data_list: properties = {} kwds = {'unindexed_properties': unindexed_cols} for column, value in data.items(): # The value will already be a db.Key, but the Entity # constructor takes a name or id of the key, and will # automatically create a new key if neither is given. if column == opts.pk.column: if value is not None: kwds['id'] = value.id() kwds['name'] = value.name() # GAE does not store empty lists (and even does not allow # passing empty lists to Entity.update) so skip them. elif isinstance(value, (tuple, list)) and not len(value): continue # Use column names as property names. else: properties[column] = value entity = Entity(opts.db_table, **kwds) entity.update(properties) entity_list.append(entity) keys = Put(entity_list) return keys[0] if isinstance(keys, list) else keys
def txn(entity): entity = Get(entity.key()) try: entity[self.to_column_name] = entity[self.from_column_name] except KeyError: return Put(entity)
def txn(entity): entity = Get(entity.key()) try: del entity[column_name] except KeyError: return Put(entity)
def update_entity(self, pk): gae_query = self.build_query() key = create_key(self.query.get_meta().db_table, pk) entity = Get(key) if not gae_query.matches_filters(entity): return qn = self.quote_name_unless_alias update_dict = {} for field, o, value in self.query.values: if hasattr(value, 'prepare_database_save'): value = value.prepare_database_save(field) else: value = field.get_db_prep_save(value, connection=self.connection) if hasattr(value, "evaluate"): assert not value.negated assert not value.subtree_parents value = ExpressionEvaluator(value, self.query, entity, allow_joins=False) if hasattr(value, 'as_sql'): # evaluate expression and return the new value val = value.as_sql(qn, self.connection) update_dict[field] = val else: update_dict[field] = value for field, value in update_dict.iteritems(): db_type = field.db_type(connection=self.connection) entity[qn(field.column)] = self.convert_value_for_db(db_type, value) key = Put(entity)
def insert(self, data, return_id=False): opts = self.query.get_meta() unindexed_fields = get_model_indexes(self.query.model)['unindexed'] kwds = {'unindexed_properties': []} properties = {} for field, value in data.iteritems(): # The value will already be a db.Key, but the Entity # constructor takes a name or id of the key, and will # automatically create a new key if neither is given. if field.primary_key: if value is not None: kwds['id'] = value.id() kwds['name'] = value.name() # GAE does not store empty lists (and even does not allow # passing empty lists to Entity.update) so skip them. elif isinstance(value, (tuple, list)) and not len(value): continue # Use column names as property names. else: properties[field.column] = value if field in unindexed_fields: kwds['unindexed_properties'].append(field.column) entity = Entity(opts.db_table, **kwds) entity.update(properties) return Put(entity)
def txn(): existing = Get([new_key])[0] if existing and not self.overwrite_existing: return if isinstance(entity.key().id_or_name(), (int, long)): reserve_id(self.to_kind, entity.key().id_or_name(), self.to_namespace) new_entity = clone_entity(entity, new_key) Put(new_entity)
def insert(self, data, return_id=False): kwds = {} gae_data = {} for column, value in data.items(): if column == self.query.get_meta().pk.column: if isinstance(value, basestring): kwds['name'] = value else: kwds['id'] = value elif isinstance(value, (tuple, list)) and not len(value): # gae does not store emty lists (and even does not allow passing empty # lists to Entity.update) so skip them continue else: gae_data[column] = value entity = Entity(self.query.get_meta().db_table, **kwds) entity.update(gae_data) key = Put(entity) return key.id_or_name()
def insert(self, data, return_id=False): gae_data = {} opts = self.query.get_meta() unindexed_fields = get_model_indexes(self.query.model)['unindexed'] unindexed_cols = [opts.get_field(name).column for name in unindexed_fields] kwds = {'unindexed_properties': unindexed_cols} for column, value in data.items(): if column == opts.pk.column: if isinstance(value, GAEKey): if value.parent_key() and value.parent_key().has_real_key(): kwds['parent'] = value.parent_key().real_key() if isinstance(value.id_or_name(), basestring): kwds['name'] = value.id_or_name() elif value.id_or_name() is not None: kwds['id'] = value.id_or_name() elif isinstance(value, Key): kwds['parent'] = value.parent() if value.name(): kwds['name'] = value.name() elif value.id(): kwds['id'] = value.id() elif isinstance(value, basestring): kwds['name'] = value else: kwds['id'] = value elif isinstance(value, (tuple, list)) and not len(value): # gae does not store empty lists (and even does not allow passing empty # lists to Entity.update) so skip them continue else: gae_data[column] = value entity = Entity(opts.db_table, **kwds) entity.update(gae_data) key = Put(entity) if not isinstance(opts.pk, GAEKeyField): key = key.id_or_name() return key
def txn(): try: existing = Get(new_key) except datastore_errors.EntityNotFoundError: existing = None if existing and not self.overwrite_existing: return if isinstance(entity.key().id_or_name(), (int, long)): reserve_id(self.to_kind, entity.key().id_or_name(), self.namespace) new_entity = clone_entity(entity, new_key) Put(new_entity)
def bulk_create(instances, connection=None): """ Uses AppEngine's bulk Put() call on a number of instances this will NOT call save() but it will return the instances with their primary_key populated (unlike Django's bulk_create) """ if connection is None: from django.db import connection from .fields import AncestorKey def prepare_entity(instance): if isinstance(instance.pk, AncestorKey): parent = instance.pk._parent_key else: parent = None result = Entity(instance._meta.db_table, parent=parent) for field in instance._meta.fields: if field.name == "id": continue value = field.pre_save(instance, True) setattr(instance, field.name, value) value = field.get_db_prep_save(getattr(instance, field.attname), connection) if isinstance(value, (list, set)): value = list(value) if not value: value = None result[field.column] = value return result entities = [prepare_entity(x) for x in instances] keys = Put(entities) assert (len(keys) == len(entities)) for i, key in enumerate(keys): assert (key) if key.parent(): instances[i]._parent_key = key.parent() instances[i].pk.key_id = key.id_or_name() else: instances[i].id = key.id_or_name() return instances
def txn(entity): entity = Get(entity.key()) entity[column_name] = value Put(entity)