Property definitions

postgresql $ NativePostgres :: defaultinit
extern class NativePostgres `{PGconn *`}

  # Connect to a new database using the conninfo string as a parameter
  new connectdb(conninfo: Text) import Text.to_cstring `{
    PGconn * self = NULL;
    self = PQconnectdb(Text_to_cstring(conninfo));
    return self;
  `}

  # Submits a query to the server and waits for the result returns the ExecStatustype of the query
  fun exec(query: Text): NativePGResult import Text.to_cstring `{
    PGresult *res = PQexec(self, Text_to_cstring(query));
    return res;
  `}

  # Prepares a statement with the given parameters
  fun prepare(stmt: String, query: String, nParams: Int): NativePGResult import String.to_cstring `{
    const char * stmtName = String_to_cstring(stmt);
    const char * queryStr = String_to_cstring(query);
    PGresult * res = PQprepare(self, stmtName, queryStr, nParams, NULL);
    return res;
  `}

  fun exec_prepared(stmt: String, nParams: Int, values: Array[String], pLengths: Array[Int], pFormats: Array[Int], resultFormat: Int): NativePGResult import String.to_cstring, Array[String].[], Array[Int].[] `{
    const char * stmtName = String_to_cstring(stmt);
    const char * paramValues[nParams];
    int paramLengths[nParams];
    int paramFormats[nParams];
    int i;
    for(i = 0; i < nParams; i++)
      paramValues[i] = String_to_cstring(Array_of_String__index(values, i));
    for(i = 0; i < nParams; i++)
      paramLengths[i] = Array_of_Int__index(pLengths, i);
    for(i = 0; i < nParams; i++)
      paramFormats[i] = Array_of_Int__index(pFormats, i);
    PGresult * res = PQexecPrepared(self, stmtName, nParams, paramValues, paramLengths, paramFormats, resultFormat);
    return res;
  `}

  # Returns the error message of the last operation on the connection
  fun error: String import CString.to_s `{
    char * error = PQerrorMessage(self);
    return CString_to_s(error);
  `}

  # Returns the status of this connection
  fun status: ConnStatusType `{
    return PQstatus(self);
  `}

  # Closes the connection to the server
  fun finish  `{
    PQfinish(self);
  `}

  # Closes the connection to the server and attempts to reconnect with the previously used params
  fun reset `{
    PQreset(self);
  `}
end
lib/postgresql/native_postgres.nit:86,1--145,3