From e2f5ecdbca0057f8560c189878a40b87a6b63fde Mon Sep 17 00:00:00 2001 From: origami11 Date: Mon, 23 Jan 2017 17:30:04 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A2=D0=B8=D0=BF=20=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D1=8C=D0=BC=D0=B0.=20=D0=AD=D0=BA=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D1=87=D0=B5=D0=BA=20=D0=B2=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/database_pdo.php | 4 ++-- core/mail.php | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/database_pdo.php b/core/database_pdo.php index 21c1285..0c7770c 100644 --- a/core/database_pdo.php +++ b/core/database_pdo.php @@ -72,7 +72,7 @@ class Database extends PDO return $sth->fetch(); } - private static function assignQuote($x, $y) + private function assignQuote($x, $y) { return $x . "=" . $this->quote($y); } @@ -92,7 +92,7 @@ class Database extends PDO function updateQuery($table, array $values, $cond) { return $this->query("UPDATE $table SET " . implode(",", - array_map(array('self', 'assignQuote'), array_keys($values), array_values($values))) . " WHERE $cond"); + array_map(array($this, 'assignQuote'), array_keys($values), array_values($values))) . " WHERE $cond"); } function getIdGenerator() { diff --git a/core/mail.php b/core/mail.php index 53fa161..3bd2ef1 100644 --- a/core/mail.php +++ b/core/mail.php @@ -17,6 +17,7 @@ class Mail protected $attachment = array (); protected $uniqid; + protected $type = "text/plain"; function __construct() { $this->setEncoding("UTF-8"); @@ -70,6 +71,11 @@ class Mail $this->content = $text; } + function setType($type) + { + $this->type = $type; + } + /** * Кодировка текста в письме */ @@ -134,7 +140,7 @@ class Mail function getMessage() { $message = "--".$this->uniqid . PHP_EOL; - $message .= $this->mimeTag("Content-Type", "text/plain", array ('charset' => $this->encoding)); + $message .= $this->mimeTag("Content-Type", $this->type, array ('charset' => $this->encoding)); $message .= $this->mimeTag("Content-Transfer-Encoding", "8bit"); $message .= PHP_EOL . $this->content . PHP_EOL . PHP_EOL; From 5089a652eb6aa70b29ef8044eecb819a3a8fd1f6 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Thu, 9 Feb 2017 11:07:18 +0300 Subject: [PATCH 2/3] =?UTF-8?q?InsertQuery=20=D0=B8=D0=B7=20cms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/database_pdo.php | 51 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/core/database_pdo.php b/core/database_pdo.php index 0c7770c..0c0639f 100644 --- a/core/database_pdo.php +++ b/core/database_pdo.php @@ -18,6 +18,9 @@ class Database extends PDO return $this->dsn; } + public function isPostgres(){ + return ($this->dsn["phptype"] == "pgsql"); + } /** * Создает соединение с базой данных */ @@ -77,13 +80,55 @@ class Database extends PDO return $x . "=" . $this->quote($y); } + private function prepareValues($values) + { + if (!$values) { + return null; + } + $pg = $this->isPostgres(); + $prep = array(); + foreach ($values as $key => $value) { + $result = null; + if(is_bool($value)) { + if ($pg) { + $result = $value ? 'true' : 'false'; + } else { + $result = $value ? 1 : 0; + } + } else { + $result = $value; + } + $prep[":" . $key] = $result; + } + return $prep; + } /** * Создает INSERT запрос */ - function insertQuery($table, array $values) + function insertQuery($table, array $values, $return_id = false, $index = null) { - return $this->query("INSERT INTO $table (" . implode(",", array_keys($values)) - . ") VALUES (" . implode(",", array_map(array($this, 'quote'), array_values($values))) . ")"); + $prep = $this->prepareValues($values); + + $sql = "INSERT INTO $table (" . implode(",", array_keys($values)) + . ") VALUES (" . implode(",", array_keys($prep)). ")"; + + if($return_id){ + if ($this->isPostgres()){ + $sql = $sql." RETURNING $index"; + } + } + $stmt = $this->prepare($sql); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $stmt->execute($prep); + $result = $stmt->fetch(); + if ($return_id) { + if ($this->isPostgres()) { + return $result[$index]; + } else { + $result = $this->fetchOneArray("SELECT $index AS lastid FROM $table WHERE OID = last_insert_rowid()"); + return $result['lastid']; + } + } } /** From 0a8c482b41a2e9d15449c1284933c044a62eb549 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Thu, 9 Feb 2017 12:59:38 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/database_pdo.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/database_pdo.php b/core/database_pdo.php index 0c0639f..f8db49c 100644 --- a/core/database_pdo.php +++ b/core/database_pdo.php @@ -56,23 +56,23 @@ class Database extends PDO /** * Извлекает из базы все элементы по запросу */ - public function fetchAllArray($query) + public function fetchAllArray($query,$values=null) { $sth = $this->prepare($query); - $sth->setFetchMode(PDO::FETCH_ASSOC); - $sth->execute(); - return $sth->fetchAll(); + $prep = $this->prepareValues($values); + $sth->execute($prep); + return $sth->fetchAll(PDO::FETCH_ASSOC); } /** * Извлекает из базы первый элемент по запросу */ - public function fetchOneArray($query) + public function fetchOneArray($query,$values=null) { $sth = $this->prepare($query); - $sth->setFetchMode(PDO::FETCH_ASSOC); - $sth->execute(); - return $sth->fetch(); + $prep = $this->prepareValues($values); + $sth->execute($prep); + return $sth->fetch(PDO::FETCH_ASSOC); } private function assignQuote($x, $y)