-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Perl doc for new database framework (#303)
* Update Pelr doc for new database framework * UPdated PR following cmadjar's comments. * Minor corrections to the perldoc of Database.pm.
- Loading branch information
1 parent
baa2a9a
commit aafaf45
Showing
22 changed files
with
975 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# NAME | ||
|
||
NeuroDB::objectBroker::ConfigOB -- An object broker for configuration settings | ||
|
||
# SYNOPSIS | ||
|
||
use NeuroDB::Database; | ||
use NeuroDB::objectBroker::ConfigOB; | ||
use TryCatch; | ||
|
||
my $db = NeuroDB::Database->new( | ||
userName => 'user', | ||
databaseName => 'my_db', | ||
hostName => 'my_hostname', | ||
password => 'pwd' | ||
); | ||
|
||
try { | ||
$db->connect(); | ||
} catch(NeuroDB::DatabaseException $e) { | ||
die sprintf( | ||
"User %s failed to connect to %s on %s: %s (error code %d)\n", | ||
'user', | ||
'my_db', | ||
'my_hostname', | ||
$e->errorMessage, | ||
$e->errorCode | ||
); | ||
} | ||
|
||
. | ||
. | ||
. | ||
|
||
my $configOB = NeuroDB::objectBroker::ConfigOB(db => $db); | ||
my $tarchiveLibraryPath; | ||
try { | ||
$tarchiveLibraryPath = $configOB->getTarchiveLibraryPath(); | ||
} catch(NeuroDB::objectBroker::ObjectBrokerException $e) { | ||
die sprintf( | ||
"Failed to retrieve tarchive library path: %s", | ||
$e->errorMessage | ||
); | ||
} | ||
|
||
# DESCRIPTION | ||
|
||
This class provides a set of methods to fetch specific configuration settings | ||
from the `Config` LORIS database. | ||
|
||
## Methods | ||
|
||
### new(db => $db) >> (constructor) | ||
|
||
Create a new instance of this class. The only parameter to provide is the | ||
`Database` object used to access the database. | ||
|
||
INPUT: the database object used to fetch the settings. | ||
|
||
RETURN: new instance of this class. | ||
|
||
### &$getConfigSettingRef($setting) | ||
|
||
Private method. This method fetches setting `$setting` from the LORIS table | ||
Config. It will throw a `NeuroDB::objectBroker::ObjectBrokerException` if either | ||
the database transaction failed for some reason or it succeeded but returned no | ||
results (i.e. setting $setting does not exist). | ||
|
||
INPUT: name of the setting to fetch. | ||
|
||
RETURN: the setting value (as a string). If the setting value is NULL, then this | ||
method will return `undef`. | ||
|
||
### getTarchiveLibraryDir() | ||
|
||
Gets the tarchive library dir. | ||
|
||
RETURN: value (string) of the tarchive library dir in the Config table. | ||
|
||
# TO DO | ||
|
||
Nothing planned. | ||
|
||
# BUGS | ||
|
||
None reported. | ||
|
||
# COPYRIGHT AND LICENSE | ||
|
||
License: GPLv3 | ||
|
||
# AUTHORS | ||
|
||
LORIS community <loris.info@mcin.ca> and McGill Centre for Integrative | ||
Neuroscience |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# NAME | ||
|
||
NeuroDB::Database -- Provides a set of methods to run SQL statements on a database | ||
|
||
# SYNOPSIS | ||
|
||
use NeuroDB::Database; | ||
use TryCatch; | ||
|
||
my $db = NeuroDB::Database->new( | ||
userName => 'user', | ||
databaseName => 'my_db', | ||
hostName => 'my_hostname', | ||
password => 'pwd' | ||
); | ||
|
||
try { | ||
$db->connect(); | ||
} catch(NeuroDB::DatabaseException $e) { | ||
die sprintf( | ||
"User %s failed to connect to %s on %s: %s (error code %d)\n", | ||
'user', | ||
'my_db', | ||
'my_hostname', | ||
$e->errorMessage, | ||
$e->errorCode | ||
); | ||
} | ||
|
||
. | ||
. | ||
. | ||
|
||
try { | ||
$db->pselect( | ||
'SELECT * FROM candidate WHERE pscid = ?', 'MTL135' | ||
); | ||
} catch(NeuroDB::DatabaseException $e) { | ||
die sprintf( | ||
"SELECT on candidate table failed: %s (error code=%d)", | ||
$e->errorMessage, | ||
$e->errorCode | ||
); | ||
} | ||
|
||
# DESCRIPTION | ||
|
||
This class provides the basic `SELECT`, `INSERT`, `UPDATE` and `DELETE` methods | ||
for all object brokers. The methods of this class should only be used by | ||
the object brokers themselves (except `new` for creating a new database | ||
instance and `connect`). Scripts and 'non-broker' classes that need access | ||
to the database should rely on an appropriate object broker class to handle | ||
the requests. | ||
|
||
## Methods | ||
|
||
### `new(userName => $u, databaseName => $d, hostname => $h, password => $pwd, port =>$port)` (constructor) | ||
|
||
Create a new instance of this class, without actually trying to connect | ||
to the database specified. All parameters are required except `port`, which | ||
defaults to 3306 if not specified. If the user name, database name or host | ||
name are the empty string, the constructor will call `die`. | ||
|
||
INPUTS: | ||
- name of the user for the (upcoming) connection. | ||
- name of the database. | ||
- name of the host on which the database resides. | ||
- password for the (upcoming) connection. | ||
- port used for the (upcoming) connection (defaults to 3306 if not provided). | ||
|
||
RETURN: new instance of this class. | ||
|
||
### `connect()` | ||
|
||
Attempts to connect to the database using the connection parameters passed | ||
at construction time. This method will throw a `DatabaseException` if the | ||
connection could not be established. | ||
|
||
### `pselect($query, @args)` | ||
|
||
Executes a `SELECT` query on the database. This method will first `prepare` | ||
the statement passed as parameter before sending the request to the database. | ||
|
||
INPUTS: | ||
- `SELECT` query to execute (containing the argument placeholders if any). | ||
- list of arguments to replace the placeholders with. | ||
|
||
RETURN: a reference to the array of records found. Each record is in fact a | ||
reference to the list of values for the columns selected. | ||
|
||
### `insertOne($tableName, $valuesRef)` | ||
|
||
Inserts one record in a given database table with the specified column values. | ||
This method will throw a `DatabaseException` if the record cannot be inserted. | ||
|
||
INPUTS: | ||
- name of the table in which to insert the record. | ||
- reference to a hash array describing the column names and their values | ||
for the given record. | ||
|
||
RETURN: the ID of the record inserted. | ||
|
||
### `insert($tableName, $columnNamesRef, $valuesRef)` | ||
|
||
Inserts one record in a given database table with the specified column values. | ||
This method will throw a `DatabaseException` if the record cannot be inserted. | ||
|
||
INPUTS: | ||
- name of the table in which to insert the record. | ||
- reference to an array containing the names of the columns whose values | ||
will be modified by this `INSERT` statement. | ||
- reference to an array of array references. This "matrix" contains the | ||
values of each column for each record. | ||
|
||
### `disconnect()` | ||
|
||
Terminates the connection previously instantiated to the database if a | ||
connection was previously established. | ||
|
||
### `DESTROY()` | ||
|
||
Object destructor: terminates the connection previously instantiated to the | ||
database (if any). | ||
|
||
# TO DO | ||
|
||
Nothing planned. | ||
|
||
# BUGS | ||
|
||
None reported. | ||
|
||
# COPYRIGHT AND LICENSE | ||
|
||
License: GPLv3 | ||
|
||
# AUTHORS | ||
|
||
LORIS community <loris.info@mcin.ca> and McGill Centre for Integrative | ||
Neuroscience |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# NAME | ||
|
||
NeuroDB::DatabaseException -- Exception for database related errors. | ||
|
||
# SYNOPSIS | ||
|
||
use NeuroDB::DatabaseException; | ||
|
||
. | ||
. | ||
. | ||
|
||
NeuroDB::DatabaseException->throw( | ||
statement => 'SELECT * from Foo WHERE x=? AND y=?', | ||
args => [1, 2], | ||
errorCode => $DBI::err, | ||
errorMessage => $DBI::errstr | ||
); | ||
|
||
# DESCRIPTION | ||
|
||
This class is the base class for database-related exceptions. You use this | ||
class by calling the `throw` method with the specified parameters (all of | ||
which are required). This will build a new instance of this class and throw | ||
it as expected. The throw method of this class will call `die` if the error | ||
code is zero and the error message is defined or if the error code is not | ||
0 and the error message is undefined. | ||
|
||
### `toString()` | ||
|
||
Default representation of this exception when used in a string context. | ||
Among other things, the returned string will be used for uncaught exceptions | ||
that make a script die. Note that the returned string can be useful for debugging | ||
purposes when trying to diagnose why a particular SQL statement did not execute | ||
successfully. | ||
|
||
# TO DO | ||
|
||
Nothing planned. | ||
|
||
# BUGS | ||
|
||
None reported. | ||
|
||
# COPYRIGHT AND LICENSE | ||
|
||
License: GPLv3 | ||
|
||
# AUTHORS | ||
|
||
LORIS community <loris.info@mcin.ca> and McGill Centre for Integrative | ||
Neuroscience |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.