PHP-databaskonstruktion

Log | Files | Refs

insertHandler.php (4168B)


      1 <?php
      2     class InsertHandlerFactory {
      3         public function createHandler($tableName) {
      4             switch ($tableName) {
      5                 case 'Operation':
      6                     return new OperationInsertHandler($tableName);
      7                 default:
      8                     return new GenericInsertHandler($tableName);
      9             }
     10         }
     11     }
     12     
     13     class OperationInsertHandler implements PostHandler {
     14         private $tableName;
     15         private $pdo;
     16 
     17         public function __construct($tableName) {
     18             $this->tableName = $tableName;
     19             $db = dbconnection::getInstance();
     20             $this->pdo = $db->getPdo();
     21         }
     22 
     23         public function handlePostData($data) {
     24             if (!isInsert($data) || !isset($data["OperationName"])) return;
     25 
     26             logg("Inserting into: " . $this->tableName);
     27 
     28             $operationName = $data["OperationName"];
     29             $startDate = $data["StartDate"];
     30             $endDate = $data["EndDate"];
     31             $successRate = (bool) $data["SuccessRate"] ? true : false;
     32             $groupLeader = $data["GroupLeader"];
     33             $incident = $data["Incident"];
     34 
     35             logg("calling from OperationInsertHandler: \$incident = " . $incident);
     36 
     37             list($incidentName, $incidentNumber) = explode(", ", $incident);
     38 
     39             logg("IncidentName= " . $incidentName);
     40             logg("IncidentNumber= " . $incidentNumber);
     41 
     42             $query = "INSERT INTO {$this->tableName} (OperationName, StartDate, EndDate, SuccessRate, GroupLeader, IncidentName, IncidentNumber) VALUES (?, ?, ?, ?, ?, ?, ?)";
     43 
     44             $stmt = $this->pdo->prepare($query);
     45 
     46             $stmt->bindParam(1, $operationName, PDO::PARAM_STR);
     47             $stmt->bindParam(2, $startDate, PDO::PARAM_STR);
     48             $stmt->bindParam(3, $endDate, PDO::PARAM_STR);
     49             $stmt->bindParam(4, $successRate, PDO::PARAM_BOOL);
     50             $stmt->bindParam(5, $groupLeader, PDO::PARAM_STR);
     51             $stmt->bindParam(6, $incidentName, PDO::PARAM_STR);
     52             $stmt->bindParam(7, $incidentNumber, PDO::PARAM_INT);
     53 
     54             if (!$stmt->execute()) {
     55                 throw new Exception("Failed to insert data into {$this->tableName}.");
     56             }
     57 
     58             RefreshTables();
     59         }
     60 
     61         public function getOperationType() {
     62             return "INSERT";
     63         }
     64     }
     65 
     66     class GenericInsertHandler implements PostHandler {
     67         private $tableName;
     68         private $pdo;
     69 
     70         public function __construct($tableName) {
     71             $this->tableName = $tableName;
     72             $db = dbconnection::getInstance();
     73             $this->pdo = $db->getPdo();
     74         }
     75 
     76         public function handlePostData($data) { 
     77             if (!isInsert($data)) return;
     78 
     79             logg("Inserting into: " . $this->tableName);
     80 
     81             unset($data['tableName']);
     82             unset($data['operationType']);
     83 
     84             $data = $this->convertStringsToAppropriateTypes($data);
     85 
     86             $columns = array_keys($data);
     87             $placeholders = array_fill(0, count($columns), '?');
     88             
     89             $sql = "INSERT INTO {$this->tableName} (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
     90             
     91             $stmt = $this->pdo->prepare($sql);
     92 
     93             if (!$stmt->execute(array_values($data))) {
     94                 throw new Exception("Failed to insert data into {$this->tableName}.");
     95             }
     96 
     97             RefreshTables();
     98         }
     99 
    100         private function convertStringsToAppropriateTypes(array $data): array {
    101             foreach ($data as $key => $value) {
    102                 if (strtolower($value) === 'true') {
    103                     $data[$key] = 1;
    104                 } elseif (strtolower($value) === 'false') {
    105                     $data[$key] = 0;
    106                 } elseif (is_string($value) && ctype_digit($value)) {
    107                     $data[$key] = (int) $value;
    108                 }
    109             }
    110             return $data;
    111         }
    112 
    113         public function getOperationType() {
    114             return "INSERT";
    115         }
    116     }
    117 
    118     function isInsert($postData) {
    119         return $postData["operationType"] === "INSERT" ? true : false;
    120     }
    121 ?>