@@ -41,9 +41,9 @@ class Model
4141 // sub class must also redeclare public static $_db;
4242
4343 /**
44- * @var \PDOStatement[]
44+ * @var array<int, array<string, \PDOStatement>>
4545 */
46- protected static $ _stmt = array (); // prepared statements cache
46+ protected static $ _stmt = array (); // prepared statements cache keyed by PDO connection and SQL
4747
4848 /**
4949 * @var string|null
@@ -226,10 +226,13 @@ public function __isset($name)
226226 */
227227 public static function connectDb (string $ dsn , string $ username , string $ password , array $ driverOptions = array ()): void
228228 {
229+ $ previousDb = static ::$ _db ;
230+ if ($ previousDb instanceof \PDO ) {
231+ unset(static ::$ _stmt [spl_object_id ($ previousDb )]);
232+ }
229233 static ::$ _db = new \PDO ($ dsn , $ username , $ password , $ driverOptions );
230234 static ::$ _db ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION ); // Set Errorhandling to Exception
231235 static ::$ _identifier_quote_character = null ;
232- static ::$ _stmt = array ();
233236 self ::$ _tableColumns = array ();
234237 static ::_setup_identifier_quote_character ();
235238 }
@@ -966,7 +969,7 @@ public function insert($autoTimestamp = true, $allowSetPrimaryKey = false)
966969 return false ;
967970 }
968971
969- if ($ set ['sql ' ] === '' ) {
972+ if (count ( $ set ['columns ' ]) === 0 ) {
970973 if ($ driver === 'sqlite ' || $ driver === 'sqlite2 ' ) {
971974 $ query = 'INSERT INTO ' . static ::_quote_identifier (static ::$ _tableName ) . ' DEFAULT VALUES ' ;
972975 $ st = static ::execute ($ query );
@@ -975,7 +978,9 @@ public function insert($autoTimestamp = true, $allowSetPrimaryKey = false)
975978 $ st = static ::execute ($ query );
976979 }
977980 } else {
978- $ query = 'INSERT INTO ' . static ::_quote_identifier (static ::$ _tableName ) . ' SET ' . $ set ['sql ' ];
981+ $ query = 'INSERT INTO ' . static ::_quote_identifier (static ::$ _tableName ) .
982+ ' ( ' . implode (', ' , $ set ['columns ' ]) . ') ' .
983+ ' VALUES ( ' . implode (', ' , $ set ['values ' ]) . ') ' ;
979984 $ st = static ::execute ($ query , $ set ['params ' ]);
980985 }
981986 if ($ st ->rowCount () == 1 ) {
@@ -1051,11 +1056,15 @@ protected static function _prepare($query)
10511056 if (!$ db ) {
10521057 throw new \Exception ('No database connection setup ' );
10531058 }
1054- if (!isset (static ::$ _stmt [$ query ])) {
1059+ $ connectionId = spl_object_id ($ db );
1060+ if (!isset (static ::$ _stmt [$ connectionId ])) {
1061+ static ::$ _stmt [$ connectionId ] = array ();
1062+ }
1063+ if (!isset (static ::$ _stmt [$ connectionId ][$ query ])) {
10551064 // cache prepared query if not seen before
1056- static ::$ _stmt [$ query ] = $ db ->prepare ($ query );
1065+ static ::$ _stmt [$ connectionId ][ $ query ] = $ db ->prepare ($ query );
10571066 }
1058- return static ::$ _stmt [$ query ]; // return cache copy
1067+ return static ::$ _stmt [$ connectionId ][ $ query ]; // return cache copy
10591068 }
10601069
10611070 /**
@@ -1138,7 +1147,7 @@ protected function setString($ignorePrimary = true)
11381147 protected static function supportsUpdateLimit ()
11391148 {
11401149 $ driver = static ::getDriverName ();
1141- return ($ driver === 'mysql ' || $ driver === ' sqlite ' || $ driver === ' sqlite2 ' );
1150+ return ($ driver === 'mysql ' );
11421151 }
11431152
11441153 /**
@@ -1150,7 +1159,7 @@ protected static function supportsUpdateLimit()
11501159 protected static function supportsDeleteLimit ()
11511160 {
11521161 $ driver = static ::getDriverName ();
1153- return ($ driver === 'mysql ' || $ driver === ' sqlite ' || $ driver === ' sqlite2 ' );
1162+ return ($ driver === 'mysql ' );
11541163 }
11551164
11561165 /**
0 commit comments