MySQL Shell API 9.0.1
Unified development interface for MySQL Products
Methods | Properties | List of all members
ClassicSession Class Reference

Enables interaction with a MySQL Server using the MySQL Protocol. More...

Methods

void close () override
 Closes the internal connection to the MySQL Server held on this session object.
 
void start_transaction () override
 Starts a transaction context on the server. More...
 
void commit () override
 Commits all the operations executed after a call to start_transaction(). More...
 
void rollback () override
 Discards all the operations executed after a call to start_transaction(). More...
 
str get_uri ()
 Retrieves the URI for the current session. More...
 
str get_ssh_uri ()
 Retrieves the SSH URI for the current session. More...
 
str get_connection_id ()
 Retrieves the connection id for the current session. More...
 
ClassicResult run_sql (str query, list args=[])
 Executes a query and returns the corresponding ClassicResult object. More...
 
bool is_open ()
 Returns true if session is known to be open. More...
 
None set_query_attributes (dict attributes)
 Defines query attributes that apply to the next statement sent to the server for execution. More...
 

Properties

str uri
 Same as get_uri()
 
str ssh_uri
 Same as get_ssh_uri()
 
int connection_id
 Same as get_connection_id()
 

Detailed Description

Enables interaction with a MySQL Server using the MySQL Protocol.

Provides facilities to execute queries.

# Begins a transaction
classicSession.start_transaction()
# Inserts some records
res1 = classicSession.run_sql('insert into sample values ("john")')
res2 = classicSession.run_sql('insert into sample values ("carol")')
res3 = classicSession.run_sql('insert into sample values ("jack")')
# Commits the transaction
classicSession.commit()

Member Function Documentation

◆ start_transaction()

void start_transaction ( )
override

Starts a transaction context on the server.

Calling this function will turn off the autocommit mode on the server.

All the operations executed after calling this function will take place only when commit() is called.

All the operations executed after calling this function, will be discarded if rollback() is called.

When commit() or rollback() are called, the server autocommit mode will return back to it's state before calling start_transaction().

◆ commit()

void commit ( )
override

Commits all the operations executed after a call to start_transaction().

All the operations executed after calling start_transaction() will take place when this function is called.

The server autocommit mode will return back to it's state before calling start_transaction().

◆ rollback()

void rollback ( )
override

Discards all the operations executed after a call to start_transaction().

All the operations executed after calling start_transaction() will be discarded when this function is called.

The server autocommit mode will return back to it's state before calling start_transaction().

◆ get_uri()

str get_uri ( )

Retrieves the URI for the current session.

Returns
A string representing the connection data.

◆ get_ssh_uri()

str get_ssh_uri ( )

Retrieves the SSH URI for the current session.

Returns
A string representing the SSH connection data.

◆ get_connection_id()

int get_connection_id ( )

Retrieves the connection id for the current session.

Returns
An integer value representing the connection id.

◆ run_sql()

ClassicResult run_sql ( str  query,
list  args = [] 
)

Executes a query and returns the corresponding ClassicResult object.

Parameters
querythe SQL query to execute against the database.
argsOptional list of literals to use when replacing ? placeholders in the query string.
Returns
A ClassicResult object.
Exceptions
LogicErrorif there's no open session.
ArgumentErrorif the parameters are invalid.

◆ is_open()

bool is_open ( )

Returns true if session is known to be open.

Returns
A boolean value indicating if the session is still open.

Returns true if the session is still open and false otherwise.

Note
This function may return true if connection is lost.

◆ set_query_attributes()

None set_query_attributes ( dict  attributes)

Defines query attributes that apply to the next statement sent to the server for execution.

It is possible to define up to 32 pairs of attribute and values for the next executed SQL statement.

To access query attributes within SQL statements for which attributes have been defined, the query_attributes component must be installed, this component implements a mysql_query_attribute_string() loadable function that takes an attribute name argument and returns the attribute value as a string, or NULL if the attribute does not exist.

To install the query_attributes component execute the following statement:

session.run_sql('INSTALL COMPONENT "file://component_query_attributes"');