45def connect( connectString, verbose = False):
46 """
47 Connects to the given database and returns a tuple
48 database, connectString
49 where 'database' is a cool.IDatabase object and 'connectString' is the
50 possibly expanded connectString that 'database' is based on.
51
52 This expansion can occur when a connect string without a containing
53 '://' is specified. In this case the string is interpreted as a sqlite
54 file name and rewritten to a RAL compliant format:
55
56 TEST.db --> 'sqlite://;schema=TEST.db;dbname=TEST'
57 TEST --> 'sqlite://;schema=TEST;dbname=TEST'
58
59 The filename can have a '.db' suffix which will be stripped for the
60 'dbname' part of the connect string. Other suffixes will not be recognized.
61
62 Note that the COOL database inside the file must have the same name as the
63 base of the filename for this shortcut to work. Storing a COOL database
64 MYTEST in a file mytest.db will not work.
65
66 Set verbose to True to obtain an error print out.
67 """
68 connectString = expandConnectString( connectString )
69 debug=False
70 if (';readoracle' in connectString):
71
72
73 connectString=connectString.replace(';readoracle','')
74 debug=True
75 if (';readsqlite' in connectString):
76 connectString=connectString.replace(';readsqlite','')
77 debug=True
78 try:
79 dbSvc = cool.DatabaseSvcFactory.databaseService()
80 readonly=True
81
82 if ('oracle' in connectString or 'mysql' in connectString or 'sqlite' in connectString): readonly=False
83 db=AtlCoolLib.indirectOpen(connectString,readonly,debug)
84 except Exception as e:
85 if 'The database does not exist' in str(e):
86 print ("Creating new database")
87 db = dbSvc.createDatabase( connectString )
88 else:
89 if verbose: print ('Error while connecting:', str(e))
90 db = None
91 return db, connectString
92
93