-
Notifications
You must be signed in to change notification settings - Fork 6
/
sqlite-commander.cs
267 lines (221 loc) · 5.88 KB
/
sqlite-commander.cs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Sqlite-Commander: NCurses Interface for your SQLite databases
*
* Author: Sankar P <[email protected]>
*
* License: LGPL V2.1 Only
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using Mono.Terminal;
using Mono.Data.SqliteClient;
namespace SqliteCommander
{
public class TablesList : IListProvider
{
public List<string> items = new List<string> ();
public ListView view;
public event TableSelectionChanged newSel;
public EventArgs e = null;
public delegate void TableSelectionChanged(TablesList t, EventArgs e);
public void Render (int line, int col, int width, int item)
{
Curses.addstr (items[item]);
}
public bool AllowMark {
get {
return false;
}
}
public int Items {
get {
return items.Count;
}
}
public bool IsMarked (int item)
{
return false;
}
public bool ProcessKey (int ch)
{
return false;
}
public void SelectedChanged ()
{
newSel (this, e);
return;
}
public void SetListView (ListView target)
{
view = target;
}
public void Add (string s)
{
items.Add (s);
}
}
public class RecordsList : IListProvider
{
public List<IEnumerable> items = new List<IEnumerable> ();
public ListView view;
static int MAX_CHARS_PER_COLUMN = 30;
public void Render (int line, int col, int width, int item)
{
string record = "";
List<string> l = (List<string>) items[item];
foreach (string column in l) {
if (column.Length > MAX_CHARS_PER_COLUMN)
record = record + column.Substring (0, MAX_CHARS_PER_COLUMN) + "... ";
else
record = record + column + " ";
}
Curses.addstr (record);
}
public bool AllowMark {
get {
return false;
}
}
public int Items {
get {
return items.Count;
}
}
public bool IsMarked (int item)
{
return false;
}
public bool ProcessKey (int ch)
{
return false;
}
public void SelectedChanged ()
{
return;
}
public void SetListView (ListView target)
{
view = target;
}
public void Add (IEnumerable s)
{
items.Add (s);
}
}
public class Shell : Container
{
IDbCommand command;
IDbConnection dbConnection;
IDataReader reader;
TablesList tables;
RecordsList records;
ListView tables_view;
ListView records_view;
Frame recordsFrame;
static int TABLES_WIDTH = 25;
static int MAX_NUMBER_OF_RECORDS = 30;
string currentTable;
string sql;
public void UpdateRecordsView ()
{
/* In C#, IIUC, it is better to create a new List object and
* let the GC clear the old list object, is faster than
* emptying the list manually and adding items again.
*/
records = new RecordsList ();
sql = "SELECT * FROM " + currentTable;
command.CommandText = sql;
reader = command.ExecuteReader ();
int n = 0, col_count;
while (reader.Read () && n++ < MAX_NUMBER_OF_RECORDS) {
List <string> record;
record = new List<string> ();
for (col_count = 0; col_count < reader.FieldCount; ++col_count) {
try {
//Console.WriteLine (col_count + "Adding column " + reader.GetString (col_count));
record.Add (reader.GetString (col_count));
} catch (System.NullReferenceException ex){
record.Add ("???");
}
}
records.Add (record);
}
if (records_view != null)
recordsFrame.Remove (records_view);
records_view = new ListView (1, 1, 1, records.Items, records);
recordsFrame.Add (records_view);
recordsFrame.Redraw ();
}
public Shell (string filename) : base (0, 0, Application.Cols, Application.Lines)
{
string connectionString;
tables = new TablesList ();
connectionString = "URI=file:" + filename;
dbConnection = (IDbConnection) new SqliteConnection(connectionString);
dbConnection.Open ();
command = dbConnection.CreateCommand ();
sql = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
command.CommandText = sql;
reader = command.ExecuteReader ();
while (reader.Read ()) {
tables.Add (reader.GetString (0));
}
currentTable = tables.items [0];
Frame tablesFrame = new Frame ("Tables");
Add (tablesFrame);
tablesFrame.x = 0;
tablesFrame.y = 0;
tablesFrame.w = TABLES_WIDTH;
tablesFrame.h = Application.Lines;
tables_view = new ListView (1, 1, 1, tables.Items, tables);
tablesFrame.Add (tables_view);
recordsFrame = new Frame ("Records");
recordsFrame.x = TABLES_WIDTH;
recordsFrame.y = 0;
recordsFrame.w = Application.Cols - TABLES_WIDTH - 1;
recordsFrame.h = Application.Lines;
Add (recordsFrame);
UpdateRecordsView ();
tables.newSel += new TablesList.TableSelectionChanged (UpdateRecordsForNewSelectedTable);
}
private void UpdateRecordsForNewSelectedTable (TablesList t, EventArgs e)
{
currentTable = t.items[tables_view.Selected];
UpdateRecordsView ();
}
~Shell ()
{
// clean up
reader.Close (); reader = null;
command.Dispose (); command = null;
dbConnection.Close (); dbConnection = null;
}
static void Main (String [] args)
{
if (args.Length != 1)
Console.WriteLine ("Insufficient number of parameters");
else {
Application.Init (false);
Shell s = new Shell (args [0]);
Application.Run (s);
}
}
}
}