dino/qlite/src/column.vala

191 lines
6.1 KiB
Vala
Raw Normal View History

2017-03-02 14:37:32 +00:00
using Sqlite;
namespace Qlite {
public abstract class Column<T> {
public const string DEFAULT_TABLE_NAME = "";
2017-03-02 14:37:32 +00:00
public string name { get; private set; }
2017-04-16 13:11:00 +00:00
public string? default { get; set; }
2017-03-02 14:37:32 +00:00
public int sqlite_type { get; private set; }
public bool primary_key { get; set; }
public bool auto_increment { get; set; }
public bool unique { get; set; }
2017-04-16 13:11:00 +00:00
public virtual bool not_null { get; set; }
2017-03-02 14:37:32 +00:00
public long min_version { get; set; default = -1; }
public long max_version { get; set; default = long.MAX; }
internal Table table { get; set; }
2017-03-02 14:37:32 +00:00
public abstract T get(Row row, string? table_name = DEFAULT_TABLE_NAME);
2017-03-02 14:37:32 +00:00
public virtual bool is_null(Row row, string? table_name = DEFAULT_TABLE_NAME) {
2017-03-02 14:37:32 +00:00
return false;
}
internal abstract void bind(Statement stmt, int index, T value);
2017-03-02 14:37:32 +00:00
public string to_string() {
return table == null ? name : (table.name + "." + name);
}
public string to_column_definition() {
2017-03-02 14:37:32 +00:00
string res = name;
switch (sqlite_type) {
case INTEGER:
res += " INTEGER";
break;
case FLOAT:
res += " REAL";
break;
case TEXT:
res += " TEXT";
break;
default:
res += " UNKNOWN";
break;
}
if (primary_key) {
res += " PRIMARY KEY";
if (auto_increment) res += " AUTOINCREMENT";
}
if (not_null) res += " NOT NULL";
if (unique) res += " UNIQUE";
2017-04-16 13:11:00 +00:00
if (default != null) res += @" DEFAULT $((!) default)";
2017-03-02 14:37:32 +00:00
return res;
}
Column(string name, int type) {
2017-03-02 14:37:32 +00:00
this.name = name;
this.sqlite_type = type;
}
public class Integer : Column<int> {
public Integer(string name) {
base(name, INTEGER);
}
public override int get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return (int) row.get_integer(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
public override bool is_null(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return !row.has_integer(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
internal override void bind(Statement stmt, int index, int value) {
2017-03-02 14:37:32 +00:00
stmt.bind_int(index, value);
}
}
public class Long : Column<long> {
public Long(string name) {
base(name, INTEGER);
}
public override long get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return (long) row.get_integer(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
public override bool is_null(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return !row.has_integer(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
internal override void bind(Statement stmt, int index, long value) {
2017-03-02 14:37:32 +00:00
stmt.bind_int64(index, value);
}
}
public class NullableReal : Column<double?> {
public NullableReal(string name) {
2017-03-02 14:37:32 +00:00
base(name, FLOAT);
}
public override bool not_null { get { return false; } set {} }
public override double? get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return row.get_real(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
public override bool is_null(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return !row.has_real(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
internal override void bind(Statement stmt, int index, double? value) {
2017-03-02 14:37:32 +00:00
stmt.bind_double(index, value);
}
}
public class Text : Column<string?> {
public Text(string name) {
base(name, TEXT);
}
public override string? get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return row.get_text(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-03-02 14:37:32 +00:00
}
public override bool is_null(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return get(row, table_name == DEFAULT_TABLE_NAME ? table.name : table_name) == null;
2017-03-02 14:37:32 +00:00
}
internal override void bind(Statement stmt, int index, string? value) {
2017-03-02 14:37:32 +00:00
if (value != null) {
2017-04-16 13:11:00 +00:00
stmt.bind_text(index, (!) value);
2017-03-02 14:37:32 +00:00
} else {
stmt.bind_null(index);
}
}
}
2017-04-16 13:11:00 +00:00
public class NonNullText : Column<string> {
public NonNullText(string name) {
base(name, TEXT);
}
public override bool not_null { get { return true; } set {} }
public override string get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return (!)row.get_text(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name);
2017-04-16 13:11:00 +00:00
}
public override bool is_null(Row row, string? table_name = DEFAULT_TABLE_NAME) {
2017-04-16 13:11:00 +00:00
return false;
}
internal override void bind(Statement stmt, int index, string value) {
stmt.bind_text(index, (!) value);
}
}
2017-03-02 14:37:32 +00:00
public class BoolText : Column<bool> {
public BoolText(string name) {
base(name, TEXT);
}
public override bool get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return row.get_text(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name) == "1";
2017-03-02 14:37:32 +00:00
}
internal override void bind(Statement stmt, int index, bool value) {
2017-03-02 14:37:32 +00:00
stmt.bind_text(index, value ? "1" : "0");
}
}
public class BoolInt : Column<bool> {
public BoolInt(string name) {
base(name, INTEGER);
}
public override bool get(Row row, string? table_name = DEFAULT_TABLE_NAME) {
return row.get_integer(name, table_name == DEFAULT_TABLE_NAME ? table.name : table_name) == 1;
2017-03-02 14:37:32 +00:00
}
internal override void bind(Statement stmt, int index, bool value) {
2017-03-02 14:37:32 +00:00
stmt.bind_int(index, value ? 1 : 0);
}
}
}
}