This commit is contained in:
Pererinha 2015-03-02 12:17:10 -03:00
commit b4d7050b62
5 changed files with 65 additions and 30 deletions

View File

@ -33,13 +33,24 @@ class Cana_Db_Base {
//var_dump($args); exit; //var_dump($args); exit;
throw new Exception('blah'); throw new Exception('blah');
} }
if ($args && array_keys($args) !== range(0, count($args) - 1)) { if ($args && array_keys($args) !== range(0, count($args) - 1)) {
/*
foreach ($args as $key => $value) { foreach ($args as $key => $value) {
$stmt->bindValue(':'.$key, $value); switch (gettype($value)) {
case 'integer':
$type = PDO::PARAM_INT;
break;
case 'string':
$type = PDO::PARAM_STR;
break;
default:
$type = null;
break;
}
$stmt->bindValue(':'.$key, $value, $type);
} }
$args = null; $args = null;
*/
} }
$stmt->execute($args); $stmt->execute($args);

View File

@ -22,4 +22,9 @@ class Cana_Db_MySQL_Db extends Cana_Db_Base {
return $db; return $db;
} }
public function getFields($table) {
$res = $this->db()->query('SHOW COLUMNS FROM `'.$table.'`');
return $res;
}
} }

View File

@ -29,7 +29,18 @@ class Cana_Db_PostgreSQL_Db extends Cana_Db_Base {
return $db; return $db;
} }
function getFields() { function getFields($table) {
//SELECT column_name FROM information_schema.columns WHERE table_name ='table'; $res = $this->db()->query("SELECT column_name as Field, data_type as Type, is_nullable as Null, column_default as Default FROM information_schema.columns WHERE table_name = '".$table."'");
return $res;
}
public function query($query, $args = [], $type = 'object') {
$query = str_replace('`','"', $query);
return parent::query($query, $args, $type);
}
public function exec($query) {
$query = str_replace('`','"', $query);
return parent::exec($query);
} }
} }

View File

@ -122,12 +122,20 @@ class Cana_Table extends Cana_Model { //
} else { } else {
$fields = []; $fields = [];
$res = $this->db()->query('SHOW COLUMNS FROM `'.$this->table().'`'); $res = $this->db()->getFields($this->table());
while ($row = $res->fetch()) { while ($row = $res->fetch()) {
$row->Null = $row->Null == 'YES' ? true : false; $row = get_object_vars($row);
if (strpos($row->Type, 'int') !== false) { $props = [];
$row->Type = 'int';
foreach ($row as $k => $v) {
$props[strtolower($k)] = $v;
}
$row = (object)$props;
$row->null = $row->null == 'YES' ? true : false;
if (strpos($row->type, 'int') !== false) {
$row->type = 'int';
} }
$fields[] = $row; $fields[] = $row;
} }
@ -209,7 +217,7 @@ class Cana_Table extends Cana_Model { //
} else { } else {
// fill the object with blank properties based on the fields of that table // fill the object with blank properties based on the fields of that table
foreach ($this->fields() as $field) { foreach ($this->fields() as $field) {
$this->{$field->Field} = $this->{$field->Field} ? $this->{$field->Field} : ''; $this->{$field->field} = $this->{$field->field} ? $this->{$field->field} : '';
} }
} }
} }
@ -230,9 +238,9 @@ class Cana_Table extends Cana_Model { //
} }
foreach ($this->fields() as $field) { foreach ($this->fields() as $field) {
switch ($field->Type) { switch ($field->type) {
case 'int': case 'int':
$this->{$field->Field} = (int)$this->{$field->Field}; $this->{$field->field} = (int)$this->{$field->field};
break; break;
} }
} }
@ -271,33 +279,33 @@ class Cana_Table extends Cana_Model { //
$numset = 0; $numset = 0;
foreach ($fields as $field) { foreach ($fields as $field) {
if ($this->property($field->Field) !== false) { if ($this->property($field->field) !== false) {
if ($this->{$field->Field} == '' && $field->Null) { if ($this->{$field->field} == '' && $field->null) {
$this->{$field->Field} = null; $this->{$field->field} = null;
} elseif ($this->{$field->Field} == null && !$field->Null) { } elseif ($this->{$field->field} == null && !$field->null) {
$this->{$field->Field} = ''; $this->{$field->field} = '';
} }
$query .= !$numset ? ' SET' : ','; $query .= !$numset ? ' SET' : ',';
//$query .= ' `'.$field->Field.'`='.(is_null($this->{$field->Field}) ? 'NULL' : (':'.$field->Field)); //$query .= ' `'.$field->field.'`='.(is_null($this->{$field->field}) ? 'NULL' : (':'.$field->field));
//var_dump($field); //var_dump($field);
switch ($field->Type) { switch ($field->type) {
case 'int': case 'int':
$value = intval($this->{$field->Field}); $value = intval($this->{$field->field});
break; break;
default: default:
$value = $this->{$field->Field}; $value = $this->{$field->field};
break; break;
} }
//var_dump($value); //var_dump($value);
$value = (!$value && $field->Null) ? null : $value; $value = (!$value && $field->null) ? null : $value;
$query .= ' `'.$field->Field.'`=:'.$field->Field; $query .= ' `'.$field->field.'`=:'.$field->field;
//if (!is_null($this->{$field->Field})) { //if (!is_null($this->{$field->field})) {
$fs[$field->Field] = $value; $fs[$field->field] = $value;
//} //}
$numset++; $numset++;
@ -334,7 +342,7 @@ class Cana_Table extends Cana_Model { //
public function strip() { public function strip() {
$fieldsMeta = $this->fields(); $fieldsMeta = $this->fields();
foreach ($fieldsMeta as $field) { foreach ($fieldsMeta as $field) {
$fields[] = $field->Field; $fields[] = $field->field;
} }
$vars = get_object_vars($this); $vars = get_object_vars($this);

View File

@ -6,11 +6,11 @@ class Crunchbutton_Config extends Cana_Table {
select * from config where `key`="'.$key.'" and id_site select * from config where `key`="'.$key.'" and id_site
'; ';
if ($site) { if ($site) {
$q .= ' ="'.$site.'"'; $q .= ' = ?';
} else { } else {
$q .= ' is null'; $q .= ' is null';
} }
$config = Crunchbutton_Config::q($q); $config = Crunchbutton_Config::q($q, [$site]);
if (!$config->id_config) { if (!$config->id_config) {
$config = Crunchbutton_Config::blank($key, $value, $site); $config = Crunchbutton_Config::blank($key, $value, $site);
} }
@ -19,7 +19,7 @@ class Crunchbutton_Config extends Cana_Table {
} }
public static function getVal( $key ){ public static function getVal( $key ){
$config = Crunchbutton_Config::q( "SELECT * FROM config WHERE `key` = '{$key}'" ); $config = Crunchbutton_Config::q('SELECT * FROM config WHERE `key` = ?', [$key] );
if( $config->value ){ if( $config->value ){
return $config->value; return $config->value;
} }
@ -27,7 +27,7 @@ class Crunchbutton_Config extends Cana_Table {
} }
public static function getConfigByKey( $key ){ public static function getConfigByKey( $key ){
return Crunchbutton_Config::q( "SELECT * FROM config WHERE `key` = '{$key}' LIMIT 1 " ); return Crunchbutton_Config::q('SELECT * FROM config WHERE `key` = ? LIMIT 1', [$key]);
} }
public static function blank($key, $value, $site = null) { public static function blank($key, $value, $site = null) {