Exemple #1
0
    def select(self,
               table,
               field=None,
               condition=None,
               sort=None,
               top=None,
               cache=False,
               offset=None):
        data = None
        payload = {
            'method': 'select',
            'source_id': self.__source_id,
            'table': table,
            'field': field,
            'condition': condition,
            'sort': sort,
            'top': top,
            'offset': offset
        }
        if cache is True:
            hash_payload = ReaPy.hash().md5(ReaPy.json().dumps(payload))
            cache = self.get_cache(hash_payload)
            if cache is not None:
                cache_data = cache[0][1]
                data_json = ReaPy.json().loads(cache_data)
                data = data_json['data']
            else:
                request = self.connect(payload)
                if request is not None:
                    if request.status_code == 200:
                        data = request.json()['data']
                        self.set_cache(hash_payload, data)

        else:
            request = self.connect(payload)
            if request is not None:
                if request.status_code == 200:
                    data = request.json()['data']

        if data is not None:
            self.__count = data['count']
            self.__result = data['result']
            self.__error = data['error']
        return self
Exemple #2
0
    def select(self, table, field=None, condition=None, sort=None, top=None, is_first=False, cache=False,
               offset=None):

        if self._pagination is not None:
            top = self._pagination[1]
            offset = self._pagination[0]

        try:
            cache_payload = {"table": table, 
                            "field": field, 
                            "condition": condition, 
                            "sort": sort, 
                            "top": top,
                            "offset": offset,
                            "is_first": is_first}

            #database aktif_mi add condition
            if self._aktif_mi is not None:
                if condition is None:
                    condition = []
                combiner_number = len(condition)
                if combiner_number>0:
                    condition[combiner_number-1]['combiner'] = 'AND'
                    condition[combiner_number] = {'col': 'aktif_mi', 'operator': '=', 'value': self._aktif_mi}
                else:
                    condition = {0:{'col': 'aktif_mi', 'operator': '=', 'value': self._aktif_mi}}
                
            if cache is True:
                hash_payload = ReaPy.hash().md5(ReaPy.json().dumps(cache_payload))
                
                cache = self.get_cache(hash_payload)
                sys.stdout.flush()
                if cache is not None:
                    try:
                        cache_data = cache[0][1]
                        data_json = ReaPy.json().loads(cache_data)
                        data = data_json['data']
                        count = data['count']
                        self._error = data['error']
                        self._db.coll = None
                        if count:
                            data_len = len(data['result'])
                            if data_len>0:
                                self._data = data['result']
                            else:
                                self._data = None
                        else:
                            self._data = None
                            try:
                                self._error = data.error()
                            except Exception as e:
                                print(e)
                        return self
                    except Exception as e:
                        print(e)

                else:
                    data = self._db.select(table, field, condition, sort, top, offset)
                    if data is None:
                        self._data = None
                        return self
                    if data.error()==False:
                        if is_first is True:
                            if data.count():
                                self._data = data.first()
                            else:
                                self._data = None
                        else: 
                            data_len = len(data.result())
                            sys.stdout.flush()
                            if data_len>0:
                                self._data = data.result()
                            else:
                                self._data = None
                            
                        cache_data = {"result": self.data(), "error": self.error(), "count": data.count()}
                        self.set_cache(hash_payload, cache_data)
                        sys.stdout.flush()
                    else:
                        self._data = None
                        self._error = data.error()
                    return self
            else:
                data = self._db.select(table, field, condition, sort, top, offset)
                if data is None:
                    self._data = None
                    return self
                if data.error()==False:
                    if is_first is True:
                        if data.count():
                            self._data = data.first()
                        else:
                            self._data = None
                    else:
                        data_len = len(data.result())
                        if data_len>0:
                            self._data = data.result()
                        else:
                            self._data = None
                else:
                    self._data = None
                    self._error = data.error()
                return self
                

        except Exception as e:
            print(e)