Skip to content

Commit 17cffbd

Browse files
committed
initial release
0 parents  commit 17cffbd

File tree

8 files changed

+631
-0
lines changed

8 files changed

+631
-0
lines changed

.gitignore

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
33+
# Android related
34+
**/android/**/gradle-wrapper.jar
35+
**/android/.gradle
36+
**/android/captures/
37+
**/android/gradlew
38+
**/android/gradlew.bat
39+
**/android/local.properties
40+
**/android/**/GeneratedPluginRegistrant.java
41+
42+
# iOS/XCode related
43+
**/ios/**/*.mode1v3
44+
**/ios/**/*.mode2v3
45+
**/ios/**/*.moved-aside
46+
**/ios/**/*.pbxuser
47+
**/ios/**/*.perspectivev3
48+
**/ios/**/*sync/
49+
**/ios/**/.sconsign.dblite
50+
**/ios/**/.tags*
51+
**/ios/**/.vagrant/
52+
**/ios/**/DerivedData/
53+
**/ios/**/Icon?
54+
**/ios/**/Pods/
55+
**/ios/**/.symlinks/
56+
**/ios/**/profile
57+
**/ios/**/xcuserdata
58+
**/ios/.generated/
59+
**/ios/Flutter/App.framework
60+
**/ios/Flutter/Flutter.framework
61+
**/ios/Flutter/Flutter.podspec
62+
**/ios/Flutter/Generated.xcconfig
63+
**/ios/Flutter/app.flx
64+
**/ios/Flutter/app.zip
65+
**/ios/Flutter/flutter_assets/
66+
**/ios/Flutter/flutter_export_environment.sh
67+
**/ios/ServiceDefinitions.json
68+
**/ios/Runner/GeneratedPluginRegistrant.*
69+
70+
# Exceptions to above rules.
71+
!**/ios/**/default.mode1v3
72+
!**/ios/**/default.mode2v3
73+
!**/ios/**/default.pbxuser
74+
!**/ios/**/default.perspectivev3
75+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

.metadata

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: f139b11009aeb8ed2a3a3aa8b0066e482709dde3
8+
channel: stable
9+
10+
project_type: package

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.1
2+
initial release
3+

LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# easiestdb
2+

lib/easiestdb.dart

+268
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
import 'package:sqflite/sqflite.dart';
2+
import 'package:path/path.dart';
3+
4+
class EasiestDb {
5+
// Vars
6+
static String _dbName;
7+
static int _version = 1;
8+
static String _dbPath;
9+
static List<DbTable> _tables = [];
10+
static Database _db;
11+
12+
//
13+
static void deleteDatabase() {
14+
String SQL = ' DROP DATABASE ${_dbName.replaceAll(".db", "")} ';
15+
_db.execute(SQL);
16+
}
17+
18+
//
19+
static void deleteTable(int tableIndex) {
20+
String SQL = ' DROP TABLE ${_tables[tableIndex]._tableName} ';
21+
_db.execute(SQL);
22+
}
23+
24+
//
25+
static Future<int> deleteDataBySearchingInColumn(
26+
int tableIndex, Datum datum) {
27+
String SQL =
28+
' DELETE FROM ${_tables[tableIndex]._tableName} WHERE ${_tables[tableIndex]._dbColumns[datum._columnIndex]._columnName} = ${datum._value} ';
29+
return _db.rawDelete(SQL);
30+
}
31+
32+
//
33+
static Future<int> deleteOneData(int tableIndex, int rowId) {
34+
String SQL =
35+
' DELETE FROM ${_tables[tableIndex]._tableName} WHERE ID = $rowId ';
36+
return _db.rawDelete(SQL);
37+
}
38+
39+
//
40+
static Future<int> updateOneDataById(
41+
int tableIndex, int rowId, List<Datum> data) {
42+
//
43+
String SQL = ' UPDATE ${_tables[tableIndex]._tableName} SET ';
44+
List<DbColumn> columns = _tables[tableIndex]._dbColumns;
45+
46+
for (int i = 0; i < data.length; i++) {
47+
SQL +=
48+
" ${columns[data[i]._columnIndex]._columnName} = \'${data[i]._value}\' ";
49+
if (i == data.length - 1) {
50+
SQL += " ";
51+
} else {
52+
SQL += " , ";
53+
}
54+
}
55+
56+
SQL += " WHERE ID = $rowId ";
57+
58+
return _db.rawUpdate(SQL);
59+
}
60+
61+
//
62+
static Future<List<Map<String, dynamic>>> getRowsByMatchingColumnData(
63+
int tableIndex, int columnIndex, var valueToMatch,
64+
{bool ascending = true}) {
65+
//
66+
String orderBy = ascending ? "ASC" : "DESC";
67+
String SQL =
68+
' SELECT * FROM ${_tables[tableIndex]._tableName} WHERE ${_tables[tableIndex]._dbColumns[columnIndex]._columnName}=$valueToMatch ORDER BY ID $orderBy ';
69+
return _db.rawQuery(SQL);
70+
}
71+
72+
//
73+
static Future<List<Map<String, dynamic>>> getOneRowData(
74+
int tableIndex, int rowId) {
75+
//
76+
String SQL =
77+
' SELECT * FROM ${_tables[tableIndex]._tableName} WHERE ID=$rowId ';
78+
return _db.rawQuery(SQL);
79+
}
80+
81+
//
82+
static Future<List<Map<String, dynamic>>> getAllData(int tableIndex,
83+
{bool ascending = true}) {
84+
//
85+
String orderBy = ascending ? "ASC" : "DESC";
86+
String SQL =
87+
' SELECT * FROM ${_tables[tableIndex]._tableName} ORDER BY ID $orderBy ';
88+
return _db.rawQuery(SQL);
89+
}
90+
91+
//
92+
static Future<int> addData(int tableIndex, List<Datum> data) {
93+
//
94+
String SQL = ' INSERT INTO ${_tables[tableIndex]._tableName} ( ';
95+
List<DbColumn> columns = _tables[tableIndex]._dbColumns;
96+
97+
for (int i = 0; i < data.length; i++) {
98+
SQL += " " + columns[data[i]._columnIndex]._columnName + " ";
99+
100+
if (i == data.length - 1) {
101+
SQL += " ) ";
102+
} else {
103+
SQL += " , ";
104+
}
105+
}
106+
107+
SQL += " VALUES ( ";
108+
109+
for (int i = 0; i < data.length; i++) {
110+
SQL += " \'" + data[i]._value + "\' ";
111+
112+
if (i == data.length - 1) {
113+
SQL += " ) ";
114+
} else {
115+
SQL += " , ";
116+
}
117+
}
118+
119+
return _db.rawInsert(SQL);
120+
}
121+
122+
//
123+
static Future<List<Map<String, dynamic>>> runCustomSqlQuery(
124+
String sqlCommand) {
125+
//
126+
return _db.rawQuery(sqlCommand);
127+
}
128+
129+
static void runCustomSqlCommand(String sqlCommand) {
130+
_db.execute(sqlCommand);
131+
}
132+
133+
//
134+
static Future<Database> init({
135+
String dbName = 'demo.db',
136+
int version = 1,
137+
List<DbTable> tables,
138+
}) async {
139+
//
140+
_tables = tables;
141+
_dbPath = await getDatabasesPath();
142+
_dbName = dbName.replaceAll(" ", "_").toUpperCase();
143+
if (!_dbName.endsWith(".db")) {
144+
_dbName += ".db";
145+
}
146+
_version = version;
147+
148+
print("Creating database");
149+
150+
return _db = await openDatabase(join(_dbPath, _dbName),
151+
onCreate: (db, version) {
152+
print("onCreate");
153+
for (int i = 0; i < tables.length; i++) {
154+
String SQL = " CREATE TABLE " + tables[i]._tableName + " ( ";
155+
156+
tables[i]._dbColumns.insert(
157+
0,
158+
DbColumn("ID",
159+
columnDataType: " INTEGER PRIMARY KEY AUTOINCREMENT "));
160+
List<DbColumn> columns = tables[i].dbColumns;
161+
162+
for (int j = 0; j < columns.length; j++) {
163+
SQL += " " +
164+
columns[j]._columnName +
165+
" " +
166+
columns[j]._columnDataType +
167+
" ";
168+
169+
if (j == columns.length - 1) {
170+
SQL += " ) ";
171+
} else {
172+
SQL += " , ";
173+
}
174+
}
175+
_tables = tables;
176+
db.execute(SQL);
177+
print("$_dbName creatd");
178+
}
179+
},
180+
version: _version,
181+
onConfigure: (Database db) {
182+
print("onConfigure");
183+
_tables = tables;
184+
},
185+
onOpen: (Database db) {
186+
print("onOpen");
187+
_tables = tables;
188+
},
189+
onDowngrade: (Database db, int oldVersion, int newVersion) {
190+
print("onDowngrade");
191+
_tables = tables;
192+
},
193+
onUpgrade: (Database db, int oldVersion, int newVersion) {
194+
print("onUpgrade");
195+
_tables = tables;
196+
for (int i = 0; i < tables.length; i++) {
197+
db.execute(" DROP TABLE IF EXISTS " + tables[i]._tableName);
198+
}
199+
});
200+
}
201+
}
202+
203+
class DbTable {
204+
String _tableName;
205+
List<DbColumn> _dbColumns = [];
206+
207+
DbTable(String tableName, {List<DbColumn> dbColumns}) {
208+
_tableName = tableName.replaceAll(" ", "_").toUpperCase();
209+
_dbColumns = dbColumns;
210+
}
211+
212+
List<DbColumn> get dbColumns => _dbColumns;
213+
214+
set dbColumns(List<DbColumn> value) {
215+
_dbColumns = value;
216+
}
217+
218+
String get tableName => _tableName;
219+
220+
set tableName(String value) {
221+
_tableName = value.replaceAll(" ", "_");
222+
}
223+
}
224+
225+
class DbColumn {
226+
String _columnName = "", _columnDataType = "";
227+
228+
DbColumn(String columnName, {String columnDataType = " TEXT "}) {
229+
_columnName = " " +
230+
columnName.replaceAll(" ", "_").replaceAll(".", "_").toUpperCase() +
231+
" ";
232+
_columnDataType = columnDataType.toUpperCase();
233+
}
234+
235+
get columnDataType => _columnDataType;
236+
237+
set columnDataType(value) {
238+
_columnDataType = value;
239+
}
240+
241+
String get columnName => _columnName;
242+
243+
set columnName(String value) {
244+
_columnName = value.replaceAll(" ", "_");
245+
}
246+
}
247+
248+
class Datum {
249+
int _columnIndex;
250+
String _value = "";
251+
252+
Datum(int columnIndex, String value) {
253+
_columnIndex = columnIndex;
254+
_value = value;
255+
}
256+
257+
String get value => _value;
258+
259+
set value(String value) {
260+
_value = value;
261+
}
262+
263+
int get columnIndex => _columnIndex;
264+
265+
set columnIndex(int value) {
266+
_columnIndex = value;
267+
}
268+
}

0 commit comments

Comments
 (0)