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;
throw new Exception('blah');
}
if ($args && array_keys($args) !== range(0, count($args) - 1)) {
/*
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;
*/
}
$stmt->execute($args);

View File

@ -22,4 +22,9 @@ class Cana_Db_MySQL_Db extends Cana_Db_Base {
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;
}
function getFields() {
//SELECT column_name FROM information_schema.columns WHERE table_name ='table';
function getFields($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 {
$fields = [];
$res = $this->db()->query('SHOW COLUMNS FROM `'.$this->table().'`');
$res = $this->db()->getFields($this->table());
while ($row = $res->fetch()) {
$row->Null = $row->Null == 'YES' ? true : false;
if (strpos($row->Type, 'int') !== false) {
$row->Type = 'int';
$row = get_object_vars($row);
$props = [];
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;
}
@ -209,7 +217,7 @@ class Cana_Table extends Cana_Model { //
} else {
// fill the object with blank properties based on the fields of that table
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) {
switch ($field->Type) {
switch ($field->type) {
case 'int':
$this->{$field->Field} = (int)$this->{$field->Field};
$this->{$field->field} = (int)$this->{$field->field};
break;
}
}
@ -271,33 +279,33 @@ class Cana_Table extends Cana_Model { //
$numset = 0;
foreach ($fields as $field) {
if ($this->property($field->Field) !== false) {
if ($this->property($field->field) !== false) {
if ($this->{$field->Field} == '' && $field->Null) {
$this->{$field->Field} = null;
} elseif ($this->{$field->Field} == null && !$field->Null) {
$this->{$field->Field} = '';
if ($this->{$field->field} == '' && $field->null) {
$this->{$field->field} = null;
} elseif ($this->{$field->field} == null && !$field->null) {
$this->{$field->field} = '';
}
$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);
switch ($field->Type) {
switch ($field->type) {
case 'int':
$value = intval($this->{$field->Field});
$value = intval($this->{$field->field});
break;
default:
$value = $this->{$field->Field};
$value = $this->{$field->field};
break;
}
//var_dump($value);
$value = (!$value && $field->Null) ? null : $value;
$value = (!$value && $field->null) ? null : $value;
$query .= ' `'.$field->Field.'`=:'.$field->Field;
//if (!is_null($this->{$field->Field})) {
$fs[$field->Field] = $value;
$query .= ' `'.$field->field.'`=:'.$field->field;
//if (!is_null($this->{$field->field})) {
$fs[$field->field] = $value;
//}
$numset++;
@ -334,7 +342,7 @@ class Cana_Table extends Cana_Model { //
public function strip() {
$fieldsMeta = $this->fields();
foreach ($fieldsMeta as $field) {
$fields[] = $field->Field;
$fields[] = $field->field;
}
$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
';
if ($site) {
$q .= ' ="'.$site.'"';
$q .= ' = ?';
} else {
$q .= ' is null';
}
$config = Crunchbutton_Config::q($q);
$config = Crunchbutton_Config::q($q, [$site]);
if (!$config->id_config) {
$config = Crunchbutton_Config::blank($key, $value, $site);
}
@ -19,7 +19,7 @@ class Crunchbutton_Config extends Cana_Table {
}
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 ){
return $config->value;
}
@ -27,7 +27,7 @@ class Crunchbutton_Config extends Cana_Table {
}
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) {