Add support for specifying DB port

In case someone needs to connect to a DB engine in non-standard port,
defining the TCP port is required. This is a rare corner case which
should only arise when a given DB host runs multiple versions of the
same DB engine.

However, the support is relatively straightforward to implement and it
shouldn't conflict with any other options.
This commit is contained in:
Mika Bostrom 2011-01-16 10:43:36 +02:00
parent 02a087c5b0
commit 8d78e11a4f
3 changed files with 29 additions and 9 deletions

View File

@ -409,12 +409,13 @@ class Database:
self.db_name = node.getAttribute("db_name") self.db_name = node.getAttribute("db_name")
self.db_desc = node.getAttribute("db_desc") self.db_desc = node.getAttribute("db_desc")
self.db_server = node.getAttribute("db_server").lower() self.db_server = node.getAttribute("db_server").lower()
self.db_ip = node.getAttribute("db_ip") self.db_ip = node.getAttribute("db_ip")
self.db_port = node.getAttribute("db_port")
self.db_user = node.getAttribute("db_user") self.db_user = node.getAttribute("db_user")
self.db_pass = node.getAttribute("db_pass") self.db_pass = node.getAttribute("db_pass")
self.db_selected = string_to_bool(node.getAttribute("default"), default=False) self.db_selected = string_to_bool(node.getAttribute("default"), default=False)
log.debug("Database db_name:'%(name)s' db_server:'%(server)s' db_ip:'%(ip)s' db_user:'%(user)s' db_pass (not logged) selected:'%(sel)s'" \ log.debug("Database db_name:'%(name)s' db_server:'%(server)s' db_ip:'%(ip)s' db_port:'%(port)s' db_user:'%(user)s' db_pass (not logged) selected:'%(sel)s'" \
% { 'name':self.db_name, 'server':self.db_server, 'ip':self.db_ip, 'user':self.db_user, 'sel':self.db_selected} ) % { 'name':self.db_name, 'server':self.db_server, 'ip':self.db_ip, 'port':self.db_port, 'user':self.db_user, 'sel':self.db_selected} )
def __str__(self): def __str__(self):
temp = 'Database = ' + self.db_name + '\n' temp = 'Database = ' + self.db_name + '\n'
@ -1059,6 +1060,9 @@ class Config:
try: db['db-host'] = self.supported_databases[name].db_ip try: db['db-host'] = self.supported_databases[name].db_ip
except: pass except: pass
try: db['db-port'] = self.supported_databases[name].db_port
except KeyError: pass
try: db['db-user'] = self.supported_databases[name].db_user try: db['db-user'] = self.supported_databases[name].db_user
except: pass except: pass
@ -1072,15 +1076,16 @@ class Config:
return db return db
def set_db_parameters(self, db_name = 'fpdb', db_ip = None, db_user = None, def set_db_parameters(self, db_name = 'fpdb', db_ip = None, db_port = None,
db_pass = None, db_desc = None, db_server = None, db_user = None, db_pass = None, db_desc = None,
default = "False"): db_server = None, default = "False"):
db_node = self.get_db_node(db_name) db_node = self.get_db_node(db_name)
default = default.lower() default = default.lower()
defaultb = string_to_bool(default, False) defaultb = string_to_bool(default, False)
if db_node != None: if db_node != None:
if db_desc is not None: db_node.setAttribute("db_desc", db_desc) if db_desc is not None: db_node.setAttribute("db_desc", db_desc)
if db_ip is not None: db_node.setAttribute("db_ip", db_ip) if db_ip is not None: db_node.setAttribute("db_ip", db_ip)
if db_port is not None: db_node.setAttribute("db_port", db_port)
if db_user is not None: db_node.setAttribute("db_user", db_user) if db_user is not None: db_node.setAttribute("db_user", db_user)
if db_pass is not None: db_node.setAttribute("db_pass", db_pass) if db_pass is not None: db_node.setAttribute("db_pass", db_pass)
if db_server is not None: db_node.setAttribute("db_server", db_server) if db_server is not None: db_node.setAttribute("db_server", db_server)
@ -1094,6 +1099,7 @@ class Config:
if self.supported_databases.has_key(db_name): if self.supported_databases.has_key(db_name):
if db_desc is not None: self.supported_databases[db_name].dp_desc = db_desc if db_desc is not None: self.supported_databases[db_name].dp_desc = db_desc
if db_ip is not None: self.supported_databases[db_name].dp_ip = db_ip if db_ip is not None: self.supported_databases[db_name].dp_ip = db_ip
if db_port is not None: self.supported_databases[db_name].dp_port = db_port
if db_user is not None: self.supported_databases[db_name].dp_user = db_user if db_user is not None: self.supported_databases[db_name].dp_user = db_user
if db_pass is not None: self.supported_databases[db_name].dp_pass = db_pass if db_pass is not None: self.supported_databases[db_name].dp_pass = db_pass
if db_server is not None: self.supported_databases[db_name].dp_server = db_server if db_server is not None: self.supported_databases[db_name].dp_server = db_server
@ -1148,6 +1154,7 @@ class Config:
if self.supported_databases.has_key(db_name): if self.supported_databases.has_key(db_name):
if db_desc is not None: self.supported_databases[db_name].dp_desc = db_desc if db_desc is not None: self.supported_databases[db_name].dp_desc = db_desc
if db_ip is not None: self.supported_databases[db_name].dp_ip = db_ip if db_ip is not None: self.supported_databases[db_name].dp_ip = db_ip
if db_port is not None: self.supported_databases[db_name].dp_port = db_port
if db_user is not None: self.supported_databases[db_name].dp_user = db_user if db_user is not None: self.supported_databases[db_name].dp_user = db_user
if db_pass is not None: self.supported_databases[db_name].dp_pass = db_pass if db_pass is not None: self.supported_databases[db_name].dp_pass = db_pass
if db_server is not None: self.supported_databases[db_name].dp_server = db_server if db_server is not None: self.supported_databases[db_name].dp_server = db_server

View File

@ -254,6 +254,7 @@ class Database:
self.db_server = db_params['db-server'] self.db_server = db_params['db-server']
self.database = db_params['db-databaseName'] self.database = db_params['db-databaseName']
self.host = db_params['db-host'] self.host = db_params['db-host']
self.db_port = db_params['db-port']
self.db_path = '' self.db_path = ''
gen = c.get_general_params() gen = c.get_general_params()
self.day_start = 0 self.day_start = 0
@ -349,6 +350,7 @@ class Database:
try: try:
self.connect(backend=db['db-backend'], self.connect(backend=db['db-backend'],
host=db['db-host'], host=db['db-host'],
port=db['db-port'],
database=db['db-databaseName'], database=db['db-databaseName'],
user=db['db-user'], user=db['db-user'],
password=db['db-password']) password=db['db-password'])
@ -363,14 +365,17 @@ class Database:
self.db_server = db_params['db-server'] self.db_server = db_params['db-server']
self.database = db_params['db-databaseName'] self.database = db_params['db-databaseName']
self.host = db_params['db-host'] self.host = db_params['db-host']
self.db_port = db_params['db-port']
def connect(self, backend=None, host=None, database=None, def connect(self, backend=None, host=None, port=None,
user=None, password=None, create=False): database=None, user=None, password=None,
create=False):
"""Connects a database with the given parameters""" """Connects a database with the given parameters"""
if backend is None: if backend is None:
raise FpdbError('Database backend not defined') raise FpdbError('Database backend not defined')
self.backend = backend self.backend = backend
self.host = host self.host = host
self.port = port
self.user = user self.user = user
self.password = password self.password = password
self.database = database self.database = database
@ -382,7 +387,8 @@ class Database:
if use_pool: if use_pool:
MySQLdb = pool.manage(MySQLdb, pool_size=5) MySQLdb = pool.manage(MySQLdb, pool_size=5)
try: try:
self.connection = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True) self.connection = MySQLdb.connect(host=host, port=port, user=user,
passwd=password, db=database, use_unicode=True)
self.__connected = True self.__connected = True
#TODO: Add port option #TODO: Add port option
except MySQLdb.Error, ex: except MySQLdb.Error, ex:
@ -416,6 +422,7 @@ class Database:
if not self.is_connected(): if not self.is_connected():
try: try:
self.connection = psycopg2.connect(host = host, self.connection = psycopg2.connect(host = host,
port = port,
user = user, user = user,
password = password, password = password,
database = database) database = database)

View File

@ -796,6 +796,12 @@ Left-Drag to Move"
<raw_tourneys save="none" compression="none"/> <raw_tourneys save="none" compression="none"/>
<supported_databases> <supported_databases>
<!-- If you have multiple versions of database engine, or you
have the database listening on non-standard port, you can
add the option: db_port="PORTNUMBER"
NOTE: This only applies to users of MySQL and PostgreSQL,
sqlite users can happily ignore.
-->
<!-- <database db_name="fpdb" db_server="mysql" db_ip="localhost" db_user="fpdb" db_pass="YOUR MYSQL PASSWORD"></database> --> <!-- <database db_name="fpdb" db_server="mysql" db_ip="localhost" db_user="fpdb" db_pass="YOUR MYSQL PASSWORD"></database> -->
<database db_ip="localhost" db_server="sqlite" db_name="fpdb.db3" db_user="fpdb" db_pass="fpdb"/> <database db_ip="localhost" db_server="sqlite" db_name="fpdb.db3" db_user="fpdb" db_pass="fpdb"/>
</supported_databases> </supported_databases>