Commit cb0dc7ec authored by Grégory Wisniewski's avatar Grégory Wisniewski

Reorganize database management

* Move them into a database module
* Add a buildDatabaseManager() helper function
* Add the '--adapter' parameter to select the database backend
* Move specific database string parsing to MySQL manager.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1385 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 382f1134
...@@ -65,16 +65,10 @@ class ConfigurationManager(object): ...@@ -65,16 +65,10 @@ class ConfigurationManager(object):
return (ip, int(port)) return (ip, int(port))
def getDatabase(self): def getDatabase(self):
""" Get the database credentials (username, password, database) """ return self.__get('database')
# expected pattern : [user[:password]@]database
username = None def getAdapter(self):
password = None return self.__get('adapter')
database = self.__get('database')
if '@' in database:
(username, database) = database.split('@')
if ':' in username:
(username, password) = username.split(':')
return (username, password, database)
def getCluster(self): def getCluster(self):
cluster = self.__get('cluster') cluster = self.__get('cluster')
......
...@@ -22,12 +22,12 @@ from collections import deque ...@@ -22,12 +22,12 @@ from collections import deque
from neo.protocol import NodeTypes, CellStates, Packets from neo.protocol import NodeTypes, CellStates, Packets
from neo.node import NodeManager from neo.node import NodeManager
from neo.event import EventManager from neo.event import EventManager
from neo.storage.mysqldb import MySQLDatabaseManager
from neo.connection import ListeningConnection from neo.connection import ListeningConnection
from neo.exception import OperationFailure, PrimaryFailure from neo.exception import OperationFailure, PrimaryFailure
from neo.storage.handlers import identification, verification, initialization from neo.storage.handlers import identification, verification, initialization
from neo.storage.handlers import master, hidden from neo.storage.handlers import master, hidden
from neo.storage.replicator import Replicator from neo.storage.replicator import Replicator
from neo.storage.database import buildDatabaseManager
from neo.connector import getConnectorHandler from neo.connector import getConnectorHandler
from neo.pt import PartitionTable from neo.pt import PartitionTable
from neo.util import dump, parseMasterList from neo.util import dump, parseMasterList
...@@ -50,15 +50,11 @@ class Application(object): ...@@ -50,15 +50,11 @@ class Application(object):
# load master node list # load master node list
self.master_node_list = config.getMasters() self.master_node_list = config.getMasters()
logging.debug('master nodes are %s', self.master_node_list) logging.debug('master nodes are %s', self.master_node_list)
# load database connection credentials, from user:password@database
(username, password, database) = config.getDatabase()
# Internal attributes. # Internal attributes.
self.em = EventManager() self.em = EventManager()
self.nm = NodeManager() self.nm = NodeManager()
self.dm = MySQLDatabaseManager(database=database, user=username, self.dm = buildDatabaseManager(config.getAdapter(), config.getDatabase())
password=password)
# The partition table is initialized after getting the number of # The partition table is initialized after getting the number of
# partitions. # partitions.
......
#
# Copyright (C) 2006-2009 Nexedi SA
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from neo.exception import DatabaseFailure
from neo.storage.database.manager import DatabaseManager
from neo.storage.database.mysqldb import MySQLDatabaseManager
from neo.storage.database.sqlite import SQLiteManager
DATABASE_MANAGER_DICT = {
'MySQL': mysqldb.MySQLDatabaseManager,
'SQLite': sqlite.SQLiteManager,
}
def buildDatabaseManager(name, config):
adapter_klass = DATABASE_MANAGER_DICT.get(name, None)
if adapter_klass is None:
raise DatabaseFailure('Cannot find a database adapter <%s>' % name)
return adapter_klass(config)
...@@ -21,7 +21,7 @@ from neo.exception import DatabaseFailure ...@@ -21,7 +21,7 @@ from neo.exception import DatabaseFailure
class DatabaseManager(object): class DatabaseManager(object):
"""This class only describes an interface for database managers.""" """This class only describes an interface for database managers."""
def __init__(self, **kwargs): def __init__(self):
""" """
Initialize the object. Initialize the object.
""" """
......
...@@ -39,13 +39,22 @@ def u64(s): ...@@ -39,13 +39,22 @@ def u64(s):
class MySQLDatabaseManager(DatabaseManager): class MySQLDatabaseManager(DatabaseManager):
"""This class manages a database on MySQL.""" """This class manages a database on MySQL."""
def __init__(self, **kwargs): def __init__(self, database):
self.db = kwargs['database'] super(MySQLDatabaseManager, self).__init__()
self.user = kwargs['user'] self.user, self.passwd, self.db = self._parse(database)
self.passwd = kwargs.get('password')
self.conn = None self.conn = None
self._connect() self._connect()
super(MySQLDatabaseManager, self).__init__(**kwargs)
def _parse(self, database):
""" Get the database credentials (username, password, database) """
# expected pattern : [user[:password]@]database
username = None
password = None
if '@' in database:
(username, database) = database.split('@')
if ':' in username:
(username, password) = username.split(':')
return (username, password, database)
# XXX: unused ? # XXX: unused ?
def close(self): def close(self):
......
...@@ -38,6 +38,7 @@ parser.add_option('-n', '--name', help = 'the node name (impove logging)') ...@@ -38,6 +38,7 @@ parser.add_option('-n', '--name', help = 'the node name (impove logging)')
parser.add_option('-b', '--bind', help = 'the local address to bind to') parser.add_option('-b', '--bind', help = 'the local address to bind to')
parser.add_option('-c', '--cluster', help = 'the cluster name') parser.add_option('-c', '--cluster', help = 'the cluster name')
parser.add_option('-m', '--masters', help = 'master node list') parser.add_option('-m', '--masters', help = 'master node list')
parser.add_option('-a', '--adapter', help = 'database adapter to use')
parser.add_option('-d', '--database', help = 'database connections string') parser.add_option('-d', '--database', help = 'database connections string')
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
...@@ -48,11 +49,13 @@ arguments = dict( ...@@ -48,11 +49,13 @@ arguments = dict(
masters = options.masters, masters = options.masters,
database = options.database, database = options.database,
reset = options.reset, reset = options.reset,
adapter = options.adapter,
) )
defaults = dict( defaults = dict(
name = 'storage', name = 'storage',
bind = '127.0.0.1:20000', bind = '127.0.0.1:20000',
masters = '127.0.0.1:10000', masters = '127.0.0.1:10000',
adapter = 'MySQL',
) )
config = ConfigurationManager( config = ConfigurationManager(
defaults, defaults,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment