More documentation¶
The PySQLXEngine has more some methods to you can use the database more easily. See the documentation below:
-
.connect()
create connection with db. -
.close()
disconnected from db. -
.is_healthy()
check if the connection is healthy. -
.requires_isolation_first()
this is used to determine if the connection should be isolated before executing a sql. -
.raw_cmd()
run a command in the database, for queries that can't be run using prepared statements. -
.query*()
to return actual records (for example, using SELECT). -
.execute()
to return a count of affected rows (for example, after an INSERT, UPDATE or DELETE). -
.set_isolation_level()
the isolation level is set before the transaction is started. Is used to separate the transaction per level. -
.begin()
starts a transaction. -
.commit()
commits a transaction. -
.rollback()
rollbacks a transaction. -
.start_transaction()
starts a transaction with BEGIN/BEGIN TRANSACTION. By default, does not set the isolation level. But is possible to set the isolation level using the parameterisolation_level
.
Parameters Helper¶
The PySQLXEngine has five methods that accept the parameters with sql.
Parameters are passed to the database with the SQL query.
The parameters are used to avoid SQL injection and to make the code more secure.
SQL with parameters syntax¶
- SQL
- Parameters
Parameters¶
- dict
key
must be a valid string. - dict
value
can be one of the types:bool
bytes
date
datetime
Decimal
dict
float
int
list
str
time
tuple
UUID
None
Python types vs SQL types¶
Python | Databases |
---|---|
bool | bool/bit/boolean/tinyint/etc. |
bytes | bytea/binary/varbinary/blob/etc. |
date | date/nvarchar/varchar/string/etc. |
datetime | timestamp/timestamptz/datetime/datetime2/nvarchar/varchar/string/etc. |
Decimal | decimal/numeric/etc. |
dict | json/jsonb/nvarchar/varchar/string/etc. |
float | float/real/numeric/etc. |
int | int/integer/smallint/bigint/tinyint/etc. |
list | json/jsonb/nvarchar/varchar/string/etc. |
str | varchar/text/nvarchar/char/etc. |
time | time/nvarchar/varchar/string/etc. |
tuple | array(Postgres Native), error for other databases. |
UUID | uuid/varchar/text/nvarchar/etc. |
None | null. |
Methods¶
connect¶
Description¶
Each connection instance is lazy; only after .connect()
is the database checked and the connection established.
When you use with/async with
the connection is automatically opened and closed.
Example
PySQLXEngine
also supports with/async with
, where the connection is automatically opened and closed.
Helper¶
-
Arguments:
None
-
Returns:
None
-
Raises:
ConnectError
Example¶
close¶
Description¶
It's a good idea to close the connection, but PySQLXEngine has the core in Rust; closing is automatic when your code leaves the context.
Even if you don't close the connection, don't worry; when the process ends automatically, the connections will be closed, so the database doesn't have an idle connection.
Helper¶
-
Arguments:
None
-
Returns:
None
-
Raises:
None
Example¶
is_healthy¶
Description¶
Check if the connection is healthy.
Returns false
, if connection is considered to not be in a working state.
Helper¶
-
Arguments:
None
-
Returns:
bool
-
Raises:
None
requires_isolation_first¶
Description¶
Returns True
if the connection requires isolation first, False
otherwise.
This is used to determine if the connection should be isolated before executing a query.
For example, SQL Server requires isolation before executing a statement using begin in some cases.
Signals if the isolation level
SET needs to happen before or after the BEGIN
.
Helper¶
-
Arguments:
None
-
Returns:
bool
-
Raises:
None
Extra documentation:¶
raw_cmd¶
Description¶
Run a command in the database, for queries that can't be run using prepared statements(queries/execute/etc).
Helper¶
-
Arguments:
sql(str)
: sql to be executed.
-
Returns:
None
-
Raises:
RawCmdError
Example¶
query¶
Description¶
Returns all rows from query result as BaseRow list
, MyModel list
or empty list
.
Helper¶
-
Arguments:
-
sql(str)
: sql query to be executed. -
parameters(dict)
: (default is None) dictionary with the name of the parameter and the value. -
model(BaseRow)
: (default is None) is your model that inherits from BaseRow.
-
-
Returns:
-
List[BaseRow]
: default if you don't pass a model. -
List[MyModel]
: If you pass a model. -
List
: If don't have rows.
-
-
Raises:
QueryError
TypeError
ParameterInvalidProviderError
ParameterInvalidValueError
ParameterInvalidJsonValueError
query_first¶
Description¶
Returns first row from query result as BaseRow
, MyModel
or None
.
Helper¶
-
Arguments:
-
sql(str)
: sql query to be executed. -
parameters(dict)
: (default is None) parameters must be a dictionary with the name of the parameter and the value. -
model(BaseRow)
: (default is None) is your model that inherits from BaseRow.
-
-
Returns:
BaseRow
: default if you don't pass a model.MyModel
: If you pass a model.None
: if no rows are found.
-
Raises:
QueryError
TypeError
ParameterInvalidProviderError
ParameterInvalidValueError
ParameterInvalidJsonValueError
Example¶
query_as_dict¶
Description¶
Returns all rows from query result as dict list
or empty list
.
Helper¶
-
Arguments:
-
sql(str)
: sql query to be executed. -
parameters(dict)
: (default is None) dictionary with the name of the parameter and the value.
-
-
Returns:
-
List[Dict[str, Any]]
: dict list. -
List
: empty list.
-
-
Raises:
QueryError
TypeError
ParameterInvalidProviderError
ParameterInvalidValueError
ParameterInvalidJsonValueError
Example¶
query_first_as_dict¶
Description¶
Returns first row from query result as dict
or None
.
Helper¶
-
Arguments:
-
sql(str)
: sql query to be executed. -
parameters(dict)
: (default is None) parameters must be a dictionary with the name of the parameter and the value.
-
-
Returns:
-
Dict[str, Any]
: row as dict. -
None
: if no rows are found.
-
-
Raises:
QueryError
TypeError
ParameterInvalidProviderError
ParameterInvalidValueError
ParameterInvalidJsonValueError
Example¶
execute¶
Description¶
Executes a query/sql and returns the number of rows affected.
Helper¶
-
Arguments:
-
sql(str)
: sql to be executed. -
parameters(dict)
: (Default is None) dictionary with the name of the parameter and the value.
-
-
Returns:
int
: number of rows affected.
-
Raises:
ExecuteError
TypeError
ParameterInvalidProviderError
ParameterInvalidValueError
ParameterInvalidJsonValueError
Example¶
set_isolation_level¶
Description¶
Sets the isolation level of the connection.
The isolation level is set before the transaction is started. Is used to separate the transaction per level.
The Snapshot
isolation level is supported by MS SQL Server.
The Sqlite does not support the isolation level.
Helper¶
-
Arguments:
isolation_level(str)
: isolation level to be set (ReadUncommitted
,ReadCommitted
,RepeatableRead
,Snapshot
,Serializable
)
-
Returns:
None
-
Raises:
IsolationLevelError
ValueError
Example¶
Isolation Level Help¶
begin¶
Description¶
Starts a transaction using BEGIN
. begin()
is equivalent to start_transaction()
without setting the isolation level.
Helper¶
-
Arguments:
None
-
Returns:
None
-
Raises:
RawCmdError
Example¶
commit¶
Description¶
Commits the current transaction.
The begin()
method must be called before calling commit()
.
If the database not need set the isolation level, maybe you can not use begin()
and commit()
.
The PySQLXEngine by default uses the begin()
and commit()
in all transactions.
Helper¶
* Arguments: ``None`
* Returns: ``None``
* Raises: ``RawCmdError``
Example¶
rollback¶
Description¶
Rollbacks the current transaction.
Rollback is used to cancel the transaction, when you uses the rollback, the transaction is canceled and the changes are not saved.
The begin()
method must be called before calling rollback()
.
If the database not need set the isolation level, maybe you can not use begin()
and rollback()
.
The PySQLXEngine by default try uses the begin()
and commit()
in all transactions.
Helper¶
-
Arguments:
None
-
Returns:
None
-
Raises:
RawCmdError
Example¶
start_transaction¶
Description¶
Starts a transaction with BEGIN/BEGIN TRANSACTION
. by default, does not set the isolation level.
The Snapshot
isolation level is supported by MS SQL Server.
The Sqlite does not support the isolation level.
Helper¶
-
Arguments:
isolation_level(str)
: by default is None. Isolation level to be set (ReadUncommitted
,ReadCommitted
,RepeatableRead
,Snapshot
,Serializable
)
-
Returns:
None
-
Raises:
IsolationLevelError
StartTransactionError
ValueError