CassandraTable¶
Inherits: primeight.base.CassandraBase
The CassandraTable
manages table interactions.
Import¶
from primeight import CassandraTable
Constructor¶
- config
dict
[Required]: Table configuration, as returned by one of the parsers. - query_name
primeight.keyspace.CassandraKeyspace
[Required]: Keyspace. - cassandra_manager
primeight.manager.CassandraManager
(Default:None
): Cassandra manager.
Attributes¶
name¶
Type: str
Table name.
keyspace¶
Type: primeight.CassandraKeyspace
Keyspace passed during object initialization.
columns¶
Type: List[primeight.CassandraColumn]
List of columns.
col¶
Type: Dict[str, primeight.CassandraColumn]
Dictionary of columns, maping column name to the column itself.
model¶
Type: pydantic.BaseModel
Pydantic model representing the table.
Methods¶
get_columns¶
Filter list of columns by list of names and/or alias, working as a white list.
If names
is None
and alias
is None
, all columns are returned.
Parameters:
- names
List[str]
(Default:None
): List of names. - alias
List[str]
(Default:None
): List of alias.
Return: List[primeight.CassandraColumn]
get¶
Get column by name.
If there is no column with the specified name, None
is returned.
Parameters:
- name
str
[Required]: Column name.
Return: Optional[CassandraColumn]
has_split¶
Whether a table has a temporal split or not.
Return: bool
create¶
Build table CREATE
statement(s).
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- keyspace
str or List[str]
(Default:None
): Keyspace name(s). If aList[str]
is passed it will create multiple statements, one for each name. If set toNone
,CassandraMaterializedView.keyspace
is used instead. - gc_grace_seconds
int
(Default:86400
): Grace period in seconds. Time to wait before garbage collecting tombstones (deletion markers). - if_not_exists
bool
(Default:False
): IfTrue
addsIF NOT EXISTS
option to the statement(s). Attempting to create an already existing materialized view will return an error unless theIF NOT EXISTS
option is used. If it is used, the statement will be a no-op if the materialized view already exists. - create_materialized_views
bool
(Default:True
): Whether to create the materialized viewCREATE
statement(s) after the table statement(s).
Return: self
drop¶
Build table DROP
statement(s).
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- keyspace
str or List[str]
(Default:None
): Keyspace name(s). If aList[str]
is passed it will create multiple statements, one for each name. If set toNone
,CassandraMaterializedView.keyspace
is used instead. - if_exists
bool
(Default:False
): IfTrue
addsIF EXISTS
option to the statement(s). If the materialized view(s) do not exist, the statement will return an error, unlessIF EXISTS
is used in which case the operation is a no-op. - drop_materialized_views
bool
(Default:True
): Whether to create the materialized viewDROP
statement(s) before the table statement(s).
Return: self
insert¶
Build table INSERT
statement.
When Cassandra does an insert and the row already exists, it overwrites the current column values.
Also, for Cassandra, None
row values are considered as empty,
meaning that if you overwrite a column with a None
value
it will delete that row column.
This means that you need to be careful whenever making inserts,
so that you do not delete any undesired columns.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- row
Dict[str, Any]
[Required]: Row. - keyspace
str or List[str]
(Default:None
): Keyspace name(s). If aList[str]
is passed it will create multiple statements, one for each name. If set toNone
,CassandraMaterializedView.keyspace
is used instead. - ttl
bool
(Default:False
): Specifies an optional Time To Live in seconds for the inserted values. If set, the inserted values are automatically removed from the database after the specified time. Note that the TTL concerns the inserted values, not the columns themselves. This means that any subsequent update of the column will also reset the TTL (to whatever TTL is specified in that update). By default, values never expire. A TTL of 0 is equivalent to no TTL. If the table has a default_time_to_live, a TTL of 0 will remove the TTL for the inserted or updated values. A TTL of null is equivalent to inserting with a TTL of 0.
Return: self
query¶
Build table or materialized view SELECT
statement(s), depending on the query name
selected.
This method can be chained with the methods required to define restrictions,
and ending with the CassandraTable.execute
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- name
str
(Default:'base'
): Query name. - keyspace
str or List[str]
(Default:None
): Keyspace name(s). If aList[str]
is passed it will create multiple statements, one for each name. If set toNone
,CassandraTable.keyspace
is used instead.
Return: self
select¶
Filter columns of SELECT
statement(s), works as a white list.
If not called, all columns will be returned.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- columns
List[str]
(Default:'base'
): List of column names.
Return: self
time¶
Complement SELECT
statement(s), with time information.
This method complements the table split
and ,
if split_only
is False
, adds the where clause
for the time
column specified in the table configuration.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- start
datetime.datetime
[Required]: Start date. - end
datetime.datetime
[Required]: End date. - prepare
bool
(Default:False
): Whether to create statement ready to be a prepared statement. If set toTrue
, will only define tablesplit
and will leave the required key as a prepared statement parameter. - split_only
bool
(Default:False
): Whether to only complement the tablesplit
as specified in the table configuration.
Return: self
space¶
Complement SELECT
statement(s), with space information.
This method adds the where clause for the space
column specified in the table configuration.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- identifier
str or List[str]
(Default:None
): Space identifier or list of identifiers. If set toNone
, will leave the required key as a prepared statement parameter.
Return: self
id¶
Complement SELECT
statement(s), with identifier information.
This method adds the where clause for the id
column specified in the table configuration.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- identifier
str or List[str]
(Default:None
): Identifier or list of identifiers. If set toNone
, will leave the required key as a prepared statement parameter.
Return: self
equals¶
Complement SELECT
statement(s), with an equality where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - value
Any
(Default:None
): Value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
among¶
Complement SELECT
statement(s), with an IN
where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - value
List[Any]
(Default:None
): List of values. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
between¶
Complement SELECT
statement(s), with a range where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - lower
Any
(Default:None
): Lower value. If set toNone
, will leave the column as a prepared statement parameter. - higher
Any
(Default:None
): Higher value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
between_including¶
Complement SELECT
statement(s), with an inclusive range where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - lower
Any
(Default:None
): Lower value. If set toNone
, will leave the column as a prepared statement parameter. - higher
Any
(Default:None
): Higher value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
lower_than¶
Complement SELECT
statement(s), with a lower than inequality where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - boundary
Any
(Default:None
): Value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
lower_or_equal_than¶
Complement SELECT
statement(s), with a lower or equal than inequality where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - boundary
Any
(Default:None
): Value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
higher_than¶
Complement SELECT
statement(s), with a higher than inequality where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - boundary
Any
(Default:None
): Value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
higher_or_equal_than¶
Complement SELECT
statement(s), with a higher or equal than inequality where clause.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- column
str
[Required]: Column name. - boundary
Any
(Default:None
): Value. If set toNone
, will leave the column as a prepared statement parameter.
Return: self
limit¶
Complement SELECT
statement(s), with a LIMIT
clause.
The LIMIT
option to a SELECT
statement limits the number of rows returned by a query.
This method must be chained after the CassandraTable.query
method.
Note
The statement is only executed using the execute methods (e.g CassandraTable.execute
).
Parameters:
- value
int
[Required]: Limiting value.
Return: self