5 A Linux/Unix GUI for Nokia mobile phones.
6 Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
7 & Ján Derfiòák <ja@mail.upjs.sk>.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
11 Last modification: Sun Dec 17 2000
12 Modified by Jan Derfinak
18 #include <pgsql/libpq-fe.h>
21 #include "gsm-common.h"
23 static PGconn *connIn = NULL;
24 static PGconn *connOut = NULL;
36 gint DB_ConnectInbox (const gchar * const conninfo)
38 connIn = PQconnectdb (conninfo);
40 if (PQstatus (connIn) == CONNECTION_BAD)
42 g_print ("Connection to database '%s' failed.\n", conninfo);
43 g_print ("%s", PQerrorMessage(connIn));
51 gint DB_ConnectOutbox (const gchar * const conninfo)
53 connOut = PQconnectdb (conninfo);
55 if (PQstatus (connOut) == CONNECTION_BAD)
57 g_print ("Connection to database '%s' failed.\n", conninfo);
58 g_print ("%s", PQerrorMessage(connOut));
66 gint DB_InsertSMS (const GSM_SMSMessage * const data)
71 buf = g_string_sized_new (128);
72 g_string_sprintf (buf, "INSERT INTO inbox VALUES ('%s',
73 '%02d-%02d-%02d %02d:%02d:%02d+01', 'now', '%s', 'f')",
74 data->Sender, data->Time.Year + 2000, data->Time.Month,
75 data->Time.Day, data->Time.Hour, data->Time.Minute,
76 data->Time.Second, data->MessageText);
77 res = PQexec(connIn, buf->str);
78 g_string_free(buf, TRUE);
79 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
94 PGresult *res1, *res2;
97 buf = g_string_sized_new (128);
99 g_string_sprintf (buf, "BEGIN");
101 res1 = PQexec(connOut, buf->str);
104 g_string_sprintf (buf, "SELECT id, number, text FROM outbox \
105 WHERE processed='f' FOR UPDATE");
107 res1 = PQexec(connOut, buf->str);
108 if (!res1 || PQresultStatus (res1) != PGRES_TUPLES_OK)
110 g_print ("%s\n", PQcmdStatus (res1));
112 g_print ("%d: SELECT FROM command failed\n", __LINE__);
113 res1 = PQexec (connOut, "ROLLBACK TRANSACTION");
115 g_string_free (buf, TRUE);
119 for (i = 0; i < PQntuples (res1); i++)
123 sms.MessageCenter.No = 1;
126 sms.Compression = false;
127 sms.EightBit = false;
129 sms.UDHType = GSM_NoUDH;
131 strncpy (sms.Destination, PQgetvalue (res1, i, 1), GSM_MAX_DESTINATION_LENGTH + 1);
132 sms.Destination[GSM_MAX_DESTINATION_LENGTH] = '\0';
134 strncpy (sms.MessageText, PQgetvalue (res1, i, 2), GSM_MAX_SMS_LENGTH + 1);
135 sms.MessageText[GSM_MAX_SMS_LENGTH] = '\0';
138 g_print ("%s, %s\n", sms.Destination, sms.MessageText);
141 if (WriteSMS (&sms) != 0)
143 g_string_sprintf (buf, "UPDATE outbox SET processed='t' WHERE id='%s'",
144 PQgetvalue (res1, i, 0));
145 res2 = PQexec(connOut, buf->str);
146 if (!res2 || PQresultStatus (res2) != PGRES_COMMAND_OK)
148 g_print ("%s\n", PQcmdStatus (res2));
149 g_print ("%d: UPDATE command failed\n", __LINE__);
157 g_string_sprintf (buf, "COMMIT");
158 res1 = PQexec(connOut, buf->str);
160 g_string_free(buf, TRUE);