require_once(dirname(__FILE__).'/functions.php');
require_once(dirname(__FILE__).'/db/table.php');
require_once(dirname(__FILE__).'/db/field.php');
-if (!defined('DB_ERR_PRINT')) define('DB_ERR_PRINT',false);
if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS',''
.'<input type="submit" class="button" name="edit" value="Add"/>'
.'<a class="button" href="#" onclick="return nb.form_clear(this.parentElement)">X</a>'
class db {
- public $tables = array();
+ public $pdo;
public $conn;
+ public $options;
+ public $username = null;
+ public $password = null;
+ public $pdo_error = PDO::ERRMODE_EXCEPTION; # See: http://php.net/manual/en/pdo.error-handling.php
public $title;
public $name;
public $type;
+ public $tables = array();
public $help_criterias = array(
' * or % for wildcar',
' ! to negate',
function __construct($db) {
- $db = is_scalar($db) ? array('conn' => $db) : $db;
+ $db = is_scalar($db) ? array('pdo' => $db) : $db;
foreach ($db as $k=>$v) $this->$k = $v;
- $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$db['conn']));
+ $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
+ #bye($this->pdo);
+ #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
- #bye($db['conn']);
try {
- $this->conn = new PDO($db['conn']);
- if (DB_ERR_PRINT) $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+ $this->conn = new PDO($this->pdo,$this->username,$this->password,$options);
+ if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
+
} catch (PDOException $e) {
+
err($e->getMessage(), "Db.new()");
return false;
+
}
- #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$db['conn']);
- if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$db['conn'],$m)) {
+ #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$this->pdo);
+ if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$this->pdo,$m)) {
$this->name = $m[1];
}
if (empty($this->title)) $this->title = prettyText($this->name);
}
*/
- function query($sql) {
- return $this->query2v($sql);
+ function exec($sql) {
+ return $this->conn->exec($sql);
+ }
+
+ function query($sql,$o='') {
return $this->conn->query($sql);
}
}
function help($tables=null) {
- header('Content-type: text/plain');
+ if (!empty($_SERVER['DOCUMENT_ROOT'])) header('Content-type: text/plain');
if ($tables === null) $tables = $this->tables();
$tables = join('',ar_map('" ".$a."\n"',$tables));
}
function print_header($type) {
+ if (empty($_SERVER['DOCUMENT_ROOT'])) return null;
if ($this->p('format')=='csv') {
header('Content-type: text/csv');
require_once(dirname(__FILE__).'/../functions.php');
require_once(dirname(__FILE__).'/../db.php');
-if (empty($_SERVER['DOCUMENT_ROOT'])) {
+if (false and empty($_SERVER['DOCUMENT_ROOT'])) {
$_REQUEST['db'] = 'rent'; $name = 'tenant';
$_REQUEST['db'] = 'rt'; $name = 'classes';
+ $_REQUEST['db'] = 'mysql'; $name = 'addr';
+ $_REQUEST['db'] = 'izi'; $name = 'zone';
require_once('/opt/rent/www/config/db.php');
#$db = new db('mysql:host=big;port=3306;dbname=rent;user=nico;password=');
print_r($Db->tables());
} else {
err('table.sql(): Unknow db type: '.$this->db->type);
}
- return $this->db->query($sql);
+ return $this->db->conn->query($sql,PDO::FETCH_COLUMN,0);
}
/*
$sql = 'DELETE FROM ' . $this->sql_name() . $where;
debug($sql);
try {
- $this->db->query($sql);
+ $this->db->conn->query($sql);
} catch (PDOException $e) {
return err($e->getMessage(), "Table.delete()");
}