Fakoli Framework
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 Public Attributes

static $dsn
 
static $user
 
static $password
 
static $conn
 

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 45 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
the string with quote characters escaped

Definition at line 127 of file connection_manager.inc.

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

◆ 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 57 of file connection_manager.inc.

58  {
59  try
60  {
62  {
63  $startTime = microtime(true);
65  ConnectionManager::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
66  $endTime = microtime(true);
67  trace("Connection opened in ".number_format($endTime-$startTime, 3)." seconds", 3);
68 
69  }
71  }
72  catch(PDOException $e)
73  {
74  trace("Database connection failed - " . $e->getMessage(), 1);
75  throw new DataItemException("Database connection failed - " . $e->getMessage());
76  }
77  }
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959

◆ newConnection()

static ConnectionManager::newConnection ( )
static

Returns a new connection to the database.

This is separate from the global connection.

Definition at line 83 of file connection_manager.inc.

84  {
85  try
86  {
87  $startTime = microtime(true);
89  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
90  $endTime = microtime(true);
91  trace("New connection opened in ".number_format($endTime-$startTime, 3)." seconds", 3);
92  return $conn;
93  }
94  catch(PDOException $e)
95  {
96  trace("Database connection failed - " . $e->getMessage(), 1);
97  throw new DataItemException("Database connection failed - " . $e->getMessage());
98  }
99  }
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959

◆ 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
the string, wrapped in quotes, with other quote characters escaped

Definition at line 115 of file connection_manager.inc.

116  {
117  return ConnectionManager::getConnection()->quote($str);
118  }
static getConnection()
Retrieves a reference to the global database connection.

◆ releaseConnection()

static ConnectionManager::releaseConnection ( )
static

Releases the global connection to the database.

Definition at line 104 of file connection_manager.inc.

105  {
107  }

Member Data Documentation

◆ $conn

ConnectionManager::$conn
static

Definition at line 50 of file connection_manager.inc.

◆ $dsn

ConnectionManager::$dsn
static

Definition at line 47 of file connection_manager.inc.

◆ $password

ConnectionManager::$password
static

Definition at line 49 of file connection_manager.inc.

◆ $user

ConnectionManager::$user
static

Definition at line 48 of file connection_manager.inc.


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