Framework  3.9
ConnectionManager Class Reference

The ConnectionManager class provides the common point of entry by which DataItems can access the global database connection via the getConnection() method. More...

Static Public Member Functions

static getConnection ()
 Retrieves a reference to the global database connection. More...
 
static newConnection ()
 Returns a new connection to the database. More...
 
static releaseConnection ()
 Releases the global connection to the database. More...
 
static quote ($str)
 Quote a string value based on the character set of the global connection. More...
 
static escape ($str)
 Escapes a string based on the character set of the global connection. More...
 
static getVersion ()
 Determine the version of the connected database. More...
 

Static Public Attributes

static $dsn
 
static $user
 
static $password
 
static $conn
 
static $options = null
 

Detailed Description

The ConnectionManager class provides the common point of entry by which DataItems can access the global database connection via the getConnection() method.

The global connection can be manually released via the releaseConnection() method. If needed, additional connections to the database can be created using the newConnection() method. These additional connections can be released simply by setting the variable used to store them to null.

Definition at line 47 of file connection_manager.inc.

Member Function Documentation

◆ escape()

static ConnectionManager::escape (   $str)
static

Escapes a string based on the character set of the global connection.

This is a kludge - try not to use it. I'm adding it for backward compatibility only when migrating to PHP 7 where mysql_escape_string() is no longer available.

Parameters
stringthe string to escape
Returns
string the string with quote characters escaped

Definition at line 129 of file connection_manager.inc.

130  {
131  $quoted = ConnectionManager::getConnection()->quote($str);
132  $escaped = preg_replace("/^'(.*)'$/", "$1", $quoted);
133  trace("ESCAPE: $str -> $quoted -> $escaped", 1);
134  return $escaped;
135  }
static getConnection()
Retrieves a reference to the global database connection.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ getConnection()

static ConnectionManager::getConnection ( )
static

Retrieves a reference to the global database connection.

If no connection to the database has yet been made, one will be created as a result of this call.

Definition at line 59 of file connection_manager.inc.

60  {
61  try
62  {
64  {
65  $startTime = microtime(true);
67  ConnectionManager::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
68  $endTime = microtime(true);
69  trace("Connection opened in ".number_format($endTime-$startTime, 3)." seconds", 3);
70 
71  }
73  }
74  catch(PDOException $e)
75  {
76  trace("Database connection failed - " . $e->getMessage(), 1);
77  throw new DataItemException("Database connection failed - " . $e->getMessage());
78  }
79  }

◆ getVersion()

static ConnectionManager::getVersion ( )
static

Determine the version of the connected database.

Returns
string the version of the connected database.

Definition at line 141 of file connection_manager.inc.

142  {
143  $version = Cache::get("sql_version");
144  if (!$version)
145  {
147  $version = $conn->query('select version()')->fetchColumn();
148  Cache::put("sql_version", $version);
149  }
150 
151  return $version;
152  }
static get($key)
Retrieve the specified object from the cache.
Definition: cache.inc:88
static put($key, $obj, $ttl=0)
Store the specified object in the cache at the specified key.
Definition: cache.inc:106

◆ newConnection()

static ConnectionManager::newConnection ( )
static

Returns a new connection to the database.

This is separate from the global connection.

Definition at line 85 of file connection_manager.inc.

86  {
87  try
88  {
89  $startTime = microtime(true);
91  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
92  $endTime = microtime(true);
93  trace("New connection opened in ".number_format($endTime-$startTime, 3)." seconds", 3);
94  return $conn;
95  }
96  catch(PDOException $e)
97  {
98  trace("Database connection failed - " . $e->getMessage(), 1);
99  throw new DataItemException("Database connection failed - " . $e->getMessage());
100  }
101  }

◆ quote()

static ConnectionManager::quote (   $str)
static

Quote a string value based on the character set of the global connection.

Parameters
string$strthe string to quote
Returns
string the string, wrapped in quotes, with other quote characters escaped

Definition at line 117 of file connection_manager.inc.

118  {
119  return ConnectionManager::getConnection()->quote($str);
120  }

◆ releaseConnection()

static ConnectionManager::releaseConnection ( )
static

Releases the global connection to the database.

Definition at line 106 of file connection_manager.inc.

107  {
109  }

Member Data Documentation

◆ $conn

ConnectionManager::$conn
static

Definition at line 52 of file connection_manager.inc.

◆ $dsn

ConnectionManager::$dsn
static

Definition at line 49 of file connection_manager.inc.

◆ $options

ConnectionManager::$options = null
static

Definition at line 53 of file connection_manager.inc.

◆ $password

ConnectionManager::$password
static

Definition at line 51 of file connection_manager.inc.

◆ $user

ConnectionManager::$user
static

Definition at line 50 of file connection_manager.inc.


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