Updated the HEAD build version to 20090527
[moodle.git] / lib / dmllib.php
CommitLineData
7e13be08 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
ddaadc7c 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
7e13be08 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
eef868d1 26/// This library contains all the Data Manipulation Language (DML) functions
7e13be08 27/// used to interact with the DB. All the dunctions in this library must be
28/// generic and work against the major number of RDBMS possible. This is the
29/// list of currently supported and tested DBs: mysql, postresql, mssql, oracle
30
9f948d11 31/// This library is automatically included by Moodle core so you never need to
32/// include it yourself.
7e13be08 33
34/// For more info about the functions available in this library, please visit:
35/// http://docs.moodle.org/en/DML_functions
36/// (feel free to modify, improve and document such page, thanks!)
37
8aff8482 38require_once($CFG->libdir.'/dml/moodle_database.php');
39
49459eb0 40/**
41 * DML exception class, use instead of error() in dml code.
42 */
43class dml_exception extends moodle_exception {
44 function __construct($errorcode, $a=NULL, $debuginfo=null) {
45 parent::__construct($errorcode, '', '', $a, $debuginfo);
46 }
47}
48
ce152606 49/**
50 * DML db connection exception - triggered if database not accessible.
51 */
52class dml_connection_exception extends dml_exception {
53 function __construct($error) {
54 $errorinfo = '<em>'.s($error).'</em>';
55 parent::__construct('dbconnectionfailed', NULL, $errorinfo);
56 }
57}
58
9214025e 59/**
60 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
61 */
62class dml_read_exception extends dml_exception {
63 public $error;
64 public $sql;
65 public $params;
66
67 function __construct($error, $sql=null, array $params=null) {
68 $this->error = $error;
69 $this->sql = $sql;
70 $this->params = $params;
71 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
72 parent::__construct('dmlreadexception', NULL, $errorinfo);
73 }
74}
75
76/**
77 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
78 */
79class dml_write_exception extends dml_exception {
80 public $error;
81 public $sql;
82 public $params;
83
84 function __construct($error, $sql=null, array $params=null) {
85 $this->error = $error;
86 $this->sql = $sql;
87 $this->params = $params;
88 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
89 parent::__construct('dmlwriteexception', NULL, $errorinfo);
90 }
91}
92
8aff8482 93/**
94 * Sets up global $DB moodle_database instance
95 * @return void
96 */
97function setup_DB() {
98 global $CFG, $DB;
99
100 if (isset($DB)) {
101 return;
102 }
103
104 if (!isset($CFG->dbuser)) {
105 $CFG->dbuser = '';
106 }
107
108 if (!isset($CFG->dbpass)) {
109 $CFG->dbpass = '';
110 }
111
112 if (!isset($CFG->dbname)) {
113 $CFG->dbname = '';
114 }
115
8aff8482 116 if (!isset($CFG->dblibrary)) {
3eae57b7 117 switch ($CFG->dbtype) {
118 case 'postgres7' :
119 $CFG->dbtype = 'pgsql';
120 // continue, no break here
121 case 'pgsql' :
122 $CFG->dblibrary = 'native';
123 break;
124
125 case 'mysql' :
a7b4fc7f 126 if (!extension_loaded('mysqli')) {
127 $CFG->dblibrary = 'adodb';
128 break;
129 }
3eae57b7 130 $CFG->dbtype = 'mysqli';
131 // continue, no break here
132 case 'mysqli' :
133 $CFG->dblibrary = 'native';
134 break;
135
136 default:
137 // the rest of drivers is not converted yet - keep adodb for now
138 $CFG->dblibrary = 'adodb';
139 }
8aff8482 140 }
141
142 if (!isset($CFG->dboptions)) {
143 $CFG->dboptions = array();
144 }
145
beaa43db 146 if (isset($CFG->dbpersist)) {
147 $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
148 }
149
3eae57b7 150 if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
151 throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
152 }
8aff8482 153
3eae57b7 154 try {
ce152606 155 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
156 } catch (moodle_exception $e) {
8aff8482 157 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
158 if (file_exists($CFG->dataroot.'/emailcount')){
ce152606 159 $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
160 $content = @fread($fp, 24);
161 @fclose($fp);
8aff8482 162 if((time() - (int)$content) > 600){
163 @mail($CFG->emailconnectionerrorsto,
164 'WARNING: Database connection error: '.$CFG->wwwroot,
165 'Connection error: '.$CFG->wwwroot);
ce152606 166 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
167 @fwrite($fp, time());
8aff8482 168 }
169 } else {
170 @mail($CFG->emailconnectionerrorsto,
171 'WARNING: Database connection error: '.$CFG->wwwroot,
172 'Connection error: '.$CFG->wwwroot);
ce152606 173 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
174 @fwrite($fp, time());
8aff8482 175 }
176 }
ce152606 177 // rethrow the exception
178 throw $e;
8aff8482 179 }
180
ce152606 181 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
182
8aff8482 183 return true;
184}