Browse Source

Give better errors. Return row count even if PDO doesn't want to behave. Attempt to return the lastInsertId properly

Nathaniel van Diepen 6 years ago
parent
commit
4bc7f6a47d
4 changed files with 41 additions and 9 deletions
  1. 1 1
      Data/securestring.class.php
  2. 21 0
      PDO/table.class.php
  3. 4 4
      orm.abstract.class.php
  4. 15 4
      pdo.class.php

+ 1 - 1
Data/securestring.class.php

@@ -18,7 +18,7 @@
 					$this->password = openssl_random_pseudo_bytes($ivlen);
 					$this->iv = openssl_random_pseudo_bytes($ivlen);
 					$this->data = $this->encryption->encrypt($data, $this->password, $this->iv);
-				}catch(Exception $e){}
+				}catch(\Exception $e){}
 				if(++$tries == count($methods)){
 					throw new \Exception("Too many failed attempts to encrypt data");
 				}

+ 21 - 0
PDO/table.class.php

@@ -435,5 +435,26 @@
 		public function count(array $filter = null){
 			return $this->exists ? $this->pdo->exec("select 1 from `{$this->name}` {$this->pdo->stringFilter($filter)}") : 0;
 		}
+		public function lastInsertId(){
+			if(!$this->exists){
+				return 0;
+			}
+			$id = $this->pdo->lastInsertId();
+			if($id === 0){
+				$col;
+				foreach($this->primaryKey as $key => $name){
+					$column = $this->column($name);
+					if($column['increment']){
+						$col = $name;
+						break;
+					}
+				}
+				if(count($filter) > 0){
+					$query = $this->pdo->query("select {$col} from `{$this->name}` where {$col} = (select max({$col}) from `{$this->name}`)");
+					$id = $query->fetchColumn();
+				}
+			}
+			return $id;
+		}
 	}
 ?>

+ 4 - 4
orm.abstract.class.php

@@ -321,15 +321,15 @@
 					if($table->update($data, [
 						$pk => $this->id
 					]) === 0){
-						trigger_error("Save of {$this->name} may have failed. No affected rows.", E_USER_WARNING);
+						trigger_error("Save of {$this->name}#{$this->id} may have failed. No affected rows.", E_USER_WARNING);
 					}
 				}else{
 					if($table->insert($data) === 0){
-						trigger_error("First save of {$this->name} may have failed. No affected rows.", E_USER_WARNING);
+						trigger_error("First save of {$this->name} instance may have failed. No affected rows.", E_USER_WARNING);
 					}
-					$this->_data[$pk] = self::$pdo->lastInsertId();
+					$this->_data[$pk] = $table->lastInsertId();
 					if($this->_data[$pk] === 0){
-						trigger_error("First save of {$this->name} may have failed. PK is 0", E_USER_WARNING);
+						trigger_error("First save of {$this->name} instance may have failed. PK is 0", E_USER_WARNING);
 					}
 				}
 				foreach($this->_related as $related){

+ 15 - 4
pdo.class.php

@@ -94,13 +94,18 @@
 		}
 		public function exec(string $statement){
 			$query = $this->prepare($statement);
-			$count = 0;
 			$query->execute();
+			$count = 0;
 			while($query->fetch() !== false){
 				$count++;
 			}
-			if($count == 0){
-				$count = $query->rowCount();
+			if($count === 0){
+				$query2 = $this->query("select ROW_COUNT()");
+				$count = $query2->fetchColumn();
+				$query2->closeCursor();
+				if($count === 0){
+					$count = $query->rowCount();
+				}
 			}
 			$query->closeCursor();
 			return $count;
@@ -139,7 +144,13 @@
 			return $this->pdo->getAttribute(...$args);
 		}
 		public function lastInsertId(...$args){
-			return $this->pdo->lastInsertId(...$args);
+			$id = $this->pdo->lastInsertId(...$args);
+			if($id === 0){
+				$query = $this->pdo->query("select LAST_INSERT_ID()");
+				$id = $query->fetchColumn();
+				$query->closeCursor();
+			}
+			return $id;
 		}
 		public function getError(){
 			$error = $this->pdo->errorInfo();