Package cjklib :: Module dbconnector :: Class DatabaseConnector
[hide private]
[frames] | no frames]

Class DatabaseConnector

source code

A DatabaseConnector provides simple read access to a SQL database.

On initialisation it connects to a given database. Once connected it's select methods can be used to quickly access the database content.

DatabaseConnector supports a wide range of database systems through SQLalchemy.

As only standard select commands are issued further systems should be easy to incorporate.

For selecting entries there are for different methods given:

  1. selectRows(): the most general select method
  2. selectScalars(): returns entries for only one column
  3. selectRow(): returns only one entry
  4. selectScalar(): returns one single value
Instance Methods [hide private]
 
__init__(self, databaseUrl)
Constructs the DatabaseConnector object and connects to the database specified by the options given in databaseSettings.
source code
 
_registerViews(self)
Registers all views and makes them accessible through the same methods as tables in SQLalchemy.
source code
 
execute(self, *options, **keywords)
Executes a request on the given database.
source code
 
selectScalar(self, request)
Executes a select query and returns a single variable.
source code
 
selectScalars(self, request)
Executes a select query and returns a list of scalars.
source code
 
selectRow(self, request)
Executes a select query and returns a single table row.
source code
 
selectRows(self, request)
Executes a select query and returns a list of table rows.
source code
Class Methods [hide private]
 
getDBConnector(cls, databaseUrl=None)
Returns a shared DatabaseConnector instance.
source code
Static Methods [hide private]
dict
getConfigSettings(projectName)
Gets the SQL connection parameter from a config file.
source code
Class Variables [hide private]
  dbconnectInst = None
Instance of a DatabaseConnector used for all connections to SQL server.
  databaseUrl = None
Database url used to create the connector instance.
Method Details [hide private]

getDBConnector(cls, databaseUrl=None)
Class Method

source code 

Returns a shared DatabaseConnector instance.

Parameters:

getConfigSettings(projectName)
Static Method

source code 

Gets the SQL connection parameter from a config file.

Parameters:
  • projectName (str) - name of project which will be used as name of the config file
Returns: dict
configuration settings for the given project

__init__(self, databaseUrl)
(Constructor)

source code 

Constructs the DatabaseConnector object and connects to the database specified by the options given in databaseSettings.

Parameters:
  • databaseUrl (str) - database connection setting in the format driver://user:pass@host/database.

_registerViews(self)

source code 

Registers all views and makes them accessible through the same methods as tables in SQLalchemy.

Attention: Currently only works for MySQL and SQLite.

selectScalar(self, request)

source code 

Executes a select query and returns a single variable.

Parameters:
  • request - SQL request
Returns:
a scalar

selectScalars(self, request)

source code 

Executes a select query and returns a list of scalars.

Parameters:
  • request - SQL request
Returns:
a list of scalars

selectRow(self, request)

source code 

Executes a select query and returns a single table row.

Parameters:
  • request - SQL request
Returns:
a list of scalars

selectRows(self, request)

source code 

Executes a select query and returns a list of table rows.

Parameters:
  • request - SQL request
Returns:
a list of scalars