/
table_db.dart
104 lines (88 loc) · 3.36 KB
/
table_db.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
import 'package:psqlite/src/iterable_extension.dart';
import 'column_db.dart';
/// Defines a table in a database.
/// It consists of a collection of ColumnDb objects and a table name.
class TableDb {
/// The collection of columns that make up the table.
final List<ColumnDb> _columns;
/// The name of the table.
final String _name;
/// Create a table with a [name] and a list of empty columns.
TableDb(this._name) : _columns = [];
/// Check if a list of column has unique names.
static bool areUniqueColumnNames(List<ColumnDb> columns) =>
columns.toSet().length == columns.length;
/// Create a table with a [name] and a list of [columns].
TableDb.create({required String name, required List<ColumnDb> columns})
: _name = name,
_columns = columns,
assert(areUniqueColumnNames(columns));
/// Add a [columnDb] in the column list.
/// Throw a exception if the name of the column already exist.
void addColumn(ColumnDb columnDb) {
if (!_columns.contains(columnDb)) {
_columns.add(columnDb);
} else {
throw Exception('Column already exist in the table. $columnDb');
}
}
/// Replace a [newColumn] with the first column that satisfy [whereFunction].
void replaceColumn(
ColumnDb newColumn, bool Function(ColumnDb element) whereFunction) {
int index = _columns.indexWhere(whereFunction);
if (index != -1) {
_columns.removeAt(index);
_columns.insert(index, newColumn);
}
}
/// Search and remove all columns from this table that satisfy [whereFunction].
void searchColumnAndRemoveIt(bool Function(ColumnDb element) whereFunction) =>
_columns.removeWhere(whereFunction);
/// Remove the [column] passed.
bool removeColumn(ColumnDb column) => _columns.remove(column);
/// Return all columns from this table.
List<ColumnDb> getColumns() => _columns;
String getName() => _name;
/// Check if the table make no sense.
void checkInconsistentTable() {
if (_columns.countWhere((column) => column.getName().isEmpty) > 0) {
throw Exception(
'Table $_name has inconsistencies. There is/are empty column names.');
} else if (_columns.countWhere((column) => column.isPrimaryKey()) == 0) {
throw Exception(
'Table $_name has inconsistencies. There is no any primary key column.');
} else if (_columns.countWhere((column) => column.isPrimaryKey()) > 1) {
throw Exception(
'Table $_name has inconsistencies. There are multiple primary keys columns.');
}
}
/// Return the primary key column of the table.
ColumnDb getPrimaryColumn() =>
_columns.firstWhere((column) => column.isPrimaryKey());
/// Return the to create this table based on the columns it currently has.
String getCreateDbRequest() {
checkInconsistentTable();
String query = 'CREATE TABLE $_name(';
for (int i = 0; i < _columns.length; i++) {
final column = _columns[i];
query += column.toString();
if (i != _columns.length - 1) {
query += ', ';
} else {
query += ')';
}
}
return query;
}
/// Return if there is a column with that name.
bool existColumnWith(String name) {
try {
_columns.firstWhere((column) => column.getName() == name);
return true;
} catch (error) {
return false;
}
}
@override
String toString() => 'TableDb{_name: $_name, _columns: $_columns}';
}