-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_page.dart
257 lines (236 loc) · 8.41 KB
/
home_page.dart
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
import 'package:flutter/material.dart';
import 'package:sqflite_common/sqlite_api.dart' as sqflite;
import 'package:sqflite_web/sqflite_web.dart';
import '../widgets/radio_button.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late String _commandType;
final _descriptions = <String, String>{
'Execute':
'Execute an SQL query with no return value.\n\nFor example:\nCREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER);',
'Insert':
'Execute an SQL INSERT query and show the last inserted row ID\n\nFor example:\nINSERT INTO Test(name, value) VALUES("Adit", 1234);',
'Query':
'Execute an SQL SELECT query and show the output.\n\nFor example:\nSELECT * FROM Test;',
'Update':
'Execute an SQL UPDATE query and show the number of changes made.\n\nFor example:\nUPDATE Test SET name = "Adit Luhadia", value = 1234 WHERE name = "Adit";',
'Delete':
'Execute an SQL DELETE query and show the number of changes made.\n\nFor example:\nDELETE FROM Test WHERE name = "Adit";',
};
final _commandController = TextEditingController();
Widget _output = Container();
@override
void initState() {
super.initState();
_commandType = 'Execute';
}
@override
void dispose() {
_commandController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('AllSQL'),
),
body: ListView(
padding: const EdgeInsets.all(50.0),
children: [
TextField(
controller: _commandController,
minLines: 4,
maxLines: 10,
decoration: const InputDecoration(
hintText: 'Enter your SQL command',
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(15.0),
),
),
),
),
const SizedBox(height: 20.0),
Row(
children: [
const SizedBox(width: 20.0),
RadioButton(
value: 'Execute',
groupValue: _commandType,
onChanged: (value) {
setState(() {
if (value != null) {
_commandType = value;
}
});
},
),
const SizedBox(width: 20.0),
RadioButton(
value: 'Insert',
groupValue: _commandType,
onChanged: (value) {
setState(() {
if (value != null) {
_commandType = value;
}
});
},
),
const SizedBox(width: 20.0),
RadioButton(
value: 'Query',
groupValue: _commandType,
onChanged: (value) {
setState(() {
if (value != null) {
_commandType = value;
}
});
},
),
const SizedBox(width: 20.0),
RadioButton(
value: 'Update',
groupValue: _commandType,
onChanged: (value) {
setState(() {
if (value != null) {
_commandType = value;
}
});
},
),
const SizedBox(width: 20.0),
RadioButton(
value: 'Delete',
groupValue: _commandType,
onChanged: (value) {
setState(() {
if (value != null) {
_commandType = value;
}
});
},
),
const SizedBox(width: 20.0),
const Spacer(),
ElevatedButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(const EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 15.0,
)),
shape: MaterialStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
)),
),
onPressed: () async {
final databaseFactory = databaseFactoryWeb;
final db = await databaseFactory
.openDatabase(sqflite.inMemoryDatabasePath);
switch (_commandType) {
case 'Execute':
await db.execute(_commandController.text);
setState(() {
_output = const Text('Executed the command');
});
break;
case 'Insert':
final int lastRow =
await db.rawInsert(_commandController.text);
setState(() {
_output = Text('ID of last row inserted is $lastRow.');
});
break;
case 'Query':
final List<Map<String, Object?>> queryOutput =
await db.rawQuery(_commandController.text);
if (queryOutput.isEmpty) {
_output = const Text('No output!');
} else {
_output = DataTable(
columns: queryOutput.first.keys
.map((e) => DataColumn(
label: Text(e),
))
.toList(),
rows: queryOutput
.map((e) => DataRow(
cells: queryOutput.first.keys
.map((a) => DataCell(
Text(e[a]?.toString() ?? 'null')))
.toList()))
.toList(),
);
}
setState(() {});
break;
case 'Update':
final int rowsUpdated =
await db.rawUpdate(_commandController.text);
setState(() {
_output = Text('$rowsUpdated rows deleted!');
});
break;
case 'Delete':
final int rowsDeleted =
await db.rawDelete(_commandController.text);
setState(() {
_output = Text('$rowsDeleted rows deleted!');
});
break;
default:
}
// print(
// 'sqlite_master table: ${await db.rawQuery("SELECT * FROM sqlite_master;")}');
},
child: Row(
children: const [
Text(
'RUN',
style: TextStyle(
fontSize: 16.0,
),
),
SizedBox(width: 15.0),
Icon(Icons.play_circle_outline),
],
),
),
const SizedBox(width: 20.0),
],
),
const SizedBox(height: 20.0),
Container(
width: double.infinity,
padding: const EdgeInsets.all(20.0),
decoration: BoxDecoration(
color: Colors.grey.shade300,
borderRadius: BorderRadius.circular(15.0),
),
child: Text(
_descriptions[_commandType] ?? 'Error!',
style: TextStyle(
color: Colors.grey.shade600,
),
),
),
const SizedBox(height: 20.0),
Text(
'OUTPUT',
style: Theme.of(context).textTheme.headline6,
),
const SizedBox(height: 20.0),
_output,
],
),
);
}
}