|
| 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