current_timestamp() is needed on sqlite.
current_timestamp() works on mysql
current_timestamp() does NOT work on postgresql (well, at least, not on
all versions)
this patch adds a SQL_CURRENT_TIMESTAMP parameter, fixes db.c to use it,
the debian patch and the sqlite driver.
This also includes a fix for oom condition
--
Internet Connection High Quality Web Hosting
http://www.internetconnection.net/
Index: db.c
===================================================================
RCS file: /cvsroot-dbmail/dbmail/db.c,v
retrieving revision 1.91
diff -u -r1.91 db.c
--- db.c 2005/02/14 12:52:01 1.91
+++ db.c 2005/02/18 19:35:28
@@ -54,6 +54,7 @@
extern const char *TO_CHAR;
extern const char *TO_DATE;
+extern const char *SQL_CURRENT_TIMESTAMP;
extern db_param_t _db_params;
@@ -777,7 +778,7 @@
snprintf(query, DEF_QUERYSIZE,
"INSERT INTO %sphysmessage (messagesize, internal_date) "
- "VALUES ('0', CURRENT_TIMESTAMP)", DBPFX);
+ "VALUES ('0', %s)", DBPFX, SQL_CURRENT_TIMESTAMP);
if (db_query(query) == -1) {
trace(TRACE_ERROR, "%s,%s: query failed", __FILE__,
@@ -979,8 +980,8 @@
if (id) {
/* this IP is already in the table, update the 'since' field */
snprintf(query, DEF_QUERYSIZE, "UPDATE %spbsp "
- "SET since = CURRENT_TIMESTAMP WHERE idnr='%llu'",
- DBPFX, id);
+ "SET since = %s WHERE idnr='%llu'",
+ DBPFX, SQL_CURRENT_TIMESTAMP, id);
if (db_query(query) == -1) {
trace(TRACE_ERROR,
@@ -993,7 +994,7 @@
/* IP not in table, insert row */
snprintf(query, DEF_QUERYSIZE,
"INSERT INTO %spbsp (since, ipnumber) "
- "VALUES (CURRENT_TIMESTAMP, '%s')", DBPFX, ip);
+ "VALUES (%s, '%s')", DBPFX, SQL_CURRENT_TIMESTAMP, ip);
if (db_query(query) == -1) {
trace(TRACE_ERROR,
"%s,%s: could not log IP number to dbase "
@@ -4063,16 +4064,16 @@
snprintf(query, DEF_QUERYSIZE, "INSERT INTO %susers "
"(userid,passwd,client_idnr,maxmail_size,"
"encryption_type, last_login) VALUES "
- "('%s','%s',%llu,'%llu','%s', CURRENT_TIMESTAMP)",
+ "('%s','%s',%llu,'%llu','%s', %s)",
DBPFX, escaped_username, escapedpass, clientid,
- maxmail, enctype ? enctype : "");
+ maxmail, enctype ? enctype : "", SQL_CURRENT_TIMESTAMP);
} else {
snprintf(query, DEF_QUERYSIZE, "INSERT INTO %susers "
"(userid,user_idnr,passwd,client_idnr,maxmail_size,"
"encryption_type, last_login) VALUES "
- "('%s',%llu,'%s',%llu,'%llu','%s', CURRENT_TIMESTAMP)",
+ "('%s',%llu,'%s',%llu,'%llu','%s', %s)",
DBPFX,escaped_username,*user_idnr,escapedpass,clientid,
- maxmail, enctype ? enctype : "");
+ maxmail, enctype ? enctype : "", SQL_CURRENT_TIMESTAMP);
}
dm_free(escaped_username);
Index: debian/patches/01_my_strdup.dpatch
===================================================================
RCS file: /cvsroot-dbmail/dbmail/debian/patches/01_my_strdup.dpatch,v
retrieving revision 1.1
diff -u -r1.1 01_my_strdup.dpatch
--- debian/patches/01_my_strdup.dpatch 2004/12/10 19:34:04 1.1
+++ debian/patches/01_my_strdup.dpatch 2005/02/18 19:35:29
@@ -175,9 +175,9 @@
"escaped username", __FILE__, __func__);
return -1;
@@ -538,7 +538,7 @@
- "('%s','%s',%llu,'%llu','%s', CURRENT_TIMESTAMP)",DBPFX,
+ "('%s','%s',%llu,'%llu','%s', %s)",DBPFX,
escaped_username, escapedpass, clientid, maxmail,
- enctype ? enctype : "");
+ enctype ? enctype : "", SQL_CURRENT_TIMESTAMP);
- free(escaped_username);
+ my_free(escaped_username);
Index: mysql/dbmysql.c
===================================================================
RCS file: /cvsroot-dbmail/dbmail/mysql/dbmysql.c,v
retrieving revision 1.94
diff -u -r1.94 dbmysql.c
--- mysql/dbmysql.c 2005/01/14 15:37:44 1.94
+++ mysql/dbmysql.c 2005/02/18 19:35:29
@@ -38,6 +38,7 @@
#define DB_MYSQL_STANDARD_PORT 3306
+const char *SQL_CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
const char *TO_CHAR = "DATE_FORMAT(%s, '%%Y-%%m-%%d %%T')";
/* there is supposed to be a better way of doing this, problem is, it's
only available from MYSQL version 4.1.1 and up, using the
Index: pgsql/dbpgsql.c
===================================================================
RCS file: /cvsroot-dbmail/dbmail/pgsql/dbpgsql.c,v
retrieving revision 1.118
diff -u -r1.118 dbpgsql.c
--- pgsql/dbpgsql.c 2005/01/14 15:37:44 1.118
+++ pgsql/dbpgsql.c 2005/02/18 19:35:29
@@ -38,6 +38,7 @@
const char *TO_CHAR = "TO_CHAR(%s, 'YYYY-MM-DD HH24:MI:SS' )";
const char *TO_DATE = "TO_TIMESTAMP('%s', 'YYYY-MM-DD HH:MI:SS')";
+const char *SQL_CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
static PGconn *conn;
static PGresult *res = NULL;
Index: sqlite/dbsqlite.c
===================================================================
RCS file: /cvsroot-dbmail/dbmail/sqlite/dbsqlite.c,v
retrieving revision 1.1
diff -u -r1.1 dbsqlite.c
--- sqlite/dbsqlite.c 2005/02/18 09:02:34 1.1
+++ sqlite/dbsqlite.c 2005/02/18 19:35:29
@@ -36,6 +36,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <time.h>
db_param_t _db_params;
@@ -43,6 +44,8 @@
const char *TO_CHAR = "%s";
const char *TO_DATE = "%s";
+const char *SQL_CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP()";
+
static sqlite *conn;
struct qtmp {
@@ -52,6 +55,19 @@
struct qtmp *lastq = 0, *saveq = 0, *tempq = 0;
+
+static void dbsqlite_current_timestamp(sqlite_func *f, int argc UNUSED, const char **argv UNUSED)
+{
+ char timestr[21];
+ struct tm tm;
+ time_t now;
+
+ time(&now);
+ localtime_r(&now, &tm);
+ strftime(timestr, sizeof(timestr)-1, "%Y-%m-%d %H:%M:%S", &tm);
+ (void)sqlite_set_result_string(f,timestr,-1);
+}
+
int db_connect()
{
char *errmsg;
@@ -62,6 +78,16 @@
sqlite_freemem(errmsg);
return -1;
}
+ if (sqlite_create_function(conn, "CURRENT_TIMESTAMP", 0,
+ dbsqlite_current_timestamp,
+ 0) != SQLITE_OK) {
+ sqlite_close(conn);
+ trace(TRACE_ERROR,
+ "%si,%s: sqlite_create_function failed",
+ __FILE__, __func__);
+ return -1;
+ }
+
return 0;
}
@@ -127,6 +153,7 @@
trace(TRACE_ERROR,
"%si,%s: malloc failed: %s",
__FILE__, __func__, strerror(errno));
+ return -1;
}
}
@@ -138,6 +165,8 @@
sqlite_freemem(errmsg);
return -1;
}
+
+ return 0;
}
unsigned long db_escape_string(char *to, const char *from, unsigned long length)
{