xia_engine_mysql.engine.MysqlEngine
- class xia_engine_mysql.engine.MysqlEngine
Bases:
SqlEngine
SQLite Engine
- Connection Parameters:
- Should be hold in the class attribute class._sqlite = {“db”: “”, “path”: “”}
db: Database name, default database name is “”
kwarg: Where the database should be stored, default is in memory
- __init__()
Methods
__init__
()connect
([document_class])Connect to the engine
create
(document_class, db_content)Create a document
create_table
(table_name, field_dict, ...)Create table
delete
(document_class, doc_id)Delete a document by using id
drop
(document_class)Drop the given collection
get
(document_class, doc_id)Get Document
get_connection
([document_class])Get engine connection。 Always using existed one when it is possible
get_decoder
(field)Get Decoder for a field
get_encoder
(field)Get Encoder for a field
get_search_sql
(data_model, **kwargs)Get SQL to be executed by using provided data model
parse_content
(data_content, data_model, ...)Parse database result to db content (json format)
parse_data
(db_content, data_model, ...)Split database content to multiple table update instruction
parse_search_option
(key)Reference to search method for the specifications
parse_update_option
(key)Reference to update method for the specifications
search
(document_class, *args, **kwargs)It is a write-only engine, we don't support any search activities
set
(document_class, doc_id, db_content)Overwrite whole document
truncate
(document_class)Remove all data from the given collection
update
(document_class, doc_id, **kwargs)Update a document
Attributes
Use to generate the field name in embedded documents
Use to generate ID by concatenate keys
search options
order options
Use to separate the table name of embedded documents
update options
add_column_sql_template
create_sql_template
Engine specific value decoder
delete_sql_template
drop_sql_template
Engine specific value encoder
Connector use an object to handler parameters
if the value is False, each data model will have its own connector by default
default connection parameter
Engine parameter name
Scope check will be performed at engine level (pass document level check)
Unique check is commonly applied in SQL Engine
insert_sql_template
Left quote for identifier
paramstyle
Left quote for identifier
select_sql_template
truncate_sql_template
type_dict
- FIELD_SEPARATOR = '_'
Use to generate the field name in embedded documents
- KEY_SEPARATOR = '|'
Use to generate ID by concatenate keys
- OPERATORS = {'__eq__': '=', '__ge__': '>=', '__gt__': '>', '__in__': 'IN', '__le__': '<=', '__lt__': '<', '__ne__': '!=', '__not_in__': 'NOT IN'}
search options
- ORDER_TYPES = {'__asc__': 'asc', '__desc__': 'desc'}
order options
- TABLE_NAME_SEPARATOR = '_'
Use to separate the table name of embedded documents
- UPDATE_TYPES = {'__append__': 'append', '__delete__': 'delete', '__remove__': 'remove'}
update options
- classmethod connect(document_class: Optional[Type[BaseDocument]] = None)
Connect to the engine
- Parameters
document_class – (subclass of BaseDocument): Document definition
- Returns
Connection
- classmethod create(document_class: Type[BaseDocument], db_content: dict)
Create a document
- Parameters
document_class (subclass of BaseDocument) – Document definition
db_content (dict) – content to be put to engine
- Returns
Document ID
- classmethod create_table(table_name: str, field_dict: dict, document_class: Type[BaseDocument])
Create table
- Parameters
table_name – table name to be created
field_dict – dictionary of field information
document_class – document_class
- decoders = {<class 'xia_fields.fields.DecimalField'>: <function MysqlEngine.<lambda>>, <class 'xia_fields.fields_ext.TimestampField'>: <function MysqlEngine.<lambda>>, <class 'xia_fields.fields_ext.TimeField'>: <function MysqlEngine.<lambda>>}
Engine specific value decoder
- classmethod delete(document_class: Type[BaseDocument], doc_id: str)
Delete a document by using id
- Parameters
document_class (subclass of BaseDocument) – Document definition
doc_id – Document ID
- classmethod drop(document_class: Type[BaseDocument])
Drop the given collection
- Parameters
document_class (subclass of BaseDocument) – Document definition
- encoders = {<class 'xia_fields.fields.DecimalField'>: <function MysqlEngine.<lambda>>, <class 'xia_fields.fields_ext.DateField'>: <function MysqlEngine.<lambda>>, <class 'xia_fields.fields_ext.TimestampField'>: <function MysqlEngine.<lambda>>, <class 'xia_fields.fields_ext.DateTimeField'>: <function MysqlEngine.<lambda>>, <class 'xia_fields.fields_ext.TimeField'>: <function MysqlEngine.<lambda>>}
Engine specific value encoder
- engine_connector
alias of
Connection
- engine_connector_object = None
Connector use an object to handler parameters
if the value is False, each data model will have its own connector by default
- engine_default_connector_param: dict = {}
default connection parameter
- engine_param: str = 'mysql'
Engine parameter name
- engine_scope_check = False
Scope check will be performed at engine level (pass document level check)
- engine_unique_check = True
Unique check is commonly applied in SQL Engine
- classmethod get(document_class: Type[BaseDocument], doc_id: str)
Get Document
- Parameters
document_class (subclass of BaseDocument) – Document definition
doc_id – Document ID
- Returns
Document content on python dict
- classmethod get_connection(document_class: Optional[Type[BaseDocument]] = None)
Get engine connection。 Always using existed one when it is possible
- Parameters
document_class – (subclass of BaseDocument): Document definition
- Returns
Connection
- classmethod get_decoder(field: type) callable
Get Decoder for a field
- Parameters
field (type) – class type of field class
- Returns
Decoder function
- classmethod get_encoder(field: type) callable
Get Encoder for a field
- Parameters
field (type) – class type of field class
- Returns
Encoder function
- classmethod get_search_sql(data_model: dict, **kwargs)
Get SQL to be executed by using provided data model
- Parameters
data_model – Dictionary
**kwargs – Search criteria
- Returns
SQL statement and the values to be put in the place holder
- lq = '`'
Left quote for identifier
- classmethod parse_content(data_content: dict, data_model: dict, key_values: dict)
Parse database result to db content (json format)
- Parameters
data_content – dictionary of entries for each table
data_model – Parsed data model
key_values – Key value to be filtered
- Result:
db form of data
- classmethod parse_data(db_content: dict, data_model: dict, key_value: dict, data_content: dict)
Split database content to multiple table update instruction
- Parameters
db_content – Data base content of document
data_model – Parsed data model
key_value – The key value should be added
data_content – Data content to be added
- Returns
Parsed data content
- classmethod parse_search_option(key: str)
Reference to search method for the specifications
- Parameters
key (str) –
- Returns
key, operator, order
- classmethod parse_update_option(key: str)
Reference to update method for the specifications
- Parameters
key (str) –
- Returns
key, update
- rq = '`'
Left quote for identifier
- classmethod search(document_class: Type[BaseDocument], *args, **kwargs)
It is a write-only engine, we don’t support any search activities
- classmethod set(document_class: Type[BaseDocument], doc_id: str, db_content: dict) str
Overwrite whole document
- Parameters
document_class (subclass of BaseDocument) – Document definition
doc_id – Document ID
db_content – content to be put to engine
- Returns
Document ID
- classmethod truncate(document_class: Type[BaseDocument])
Remove all data from the given collection
- Parameters
document_class (subclass of BaseDocument) – Document definition
- classmethod update(document_class: Type[BaseDocument], doc_id: str, **kwargs) dict
Update a document
- Parameters
document_class (subclass of BaseDocument) – Document definition
doc_id (str) – Document ID
**kwargs – Named keyword for update
- Returns
Updated data
- Notes for delete string:
embedded update: a__b means b component of a. a.b means the key’s name is a.b
- operators: key is end with __op__. The following op are supported:
__append__: Append an item to array
__remove__: Remove an item
__delete__: Delete the field