Files
tryton/trytond/backend/database.py
T

257 lines
6.8 KiB
Python
Raw Normal View History

2014-12-02 12:54:01 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from collections import namedtuple
from sql import For
2009-02-04 15:29:28 +01:00
DatabaseIntegrityError = None
2009-02-06 10:26:38 +01:00
DatabaseOperationalError = None
2009-02-04 15:29:28 +01:00
SQLType = namedtuple('SQLType', 'base type')
2012-04-01 23:08:15 +02:00
2009-02-04 15:29:28 +01:00
class DatabaseInterface(object):
'''
Define generic interface for database connection
'''
2013-09-06 22:32:37 +02:00
flavor = None
IN_MAX = 1000
2009-02-04 15:29:28 +01:00
def __new__(cls, name=''):
2009-04-08 17:55:24 +02:00
return object.__new__(cls)
def __init__(self, name=''):
self.name = name
2009-02-04 15:29:28 +01:00
def connect(self):
'''
Connect to the database
:return: the database
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
def get_connection(self, autocommit, readonly=False):
'''Retrieve a connection on the database
2009-02-04 15:29:28 +01:00
:param autocommit: a boolean to activate autocommit
:param readonly: a boolean to specify if the transaction is readonly
'''
raise NotImplementedError
def put_connection(self, connection, close=False):
'''Release the connection
:param close: if close is True the connection is discarded
2009-02-04 15:29:28 +01:00
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
def close(self):
'''
Close all connection
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
@classmethod
def create(cls, connection, database_name):
2009-02-04 15:29:28 +01:00
'''
Create a database
:param connection: the connection to the database
:param database_name: the new database name
2009-02-04 15:29:28 +01:00
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
def drop(self, connection, database_name):
2009-02-04 15:29:28 +01:00
'''
Drop a database
:param connection: the connection to the database
2009-02-04 15:29:28 +01:00
:param database_name: the database name
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
2018-05-22 23:42:35 +02:00
def list(self, hostname=None):
2009-02-04 15:29:28 +01:00
'''
Get the list of database
:return: a list of database name
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
def init(self):
2009-02-04 15:29:28 +01:00
'''
Initialize a database
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
2018-05-22 23:42:35 +02:00
def test(self, hostname=None):
2009-02-04 15:29:28 +01:00
'''
Test if it is a Tryton database.
'''
raise NotImplementedError
2009-07-07 19:32:31 +02:00
def nextid(self, connection, table):
2009-07-07 19:32:31 +02:00
'''
Return the next sequenced id for a table.
:param connection: a connection on the database
2009-07-07 19:32:31 +02:00
:param table: the table name
:return: an integer
'''
def setnextid(self, connection, table, value):
2009-07-07 19:32:31 +02:00
'''
Set the current sequenced id for a table.
:param connection: a connection on the database
2009-07-07 19:32:31 +02:00
:param table: the table name
'''
def currid(self, connection, table):
2009-07-07 19:32:31 +02:00
'''
Return the current sequenced id for a table.
:param connection: a connection on the database
2009-07-07 19:32:31 +02:00
:param table: the table name
:return: an integer
'''
def update_auto_increment(self, connection, table, value):
2009-07-07 19:32:31 +02:00
'''
Update auto_increment value of table
2009-07-07 19:32:31 +02:00
:param connection: a connection on the database
:param table: the table name
:param value: the auto_increment value
2009-07-07 19:32:31 +02:00
'''
pass
2009-07-07 19:32:31 +02:00
@classmethod
def lock(cls, connection, table):
2009-07-07 19:32:31 +02:00
'''
2010-02-25 16:43:04 +01:00
Lock the table
2009-07-07 19:32:31 +02:00
:param connection: a connection on the database
2010-02-25 16:43:04 +01:00
:param table: the table name
2009-07-07 19:32:31 +02:00
'''
raise NotImplementedError
2018-07-27 09:32:28 +02:00
def lock_id(self, id, timeout=None):
"""Return SQL function to lock resource"""
raise NotImplementedError
2018-03-19 16:57:49 +01:00
def has_constraint(self, constraint):
'''
Return True if database handle constraint.
:return: a boolean
'''
raise NotImplementedError
2009-09-07 15:18:17 +02:00
2011-08-21 10:20:23 +02:00
def has_returning(self):
'''
Return True if database implements RETURNING clause in INSERT or UPDATE
statements.
:return: a boolean
'''
return False
2015-07-28 08:04:29 +02:00
def has_multirow_insert(self):
'Return True if database supports multirow insert'
return False
2017-01-25 23:21:57 +01:00
def has_select_for(self):
"Return if database supports FOR UPDATE/SHARE clause in SELECT."
return False
2017-03-22 10:49:26 +01:00
def get_select_for_skip_locked(self):
"Return For class with skip locked"
return For
2017-03-22 10:49:26 +01:00
def has_window_functions(self):
"Return if database supports window functions."
return False
def has_unaccent(self):
"Return if database supports unaccentuated searches"
return False
def unaccent(self, value):
"Return the expression to use for unaccentuated columns"
return value
@classmethod
def has_sequence(cls):
"Return if database supports sequence querying and assignation"
return False
def sequence_exist(self, connection, name):
"Return if a sequence exists"
if not self.has_sequence():
return
raise NotImplementedError
def sequence_create(
self, connection, name, number_increment=1, start_value=1):
"Creates a sequence"
if not self.has_sequence():
return
raise NotImplementedError
def sequence_update(
self, connection, name, number_increment=1, start_value=1):
"Modifies a sequence"
if not self.has_sequence():
return
raise NotImplementedError
def sequence_rename(self, connection, old_name, new_name):
"Renames a sequence"
if not self.has_sequence():
return
raise NotImplementedError
def sequence_delete(self, connection, name):
"Removes a sequence"
if not self.has_sequence():
return
raise NotImplementedError
def sequence_next_number(self, connection, name):
"Gets the next number of a sequence"
if not self.has_sequence():
return
raise NotImplementedError
2018-07-27 09:32:28 +02:00
def has_channel(self):
"Return True if database supports LISTEN/NOTIFY channel"
return False
def sql_type(self, type_):
'Return the SQLType tuple corresponding to the SQL type'
pass
def sql_format(self, type_, value):
'Return value correctly casted into type_'
pass
2019-01-18 11:32:47 +01:00
def json_get(self, column, key=None):
"Return the JSON value of the JSON key"
raise NotImplementedError
def json_key_exists(self, column, key):
"Return expression for key exists in JSON column"
raise NotImplementedError
def json_any_keys_exist(self, column, keys):
"Return expression for any keys exist in JSON column"
raise NotImplementedError
def json_all_keys_exist(self, column, keys):
"Rteurn expression for all keys exist in JSON column"
raise NotImplementedError
def json_contains(self, column, json):
"Return expression for column contains JSON"
raise NotImplementedError