88 *
99 * Licensed under the Apache License, Version 2.0:
1010 * http://www.apache.org/licenses/LICENSE-2.0
11- *
12- * A PHP MySQL PDO class similar to the Python MySQLdb.
11+ *
12+ * A PHP MySQL PDO class similar to the Python MySQLdb.
1313 */
1414require (__DIR__ . '/PDO.Log.class.php ' );
1515require (__DIR__ . '/PDO.Iterator.class.php ' );
@@ -58,8 +58,8 @@ public function __construct($Host, $DBPort, $DBName, $DBUser, $DBPassword)
5858 $ this ->parameters = array ();
5959 $ this ->Connect ();
6060 }
61-
62-
61+
62+
6363 private function Connect ()
6464 {
6565 try {
@@ -71,7 +71,7 @@ private function Connect()
7171 }
7272 $ dsn .= 'charset=utf8; ' ;
7373 $ this ->pdo = new PDO ($ dsn ,
74- $ this ->DBUser ,
74+ $ this ->DBUser ,
7575 $ this ->DBPassword ,
7676 array (
7777 //For PHP 5.3.6 or lower
@@ -80,7 +80,7 @@ private function Connect()
8080
8181 //长连接
8282 //PDO::ATTR_PERSISTENT => true,
83-
83+
8484 PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION ,
8585 PDO ::MYSQL_ATTR_USE_BUFFERED_QUERY => true ,
8686 PDO ::MYSQL_ATTR_FOUND_ROWS => true
@@ -95,7 +95,7 @@ private function Connect()
9595 $this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
9696 */
9797 $ this ->connectionStatus = true ;
98-
98+
9999 }
100100 catch (PDOException $ e ) {
101101 $ this ->ExceptionLog ($ e , '' , 'Connect ' );
@@ -124,7 +124,7 @@ private function Init($query, $parameters = null, $driverOptions = array())
124124 try {
125125 $ this ->parameters = $ parameters ;
126126 $ this ->sQuery = $ this ->pdo ->prepare ($ this ->BuildParams ($ query , $ this ->parameters ), $ driverOptions );
127-
127+
128128 if (!empty ($ this ->parameters )) {
129129 if (array_key_exists (0 , $ parameters )) {
130130 $ parametersType = true ;
@@ -147,10 +147,10 @@ private function Init($query, $parameters = null, $driverOptions = array())
147147 $ this ->ExceptionLog ($ e , $ this ->BuildParams ($ query ), 'Init ' , array ('query ' => $ query , 'parameters ' => $ parameters ));
148148
149149 }
150-
150+
151151 $ this ->parameters = array ();
152152 }
153-
153+
154154 private function BuildParams ($ query , $ params = null )
155155 {
156156 if (!empty ($ params )) {
@@ -162,7 +162,7 @@ private function BuildParams($query, $params = null)
162162 foreach ($ parameter as $ key => $ value ){
163163 $ name_placeholder = $ parameter_key ."_ " .$ key ;
164164 // concatenates params as named placeholders
165- $ in .= ": " .$ name_placeholder .", " ;
165+ $ in .= ": " .$ name_placeholder .", " ;
166166 // adds each single parameter to $params
167167 $ params [$ name_placeholder ] = $ value ;
168168 }
@@ -264,15 +264,82 @@ public function insert($tableName, $params = null)
264264 {
265265 $ keys = array_keys ($ params );
266266 $ rowCount = $ this ->query (
267- 'INSERT INTO ` ' . $ tableName . '` (` ' . implode ('`,` ' , $ keys ) . '`)
267+ 'INSERT INTO ` ' . $ tableName . '` (` ' . implode ('`,` ' , $ keys ) . '`)
268268 VALUES (: ' . implode (',: ' , $ keys ) . ') ' ,
269269 $ params
270270 );
271271 if ($ rowCount === 0 ) {
272272 return false ;
273273 }
274274 return $ this ->lastInsertId ();
275- }
275+ }
276+
277+ /**
278+ * insert multi rows
279+ *
280+ * @param string $tableName database table name
281+ * @param array $params structure like [[colname1 => value1, colname2 => value2], [colname1 => value3, colname2 => value4]]
282+ * @return boolean success or not
283+ */
284+ public function insertMulti ($ tableName , $ params = array ())
285+ {
286+ $ rowCount = 0 ;
287+ if (!empty ($ params )) {
288+ $ insParaStr = '' ;
289+ $ insValueArray = array ();
290+
291+ foreach ($ params as $ addRow ) {
292+ $ insColStr = implode ('`,` ' , array_keys ($ addRow ));
293+ $ insParaStr .= '( ' . implode (", " , array_fill (0 , count ($ addRow ), "? " )) . '), ' ;
294+ $ insValueArray = array_merge ($ insValueArray , array_values ($ addRow ));
295+ }
296+ $ insParaStr = substr ($ insParaStr , 0 , -1 );
297+ $ dbQuery = "INSERT INTO {$ tableName } (
298+ ` $ insColStr`
299+ ) VALUES
300+ $ insParaStr " ;
301+ $ rowCount = $ this ->query ($ dbQuery , $ insValueArray );
302+ }
303+ return (bool ) ($ rowCount > 0 );
304+ }
305+
306+ /**
307+ * update
308+ *
309+ * @param string $tableName
310+ * @param array $params
311+ * @param array $where
312+ * @return int affect rows
313+ */
314+ public function update ($ tableName , $ params = array (), $ where = array ())
315+ {
316+ $ rowCount = 0 ;
317+ if (!empty ($ params )) {
318+ $ updColStr = '' ;
319+ $ whereStr = '' ;
320+ $ updatePara = array ();
321+ // Build update statement
322+ foreach ($ params as $ key => $ value ) {
323+ $ updColStr .= "{$ key }=?, " ;
324+ }
325+ $ updColStr = substr ($ updColStr , 0 , -1 );
326+ $ dbQuery = "UPDATE {$ tableName }
327+ SET {$ updColStr }" ;
328+ // where condition
329+ if (is_array ($ where )) {
330+ foreach ($ where as $ key => $ value ) {
331+ // Is there need to add "OR" condition?
332+ $ whereStr .= "AND {$ key }=? " ;
333+ }
334+ $ dbQuery .= " WHERE 1=1 {$ whereStr }" ;
335+ $ updatePara = array_merge (array_values ($ params ), array_values ($ where ));
336+ } else {
337+ $ updatePara = array_values ($ params );
338+ }
339+ $ rowCount = $ this ->query ($ dbQuery , $ updatePara );
340+ }
341+ return $ rowCount ;
342+ }
276343
277344 /**
278345 * @return string
@@ -337,7 +404,7 @@ private function ExceptionLog(PDOException $e, $sql = "", $method = '', $paramet
337404 $ exception = 'Unhandled Exception. <br /> ' ;
338405 $ exception .= $ message ;
339406 $ exception .= "<br /> You can find the error back in the log. " ;
340-
407+
341408 if (!empty ($ sql )) {
342409 $ message .= "\r\nRaw SQL : " . $ sql ;
343410 }
0 commit comments