Package conary :: Package dbstore
[hide private]
[frames] | no frames]

Source Code for Package conary.dbstore

 1  # 
 2  # Copyright (c) 2005-2008 rPath, Inc. 
 3  # 
 4  # This program is distributed under the terms of the Common Public License, 
 5  # version 1.0. A copy of this license should have been distributed with this 
 6  # source file in a file called LICENSE. If it is not present, the license 
 7  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
 8  # 
 9  # This program is distributed in the hope that it will be useful, but 
10  # without any warranty; without even the implied warranty of merchantability 
11  # or fitness for a particular purpose. See the Common Public License for 
12  # full details. 
13  # 
14   
15  from conary.lib import cfg, cfgtypes 
16   
17  from base_drv import BaseDatabase as Database 
18  from base_drv import BaseCursor as Cursor 
19  from migration import SchemaMigration 
20  from sqlerrors import InvalidBackend 
21   
22  # default driver we want to use 
23  __DRIVER = "sqlite" 
24   
25 -def __get_driver(driver = __DRIVER):
26 global __DRIVER 27 if not driver: 28 driver = __DRIVER 29 if driver == "sqlite": 30 try: 31 from sqlite_drv import Database 32 except ImportError, e: 33 raise InvalidBackend( 34 "Could not locate driver for backend '%s'" % (driver,), 35 e.args + (driver,)) 36 else: 37 return Database 38 # postgresl support 39 if driver == "postgresql": 40 try: 41 from postgresql_drv import Database 42 except ImportError, e: 43 raise InvalidBackend( 44 "Could not locate driver for backend '%s'" % (driver,), 45 e.args + (driver,)) 46 else: 47 return Database 48 # mysql support 49 if driver == "mysql": 50 try: 51 from mysql_drv import Database 52 except ImportError, e: 53 raise InvalidBackend( 54 "Could not locate driver for backend '%s'" % (driver,), 55 e.args + (driver,)) 56 else: 57 return Database 58 # ingres support 59 if driver == "ingres": 60 try: 61 from ingres_drv import Database 62 except ImportError, e: 63 raise InvalidBackend( 64 "Could not locate driver for backend '%s'" % (driver,), 65 e.args + (driver,)) 66 else: 67 return Database 68 # ELSE, INVALID 69 raise InvalidBackend( 70 "Database backend '%s' is not supported" % (driver,), 71 driver)
72 73 # create a database connection and return an instance 74 # all drivers parse a db string in the form: 75 # [[user[:password]@]host/]database
76 -def connect(db, driver=None, **kw):
77 driver = __get_driver(driver) 78 dbh = driver(db) 79 assert(dbh.connect(**kw)) 80 return dbh
81 82 # A class for configuration of a database driver
83 -class CfgDriver(cfg.CfgType):
84 - def parseString(self, str):
85 s = str.split() 86 if len(s) != 2: 87 raise cfgtypes.ParseError("database driver and path expected") 88 return tuple(s)
89 - def format(self, val, displayOptions = None):
90 return "%s %s" % val
91 92 __all__ = [ "connect", "InvalidBackend", "CfgDriver"] 93