def find(self, filter={}, fields=None, skip=0, limit=None, sort=None): """ @param:query(dict), specify the WHERE clause {"name": "...", "id": ...} @param: fields, specify what fields are needed skip, limit: both integers, skip without defining limit is meaningless @return a QuerySet object """ if not fields: self.__ensure_columns() fields = self.columns query_obj = Query(source=self.name, filter=filter, fields=fields, skip=skip, limit=limit, sort=sort) return QuerySet(cursor=self.cursor, query=query_obj)
def find(self, filter={}, fields=None, skip=0, limit=None, sort=None): """ Searches the table using the filters provided. :Examples: >>> users = user_table.find({'id': {'$in': [10, 20]}, 'age': {'$gt': 20}}) # Complex query >>> user_count = len(users) >>> for user in users: >>> # Do something... >>> print user.id >>> >>> users = user_table.find({}, sort=[('age', monsql.ASCENDING)]) # sort by age Also support complex operators: >>> {a: 1} # a == 1 >>> {a: {$gt: 1}} # a > 1 >>> {a: {$gte: 1}} # a >= 1 >>> {a: {$lt: 1}} # a < 1 >>> {a: {$lte: 1}} # a <= 1 >>> {a: {$eq: 1}} # a == 1 >>> {a: {$in: [1, 2]}} # a == 1 or a == 2 >>> {a: {$contains: '123'}} # a like %123% >>> {$not: condition} # !(condition) >>> {$and: [condition1, condition2, ...]} # condition1 and condition2 >>> {$or: [condition1, condition2, ...]} # condition1 or condition2 :Parameters: - query(dict): specify the WHERE clause. One example is {"name": "...", "id": ...} - fields: specify what fields are needed - skip, limit: both integers, skip without defining limit is meaningless - sort: A list, each element is a two-item tuple, with the first item be the column name and the second item be either monsql.ASCENDING or monsql.DESCENDING :Return: a QuerySet object """ if not fields: self.__ensure_columns() fields = self.columns query_obj = Query(source=self.name, filter=filter, fields=fields, skip=skip, limit=limit, sort=sort) return QuerySet(cursor=self.cursor, query=query_obj)
def test_queryset(): lut = FunctionTrigger() ledger1 = pd.DataFrame({"Key1": ["A1", "B1"], "Key2": ["A2", "B2"]}) ledger2 = pd.DataFrame({ "Key1": ["A1", "D1", "E1"], "Key2": ["A2", "D2", "E2"] }) cls1 = ConstantLedgerSource(ledger1) cls2 = ConstantLedgerSource(ledger2) led1 = Ledger(lut, cls1) led2 = Ledger(lut, cls2) lut.invoke() qs = QuerySet([led1, led2], pd.DataFrame({ "Source": ["S0", "S0"], "Query": ["Q0", "Q2"] })) result_iterator = qs.join(3) results = result_iterator.as_list() target = pd.DataFrame({ "Key1": ["A1", "B1", "D1", "A1", "B1", "D1", "E1", "E1"], "Key2": ["A2", "B2", "D2", "A2", "B2", "D2", "E2", "E2"], "Source": ["S0"] * 8, "Query": ["Q0", "Q2"] * 4 }) c = 0 for result in results: row = target.loc[[c]] query = Query(**row.to_dict(orient="index")[c]) assert query == result c += 1
def get_query_set(self): return QuerySet(model=self.model, db=self.db)