1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
<?php
$hostname = $argv[1];
$port = $argv[2];
$username = $argv[3];
$password = $argv[4];
$with_ssl = $argv[5] == "0" ? false : true;
$with_sharing = $argv[6] == "0" ? false : true;
// throw exceptions if a mysql function fails.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
function result_as_array($res) {
if (!is_object($res)) return $res;
$rows = [];
while($row = $res->fetch_array(MYSQLI_NUM)) {
$rows[] = $row;
}
return $rows;
}
function markdown_code($block, $highlight = "") {
return <<<EOF
```$highlight
$block
```
EOF;
}
function md($res) {
return "* " . markdown_code(json_encode(result_as_array($res)), "javascript");
}
function multi_md($m, $res) {
$out = "";
do {
$out .= md($res);
if (!$m->wrapped_->next_result()) break;
$res->free_result();
$res = $m->wrapped_->store_result();
} while (true);
return $out;
}
function dump($v) {
return var_export($v, true);
}
/**
* print the called function with its parameters.
*/
class CallTracer {
public function __construct($wrapped) {
$this->wrapped_ = $wrapped;
}
public function __call($name, $args) {
print(markdown_code("$name(" . implode(", ", array_map("dump", $args)) . ")\n", "javascript"));
return $this->wrapped_->$name(...$args);
}
public $wrapped_;
};
$mysqli = new CallTracer(mysqli_init());
# mysqli_real_connect() checks the type of the mysqli. Pass it the wrapped class.
mysqli_real_connect($mysqli->wrapped_,
$hostname, $username, $password, "", $port, NULL,
$with_ssl ? MYSQLI_CLIENT_SSL : 0);
?>
# autocommit
<?= md($mysqli->autocommit(1)) ?>
<?= md($mysqli->autocommit(0)) ?>
# transactions
<?= md($mysqli->begin_transaction(0, "savepointname")) ?>
<?= md($mysqli->savepoint("abc")) ?>
<?= md($mysqli->release_savepoint("savepointname")) ?>
<?= md($mysqli->release_savepoint("abc")) ?>
<?= md($mysqli->commit()) ?>
<?= md($mysqli->rollback()) ?>
# debug
<?= md($mysqli->dump_debug_info()) ?>
# ping
<?= md($mysqli->ping()) ?>
# stat
<?= md($mysqli->stat()) ?>
# refresh
<?= md($mysqli->refresh(0)) ?>
# use schema
<?= md($mysqli->query("DROP SCHEMA IF EXISTS phpt")) ?>
<?= md($mysqli->query("CREATE SCHEMA phpt")) ?>
<?= md($mysqli->select_db("phpt")) ?>
# multi-resultsets
<?= md($mysqli->query("CREATE PROCEDURE p1 () BEGIN SELECT 1; SELECT 2; END")) ?>
<?= multi_md($mysqli, $mysqli->query("CALL p1()")) ?>
<?php
$res = result_as_array($mysqli->query("SELECT SCHEMA()"));
print("* " . markdown_code(json_encode($res), "javascript"));
$res[0][0] == "phpt" or throw new Exception("expected SCHEMA to be set");
?>
# change_user
<?= md($mysqli->change_user($username, $password, "")) ?>
<?php
$res = result_as_array($mysqli->query("SELECT SCHEMA()"));
print("* " . markdown_code(json_encode($res), "javascript"));
$res[0][0] == null or throw new Exception("expected SCHEMA to be null");
?>
# change_user with schema
<?= md($mysqli->change_user($username, $password, "phpt")) ?>
<?php
$res = result_as_array($mysqli->query("SELECT SCHEMA()"));
print("* " . markdown_code(json_encode($res), "javascript"));
$res[0][0] == "phpt" or throw new Exception("expected SCHEMA to be set");
?>
# query
<?= md($mysqli->query("DO 1")) ?>
## `SHOW WARNINGS`
<?php
{
$warn_res = result_as_array($mysqli->query("SHOW WARNINGS"));
count($warn_res) == 0 or throw new Exception("expected row-count == 0, got " . count($warn_res));
}
|