sernatur/lib/dbwrapper.vala

65 lines
1.9 KiB
Vala
Raw Normal View History

2019-01-03 19:47:43 -03:00
namespace Sernatur {
/**
* This is the database namespace for Sernatur
*/
namespace DB {
/**
* This is a wrapper for Postgres that allows easier use of the database
*/
namespace Wrapper {
using Postgres;
public errordomain Field {
NOTFOUND
}
public class ResultWrapper : GLib.Object {
public unowned Result result { get; set; default = null; }
public ResultWrapper (Result? result = null) {
this.result = result;
}
public string get_string_v (int tup_num, int field_num) {
return result.get_value (tup_num, field_num);
}
public int get_int_v (int tup_num, int field_num) {
return int.parse (result.get_value (tup_num, field_num));
}
public float get_float_v (int tup_num, int field_num) {
return float.parse(result.get_value (tup_num, field_num));
}
public double get_double_v (int tup_num, int field_num) {
return double.parse (result.get_value (tup_num, field_num));
}
public string get_string_n (int tup_num, string name) throws Field {
return get_string_v (tup_num, check_field_found (result.get_field_number (name), name));;
}
public int get_int_n (int tup_num, string name) throws Field {
return get_int_v (tup_num, check_field_found (result.get_field_number (name), name));;
}
public float get_float_n (int tup_num, string name) throws Field {
return get_float_v (tup_num, check_field_found (result.get_field_number (name), name));;
}
public double get_double_n (int tup_num, string name) throws Field {
return get_double_v (tup_num, check_field_found (result.get_field_number (name), name));
}
private int check_field_found (int field_num, string name) throws Field {
if (field_num == -1) {
throw new Field.NOTFOUND (dgettext (null, "The field %s was not found in the query results!"), name);
}
return field_num;
}
}
}
}
}