From d7a2635d5bee27d1039c35e4f57a9818b5f362c7 Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Sat, 28 Feb 2015 20:22:51 -0800 Subject: [PATCH 1/6] added field names for postgres --- include/library/Cana/Db/MySQL/Db.php | 5 +++ include/library/Cana/Db/PostgreSQL/Db.php | 5 ++- include/library/Cana/Table.php | 50 +++++++++++++---------- 3 files changed, 37 insertions(+), 23 deletions(-) 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..58246dfbf 100644 --- a/include/library/Cana/Db/PostgreSQL/Db.php +++ b/include/library/Cana/Db/PostgreSQL/Db.php @@ -29,7 +29,8 @@ 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; } } \ 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); From bb04c0bdb96945bd4d8cf7c0f3ec6f5000ea744d Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Sat, 28 Feb 2015 20:39:13 -0800 Subject: [PATCH 2/6] test heroku auto deploy --- include/library/Cana/Db/Base.php | 1 + 1 file changed, 1 insertion(+) diff --git a/include/library/Cana/Db/Base.php b/include/library/Cana/Db/Base.php index aa7cb7bd2..daedb3a50 100755 --- a/include/library/Cana/Db/Base.php +++ b/include/library/Cana/Db/Base.php @@ -33,6 +33,7 @@ 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) { From 35e217eb3a44adc3b3d3730af8df634560d50c1d Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Sat, 28 Feb 2015 21:21:33 -0800 Subject: [PATCH 3/6] fixed quote issue --- include/library/Cana/Db/PostgreSQL/Db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/library/Cana/Db/PostgreSQL/Db.php b/include/library/Cana/Db/PostgreSQL/Db.php index 58246dfbf..77d715fcc 100644 --- a/include/library/Cana/Db/PostgreSQL/Db.php +++ b/include/library/Cana/Db/PostgreSQL/Db.php @@ -30,7 +30,7 @@ class Cana_Db_PostgreSQL_Db extends Cana_Db_Base { } 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.'"'); + $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; } } \ No newline at end of file From af0a8b6a12a5c01808b7c7da41dfe53d4084d193 Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Sun, 1 Mar 2015 11:53:02 -0800 Subject: [PATCH 4/6] replace backticks --- include/library/Cana/Db/PostgreSQL/Db.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/library/Cana/Db/PostgreSQL/Db.php b/include/library/Cana/Db/PostgreSQL/Db.php index 77d715fcc..c292764d9 100644 --- a/include/library/Cana/Db/PostgreSQL/Db.php +++ b/include/library/Cana/Db/PostgreSQL/Db.php @@ -33,4 +33,14 @@ class Cana_Db_PostgreSQL_Db extends Cana_Db_Base { $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 From 5c04b693da0c15dbd88b3e86669b2c1b1a4dcfbb Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Sun, 1 Mar 2015 11:57:47 -0800 Subject: [PATCH 5/6] exlpicit type binding --- include/library/Cana/Db/Base.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/include/library/Cana/Db/Base.php b/include/library/Cana/Db/Base.php index daedb3a50..97baf3dbe 100755 --- a/include/library/Cana/Db/Base.php +++ b/include/library/Cana/Db/Base.php @@ -35,12 +35,22 @@ class Cana_Db_Base { } 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); From 7ef671ff183be593fb1e269dddff9986ec98a1a9 Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Sun, 1 Mar 2015 12:01:07 -0800 Subject: [PATCH 6/6] add prepared statement --- include/library/Crunchbutton/Config.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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) {