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 {
|
2019-01-04 11:48:31 -03:00
|
|
|
return get_string_v (tup_num, check_field_found (result.get_field_number (name), name));
|
2019-01-03 19:47:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public int get_int_n (int tup_num, string name) throws Field {
|
2019-01-04 11:48:31 -03:00
|
|
|
return get_int_v (tup_num, check_field_found (result.get_field_number (name), name));
|
2019-01-03 19:47:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public float get_float_n (int tup_num, string name) throws Field {
|
2019-01-04 11:48:31 -03:00
|
|
|
return get_float_v (tup_num, check_field_found (result.get_field_number (name), name));
|
2019-01-03 19:47:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|