package_diagram postgresql::native_postgres native_postgres core core postgresql::native_postgres->core postgresql::postgres postgres postgresql::postgres->postgresql::native_postgres a_star-m a_star-m a_star-m->postgresql::postgres a_star-m... ... a_star-m...->a_star-m

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.

Children

module postgres

postgresql :: postgres

Services to manipulate a Postgres database

Descendants

module a_star-m

a_star-m

# A native wrapper ove the postgres c api
module native_postgres is pkgconfig("libpq")

in "C header" `{
  #include <libpq-fe.h>
`}

extern class ExecStatusType `{int`}
  new empty           `{ return PGRES_EMPTY_QUERY; `}
  new command_ok      `{ return PGRES_COMMAND_OK; `}
  new tuples_ok       `{ return PGRES_TUPLES_OK; `}
  new copy_out        `{ return PGRES_COPY_OUT; `}
  new copy_in         `{ return PGRES_COPY_IN; `}
  new bad_response    `{ return PGRES_BAD_RESPONSE; `}
  new nonfatal_error  `{ return PGRES_NONFATAL_ERROR; `}
  new fatal_error     `{ return PGRES_FATAL_ERROR; `}

  fun is_ok: Bool `{
    return !(self == PGRES_BAD_RESPONSE || self == PGRES_NONFATAL_ERROR || self == PGRES_FATAL_ERROR);
  `}

  redef fun to_s import CString.to_s `{
    char * err = PQresStatus(self);
    if(err == NULL) err = "";
    return CString_to_s(err);
  `}
end

extern class ConnStatusType `{int`}
  new connection_ok `{ return CONNECTION_OK; `}
  new connection_bad `{ return CONNECTION_BAD; `}

  fun is_ok: Bool `{return self == CONNECTION_OK; `}
end

extern class NativePGResult `{PGresult *`}
  # Frees the memory block associated with the result
  fun clear `{PQclear(self); `}

  # Returns the number of rows in the query result
  fun ntuples:Int `{ return PQntuples(self); `}

  # Returns the number of columns in each row of the query result
  fun nfields:Int `{return PQnfields(self); `}

  # Returns the ExecStatusType of a result
  fun status: ExecStatusType `{ return PQresultStatus(self); `}

  # Returns the field name of a given column_number
  fun fname(column_number:Int):String import CString.to_s `{
    return CString_to_s( PQfname(self, column_number));
  `}

  # Returns the column number associated with the column name
  fun fnumber(column_name:String):Int import String.to_cstring `{
    return PQfnumber(self, String_to_cstring(column_name));
  `}

  # Returns a single field value of one row of the result at row_number, column_number
  fun value(row_number:Int, column_number:Int):String import CString.to_s `{
    return CString_to_s(PQgetvalue(self, row_number, column_number));
  `}

  # Tests wether a field is a null value
  fun is_null(row_number:Int, column_number: Int): Bool `{
    return PQgetisnull(self, row_number, column_number);
  `}

end
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:17,1--145,3