Framework  3.9
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 
34 require_once realpath(dirname(__FILE__)."/cache.inc");
35 
48 {
49  static $dsn;
50  static $user;
51  static $password;
52  static $conn;
53  static $options = null;
59  static function getConnection()
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  }
80 
85  static function newConnection()
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  }
102 
106  static function releaseConnection()
107  {
109  }
110 
117  static function quote($str)
118  {
119  return ConnectionManager::getConnection()->quote($str);
120  }
121 
129  static function escape($str)
130  {
131  $quoted = ConnectionManager::getConnection()->quote($str);
132  $escaped = preg_replace("/^'(.*)'$/", "$1", $quoted);
133  trace("ESCAPE: $str -> $quoted -> $escaped", 1);
134  return $escaped;
135  }
136 
141  static function getVersion()
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  }
153 }
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
The ConnectionManager class provides the common point of entry by which DataItems can access the glob...
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.
static newConnection()
Returns a new connection to the database.
static getVersion()
Determine the version of the connected database.
static getConnection()
Retrieves a reference to the global database connection.
static escape($str)
Escapes a string based on the character set of the global connection.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010