View this PageEdit this PageUploads to this PageVersions of this PageHomeRecent ChangesSearchHelp Guide

Desarrollando para web (weblib)




Tuesday, 1 April 2014, 4:55:17 pm
postgres Para acceder a horus desde salchicha El usuario es metro.
/usr/lib/postgresql/bin/psql -h 3.0.1.198 -d horus_db -u
select tablename from pg_tables;
\d "TIPOS_SUBSISTEMA";
select * from pg_tables where tablename = 'TIPOS_SUBSISTEMA';
select * from "TIPOS_SUBSISTEMA";


Tuesday, 18 March 2014, 8:59:39 am
postgres: sync/async Las conexiones a la BBDD se pueden hacer síncronas o asíncronas:
  • Para las síncronas se usa PQexec()
  • Para las asíncronas se usa PQsendQuery()/PQgetResult(). Estas funciones se han de usar con:
    • PQsocket() te proporcional el fd de la conexión para poderla integrar en un select
    • PQconsumeInput() lee los datos a un buffer interno
    • PQbusy() se hace después de cada PQconsumeInput(), y si devuelve 0 es que se puede hacer un PQgetResult() sin bloquear.

Para integrar esto en la weblib.h, habría que poner un API de añadir fds para ser notificados con un callback (y de poder quitarlos también).

Monday, 17 March 2014, 9:13:12 am
Acceso a postgres Ejemplo de cómo hacerlo y un programa de ejemplo con libpq. Está en

 salchicha:/home/dario/Programacion/pruebas/libpq

Para añadir una nueva máquina autorizada para la BBDD:

# ssh root@gea
# cd /etc/postgresql/9.1/main
# vi pg_hba.conf
# ps -efa | grep bin/pos[t]gres | sed "s/^[^0-9]*//g" | cut -d ' ' -f 1 | xargs kill -HUP


Un programa que acceda a la BBDD:

 test.c

/*
 * test.c
 *
 * Based on the example in:
 *
 * http://stackoverflow.com/questions/1138503/good-c-c-connector-library-for-postgresql
 */

#include <stdio.h>
#include <postgresql/libpq-fe.h>

int
main(int argc, char *argv[])
{
        char *dbserver = "3.0.1.198"; /* Gea */
        char *uname    = "metro";
        char *pass     = "metro1";
        char *db       = "horus_db";

        char *SQL      = "select * from \"$OBJ_ESTACIONES\"";
        // char *SQL      = "select userid, stationid from public.auditlog";

        char     buff[200];
        PGconn   *dbconn;
        PGresult *res;
        int nFields, i, j;

        printf("Attempting to Connect to Database Server:\n");
        printf("Database: %s\n", db);
        printf("Server  : %s\n", dbserver);

        sprintf(buff, "dbname=%s host=%s port=5432 user=%s password=%s",
                 db, dbserver, uname, pass);

        dbconn = PQconnectdb(buff);

        if(PQstatus(dbconn)!=CONNECTION_OK) {
                printf("Connection Failed: %s\n", PQerrorMessage(dbconn) );
        } else {
                printf("Connected Successfully!\n");
                sprintf(buff, "BEGIN; DECLARE my_portal CURSOR FOR %s", SQL);
                res=PQexec(dbconn, buff);
                if(PQresultStatus(res)!=PGRES_COMMAND_OK) {
                        printf("Error executing SQL!: %s\n", PQerrorMessage(dbconn) );
                        PQclear(res);
                } else {
                        PQclear(res);
                        res=PQexec(dbconn, "FETCH ALL in my_portal");
                        if(PQresultStatus(res)!=PGRES_TUPLES_OK) {
                                printf("ERROR, Fetch All Failed: %s", PQerrorMessage(dbconn));
                                PQclear(res);
                        } else {
                                nFields=PQnfields(res);
                                // Print out the field names
                                for(i=0;i<nFields;i++)
                                        printf("%-15s", PQfname(res, i));
                                printf("\n");
                                // Print out the rows
                                for(i=0;i<PQntuples(res);i++) {
                                        for(j=0;j<nFields;j++)
                                                printf("%-15s", PQgetvalue(res, i, j) );
                                        printf("\n");
                                }
                                res=PQexec(dbconn, "END");
                                PQclear(res);
                        }
                }
        }
        PQfinish(dbconn);
        return(0);
}




El makefile correspondiente:

Makefile

CC=gcc
CFLAGS=-g -Wall
LDFLAGS=-lpq

all: test

clean:
        rm -f test

test: test.c






Descripción del proyecto


Documentación de cosas sobre weblib (nuestro servidor web embebido)

Está en el git de sico, y adicionalmente en

 salchicha:/home/dario/Programacion/proyectos/sico-weblib