dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
dbxAnalyzerSQL.class.php
Go to the documentation of this file.
1<?php
3 public $sql = '';
4 public $type = 'UNKNOWN';
5 public $fields = '';
6 public $values = '';
7 public $table = '';
8 public $where = '';
9 public $orderBy = '';
10 public $groupBy = '';
11 public $limit = 100;
12 public $offset = 0;
13 public $isValid = true; // Neue Eigenschaft für die Gültigkeit des SQL-Statements
14
15 public function analyze($sql) {
16 $this->sql = trim($sql);
17
18 if (empty($this->sql)) {
19 $this->markInvalid("SQL-Statement darf nicht leer sein.");
20 return;
21 }
22
23 if (preg_match('/^\s*(SELECT|DELETE|INSERT|UPDATE)\b/i', $this->sql, $matches)) {
24 $this->type = strtoupper($matches[1]);
25 } else {
26 $this->markInvalid("Unbekannter oder nicht unterstützter SQL-Typ.");
27 return;
28 }
29
30 try {
31 switch ($this->type) {
32 case 'INSERT':
33 $this->parseInsert();
34 break;
35 case 'DELETE':
36 $this->parseDelete();
37 break;
38 case 'SELECT':
39 $this->parseSelect();
40 break;
41 }
42
43 $this->where = $this->extractClause('WHERE');
44 $this->orderBy = $this->extractClause('ORDER BY');
45 $this->groupBy = $this->extractClause('GROUP BY');
46 $this->extractLimitAndOffset();
47 } catch (Exception $e) {
48 $this->markInvalid($e->getMessage());
49 }
50 }
51
52 private function markInvalid($message) {
53 $this->isValid = false;
54 $this->type = 'UNKNOWN';
55 $this->table = '';
56 $this->fields = '';
57 $this->values = '';
58 $this->where = '';
59 $this->orderBy = '';
60 $this->groupBy = '';
61 $this->limit = 100;
62 $this->offset = 0;
63 error_log("SQL Analyzer Fehler: $message");
64 }
65
66 private function parseInsert() {
67 if (preg_match('/INSERT\s+INTO\s+([^\s(]+)(?:\s*\‍((.*?)\‍))?\s*VALUES\s*\‍((.*?)\‍)/i', $this->sql, $matches)) {
68 $this->table = $matches[1];
69 $this->fields = $matches[2] ?? '';
70 $this->values = $matches[3];
71 } else {
72 throw new InvalidArgumentException("Ungültige INSERT-Syntax.");
73 }
74 }
75
76 private function parseDelete() {
77 if (preg_match('/DELETE\s+FROM\s+([^\s]+)/i', $this->sql, $matches)) {
78 $this->table = $matches[1];
79 } else {
80 throw new InvalidArgumentException("Ungültige DELETE-Syntax.");
81 }
82 }
83
84 private function parseSelect() {
85 if (preg_match('/SELECT\s+(.*?)\s+FROM\s+([^\s]+)/i', $this->sql, $matches)) {
86 $this->fields = $matches[1];
87 $this->table = $matches[2];
88 } else {
89 throw new InvalidArgumentException("Ungültige SELECT-Syntax.");
90 }
91 }
92
93 private function extractClause($clause) {
94 if (preg_match("/\b$clause\b\s+(.*?)(?=\b(ORDER BY|GROUP BY|LIMIT|OFFSET|$))/i", $this->sql, $matches)) {
95 return trim($matches[1]);
96 }
97 return '';
98 }
99
100 private function extractLimitAndOffset() {
101 if (preg_match('/\bLIMIT\b\s+(\d+)(?:\s+\bOFFSET\b\s+(\d+))?/i', $this->sql, $matches)) {
102 $this->limit = (int) ($matches[1] ?? 100);
103 $this->offset = (int) ($matches[2] ?? 0);
104 }
105 }
106
107 private function getValue($property, $default) {
108 return $property !== '' ? $property : $default;
109 }
110
111 public function getType($default = 'UNKNOWN') {
112 return $this->getValue($this->type, $default);
113 }
114
115 public function getFields($default = '*') {
116 return $this->getValue($this->fields, $default);
117 }
118
119 public function getTable($default = '') {
120 return $this->getValue($this->table, $default);
121 }
122
123 public function getWhere($default = '') {
124 return $this->getValue($this->where, $default);
125 }
126
127 public function getOrderBy($default = '') {
128 return $this->getValue($this->orderBy, $default);
129 }
130
131 public function getGroupBy($default = '') {
132 return $this->getValue($this->groupBy, $default);
133 }
134
135 public function getLimit($default = 100) {
136 return (int) $this->getValue($this->limit, $default);
137 }
138
139 public function getOffset($default = 0) {
140 return (int) $this->getValue($this->offset, $default);
141 }
142}
getFields($default=' *')
getType($default='UNKNOWN')