|
| 1 | +<?php |
| 2 | +include_once __DIR__ . "/config.php"; |
| 3 | + |
| 4 | +$table_name = "a_product_x"; |
| 5 | + |
| 6 | +echo $style; |
| 7 | + |
| 8 | +echo "<h1>DB CRUD Actions</h1> |
| 9 | +<p>Executes CRUD actions through objects/arrays</p>"; |
| 10 | + |
| 11 | +echo '<h4>Choose a DB driver: |
| 12 | + <select onChange="document.location=\'?type=\' + this.value;">'; |
| 13 | + |
| 14 | +$types = DB::getAllDriverLabelsByType(); |
| 15 | +foreach ($types as $id => $label) |
| 16 | + echo "<option value='$id'" . ($id == $type ? " selected" : "") . ">$label</option>"; |
| 17 | +echo "</select></h4>"; |
| 18 | + |
| 19 | +echo '<div class="note"> |
| 20 | + <span> |
| 21 | + The system will create 2 temp tables. Then will perform some CRUD actions on these tables and show the correspondent code and results.<br/> |
| 22 | + At the end will drop/remove the temp tables from your DB. |
| 23 | + </span> |
| 24 | +</div>'; |
| 25 | + |
| 26 | +try { |
| 27 | + if ($password) { |
| 28 | + $DBDriver->connect(); |
| 29 | + $tables = $DBDriver->listTables(); |
| 30 | + |
| 31 | + //Create product table |
| 32 | + $tn = DB::getStaticTableInNamesList($tables, $table_name); |
| 33 | + |
| 34 | + if (!$tn) { |
| 35 | + $drop_table = true; |
| 36 | + $sql = $DBDriver->getCreateTableStatement(array( |
| 37 | + "name" => $table_name, |
| 38 | + "attributes" => array( |
| 39 | + array("name" => "product_id", "type" => "bigint", "length" => 20, "primary_key" => true, "auto_increment" => true), |
| 40 | + array("name" => "client_id", "type" => "bigint", "length" => 20, "unsigned" => true, "null" => true), |
| 41 | + array("name" => "type_id", "type" => "bigint", "length" => 20, "unsigned" => true, "null" => true), |
| 42 | + array("name" => "name", "type" => "varchar", "length" => 200, "default" => "", "null" => false), |
| 43 | + array("name" => "description", "type" => "blob", "default" => "", "null" => false) |
| 44 | + ) |
| 45 | + )); |
| 46 | + $status = $DBDriver->setSQL($sql); |
| 47 | + //echo "sql: $sql<br/>status: $status<br/>";die(); |
| 48 | + } |
| 49 | + |
| 50 | + //create product_type table |
| 51 | + $tn = DB::getStaticTableInNamesList($tables, $table_name . "_type"); |
| 52 | + |
| 53 | + if (!$tn) { |
| 54 | + $drop_table_type = true; |
| 55 | + $sql = $DBDriver->getCreateTableStatement(array( |
| 56 | + "name" => $table_name . "_type", |
| 57 | + "attributes" => array( |
| 58 | + array("name" => "product_type_id", "type" => "bigint", "length" => 20, "primary_key" => true, "auto_increment" => true), |
| 59 | + array("name" => "name", "type" => "varchar", "length" => 200, "default" => "", "null" => false) |
| 60 | + ) |
| 61 | + )); |
| 62 | + $status = $DBDriver->setSQL($sql); |
| 63 | + //echo "sql: $sql<br/>status: $status<br/>";die(); |
| 64 | + |
| 65 | + if ($status) { |
| 66 | + $status = $DBDriver->insertObject($table_name . "_type", array("name" => "desert")); |
| 67 | + //echo "sql: $sql<br/>status: $status<br/>";die(); |
| 68 | + $status = $DBDriver->insertObject($table_name . "_type", array("name" => "vegetal")); |
| 69 | + //echo "sql: $sql<br/>status: $status<br/>";die(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + throw new Exception("Please edit config.php file and define your DB credentials first!"); |
| 75 | +} |
| 76 | +catch (Throwable $e) { |
| 77 | + $error = $e->getMessage() . (!empty($e->problem) ? "<br/>" . $e->problem : ""); |
| 78 | +} |
| 79 | + |
| 80 | +if (!empty($error)) |
| 81 | + echo '<div class="error">' . $error . '</div>'; |
| 82 | +else { |
| 83 | + echo "<h5>Insert object into DB:</h5>"; |
| 84 | + $attributes = array( |
| 85 | + "client_id" => 10, |
| 86 | + "type_id" => 2, |
| 87 | + "name" => "onion", |
| 88 | + "description" => "simple onion", |
| 89 | + ); |
| 90 | + $status = $DBDriver->insertObject($table_name, $attributes); |
| 91 | + echo "<div class=\"code short\"><textarea readonly>\$attributes = array( |
| 92 | + \"client_id\" => 10, |
| 93 | + \"type_id\" => 2, |
| 94 | + \"name\" => \"onion\", |
| 95 | + \"description\" => \"simple onion\", |
| 96 | +); |
| 97 | +\$status = \$DBDriver->insertObject(\"" . $table_name . "\", \$attributes);</textarea></div> |
| 98 | + <div class=\"code one-line\"><textarea readonly>$status</textarea></div>"; |
| 99 | + |
| 100 | + $id = $status ? $DBDriver->getInsertedId() : null; |
| 101 | + $id = $id ? $id : 13; |
| 102 | + |
| 103 | + echo "<h5>Update object in DB:</h5>"; |
| 104 | + $attributes = array( |
| 105 | + "name" => "onion", |
| 106 | + "description" => "new onion", |
| 107 | + ); |
| 108 | + $conditions = array( |
| 109 | + "product_id" => $id, |
| 110 | + ); |
| 111 | + $status = $DBDriver->updateObject($table_name, $attributes, $conditions); |
| 112 | + echo "<div class=\"code short\"><textarea readonly>\$attributes = array( |
| 113 | + \"name\" => \"onion\", |
| 114 | + \"description\" => \"new onion\", |
| 115 | +); |
| 116 | +\$conditions = array( |
| 117 | + \"product_id\" => " . $id . ", |
| 118 | +); |
| 119 | +\$status = \$DBDriver->updateObject(\"" . $table_name . "\", \$attributes, \$conditions);</textarea></div> |
| 120 | + <div class=\"code one-line\"><textarea readonly>$status</textarea></div>"; |
| 121 | + |
| 122 | + echo "<h5>Select objects from DB:</h5>"; |
| 123 | + $attributes = array("product_id", "name"); |
| 124 | + $conditions = array("product_id" => $id); |
| 125 | + $objects = $DBDriver->findObjects($table_name, $attributes, $conditions); |
| 126 | + echo "<div class=\"code short\"><textarea readonly> |
| 127 | +\$attributes = array(\"product_id\", \"name\"); |
| 128 | +\$conditions = array(\"product_id\" => " . $id . "); |
| 129 | +\$objects = \$DBDriver->findObjects(\"" . $table_name . "\", \$attributes, \$conditions);</textarea></div> |
| 130 | + <div class=\"code short\"><textarea readonly>" . print_r($objects, true) . "</textarea></div>"; |
| 131 | + |
| 132 | + echo "<h5>Count objects in DB:</h5>"; |
| 133 | + $total = $DBDriver->countObjects($table_name, array("product_id" => $id)); |
| 134 | + echo "<div class=\"code one-line\"><textarea readonly>\$total = \$DBDriver->countObjects(\"" . $table_name . "\", array(\"product_id\" => " . $id . "));</textarea></div> |
| 135 | + <div class=\"code one-line\"><textarea readonly>$total</textarea></div>"; |
| 136 | + |
| 137 | + echo "<h5>Get maximum value from DB:</h5>"; |
| 138 | + $max = $DBDriver->findObjectsColumnMax($table_name, "product_id"); |
| 139 | + echo "<div class=\"code one-line\"><textarea readonly>\$max = \$DBDriver->findObjectsColumnMax(\"" . $table_name . "\", \"product_id\");</textarea></div> |
| 140 | + <div class=\"code one-line\"><textarea readonly>$max</textarea></div>"; |
| 141 | + |
| 142 | + echo "<h5>Get foreign objects:</h5>"; |
| 143 | + $rel_elm = array( |
| 144 | + "keys" => array( |
| 145 | + array("pcolumn" => "type_id", "ftable" => $table_name . "_type pt", "fcolumn" => "product_type_id") |
| 146 | + ) |
| 147 | + ); |
| 148 | + $objects = $DBDriver->findRelationshipObjects($table_name, $rel_elm); |
| 149 | + echo "<div class=\"code short\"><textarea readonly>\$rel_elm = array( |
| 150 | + \"keys\" => array( |
| 151 | + array(\"pcolumn\" => \"type_id\", \"ftable\" => \"" . $table_name . "_type pt\", \"fcolumn\" => \"product_type_id\") |
| 152 | + ) |
| 153 | +); |
| 154 | +\$objects = \$DBDriver->findRelationshipObjects(\"" . $table_name . "\", \$rel_elm);</textarea></div> |
| 155 | + <div class=\"code\"><textarea readonly>" . print_r($objects, true) . "</textarea></div>"; |
| 156 | + |
| 157 | + echo "<h5>Get foreign objects together with main table:</h5>"; |
| 158 | + $rel_elm = array( |
| 159 | + "attributes" => array( |
| 160 | + array("table" => "pt", "column" => "product_type_id", "name" => "type_id"), |
| 161 | + array("table" => "pt", "column" => "name", "name" => "type"), |
| 162 | + ), |
| 163 | + "keys" => array( |
| 164 | + array("pcolumn" => "type_id", "ftable" => $table_name . "_type pt", "fcolumn" => "product_type_id") |
| 165 | + ), |
| 166 | + "conditions" => array( |
| 167 | + array("table" => $table_name, "column" => "type_id", "reftable" => "pt", "refcolumn" => "product_type_id"), |
| 168 | + array("table" => "pt", "column" => "product_type_id", "operator" => "<", "value" => 3) |
| 169 | + ), |
| 170 | + "groups_by" => array( |
| 171 | + array("table" => "", "column" => "type_id", "having" => "count(product_type_id) > 1") |
| 172 | + ) |
| 173 | + ); |
| 174 | + $parent_conditions = array( |
| 175 | + "pt.name" => "vegetal" |
| 176 | + ); |
| 177 | + $options = array( |
| 178 | + "sorts" => array( |
| 179 | + array("table" => "", "column" => "type", "order" => "desc") |
| 180 | + ), |
| 181 | + "sql_conditions" => "pt.name = 'vegetal'", |
| 182 | + ); |
| 183 | + $objects = $DBDriver->findRelationshipObjects($table_name, $rel_elm, $parent_conditions, $options); |
| 184 | + echo "<div class=\"code\"><textarea readonly>\$rel_elm = array( |
| 185 | + \"attributes\" => array( |
| 186 | + array(\"table\" => \"pt\", \"column\" => \"product_type_id\", \"name\" => \"type_id\"), |
| 187 | + array(\"table\" => \"pt\", \"column\" => \"name\", \"name\" => \"type\"), |
| 188 | + ), |
| 189 | + \"keys\" => array( |
| 190 | + array(\"pcolumn\" => \"type_id\", \"ftable\" => \$table_name . \"_type pt\", \"fcolumn\" => \"product_type_id\") |
| 191 | + ), |
| 192 | + \"conditions\" => array( |
| 193 | + array(\"table\" => \$table_name, \"column\" => \"type_id\", \"reftable\" => \"pt\", \"refcolumn\" => \"product_type_id\"), |
| 194 | + array(\"table\" => \"pt\", \"column\" => \"product_type_id\", \"operator\" => \"<\", \"value\" => 3) |
| 195 | + ), |
| 196 | + \"groups_by\" => array( |
| 197 | + array(\"table\" => \"\", \"column\" => \"type_id\", \"having\" => \"count(product_type_id) > 1\") |
| 198 | + ) |
| 199 | +); |
| 200 | +\$parent_conditions = array( |
| 201 | + \"pt.name\" => \"vegetal\" |
| 202 | +); |
| 203 | +\$options = array( |
| 204 | + \"sorts\" => array( |
| 205 | + array(\"table\" => \"\", \"column\" => \"type\", \"order\" => \"desc\") |
| 206 | + ), |
| 207 | + \"sql_conditions\" => \"pt.name = 'vegetal'\", |
| 208 | +); |
| 209 | +\$objects = \$DBDriver->findRelationshipObjects(\"" . $table_name . "\", \$rel_elm, \$parent_conditions, \$options);</textarea></div> |
| 210 | +<div class=\"code short\"><textarea readonly>" . print_r($objects, true) . "</textarea></div>"; |
| 211 | + |
| 212 | + echo "<h5>Count foreign objects:</h5>"; |
| 213 | + $rel_elm = array( |
| 214 | + "keys" => array( |
| 215 | + array("pcolumn" => "type_id", "ftable" => $table_name . "_type pt", "fcolumn" => "product_type_id") |
| 216 | + ), |
| 217 | + "conditions" => array( |
| 218 | + "type_id" => array( |
| 219 | + "operator" => "<", |
| 220 | + "value" => 3 |
| 221 | + ) |
| 222 | + ) |
| 223 | + ); |
| 224 | + $total = $DBDriver->countRelationshipObjects($table_name, $rel_elm); |
| 225 | + echo "<div class=\"code\"><textarea readonly>\$rel_elm = array( |
| 226 | + \"keys\" => array( |
| 227 | + array(\"pcolumn\" => \"type_id\", \"ftable\" => \"" . $table_name . "_type pt\", \"fcolumn\" => \"product_type_id\") |
| 228 | + ), |
| 229 | + \"conditions\" => array( |
| 230 | + \"type_id\" => array( |
| 231 | + \"operator\" => \"<\", |
| 232 | + \"value\" => 3 |
| 233 | + ) |
| 234 | + ) |
| 235 | +); |
| 236 | +\$total = \$DBDriver->countRelationshipObjects(\"" . $table_name . "\", \$rel_elm);</textarea></div> |
| 237 | + <div class=\"code one-line\"><textarea readonly>$total</textarea></div>"; |
| 238 | + |
| 239 | + echo "<h5>Delete object in DB:</h5>"; |
| 240 | + $status = $DBDriver->deleteObject($table_name, array( |
| 241 | + "product_id" => array( |
| 242 | + "operator" => ">=", |
| 243 | + "value" => $id, |
| 244 | + ), |
| 245 | + )); |
| 246 | + echo "<div class=\"code short\"><textarea readonly>\$status = \$DBDriver->deleteObject(\"" . $table_name . "\", array( |
| 247 | + \"product_id\" => array( |
| 248 | + \"operator\" => \">=\", |
| 249 | + \"value\" => " . $id . " |
| 250 | + ), |
| 251 | +));</textarea></div> |
| 252 | + <div class=\"code one-line\"><textarea readonly>$status</textarea></div>"; |
| 253 | + |
| 254 | + echo "<br/>"; |
| 255 | + |
| 256 | + if (!empty($drop_table)) { |
| 257 | + $sql = $DBDriver->getDropTableStatement($table_name); |
| 258 | + $status = $DBDriver->setSQL($sql); |
| 259 | + //echo "sql: $sql<br/>status: $status<br/>";die(); |
| 260 | + } |
| 261 | + |
| 262 | + if (!empty($drop_table_type)) { |
| 263 | + $sql = $DBDriver->getDropTableStatement($table_name . "_type"); |
| 264 | + $status = $DBDriver->setSQL($sql); |
| 265 | + //echo "sql: $sql<br/>status: $status<br/>";die(); |
| 266 | + } |
| 267 | +} |
| 268 | +?> |
0 commit comments