Advertisement
reathh

02. SoftUni SQL

Aug 30th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2. //$commands = $_GET['commands'];
  3. $commands = ["UPDAtE users SET (age = 30) WHERE (user_id = 1)", "INSERT INTO users (login, age, gender) VALUES (yana, 20, female)", "UPDATE users SET (age = 30) WHERE (user_id = 1)", "INSERT INTO users (login, age) VALUES (yana, 20, female)", "INSERT INTO users (login, age, gender) VALUES (yana, 20, female)", "INSERT INTO users (user_id, login, gender) VALUES (12, yana, female)", "INSERT INTO users (login, age, gender) VALUES (yana, 20, female)", "INSrRT INTO users (login, age, gender) VALUES (yana, 20, female)", "UPDATE users SET (age = 30) WHERE (user_id = 1)", "UPDATE users SET (age = 30) WHERE (age = undefined)", "DELETE FROM users WHERE (age = 30)"];
  4.  
  5. $insertRegex = '/INSERT INTO (\w+) \((.+)\) VALUES \((.+)\)/';
  6. $updateRegex = '/UPDATE (\w+) SET \((\w+) = (\w+)\) WHERE \((\w+) = (\w+)\)/';
  7. $deleteRegex = '/DELETE FROM (\w+) WHERE \((\w+) = (\w+)\)/';
  8.  
  9. $errors = 0;
  10. $lastID = -1;
  11. foreach ($commands as $command) {
  12.     if (preg_match($insertRegex, $command, $insertMatch)) {
  13.         $fields = explode(', ', $insertMatch[2]);
  14.         $values = explode(', ', $insertMatch[3]);
  15.         if (array_search('login', $fields) === false) {
  16.             $errors++;
  17.             continue;
  18.         } else if (count($fields) != count($values)) {
  19.             $errors++;
  20.             continue;
  21.         }
  22.         if (array_search('user_id', $fields) !== false) {
  23.             $id = array_search('user_id', $fields);
  24.             $lastID = $values[$id];
  25.         } else {
  26.             $lastID += 1;
  27.         }
  28.  
  29.         $login = $values[array_search('login', $fields)];
  30.  
  31.         if (array_search('gender', $fields) !== false) {
  32.             $gender = $values[array_search('gender', $fields)];
  33.         } else {
  34.             $gender = 'undefined';
  35.         }
  36.         if (array_search('age', $fields) !== false) {
  37.             $age = $values[array_search('age', $fields)];
  38.         } else {
  39.             $age = 'undefined';
  40.         }
  41.         $table[$lastID]['login'] = $login;
  42.         $table[$lastID]['gender'] = $gender;
  43.         $table[$lastID]['age'] = $age;
  44.  
  45.     } else if (preg_match($updateRegex, $command, $updateMatch)) {
  46. //        $updateField = $updateMatch[2];
  47. //        $existingField = false;
  48. //        if ($updateMatch[4] == 'user_id') {
  49. //            if (isset($table[$updateMatch[5]])) {
  50. //                $existingField = true;
  51. //            }
  52. //        } else {
  53. //            foreach ($table as $key => $value) {
  54. //                foreach ($table[$key] as $field => $fieldValue) {
  55. //                    if ($field == $updateMatch[4]) {
  56. //                        $existingField = true;
  57. //                    }
  58. //                }
  59. //            }
  60. //            if ($existingField) {
  61. //                if ($updateMatch[4] == 'user_id') {
  62. //                    $table[$updateMatch[5]][$updateMatch[2]] = $updateMatch[3];
  63. //                } else {
  64. //
  65. //                }
  66. //            } else {
  67. //                $errors++;
  68. //                continue;
  69. //            }
  70. //
  71. //        }
  72.  
  73.     } else if (preg_match($deleteRegex, $command, $deleteMatch)) {
  74.  
  75.     } else {
  76.         $errors++;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement