postgres added
This commit is contained in:
@@ -27,22 +27,38 @@ class RedisRow:
|
||||
|
||||
@classmethod
|
||||
def regex(cls, list_keys: List[Union[str, bytes]]) -> str:
|
||||
"""Generate Redis search pattern from list of keys."""
|
||||
search_regex = ""
|
||||
for key, list_key in enumerate(list_keys):
|
||||
if not list_key:
|
||||
continue
|
||||
"""Generate Redis search pattern from list of keys.
|
||||
|
||||
list_key = (
|
||||
list_key.decode() if isinstance(list_key, bytes) else str(list_key)
|
||||
)
|
||||
if key == 0:
|
||||
search_regex += f"{list_key}{cls.delimiter}*"
|
||||
elif key == len(list_keys) - 1:
|
||||
search_regex += f"*{cls.delimiter}{list_key}"
|
||||
else:
|
||||
search_regex += f"*{cls.delimiter}{list_key}{cls.delimiter}*"
|
||||
return search_regex
|
||||
Example:
|
||||
list_keys = [None, "example1", "example2"]
|
||||
Result: "*:example1:example2"
|
||||
"""
|
||||
# First create string with dash separators
|
||||
temp_str = "-"
|
||||
for list_key in list_keys:
|
||||
if list_key:
|
||||
list_key = (
|
||||
list_key.decode() if isinstance(list_key, bytes) else str(list_key)
|
||||
)
|
||||
temp_str += f"{list_key}-"
|
||||
|
||||
# Remove redundant dashes
|
||||
temp_str = temp_str.strip("-")
|
||||
|
||||
# If no valid keys, return empty string
|
||||
if not temp_str:
|
||||
return ""
|
||||
|
||||
# Replace dashes with delimiter
|
||||
result = temp_str.replace("-", cls.delimiter)
|
||||
|
||||
# Add wildcard at start if first item was None
|
||||
if list_keys and list_keys[0] is None:
|
||||
result = f"*{cls.delimiter}{result}"
|
||||
else:
|
||||
result = f"{result}"
|
||||
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def parse(cls) -> List[str]:
|
||||
|
||||
@@ -25,20 +25,26 @@ class RedisResponse:
|
||||
self.error = error
|
||||
|
||||
def as_dict(self) -> Dict:
|
||||
return {
|
||||
data = self.all
|
||||
main_dict = {
|
||||
"status": self.status,
|
||||
"message": self.message,
|
||||
"data": self.data,
|
||||
"count": self.count,
|
||||
"dataType": self.data_type,
|
||||
"error": self.error,
|
||||
}
|
||||
if isinstance(data, RedisRow):
|
||||
return {"data": {data.keys: data.data}, **main_dict}
|
||||
elif isinstance(data, list):
|
||||
return {"data": {row.keys: row.data for row in data}, **main_dict}
|
||||
|
||||
@property
|
||||
def all(self) -> Union[Optional[List[RedisRow]]]:
|
||||
return self.data
|
||||
return self.data or []
|
||||
|
||||
@property
|
||||
def count(self) -> int:
|
||||
return len(self.all)
|
||||
|
||||
@property
|
||||
def first(self) -> Union[RedisRow, None]:
|
||||
if self.data:
|
||||
return self.data[0]
|
||||
return None
|
||||
return self.data[0] if self.data else None
|
||||
|
||||
Reference in New Issue
Block a user