Files
tryton/trytond/backend/database.py
T

220 lines
4.9 KiB
Python
Raw Normal View History

2009-02-04 15:29:28 +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.
2012-03-12 15:34:53 +01:00
from trytond.const import MODEL_CACHE_SIZE
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
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
2009-02-04 15:29:28 +01:00
2009-04-08 17:55:24 +02:00
def __new__(cls, database_name=''):
return object.__new__(cls)
def __init__(self, database_name=''):
2009-02-04 15:29:28 +01:00
self.database_name = database_name
def connect(self):
'''
Connect to the database
:return: the database
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
2011-08-21 11:05:51 +02:00
def cursor(self, autocommit=False, readonly=False):
2009-02-04 15:29:28 +01:00
'''
Retreive a cursor on the database
:param autocommit: a boolean to active autocommit
:return: a Cursor
'''
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
@staticmethod
def create(cursor, database_name):
2009-02-04 15:29:28 +01:00
'''
Create a database
:param database_name: the database name
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
@staticmethod
def drop(cursor, database_name):
2009-02-04 15:29:28 +01:00
'''
Drop a database
:param cursor: a cursor on an other database
:param database_name: the database name
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
2009-02-05 16:44:38 +01:00
@staticmethod
def dump(database_name):
'''
Dump a database
:param database_name: the database name
:return: the dump
'''
raise NotImplementedError
2009-02-05 16:44:38 +01:00
@staticmethod
def restore(database_name, data):
'''
Restore a database
:param database_name: the database name
:param data: the data
:return: True if succeed
'''
raise NotImplementedError
2009-02-05 16:44:38 +01:00
2009-02-04 15:29:28 +01:00
@staticmethod
def list(cursor):
'''
Get the list of database
:return: a list of database name
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
@staticmethod
def init(cursor):
'''
Initialize a database
:param cursor: a cursor on the database
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
2009-02-11 14:52:22 +01:00
2009-02-04 15:29:28 +01:00
class CursorInterface(object):
'''
Define generic interface for database cursor
'''
IN_MAX = 1000
cache_keys = {'language', 'fuzzy_translation', '_datetime'}
2009-02-04 15:29:28 +01:00
def __init__(self):
self.cache = {}
2009-02-04 15:29:28 +01:00
def get_cache(self):
2011-06-14 11:44:26 +02:00
from trytond.cache import LRUDict
2011-08-22 19:12:19 +02:00
from trytond.transaction import Transaction
user = Transaction().user
context = Transaction().context
keys = tuple(((key, context[key]) for key in sorted(self.cache_keys)
if key in context))
return self.cache.setdefault((user, keys), LRUDict(MODEL_CACHE_SIZE))
2009-02-04 15:29:28 +01:00
def execute(self, sql, params=None):
'''
Execute a query
:param sql: a sql query string
:param params: a tuple or list of parameters
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
def close(self, close=False):
2009-02-04 15:29:28 +01:00
'''
Close the cursor
:param close: boolean to not release cursor in pool
2009-02-04 15:29:28 +01:00
'''
raise NotImplementedError
2009-02-04 15:29:28 +01:00
def commit(self):
'''
Commit the cursor
'''
2012-09-24 22:16:53 +02:00
for cache in self.cache.itervalues():
cache.clear()
2009-02-04 15:29:28 +01:00
def rollback(self):
'''
Rollback the cursor
'''
2012-09-24 22:16:53 +02:00
for cache in self.cache.itervalues():
cache.clear()
2009-02-04 15:29:28 +01:00
def test(self):
'''
Test if it is a Tryton database.
'''
raise NotImplementedError
2009-07-07 19:32:31 +02:00
def nextid(self, table):
'''
Return the next sequenced id for a table.
:param table: the table name
:return: an integer
'''
def setnextid(self, table, value):
'''
Set the current sequenced id for a table.
:param table: the table name
'''
def currid(self, table):
'''
Return the current sequenced id for a table.
:param table: the table name
:return: an integer
'''
def lastid(self):
'''
Return the last id inserted.
:return: an integer
'''
2010-02-25 16:43:04 +01:00
def lock(self, 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
2010-02-25 16:43:04 +01:00
:param table: the table name
2009-07-07 19:32:31 +02:00
'''
raise NotImplementedError
def has_constraint(self):
'''
Return True if database handle constraint.
:return: a boolean
'''
raise NotImplementedError
2009-09-07 15:18:17 +02:00
2010-02-25 16:43:04 +01:00
def update_auto_increment(self, table, value):
'''
Update auto_increment value of table
:param table: the table name
:param value: the auto_increment value
'''
pass
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
'''