-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydb
executable file
·87 lines (62 loc) · 1.78 KB
/
mydb
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
#!/usr/bin/php
<?php
use Fernando\MyDB\Parser;
use Fernando\MyDB\Cli\App;
use Fernando\MyDB\Cli\Color;
require_once __DIR__ . '/vendor/autoload.php';
$parser = new Parser();
$cli = new App;
$printer = $cli->GetPrinter();
$cli->Register("help", function () use ($cli, $printer) {
$txt = Color::Fg(45, 'Usage: php mydb [command] [options]') . PHP_EOL;
$txt .= 'Available commands:' . PHP_EOL;
foreach ($cli->GetAllCmds() as $i => $item) {
$txt .= ' - ' . Color::Fg(10, $i) . PHP_EOL;
unset ($item);
}
$printer->Display(trim($txt));
});
$cli->Register("init", function () use ($cli, $printer) {
$printer->Clear();
$printer->display(Color::Fg(82, "Initializing..."));
mkdir(__DIR__ . '/db');
$printer->Display(Color::Fg(82, "Initialized successfully."));
});
$cli->Register("query", function () use ($printer, $parser, $argv) {
if (count($argv) < 3 or count($argv) > 3) {
$printer->out(Color::Fg(99, "Usage: mydb query <FILE>"));
exit;
}
if (is_file($argv[2])) {
$data = file_get_contents($argv[2]);
$parser->parserAndInterpreter($data);
exit;
}
$printer->Display(Color::Fg(82, "File not found '{$argv[1]}'."));
});
$cli->Register('exec', function () use ($printer, $parser) {
$printer->Clear(); // Clear the screen
while (true) {
$input = readline("mydb> ");
if ($input == "exit") {
$printer->newLine();
$printer->out(Color::Fg(99, "Bye!"));
break;
}
if ($input == "clear") {
$printer->clear();
continue;
}
if (in_array($input, ["", " "])) {
continue;
}
$printer->newLine();
$init = microtime(true);
$parser->parserAndInterpreter($input);
$end = microtime(true);
$seconds = number_format($end - $init, 5);
$printer->newLine();
$printer->Display(Color::Fg(82, "Executed in {$seconds} seconds."));
}
});
$cli->Run($argv);