def table_create(cls, if_not_exists=True): if (if_not_exists and (cls.Meta.table_name in r.table_list().run(get_conn()))): return return r.table_create(cls.Meta.table_name, primary_key=cls.Meta.primary_key_field).run( get_conn())
def table_create(cls, if_not_exists=True): if ( if_not_exists and (cls.__table_name__ in r.table_list().run(get_conn())) ): return return r.table_create( cls.__table_name__, primary_key=cls.__primary_key__ ).run(get_conn())
def table_create(cls, if_not_exists=True): if ( if_not_exists and (cls.Meta.table_name in r.table_list().run(get_conn())) ): return return r.table_create( cls.Meta.table_name, primary_key=cls.Meta.primary_key_field ).run(get_conn())
def save(self): if not self._dirty: return True self.validate() doc = self._doc table = r.table(self.Meta.table_name) if self.pk: # TODO: implement atomic updates instead of updating entire doc result = table.get(self.pk).update(doc).run(get_conn()) else: result = table.insert(doc).run(get_conn()) self._dirty = False if 'generated_keys' in result: self._data['pk'] = result['generated_keys'][0] return True
def save(self): if not self._dirty: return True self.validate() doc = self._doc table = r.table(self.Meta.table_name) if self.pk: # TODO: implement atomic updates instead of updating entire doc result = table.get(self.pk).update(doc).run(get_conn()) else: result = table.insert(doc).run(get_conn()) self._dirty = False if "generated_keys" in result: self._data["pk"] = result["generated_keys"][0] return True
def index_create(cls, name, fields=None, mutil=False): if fields is None: fields = [] if mutil is None: mutil = False table = r.table(cls.__table_name__) if len(fields) is 0 and not mutil: return table.index_create(name).run(get_conn()) if len(fields) > 0: return table.index_create(name, [r.row[x] for x in fields]).run(get_conn()) if mutil: return table.index_create(name, multi=True).run(get_conn()) return False
def save(self): if not self._dirty: return True self.validate() doc = self._doc table = r.table(self.Meta.table_name) if self.id: # TODO: implement atomic updates instead of updating entire doc result = table.get(self.id).update(doc).run(get_conn()) else: result = table.insert(doc).run(get_conn()) if result.get('errors', False) == 1: raise RqlOperationError(result['first_error']) self._dirty = False if 'generated_keys' in result: self._data['id'] = result['generated_keys'][0] return True
def save(self): if not self._dirty: return True self.validate() doc = self._doc table = r.table(self.Meta.table_name) if self.pk: # TODO: implement atomic updates instead of updating entire doc result = table.get(self.pk).update(doc, non_atomic=True).run(get_conn()) else: result = table.insert(doc).run(get_conn()) if result.get('errors', False) == 1: raise RqlOperationError(result['first_error']) self._dirty = False if 'generated_keys' in result: self._data['pk'] = result['generated_keys'][0] return True
def save(self): if not self._dirty: return True self.validate() is_update = False try: if self.id: is_update = True self._pre_update() else: self._pre_save() except AttributeError: pass doc = self._doc table = r.table(self.__table_name__) if is_update: # TODO: implement atomic updates instead of updating entire doc result = table.get(self.id).update(doc).run(get_conn()) else: result = table.insert(doc).run(get_conn()) if result.get('errors', False) == 1: raise RqlOperationError(result['first_error']) self._dirty = False if 'generated_keys' in result: self._data['id'] = result['generated_keys'][0] try: if is_update: self._post_update() else: self._post_save() except AttributeError: pass return True
def delete(self): table = r.table(self.__table_name__) if self._get_value('id'): try: self._pre_delete() except AttributeError: pass result = table.get(self._get_value('id')).delete().run(get_conn()) try: self._post_delete() except AttributeError: pass return result
def _build_cursor_obj(self): self._cursor_obj = r.table(self._document.Meta.table_name) if self._filter: self._cursor_obj = self._cursor_obj.filter(self._filter) order_by = self._order_by or self._document.Meta.order_by if order_by: order_by_r = [] for field in order_by: if field.startswith('-'): order_by_r.append(r.desc(field[1:])) else: order_by_r.append(r.asc(field)) self._cursor_obj = self._cursor_obj.order_by(*order_by_r) if self._skip: self._cursor_obj = self._cursor_obj.skip(self._skip) if self._limit: self._cursor_obj = self._cursor_obj.limit(self._limit) self._iter_index = 0 self._cursor_iter = iter(self._cursor_obj.run(get_conn()))
def _build_cursor_obj(self): self._cursor_obj = r.table(self._document.Meta.table_name) if self._filter: self._cursor_obj = self._cursor_obj.filter(self._filter) order_by = self._order_by or self._document.Meta.order_by if order_by: order_by_r = [] for field in order_by: if field.startswith('-'): order_by_r.append(r.desc(field[1:])) else: order_by_r.append(r.asc(field)) self._cursor_obj = self._cursor_obj.order_by(*order_by_r) if self._limit: self._cursor_obj = self._cursor_obj.limit(self._limit) if self._skip: self._cursor_obj = self._cursor_obj.skip(self._skip) self._iter_index = 0 self._cursor_iter = iter(self._cursor_obj.run(get_conn()))
def insert(self, batch): self._cursor_obj = r.table(self._document.Meta.table_name) map(lambda i: i.validate(), batch) result = self._cursor_obj.insert(map(lambda i: i._doc, batch)).run(get_conn()) return result.get("generated_keys", [])
def __len__(self): if not self._cursor_obj: self._build_cursor_obj() return self._cursor_obj.count().run(get_conn())
def index_list(cls): return r.table(cls.__table_name__).index_list().run(get_conn())
def delete(self): table = r.table(self.Meta.table_name) if self._get_value("pk"): return table.get(self._get_value("pk")).delete().run(get_conn())
def table_create(cls): return r.table_create(cls.Meta.table_name, primary_key=cls.Meta.primary_key_field).run(get_conn())
def table_drop(cls): return r.table_drop(cls.__table_name__).run(get_conn())
def index_wait(cls, name): return r.table(cls.__table_name__).index_wait(name).run(get_conn())
def delete(self): table = r.table(self.Meta.table_name) if self._get_value('pk'): return table.get(self._get_value('pk')).delete().run(get_conn())
def table_drop(cls): return r.table_drop(cls.Meta.table_name).run(get_conn())
def index_status(cls, name): return r.table(cls.__table_name__).index_status(name).run(get_conn())
def test_get_conn(self): # Assert whether get_conn() returns an instance of # rethinkdb.net.Connection connect(DB_NAME) self.assertIsInstance(get_conn(), rethinkdb.net.Connection)
def table_create(cls): return r.table_create(cls.Meta.table_name, primary_key=cls.Meta.primary_key_field).run( get_conn())
def get_all(cls, *args, **kwargs): result = r.table(cls.__table_name__).get_all(*args, **kwargs).run(get_conn()) return [cls(**o) for o in result]