-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (56 loc) · 1.97 KB
/
Copy pathProgram.cs
File metadata and controls
71 lines (56 loc) · 1.97 KB
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
using DB.Engine.Database;
using DB.Engine.Execution.Parsing;
using DB.Engine.Execution.Planning;
using DB.Engine.Execution.Session;
class Program
{
static void Main()
{
Console.WriteLine("SimpleDB Engine");
Console.WriteLine("Type EXIT to quit\n");
var dbManager = new DatabaseManager();
// ------------------- REPL -------------------
while (true)
{
// Prompt: show db name if active, otherwise just "> "
string prompt = dbManager.ActiveDatabase != null
? $"{dbManager.ActiveDatabase}> "
: "> ";
Console.Write(prompt);
var input = Console.ReadLine();
if (input == null)
continue;
input = input.Trim();
if (input.Length == 0)
continue;
try
{
// 1. Session / Meta commands (USE, SHOW, EXIT)
if (SessionCommandHandler.TryHandle(input, dbManager, out bool shouldExit))
{
if (shouldExit)
break;
continue;
}
// 2. Check if database is selected before running SQL
if (dbManager.ActiveDatabase == null)
{
Console.WriteLine("No database selected. Use 'CREATE DATABASE <name>' then 'USE <name>'.");
continue;
}
// 3. SQL pipeline
var lexer = new Lexer(input);
var tokens = lexer.Tokenize();
var parser = new SqlParser(tokens);
var ast = parser.ParseStatement();
var command = CommandBuilder.Build(ast);
command.Execute(dbManager.GetActiveContext());
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
Console.WriteLine("Bye.");
}
}