# Backend

The backend manages the database connection and schema manipulation.

trytond.backend.name

  The name of the backend configured.

## Database

class trytond.backend.Database(name)

  Manage the connection to the named database.

Database.connect()

  Connect to the database and return the instance.

Database.get_connection([autocommit[, readonly]])

  Retrieve a connection object as defined by PEP 249#connection. If autocommit is set, the connection is committed after each statement. If readonly is set, the connection is read only.

Database.put_connection(connection[, close])

  Release the connection. If close is set, the connection is discarded.

Database.close()

  Close all connections.

classmethod Database.create(connection, database_name)

  Create the named database using the connection.

classmethod Database.drop(connection, database_name)

  Drop the named database using the connection.

Database.list([hostname])

  Return a list of Tryton database names. `hostname` filters the databases with the same configured hostname.

Database.init()

  Initialize the database schema.

Database.test([hostname])

  Return if the database is a Tryton database. If `hostname` is set, it checks also if it has the same configured hostname.

Database.nextid(connection, table[, count])

  Return the next `count` IDs for the `table` using the `connection`.

  Note

  It may return `None` for some database.

Database.setnextid(connection, table, value)

  Set the `value` as current ID to the `table` using the `connection`.

Database.currid(connection, table)

  Return the current ID of the `table` using the `connection`.

classmethod Database.lock(connection, table)

  Lock the `table` using the `connection`.

classmethod Database.lock_records(connection, table, ids)

  Lock the record `ids` of the `table` using the `connection`.

Database.lock_id(id[, timeout])

  Return the SQL expression to lock the `id`. Set `timeout` to wait for the lock.

Database.has_constraint(constraint)

  Return if the database handle the `constraint`.

Database.has_returning()

  Return if the database supports `RETURNING` in `INSERT` and `UPDATE`.

Database.has_multirow_insert()

  Return if the database supports `INSERT` of multi-rows.

Database.has_select_for()

  Return if the database supports `FOR UPDATE` and `FOR SHARE` in `SELECT`.

Database.get_select_for_skip_locked()

  Return For class with skip locked.

Database.has_window_functions()

  Return if the database supports window functions.

Database.has_unaccent()

  Return if the database suppport unaccentuated function.

Database.has_unaccent_indexable()

  Return if the database suppport unaccentuated function in index.

Database.unaccent(value)

  Return the SQL expression of unaccentuated `value`.

Database.has_similarity()

  Return if the database suppports similarity function.

Database.similarity(column, value)

  Return the SQL expression that compare the similarity of `column` and `value`.

Database.has_search_full_text()

  Return if the database suppports full text search.

Database.format_full_text(\*documents[, language])

  Return the SQL expression that format the `documents` into text search vectors for the `language`.

  The order of `documents` define the weight for proximity ranking.

Database.format_full_text_query(query[, language])

  Convert the `query` expression into full text query.

Database.search_full_text(document, query)

  Return the SQL expression for searching `document` with the `query`.

Database.rank_full_text(document, query[, normalize])

  Return the SQL expression to rank `document` with the `query`.

classmethod Database.has_sequence()

  Return if the database supports sequence querying and assignation.

Database.sequence_exist(connection, name)

  Return if the named sequence exists using the `connection`.

Database.sequence_create(connection, name[, number_increment[, start_value]])

  Create a named sequence incremented by `number_increment` or `1` and starting at `start_value` or `1` using the `connection`.

Database.sequence_update(connection, name[, number_increment[, start_value]])

  Modify the named sequence with `number_increment` and `start_value` using the `connection`.

Database.sequence_rename(connection, old_name, new_name)

  Rename the sequece from `old_name` to `new_name` using the `connection`.

Database.sequence_delete(connection, name)

  Delete the named sequence using the `connection`.

Database.sequence_next_number(connection, name)

  Return the next number fo the named sequence using the `connection`.

Database.has_channel(connection, name)

  Return if the database supports `LISTEN` and `NOTIFY` on channel.

Database.sql_type(type_)

  Return the namedtuple(‘SQLType’, ‘base type’) corresponding to the SQL `type_`.

Database.sql_format(type_, value)

  Return the `value` casted for the SQL `type_`.

Database.json_get(column[, key])

  Return the JSON value of the JSON `key` from the `column`.

Database.json_key_exists(column, key)

  Return the SQL expression to test if `key` exists in the JSON `column`.

Database.json_any_keys_exist(column, keys)

  Return the SQL expression to test if any `keys` exist in the JSON `column`.

Database.json_all_keys_exist(column, keys)

  Return the SQL expression to test if all `keys` exist in the JSON `column`.

Database.json_contains(column, json)

  Rteurn the SQL expression to test if the JSON `column` contains `json`.

## TableHandler

class trytond.backend.TableHandler(model[, history])

  Handle table for the `model`. If `history` is set, the table is the one storing the history.

TableHandler.namedatalen

  The maximing length of named data for the database.

TableHandler.index_translators

  Contain the `IndexTranslator` for the database.

classmethod TableHandler.table_exist(table_name)

  Return if the named table exists.

classmethod TableHandler.table_rename(old_name, new_name)

  Rename the table from `old_name` to `new_name`.

TableHandler.column_exist(column_name)

  Return if the named column exists.

TableHandler.column_rename(old_name, new_name)

  Rename the column from `old_name` to `new_name`.

TableHandler.alter_size(column_name, column_type)

  Modify the size of the named column using the column type.

TableHandler.alter_type(column_name, column_type)

  Modify the type of the named column.

TableHandler.column_is_type(column_name, type_[, size])

  Return if the column is of type `type_`. If `type` is `VARCHAR`, `size` is also compared except if the value if negative.

TableHandler.db_default(column_name, value)

  Set the default `value` on the named column.

TableHandler.add_column(column_name, abstract_type[, default[, comment]])

  Add the named column of abstract type. The `default` is a method that return the value to fill the new column. `comment` set as comment for the column.

TableHandler.add_fk(column_name, reference[, on_delete])

  Add a foreign key constraint on the named column to target the `reference` table name. `on_delete` defines the method to use when foreign record is deleted.

TableHandler.drop_fk(column_name[, table])

  Drop the foreign key constrant on the named column. `table` can be used to alter another table.

TableHandler.not_null_action(column_name[, action])

  Add or remove `NOT NULL` on the named column.

TableHandler.add_constraint(ident, constraint)

  Add the SQL expression `constraint` as constraint named `ident` on the table.

TableHandler.drop_constraint(ident[, table])

  Drop the named `ident` constraint. `table` can be used to alter another table.

TableHandler.set_indexes(indexes)

  Create the `indexes` if possible and drop others having `idx_` as prefix or `_index` as suffix.

TableHandler.drop_column(column_name)

  Drop the named column.

classmethod TableHandler.drop_table(model, table[, cascade])

  Drop the named `table` and clean `ir.model.data` from the given `model`. Set `cascade` to drop objects that depend on the table.

classmethod TableHandler.convert_name(name[, reserved])

  Convert the data name to be lower than namedatalen minus reserved.

Database.index_translator_for(index)

  Return the best `IndexTranslator` for the given index.

## IndexTranslator

class trytond.backend.IndexTranslator

  Convert an `Index` into a concrete index.

classmethod IndexTranslator.definition(index)

  Return the name, SQL query and parameters to create the `index`.

classmethod IndexTranslator.score(index)

  Return a score related to the fitness of using this translator for the `index`. A score of `0` means that the translator is unsuited for the requested usage.

## Exceptions

exception trytond.backend.DatabaseIntegrityError

  Exception raised when the relational integrity of the database is affected.

exception trytond.backend.DatabaseDataError

  Exception raised for errors that are due to problems with the processed data.

exception trytond.backend.DatabaseOperationalError

  Exception raised for errors that are related to the database’s operation.