BMLT Root Server
Static Public Member Functions | Private Member Functions | Static Private Attributes | List of all members
c_comdef_dbsingleton Class Reference

This class provides a genericized interface to the PHP PDO toolkit. It is a completely static class. More...

Static Public Member Functions

static init ($driver, $host, $database, $user=null, $password=null, $charset=null)
 Initializes connection param class members. More...
 
static connect ()
 Create internal PDO object thus connecting to database using connection param class members (passed in from previous call to c_comdef_dbsingleton::init()) More...
 
static isConnected ()
 Returns whether internal PDO object is instantiated and thus connected. More...
 
static pdoInstance ($do_connect=false)
 Provides access to internal PDO object in case this classes functionality is not enough. More...
 
static preparedQuery ($sql, $params=array(), $fetchKeyPair=false)
 Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statements. More...
 
static preparedExec ($sql, $params=array())
 Wrapper for preparing and executing a PDOStatement that does not return a resultset e.g. INSERT or UPDATE SQL statements. More...
 
static lastInsertId ()
 Wrapper for PDO::lastInsertId() More...
 
static beginTransaction ()
 
static rollBack ()
 
static commit ()
 

Private Member Functions

 __construct ()
 Private constructor (prevents direct creation of object) More...
 

Static Private Attributes

static $pdo = null
 Internal PDO object. More...
 
static $driver = null
 Connection database driver type param. More...
 
static $host = null
 Connection host param. More...
 
static $database = null
 Connection database param. More...
 
static $user = null
 Connection user param. More...
 
static $password = null
 Connection password param. More...
 
static $charset = null
 Connection charset param. More...
 
static $fetchMode = PDO::FETCH_ASSOC
 Default fetch mode for internal PDOStatements. More...
 

Detailed Description

This class provides a genericized interface to the PHP PDO toolkit. It is a completely static class.

Definition at line 69 of file c_comdef_dbsingleton.class.php.

Constructor & Destructor Documentation

__construct ( )
private

Private constructor (prevents direct creation of object)

Definition at line 100 of file c_comdef_dbsingleton.class.php.

101  {
102  }

Member Function Documentation

static beginTransaction ( )
static

Definition at line 299 of file c_comdef_dbsingleton.class.php.

Referenced by DB_Connect_and_Upgrade(), c_comdef_admin_ajax_handler\HandleMeetingUpdate(), c_comdef_admin_ajax_handler\HandleMeetingWorldIDsUpdate(), and NAWSImport\import().

300  {
301  if (!self::$pdo instanceof PDO) {
302  self::connect();
303  }
304 
305  return self::$pdo->beginTransaction();
306  }
static commit ( )
static
static connect ( )
static

Create internal PDO object thus connecting to database using connection param class members (passed in from previous call to c_comdef_dbsingleton::init())

Will destroy previous connection (if one exists) before reconnecting

Exceptions
Exceptionthrown if internal PDO object cannot be created e.g. wrong connection param(s)

For security purposes, once we connect, we get rid of all the info, and a new init needs to be called.

Definition at line 150 of file c_comdef_dbsingleton.class.php.

Referenced by whitespace_warnings().

151  {
152  if (self::$pdo instanceof pdo) {
153  self::$pdo = null;
154  }
155 
156  try {
157  $dsn = self::$driver . ':host=' . self::$host . ';dbname=' . self::$database;
158  self::$pdo = new PDO($dsn, self::$user, self::$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
159  self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
160  self::$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
161  self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
162  if (strlen(self::$charset) > 0) {
163  self::preparedExec('SET NAMES :charset', array(':charset' => self::$charset));
164  }
165 
166  /// For security purposes, once we connect, we get rid of all the info, and a new init needs to be called.
167 
168  self::$driver = null;
169  self::$host = null;
170  self::$database = null;
171  self::$user = null;
172  self::$password = null;
173  self::$charset = null;
174  } catch (PDOException $exception) {
175  throw new Exception(__METHOD__ . '() ' . $exception->getMessage());
176  }
177  }
static init (   $driver,
  $host,
  $database,
  $user = null,
  $password = null,
  $charset = null 
)
static

Initializes connection param class members.

Must be called BEFORE any attempts to connect to or query a database.

Will destroy previous connection (if one exists).

Parameters
$driverdatabase server type (ex: 'mysql')
$hostdatabase server host
$databasedatabase name
$useruser, optional
$passwordpassword, optional
$charsetconnection charset, optional

Definition at line 111 of file c_comdef_dbsingleton.class.php.

Referenced by c_comdef_server\__construct(), DB_Connect_and_Upgrade(), and whitespace_warnings().

118  {
119  if (self::$pdo instanceof pdo) {
120  self::$pdo = null;
121  }
122 
123  self::$driver = (string) $driver;
124  self::$host = (string) $host;
125  self::$database = (string) $database;
126 
127  if (!is_null($user)) {
128  self::$user = (string) $user;
129  }
130 
131  if (!is_null($password)) {
132  self::$password = (string) $password;
133  }
134 
135  if (!is_null($charset)) {
136  self::$charset = $charset;
137  }
138  }
static $driver
Connection database driver type param.
static $charset
Connection charset param.
static $database
Connection database param.
static $password
Connection password param.
static $host
Connection host param.
static $user
Connection user param.
static isConnected ( )
static

Returns whether internal PDO object is instantiated and thus connected.

Returns
true if connected.

Definition at line 184 of file c_comdef_dbsingleton.class.php.

185  {
186  if (self::$pdo instanceof PDO) {
187  return true;
188  } else {
189  return false;
190  }
191  }
static lastInsertId ( )
static

Wrapper for PDO::lastInsertId()

Returns
the ID of the last INSERT
Exceptions
Exceptionthrown if internal PDO object not instantiated

Definition at line 290 of file c_comdef_dbsingleton.class.php.

Referenced by c_comdef_meeting\UpdateToDB().

291  {
292  if (!self::$pdo instanceof PDO) {
293  throw new Exception(__METHOD__ . '() internal PDO object not instantiated');
294  }
295 
296  return self::$pdo->lastInsertId();
297  }
static pdoInstance (   $do_connect = false)
static

Provides access to internal PDO object in case this classes functionality is not enough.

Returns
the PDO object
Exceptions
Exceptionthrown if internal PDO object not instantiated
Parameters
$do_connectSet this to true to force a connection if one does not yet exist. Default is false.

Definition at line 200 of file c_comdef_dbsingleton.class.php.

202  {
203  if (!self::isConnected() && $do_connect) {
204  self::connect();
205  }
206 
207  if (self::$pdo instanceof PDO) {
208  return self::$pdo;
209  } else {
210  throw new Exception(__METHOD__ . '() internal PDO object not instantiated');
211  }
212  }
static preparedExec (   $sql,
  $params = array() 
)
static

Wrapper for preparing and executing a PDOStatement that does not return a resultset e.g. INSERT or UPDATE SQL statements.

See PDO documentation about prepared queries.

If there isn't already a database connection, it will "lazy load" the connection.

Exceptions
Exceptionthrown if internal PDO exception is thrown
Returns
true if execution is successful.
Parameters
$sqlsame as kind provided to PDO::prepare()
$paramssame as kind provided to PDO::prepare()

Definition at line 267 of file c_comdef_dbsingleton.class.php.

Referenced by DB_Connect_and_Upgrade(), c_comdef_change\DeleteFromDB(), c_comdef_format\DeleteFromDB_NoRecord(), c_comdef_service_body\DeleteFromDB_NoRecord(), c_comdef_user\DeleteFromDB_NoRecord(), c_comdef_meeting\DeleteFromDB_NoRecord(), dropEverything(), c_comdef_user\ResetChildUsers(), c_comdef_format\UpdateToDB(), c_comdef_change\UpdateToDB(), c_comdef_service_body\UpdateToDB(), c_comdef_user\UpdateToDB(), c_comdef_meeting\UpdateToDB(), and whitespace_warnings().

270  {
271  if (!self::$pdo instanceof PDO) {
272  self::connect();
273  }
274 
275  try {
276  $stmt = self::$pdo->prepare($sql);
277 
278  return $stmt->execute($params);
279  } catch (PDOException $exception) {
280  throw new Exception(__METHOD__ . '() ' . $exception->getMessage());
281  }
282  }
static preparedQuery (   $sql,
  $params = array(),
  $fetchKeyPair = false 
)
static

Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statements.

Returns a multidimensional array depending on internal fetch mode setting (self::$fetchMode) See PDO documentation about prepared queries.

If there isn't already a database connection, it will "lazy load" the connection.

Fetching key pairs- when $fetchKeyPair is set to TRUE, it will force the returned array to be a one-dimensional array indexed on the first column in the query. Note- query may contain only two columns or an exception/error is thrown. See PDO::PDO::FETCH_KEY_PAIR for more details

Returns
associative array of results.
Exceptions
Exceptionthrown if internal PDO exception is thrown
Parameters
$sqlsame as kind provided to PDO::prepare()
$paramssame as kind provided to PDO::prepare()
$fetchKeyPairSee description in method documentation

Definition at line 231 of file c_comdef_dbsingleton.class.php.

Referenced by DB_Connect_and_Upgrade(), c_comdef_server\DoesMeetingExist(), c_comdef_meeting\GetAllMeetingKeys(), c_comdef_meeting\GetAllValuesForKey(), c_comdef_server\GetChangesFromSQL(), c_comdef_server\GetCoverageArea(), c_comdef_server\GetDatabaseVersion(), c_comdef_meeting\GetDataTableTemplate(), c_comdef_meeting\GetLongDataTableTemplate(), c_comdef_server\GetMeetingsFromSQL(), c_comdef_server\GetOneFormat(), c_comdef_server\GetOneMeeting(), c_comdef_server\GetOneUser(), c_comdef_server\HuntForRadius(), c_comdef_server\IsFormatKeyUnique(), c_comdef_meeting\process_meeting_row(), c_comdef_server\ReadFormats(), c_comdef_server\ReadServiceBodies(), c_comdef_server\ReadServiceIDs(), c_comdef_server\ReadUsers(), c_comdef_change\RestoreFromDB(), c_comdef_format\RestoreFromDB(), c_comdef_service_body\RestoreFromDB(), c_comdef_user\RestoreFromDB(), c_comdef_meeting\RestoreFromDB(), NAWSImport\throwIfDuplicateMeetings(), NAWSImport\throwIfDuplicateServiceBodies(), c_comdef_change\UpdateToDB(), c_comdef_service_body\UpdateToDB(), c_comdef_user\UpdateToDB(), and whitespace_warnings().

235  {
236 
237  if (!self::$pdo instanceof PDO) {
238  self::connect();
239  }
240 
241  try {
242  $stmt = self::$pdo->prepare($sql);
243  $stmt->setFetchMode(self::$fetchMode);
244  $stmt->execute($params);
245 
246  if ($fetchKeyPair) {
247  return $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
248  } else {
249  return $stmt->fetchAll();
250  }
251  } catch (PDOException $exception) {
252  throw new Exception(__METHOD__ . '() ' . $exception->getMessage());
253  }
254  }
static rollBack ( )
static

Definition at line 308 of file c_comdef_dbsingleton.class.php.

Referenced by DB_Connect_and_Upgrade(), c_comdef_admin_ajax_handler\HandleMeetingWorldIDsUpdate(), and NAWSImport\import().

309  {
310  return self::$pdo->rollBack();
311  }

Member Data Documentation

$charset = null
staticprivate

Connection charset param.

Definition at line 92 of file c_comdef_dbsingleton.class.php.

$database = null
staticprivate

Connection database param.

Definition at line 83 of file c_comdef_dbsingleton.class.php.

$driver = null
staticprivate

Connection database driver type param.

Definition at line 77 of file c_comdef_dbsingleton.class.php.

$fetchMode = PDO::FETCH_ASSOC
staticprivate

Default fetch mode for internal PDOStatements.

Definition at line 95 of file c_comdef_dbsingleton.class.php.

$host = null
staticprivate

Connection host param.

Definition at line 80 of file c_comdef_dbsingleton.class.php.

$password = null
staticprivate

Connection password param.

Definition at line 89 of file c_comdef_dbsingleton.class.php.

$pdo = null
staticprivate

Internal PDO object.

Definition at line 74 of file c_comdef_dbsingleton.class.php.

$user = null
staticprivate

Connection user param.

Definition at line 86 of file c_comdef_dbsingleton.class.php.


The documentation for this class was generated from the following file: