1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
- using System . IO ;
5
4
using System . Text ;
6
5
using System . Text . RegularExpressions ;
7
- using MiniSQL . BufferManager . Controllers ;
8
6
using MiniSQL . IndexManager . Interfaces ;
9
7
using MiniSQL . Library . Interfaces ;
10
8
using MiniSQL . Library . Models ;
@@ -13,18 +11,11 @@ namespace MiniSQL.Startup.Controllers
13
11
{
14
12
public class View
15
13
{
16
- private bool isUsingDatabase = false ;
17
- private string nameOfDatabaseInUse ;
18
- private IApi _api ;
19
- private Pager _pager ;
20
14
private readonly DatabaseController _databaseController ;
21
15
private bool isCtrlC = false ;
22
16
23
17
public View ( DatabaseController databaseController )
24
18
{
25
- // ensure writing back when ctrl-c
26
- Console . CancelKeyPress += OnExit ;
27
-
28
19
// print prologue
29
20
Console . WriteLine ( ) ;
30
21
Console . WriteLine ( "Hello MiniSQL!" ) ;
@@ -33,32 +24,6 @@ public View(DatabaseController databaseController)
33
24
_databaseController = databaseController ;
34
25
}
35
26
36
- // TODO: wrap this to a class
37
- private void ChangeContext ( string newDatabaseName )
38
- {
39
- if ( isUsingDatabase )
40
- {
41
- _pager . Close ( ) ;
42
- }
43
- // init
44
- this . isUsingDatabase = true ;
45
- this . nameOfDatabaseInUse = newDatabaseName ;
46
- ( _api , _pager ) = _databaseController . UseDatabase ( newDatabaseName ) ;
47
- }
48
-
49
- // TODO: wrap this to a class
50
- private void DropDatabase ( string databaseName )
51
- {
52
- if ( nameOfDatabaseInUse == databaseName )
53
- {
54
- _pager . Close ( ) ;
55
- isUsingDatabase = false ;
56
- }
57
- File . Delete ( $ "{ databaseName } .minidb") ;
58
- File . Delete ( $ "{ databaseName } .indices.txt") ;
59
- File . Delete ( $ "{ databaseName } .tables.txt") ;
60
- }
61
-
62
27
// interactive(blocking) view of the whole solution
63
28
public void Interactive ( )
64
29
{
@@ -93,17 +58,17 @@ public void Interactive()
93
58
if ( Regex . IsMatch ( line , @"(?i)\s*use\s*database\s*.*(?-i)" ) )
94
59
{
95
60
string databaseName = line . Split ( new string [ ] { " " , "\n " } , StringSplitOptions . RemoveEmptyEntries ) [ 2 ] . TrimEnd ( ';' ) ;
96
- ChangeContext ( databaseName ) ;
61
+ _databaseController . ChangeContext ( databaseName ) ;
97
62
continue ;
98
63
}
99
64
// drop database
100
65
if ( Regex . IsMatch ( line , @"(?i)\s*drop\s*database\s*.*(?-i)" ) )
101
66
{
102
67
string databaseName = line . Split ( new string [ ] { " " , "\n " } , StringSplitOptions . RemoveEmptyEntries ) [ 2 ] . TrimEnd ( ';' ) ;
103
- DropDatabase ( databaseName ) ;
68
+ _databaseController . DropDatabase ( databaseName ) ;
104
69
continue ;
105
70
}
106
- if ( ! this . isUsingDatabase )
71
+ if ( ! _databaseController . IsUsingDatabase )
107
72
{
108
73
// WORKAROUND
109
74
Console . WriteLine ( "[Error] No database in use" ) ;
@@ -112,7 +77,7 @@ public void Interactive()
112
77
// flush all the dirty pages back to secondary memory and clean the main memory out of any page
113
78
if ( line == "flush" )
114
79
{
115
- _pager . CleanAllPagesFromMainMemory ( ) ;
80
+ _databaseController . FlushPages ( ) ;
116
81
continue ;
117
82
}
118
83
input . Append ( line ) ;
@@ -128,7 +93,7 @@ public void Interactive()
128
93
stopwatch . Start ( ) ;
129
94
try
130
95
{
131
- List < SelectResult > selectResults = _api . Query ( input . ToString ( ) ) ;
96
+ List < SelectResult > selectResults = _databaseController . Query ( input . ToString ( ) ) ;
132
97
// print results for select statement
133
98
foreach ( var selectResult in selectResults )
134
99
{
@@ -178,15 +143,8 @@ public void Interactive()
178
143
input . Clear ( ) ;
179
144
}
180
145
// save database file
181
- if ( this . isUsingDatabase )
182
- _pager . Close ( ) ;
183
- }
184
-
185
- // BUG: NOT WORKING
186
- private void OnExit ( object sender , EventArgs e )
187
- {
188
- isCtrlC = true ;
189
- _pager . Close ( ) ;
146
+ if ( _databaseController . IsUsingDatabase )
147
+ _databaseController . ClosePager ( ) ;
190
148
}
191
149
192
150
private static void PrintRows ( SelectResult result )
0 commit comments