diff --git a/include/library/Cana/Db/Base.php b/include/library/Cana/Db/Base.php index aa7cb7bd2..97baf3dbe 100755 --- a/include/library/Cana/Db/Base.php +++ b/include/library/Cana/Db/Base.php @@ -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); diff --git a/include/library/Cana/Db/MySQL/Db.php b/include/library/Cana/Db/MySQL/Db.php index eff9c1ba4..35e476e3e 100755 --- a/include/library/Cana/Db/MySQL/Db.php +++ b/include/library/Cana/Db/MySQL/Db.php @@ -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; + } } \ No newline at end of file diff --git a/include/library/Cana/Db/PostgreSQL/Db.php b/include/library/Cana/Db/PostgreSQL/Db.php index f3cc38b6b..c292764d9 100644 --- a/include/library/Cana/Db/PostgreSQL/Db.php +++ b/include/library/Cana/Db/PostgreSQL/Db.php @@ -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); } } \ No newline at end of file diff --git a/include/library/Cana/Table.php b/include/library/Cana/Table.php index b4d9c7445..10bfe969d 100755 --- a/include/library/Cana/Table.php +++ b/include/library/Cana/Table.php @@ -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); diff --git a/include/library/Crunchbutton/Config.php b/include/library/Crunchbutton/Config.php index 27dd0d17f..377db529a 100644 --- a/include/library/Crunchbutton/Config.php +++ b/include/library/Crunchbutton/Config.php @@ -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) {