-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
70 lines (54 loc) · 2.24 KB
/
query.py
File metadata and controls
70 lines (54 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from abc import ABC
from itertools import cycle
from typing import Tuple
from gemini_python import CqlDto, Operation
from gemini_python.history_store import HistoryStore
from gemini_python.schema import Table
class QueryGenerator(ABC):
"""Base class for CQL queries generators."""
def __init__(self, table: Table) -> None:
self._table = table
def __iter__(self) -> "QueryGenerator":
return self
def __next__(self) -> Tuple[Operation, CqlDto]:
pass
class InsertQueryGenerator(QueryGenerator):
"""Basic insert query with all table columns."""
def __init__(self, table: Table, partitions: list[tuple]) -> None:
super().__init__(table)
self._partitions = cycle(partitions)
self._stmt = (
f"insert into {table.keyspace_name}.{table.name} "
f"({', '.join([col.name for col in self._table.all_columns])}) "
f"VALUES ({','.join('?'*len(self._table.all_columns))})"
)
def __iter__(self) -> "QueryGenerator":
return self
def __next__(self) -> Tuple[Operation, CqlDto]:
return Operation.WRITE, CqlDto(
self._stmt,
next(self._partitions)
+ tuple(
column.generate_random_value()
for column in self._table.clustering_keys + self._table.columns
),
)
class SelectQueryGenerator(QueryGenerator):
"""Basic select query with all table columns."""
def __init__(self, table: Table, partitions: list[tuple], history_store: HistoryStore) -> None:
super().__init__(table)
# todo: we may want to use random here instead of cycling
self.history_store = history_store
self._partitions = cycle(partitions)
self._stmt = (
f"select {', '.join(col.name for col in self._table.all_columns)}"
f" from {table.keyspace_name}.{table.name} "
f"where {' AND '.join([col.name + '=?' for col in self._table.partition_keys + self._table.clustering_keys])}"
)
def __iter__(self) -> "QueryGenerator":
return self
def __next__(self) -> Tuple[Operation, CqlDto]:
return Operation.READ, CqlDto(
self._stmt,
self.history_store.get_random_row(),
)