Advertisement
Valleri

sql

Sep 1st, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.52 KB | None | 0 0
  1. <?php
  2. $commands = array("DELETE FROM users WHERE  (user_id = 5)" ,
  3.     "UPDATE users SET  (age = 10) WHERE  (user_id = 1)",
  4. "DELETE FROM users WHERE  (id = 5)" ,
  5. "UDATE users SET  (age = 10) WHERE  (user_id = 1)" );
  6.  
  7.  
  8.  
  9. //$commands = $_GET['commands'];
  10.  
  11. $usersDatabase = array();
  12. $db = "";
  13. $lastId = 0;
  14. $errors = 0;
  15. $insert = "/^(.*?)\s*INTO\s*(.*?)\s*\((.*?)\)\s*VALUES\s*\((.*?)\)$/";
  16. $update = "/^(\w+)\s*(.*?)\s*SET\s*\((.*?)\s*=\s*(.*?)\)\s*WHERE\s*\((.*?)\s*=\s*(.*?)\)$/";
  17. $delete = "/^(.*?)\s*FROM\s*(.*?)\s*WHERE\s*\((.*?)\s*=\s*(.*?)\)$/";
  18.  
  19. //model of a user
  20. $user = array(
  21.     "user_id",
  22.     "login",
  23.     "gender",
  24.     "age"
  25. );
  26.  
  27. function insert($insertionData) {
  28.     $keys = preg_split("/,\s*/", $insertionData[3]);
  29.     $values = preg_split("/,\s*/", $insertionData[4]);
  30.     $user = array(
  31.         "user_id" => $GLOBALS['lastId'],
  32.         "login" => "",
  33.         "gender" => "undefined",
  34.         "age" => "undefined"
  35.     );
  36.  
  37.     for($i = 0; $i < count($keys) ;$i++) {
  38.  
  39.         if($keys[$i] == "user_id") {
  40.             if($GLOBALS['lastId'] < $values[$i]) {
  41.                 $GLOBALS['lastId'] = $values[$i];
  42.             } else {
  43.                 $values[$i] = $GLOBALS['lastId'];
  44.             }
  45.         }
  46.         $user[$keys[$i]] = $values[$i];
  47.     }
  48.     $GLOBALS['usersDatabase'][] = $user;
  49.     $GLOBALS['lastId']++;
  50. }
  51.  
  52. function update($updates) {
  53.     $valueToUpdate = $updates[3];
  54.     $value = $updates[4];
  55.     $conditionId = $updates[5];
  56.     $conditionValue = $updates[6];
  57.     $found = false;
  58.     foreach ($GLOBALS['usersDatabase'] as &$user) {
  59.         if($user[$conditionId] == $conditionValue) {
  60.             $user[$valueToUpdate] = $value;
  61.             $found = true;
  62.         }
  63.     }
  64.     if(!$found) {
  65.         $GLOBALS['errors']++;
  66.     }
  67. }
  68.  
  69. function delete($deletions) {
  70.     $key = $deletions[3];
  71.     $value = $deletions[4];
  72.  
  73.     foreach ($GLOBALS['usersDatabase'] as $k => $v) {
  74.         if($GLOBALS['usersDatabase'][$k][$key] == $value) {
  75.             unset($GLOBALS['usersDatabase'][$k]);
  76.         }
  77.     }
  78. }
  79.  
  80. //game loop
  81. for($i = 0; $i < count($commands) ;$i++) {
  82.     $currCommand = $commands[$i];
  83.     preg_match($insert, $currCommand, $insertions);
  84.     preg_match($update, $currCommand, $updates);
  85.     preg_match($delete, $currCommand, $deletions);
  86.     if($insertions) {
  87.         $keys = preg_split("/,\s*/", $insertions[3]);
  88.         $diff = array_diff($keys, $user);
  89.         $values = preg_split("/,\s*/", $insertions[4]);
  90.         if($insertions[1] != "INSERT" || count($keys) != count($values) ||
  91.             !in_array("login", preg_split("/,\s*/", $insertions[3])) ||
  92.              count($diff) != 0) {
  93.             $errors++;
  94.             continue;
  95.         } else {
  96.             if($insertions[2] != "") {
  97.                 if($db == "") {
  98.                     $db = $insertions[2];
  99.                 }
  100.  
  101.                 if($insertions[2] == $db) {
  102.                     insert($insertions);
  103.                 }
  104.             }
  105.         }
  106.     } elseif($updates) {
  107.         if( !in_array($updates[3], $user) ||
  108.             !in_array($updates[5], $user) ||
  109.             $updates[1] != "UPDATE" ||
  110.             $updates[6] == "" ||
  111.             $updates[3] == 'user_id' ||
  112.             $db != $updates[2] ||
  113.             $db == "") {
  114.             $errors++;
  115.             continue;
  116.         } else {
  117.             //perform update
  118.             update($updates);
  119.         }
  120.     } elseif($deletions) {
  121.         if($deletions[1] != "DELETE" || $deletions[3] == "login" || !in_array($deletions[3], $user)) {
  122.             $errors++;
  123.             continue;
  124.         } else {
  125.             if(count($usersDatabase) == 0 || $deletions[2] != $db) {
  126.                 continue;
  127.             }
  128.             //perform deletion
  129.             delete($deletions);
  130.         }
  131.     } else {
  132.         $errors++;
  133.     }
  134. }
  135. if(count($usersDatabase) == 0) {
  136.     echo "You have " . $errors . " error/s";
  137. } else {
  138.     $table = "<table><thead><tr><th>user_id</th><th>login</th><th>gender</th><th>age</th></tr></thead><tbody>";
  139.     foreach ($usersDatabase as $user) {
  140.         $table .= "<tr><td>" . htmlspecialchars($user['user_id']) . "</td><td>" .
  141.                                htmlspecialchars($user['login']) . "</td><td>" .
  142.                                htmlspecialchars($user['gender']) . "</td><td>" .
  143.                                htmlspecialchars($user['age']) . "</td></tr>";
  144.     }
  145.     $table .= '</tbody><tfoot><tr><td colspan="4">Errors=' . $errors .'</td></tr></tfoot></table>';
  146.  
  147.     echo $table;
  148. }
  149. //var_dump($usersDatabase);
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement