2
** Copyright (C) 2008 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU General Public License as published by
6
** the Free Software Foundation; either version 3 of the License, or
7
** (at your option) any later version.
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software Foundation,
16
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
#include "mu/mu-msg-str.h"
26
#include "mu-msg-sqlite.h"
27
#include "mu-msg-sqlite-priv.h"
28
#include "mu-expr-type.h"
37
mu_msg_sqlite_new (sqlite3 *db, sqlite3_stmt *stmt)
41
g_return_val_if_fail (stmt, NULL);
43
row = (MuMsgSQLite*)malloc(sizeof(MuMsgSQLite));
54
mu_msg_sqlite_destroy (MuMsgSQLite *row)
57
sqlite3_finalize (row->_stmt);
63
mu_msg_sqlite_next (MuMsgSQLite *row, GError **err)
68
g_return_val_if_fail (row, FALSE);
70
/* do the rather ugly usleep/retry; it greatly improves reliability
71
* when mu-index is running concurrently...
74
result = sqlite3_step (row->_stmt);
75
} while (result == SQLITE_BUSY && retry-- > 0 && usleep(5000)==0);
77
if (result == SQLITE_DONE)
78
return FALSE; /* we're done */
80
if (result != SQLITE_ROW) {
81
g_set_error (err, 0, 0,
82
"failed to step statement: %s",
83
sqlite3_errmsg(row->_db));
90
mu_msg_sqlite_get_id (MuMsgSQLite *row)
92
return sqlite3_column_int64(row->_stmt, MU_EXPR_TYPE_ID);
97
mu_msg_sqlite_get_msgid (MuMsgSQLite *row)
99
return (const char*)sqlite3_column_text(row->_stmt,
100
MU_EXPR_TYPE_MSG_ID);
105
mu_msg_sqlite_get_timestamp (MuMsgSQLite *row)
107
return (time_t)sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_TIMESTAMP);
112
mu_msg_sqlite_get_path (MuMsgSQLite *row)
114
return (const char*)sqlite3_column_text(row->_stmt,
120
mu_msg_sqlite_get_date (MuMsgSQLite *row)
122
return (time_t)sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_DATE);
127
mu_msg_sqlite_get_size (MuMsgSQLite *row)
129
return (size_t)sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_SIZE);
133
mu_msg_sqlite_get_from (MuMsgSQLite *row)
135
return (const char*)sqlite3_column_text(row->_stmt, MU_EXPR_TYPE_FROM);
140
mu_msg_sqlite_get_to (MuMsgSQLite *row)
142
return (const char*)sqlite3_column_text(row->_stmt, MU_EXPR_TYPE_TO);
147
mu_msg_sqlite_get_cc (MuMsgSQLite *row)
149
return (const char*)sqlite3_column_text(row->_stmt, MU_EXPR_TYPE_CC);
154
mu_msg_sqlite_get_subject (MuMsgSQLite *row)
156
return (const char*)sqlite3_column_text(row->_stmt,
157
MU_EXPR_TYPE_SUBJECT);
162
mu_msg_sqlite_get_flags (MuMsgSQLite *row)
164
return sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_FLAGS);
169
mu_msg_sqlite_get_priority (MuMsgSQLite *row)
171
return sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_PRIORITY);
176
str_field_s (MuMsgSQLite *row, char fieldchar)
179
static char buf[2] = { '\0', '\0' };
182
case 't': str = mu_msg_sqlite_get_to (row); break;
183
case 'c': str = mu_msg_sqlite_get_cc (row); break;
184
case 'f': str = mu_msg_sqlite_get_from(row); break;
185
case 's': str = mu_msg_sqlite_get_subject (row); break;
186
case 'm': str = mu_msg_sqlite_get_msgid (row); break;
187
case 'F': str = mu_msg_str_flags_s(
188
mu_msg_sqlite_get_flags (row)); break;
189
case 'd': str = mu_msg_str_date_s(mu_msg_sqlite_get_date(row)); break;
190
case 'z': str = mu_msg_str_size_s(mu_msg_sqlite_get_size(row)); break;
191
case 'p': str = mu_msg_sqlite_get_path(row); break;
197
return str ? str : "";
202
mu_msg_sqlite_to_string (MuMsgSQLite *row, const char* rowformat)
207
g_return_val_if_fail (row, NULL);
212
str = g_string_sized_new (512);
216
g_string_append (str, str_field_s (row, c[0]));
220
return g_string_free (str, FALSE);