pg_res.c

Go to the documentation of this file.
00001 /* 
00002  * $Id$ 
00003  *
00004  * PostgreSQL Database Driver for SER
00005  *
00006  * Portions Copyright (C) 2001-2003 FhG FOKUS
00007  * Copyright (C) 2003 August.Net Services, LLC
00008  * Portions Copyright (C) 2005-2008 iptelorg GmbH
00009  *
00010  * This file is part of SER, a free SIP server.
00011  *
00012  * SER is free software; you can redistribute it and/or modify it under the
00013  * terms of the GNU General Public License as published by the Free Software
00014  * Foundation; either version 2 of the License, or (at your option) any later
00015  * version
00016  *
00017  * For a license to use the ser software under conditions other than those
00018  * described here, or to purchase support for this software, please contact
00019  * iptel.org by e-mail at the following addresses: info@iptel.org
00020  *
00021  * SER is distributed in the hope that it will be useful, but WITHOUT ANY
00022  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00023  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00024  * details.
00025  *
00026  * You should have received a copy of the GNU General Public License along
00027  * with this program; if not, write to the Free Software Foundation, Inc., 59
00028  * Temple Place, Suite 330, Boston, MA 02111-1307 USA
00029  */
00030 
00039 #include "pg_res.h"
00040 #include "pg_cmd.h"
00041 
00042 #include "../../mem/mem.h"
00043 #include "../../dprint.h"
00044 #include "../../lib/srdb2/db_gen.h"
00045 
00046 
00047 static void pg_res_free(db_res_t* res, struct pg_res* payload)
00048 {
00049         db_drv_free(&payload->gen);
00050         if (payload->res) PQclear(payload->res);
00051         pkg_free(payload);
00052 }
00053 
00054 
00055 int pg_res(db_res_t* res)
00056 {
00057         struct pg_res* pres;
00058 
00059         pres = (struct pg_res*)pkg_malloc(sizeof(struct pg_res));
00060         if (pres == NULL) {
00061                 ERR("postgres: No memory left\n");
00062                 return -1;
00063         }
00064         if (db_drv_init(&pres->gen, pg_res_free) < 0) goto error;
00065         DB_SET_PAYLOAD(res, pres);
00066         return 0;
00067         
00068  error:
00069         if (pres) {
00070                 db_drv_free(&pres->gen);
00071                 pkg_free(pres);
00072         }
00073         return -1;
00074 }
00075