private $autoincrement = false;
- function __construct($attr=array()) {
+ public function __construct($attr=array()) {
if (is_scalar($attr)) $attr = array('name'=>$attr);
foreach ($attr as $k => $v) { $this->$k = $v; }
}
- function autoincrement() {
+ public function autoincrement() {
return $this->autoincrement;
}
- function numeric() {
+ public function numeric() {
return preg_match('/int|float|number|currency/',$this->type) ? true : false;
}
- function html_edit($value) {
+ public function html_edit($value) {
return '<div class="label '.$this->name.'"><label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
.($this->extra
}
- function db() {
+ public function db() {
global $Db;
return ( isset($Db) ? $Db : null );
}
- function sql_name() {
+ public function sql_name() {
return $this->db()->sql_name($this->name);
}
- function quote($value,$force_quote=false) {
-
- if ($value === null) return 'NULL';
- if ($force_quote or !$this->numeric()) {
-
- if (($db = $this->db()) === null) return "'".preg_replace("/'/","\\'",$value)."'";
- return $db->quote($value);
-
- } else {
-
- if (strcmp($value,'') == 0) $value = 'NULL';
- #if ($value === null) $value = 'NULL';
- }
-
- return $value;
- }
-
- function size() {
+ public function size() {
if (! preg_match('/\((\d+)(?:,(\d+)\))?/',$this->type,$m) ) return null;
array_shift($m);
return count($m) == 1 ? $m[0] : $m;
// NB 02.08.15 return preg_match('/\((\d+)\)/',$this->type,$m) ? $m[1] : null;
}
- function where($values) {
+ public function where($values) {
// No empty value
$v = isset($value) ? $value : null;
if (strcmp($v,'')==0
return "$k$equal$v";
}
- function pdo_type() {
+ public function quote($value,$force_quote=false) {
+
+ if ($value === null) return 'NULL';
+ if ($force_quote or !$this->numeric()) {
+
+ if (($db = $this->db()) === null) return "'".preg_replace("/'/","\\'",$value)."'";
+ return $db->quote($value);
+
+ } else {
+
+ if (strcmp($value,'') == 0) $value = 'NULL';
+ #if ($value === null) $value = 'NULL';
+ }
+
+ return $value;
+ }
+
+ /*
+ # - NB 25.03.16
+ * Return the value quoted
+ public function sql($v) {
+ $type = $this->pdo_type($this->type);
+ if ($type === PDO::PARAM_NULL) {
+ return 'NULL';
+ } elseif ($type === PDO::PARAM_INT) {
+ return $v;
+ } elseif ($type === PDO::PARAM_BOOL) {
+ return $v ? 1 : 0;
+ } else {
+ return "'".str_replace("'","\\'",$v)."'";
+ }
+ }
+ */
+
+ public function pdo_type() {
if (func_get_args()>0 and func_get_arg(0) === null) return PDO::PARAM_NULL;
if ($this->numeric()) return PDO::PARAM_INT;
if (strpos($this->type,'bool') !== false ) return PDO::PARAM_BOOL;
return PDO::PARAM_STR;
}
- function bindParam(&$sth,$value,$name=null) {
+ public function bindParam(&$sth,$value,$name=null) {
# See: http://php.net/manual/en/pdostatement.bindparam.php
if ($name === null) $name = ':'.$this->name;
#debug($this->name . " | $name | $value | $this->type | " . $this->size()."<br/>");
// NB 27.07.15 return $sth->bindParam($name, $value, $this->pdo_type($value));
}
- function out($value,$html_escape=false) {
+ public function out($value,$html_escape=false) {
#if ($value === null) return '';
static $sqlite = null; if ($sqlite === null) $sqlite = $this->db()->type == 'sqlite';
$field['default'] = $row[$f];
}
- foreach (array('pk','key') as $f) {
+ foreach (array('pk','key','Key') as $f) {
if (!isset($row[$f])) continue;
- $field['key'] = preg_match('/^1|yes|t/i',$row[$f]) ? 1 : 0;
+ $field['key'] = preg_match('/^1|yes|t|PRI/i',$row[$f]) ? 1 : 0;
}
$this->fields[$field['name']] = new field($field);
}
if (empty($this->fields)) bye("Table `".$this->name."` does not exists into database ".$this->db->name."!");
- }
+
+ } # < $this->fields
#bye($this->fields);
if ($name !== null ) {
return $this->db->exec($sql);
}
- public function update($hvalues) {
+ public function update($hvalues,&$info=null) {
+ # dbq t=nb.mime_type a=table.update type=application/rss+xml name='RSS - Really Simple Syndication' ext=rss debug=2
$keys = array();
$keys_values = array();
$fields = array();
$fields_values = array();
- $values = array();
+
+ #$update = array();
+ #$where = array();
foreach ($this->fields() as $name => $field) {
- if (!isset($hvalues[$name])) continue;
+ if (!isset($hvalues[$name])) {
+ if ($field->key) $this->bye("Missing `$name`!");
+ continue;
+ }
+
$value = $hvalues[$name];
if ($field->key) {
$keys[] = $name;
- $keys_values[] = $name;
+ $keys_values[] = $value;
} else {
$fields[] = $name;
- $fields_values[] = $name;
+ $fields_values[] = $value;
}
}
+ if (empty($fields)) $this->bye("Missing keys!");
if (empty($keys)) {
+ $this->bye("Missing keys!");
$keys = $fields;
$keys_values = $fields_values;
}
.' WHERE ' . join(' AND ',$this->ar_map('"$a=:key_$a"',$keys))
;
- $sql = ''
- .'SELECT * FROM ' . $this->sql_name()
- .' WHERE ' . join(' AND ',$this->ar_map('"$a=:key_$a"',$keys))
- ;
-
- #bye($sql);
- $this->debug($sql,1);
-
if (!($query = $this->db->conn->prepare($sql))) {
err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .NB_EOL);
return false;
foreach ($fields as $name) {
#debug("$name: ".$hvalues[$name]);
- #$field->bindParam($query,$hvalues[$name],":$name");
+ $field->bindParam($query,$hvalues[$name],":$name");
}
foreach ($keys as $name) {
#debug("$name: ".$hvalues[$name]);
- #$field->bindParam($query,$hvalues[$name],":key_$name");
- $query->bindValue(":key_$name", $hvalues[$name], PDO::PARAM_STR);
+ $field->bindParam($query,$hvalues[$name],":key_$name");
}
#return $sql;
#bye($sql);
- if (!($execute = $query->execute())) {
+ if (!($ex = $query->execute())) {
err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .NB_EOL);
return false;
}
#debug($execute);
- debug($query->fetch());
- return $execute;
+ $info = array('sql'=>$sql,'values'=>$hvalues,'return'=>$query->rowCount());
+ $this->debug($info,1);
+ return $query->rowCount();
}
} elseif ($action == 'table.update' or $action == 'update') {
#$this->bye($this->p());
- $this->update($this->p());
- if ($this->p('referer')) {
- header('Location: '.urldecode($this->p('referer')));
- } else {
- header('Location: ?table=' . $this->name . ($this->p('db') ? '&db='.$this->p('db') : ''));
+ if ($r=$this->update($this->p(),$out)) {
+ if ($this->p('referer')) {
+ header('Location: '.urldecode($this->p('referer')));
+ } else {
+ header('Location: ?table=' . $this->name . ($this->p('db') ? '&db='.$this->p('db') : ''));
+ }
}
#header('Location: '.str_replace('&','&',$this->url_list()));
- return true;
+ $this->out2($out);
+ return $r;
} elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) {
s/^define(\([^,]\+\)/if (!defined(\1)) define(\1/
*/
-if (!defined('DEBUG')) define('DEBUG',(int)nb::p('debug'));
// NB 28.06.15 require_once((dirname(__FILE__).'/default.php'));
function file_write($file,$data,$mode='w') {
if (!$ftmp = fopen($file,$mode)) {
- if (DEBUG) warn("file_write(): Cant open $file in mode $mode");
+ bye("file_write(): Cant open $file in mode $mode");
return;
}
if (fwrite($ftmp,$data) === FALSE) {
- if (DEBUG) warn("file_write(): Cant write $file in mode $mode");
+ bye("file_write(): Cant write $file in mode $mode");
return;
}
}
function debug($msg,$level=0) {
- if ($level and $level>DEBUG) return;
+ if ($level and $level>nb::p('debug')) return;
$msg = is_scalar($msg) ? $msg : print_r($msg,true);
if (nb::client_header('Accept','ml')) {
if (isset($o['enclose'])) echo $o['enclose'][1];
}
+ public static function rows_get($type,$data,$head=array()) {
+ ob_start();
+ self::rows($type,$data,$head);
+ return ob_get_flush();
+ }
public static function rows($type,&$data,$head=array()) {
# Ex: for action=tables when header=0