aboutsummaryrefslogtreecommitdiff
path: root/qlite/src/table.vala
diff options
context:
space:
mode:
Diffstat (limited to 'qlite/src/table.vala')
-rw-r--r--qlite/src/table.vala22
1 files changed, 21 insertions, 1 deletions
diff --git a/qlite/src/table.vala b/qlite/src/table.vala
index 209a5a96..7396136e 100644
--- a/qlite/src/table.vala
+++ b/qlite/src/table.vala
@@ -6,14 +6,31 @@ public class Table {
protected Database db;
public string name { get; private set; }
protected Column[] columns;
+ private string constraints;
public Table(Database db, string name) {
this.db = db;
this.name = name;
}
- public void init(Column[] columns) {
+ public void init(Column[] columns, string? constraints = null) {
this.columns = columns;
+ this.constraints = constraints;
+ }
+
+ public void unique(Column[] columns, string? on_conflict = null) {
+ if (constraints == null) constraints = ""; else constraints += ", ";
+ constraints += "UNIQUE (";
+ bool first = true;
+ foreach(Column c in columns) {
+ if (!first) constraints += ", ";
+ constraints += c.name;
+ first = false;
+ }
+ constraints += ")";
+ if (on_conflict != null) {
+ constraints += "ON CONFLICT " + on_conflict;
+ }
}
private void ensure_init() throws DatabaseError {
@@ -63,6 +80,9 @@ public class Table {
sql += @"$(i > 0 ? "," : "") $c";
}
}
+ if (constraints != null) {
+ sql += ", " + constraints;
+ }
sql += ")";
db.exec(sql);
}