namespace LibSernatur {
	namespace DB {
		using Postgres;
		using Wrapper;

		public class Region : GLib.Object {
			public uint id_region { get; set; default = 0; }
			public string nombre_region { get; set; default = ""; }

			public Region (uint id_region = 0, string nombre_region = "") {
				this.id_region = id_region;
				this.nombre_region = nombre_region;
			}

			public static Region[]? get_all_regiones(Database conn) {
				var res = conn.exec ("
SELECT id_region, nombre_region FROM region
				");
				if (res.get_status () != ExecStatus.TUPLES_OK) {
					#if DEBUG
						error (conn.get_error_message ());
					#else
						warning (conn.get_error_message ());
						return null;
					#endif
				}

				var wra = new ResultWrapper (res);
				Region[] regiones = {};
				int n = res.get_n_tuples ();
				for (int i = 0; i < n; i++) {
					try {
						var region = new Region (wra.get_int_n (i, "id_region"),
							wra.get_string_n (i, "nombre_region")
						);
						regiones += region;
					}
					catch (Error e) {
						#if DEBUG
							error (e.message);
						#else
							warning (e.message);
						#endif
					}
				}
				return regiones;
			}
		}
	}
}
