Fakoli Framework
connection_manager.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
46 {
47  static $dsn;
48  static $user;
49  static $password;
50  static $conn;
51 
57  static function getConnection()
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  }
78 
83  static function newConnection()
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  }
100 
104  static function releaseConnection()
105  {
107  }
108 
115  static function quote($str)
116  {
117  return ConnectionManager::getConnection()->quote($str);
118  }
119 
127  static function escape($str)
128  {
129  $quoted = ConnectionManager::getConnection()->quote($str);
130  $escaped = preg_replace("/^'(.*)'$/", "$1", $quoted);
131  trace("ESCAPE: $str -> $quoted -> $escaped", 1);
132  return $escaped;
133  }
134 }
static quote($str)
Quote a string value based on the character set of the global connection.
static releaseConnection()
Releases the global connection to the database.
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959
static escape($str)
Escapes a string based on the character set of the global connection.
The ConnectionManager class provides the common point of entry by which DataItems can access the glob...
static getConnection()
Retrieves a reference to the global database connection.
static newConnection()
Returns a new connection to the database.