~ubuntu-branches/ubuntu/quantal/maildir-utils/quantal

« back to all changes in this revision

Viewing changes to find/mu-msg-sqlite.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2010-01-19 20:12:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100119201243-d8qmzgxgexhy1bs0
Tags: 0.6~beta1-1
* new upstream release 0.6-beta
  - that merges the several different programs under one binary mu
  - no sqlite storage is used anymore
* debian packaging changes:
  - debian/patches
    . remove all patches
  - remove debian/HOWTO (upstream document) it is completely outdated
  - debian/control:
    . adjust build-dep for gmime-2.4
    . remove build-dep on quilt and sqlite
    . adjust the description to new reality
  - debian/rules:
    . do not try to install doc files that are not present anymore
    . disable quilt adaptions
  - add debian/NEWS that explains that the separate programs are gone

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
** Copyright (C) 2008 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
3
 
**
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.
8
 
**  
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.
13
 
**  
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.  
17
 
**  
18
 
*/
19
 
 
20
 
#include <sqlite3.h>
21
 
#include <stdlib.h>
22
 
#include <unistd.h>
23
 
 
24
 
#include "mu/mu-msg-str.h"
25
 
 
26
 
#include "mu-msg-sqlite.h"
27
 
#include "mu-msg-sqlite-priv.h"
28
 
#include "mu-expr-type.h"
29
 
 
30
 
 
31
 
struct _MuMsgSQLite {
32
 
        sqlite3 *_db;
33
 
        sqlite3_stmt *_stmt;
34
 
};
35
 
 
36
 
MuMsgSQLite*
37
 
mu_msg_sqlite_new (sqlite3 *db, sqlite3_stmt *stmt)
38
 
{
39
 
        MuMsgSQLite *row;
40
 
 
41
 
        g_return_val_if_fail (stmt, NULL);
42
 
 
43
 
        row = (MuMsgSQLite*)malloc(sizeof(MuMsgSQLite));
44
 
        if (!row)
45
 
                return NULL;
46
 
 
47
 
        row->_db   = db;
48
 
        row->_stmt = stmt;
49
 
 
50
 
        return row;
51
 
}
52
 
 
53
 
void
54
 
mu_msg_sqlite_destroy (MuMsgSQLite *row)
55
 
{
56
 
        if (row) {
57
 
                sqlite3_finalize (row->_stmt);
58
 
                free (row);
59
 
        }
60
 
}
61
 
 
62
 
gboolean 
63
 
mu_msg_sqlite_next (MuMsgSQLite *row, GError **err)
64
 
{
65
 
        int result;
66
 
        int retry = 3;
67
 
 
68
 
        g_return_val_if_fail (row, FALSE);
69
 
 
70
 
        /* do the rather ugly usleep/retry; it greatly improves reliability
71
 
         * when mu-index is running concurrently...
72
 
         */
73
 
        do {
74
 
                result = sqlite3_step (row->_stmt);
75
 
        } while (result == SQLITE_BUSY && retry-- > 0 && usleep(5000)==0);
76
 
        
77
 
        if (result == SQLITE_DONE)
78
 
                return FALSE; /* we're done */
79
 
                
80
 
        if (result != SQLITE_ROW) {
81
 
                g_set_error (err, 0, 0,
82
 
                             "failed to step statement: %s",
83
 
                             sqlite3_errmsg(row->_db));
84
 
                return FALSE;
85
 
        }
86
 
        return TRUE;
87
 
}
88
 
 
89
 
int
90
 
mu_msg_sqlite_get_id (MuMsgSQLite *row)
91
 
{
92
 
        return sqlite3_column_int64(row->_stmt, MU_EXPR_TYPE_ID);
93
 
}
94
 
 
95
 
 
96
 
const char* 
97
 
mu_msg_sqlite_get_msgid (MuMsgSQLite *row)
98
 
{
99
 
        return (const char*)sqlite3_column_text(row->_stmt, 
100
 
                                                MU_EXPR_TYPE_MSG_ID);
101
 
}
102
 
 
103
 
 
104
 
time_t
105
 
mu_msg_sqlite_get_timestamp (MuMsgSQLite *row)
106
 
{
107
 
        return (time_t)sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_TIMESTAMP);
108
 
}
109
 
 
110
 
 
111
 
const char*
112
 
mu_msg_sqlite_get_path (MuMsgSQLite *row)
113
 
{
114
 
        return (const char*)sqlite3_column_text(row->_stmt, 
115
 
                                                MU_EXPR_TYPE_PATH);
116
 
}
117
 
 
118
 
 
119
 
time_t
120
 
mu_msg_sqlite_get_date (MuMsgSQLite *row)
121
 
{
122
 
        return (time_t)sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_DATE);
123
 
124
 
 
125
 
 
126
 
size_t
127
 
mu_msg_sqlite_get_size (MuMsgSQLite *row)
128
 
{
129
 
        return (size_t)sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_SIZE);
130
 
}
131
 
 
132
 
const char*
133
 
mu_msg_sqlite_get_from (MuMsgSQLite *row)
134
 
{
135
 
        return (const char*)sqlite3_column_text(row->_stmt, MU_EXPR_TYPE_FROM);
136
 
}
137
 
 
138
 
 
139
 
const char*
140
 
mu_msg_sqlite_get_to (MuMsgSQLite *row)
141
 
{
142
 
        return (const char*)sqlite3_column_text(row->_stmt, MU_EXPR_TYPE_TO);
143
 
}
144
 
 
145
 
 
146
 
const char* 
147
 
mu_msg_sqlite_get_cc (MuMsgSQLite *row)
148
 
{
149
 
        return (const char*)sqlite3_column_text(row->_stmt, MU_EXPR_TYPE_CC);
150
 
}
151
 
 
152
 
 
153
 
const char* 
154
 
mu_msg_sqlite_get_subject (MuMsgSQLite *row)
155
 
{
156
 
        return (const char*)sqlite3_column_text(row->_stmt, 
157
 
                                                MU_EXPR_TYPE_SUBJECT);
158
 
}
159
 
 
160
 
 
161
 
MuMsgFlags
162
 
mu_msg_sqlite_get_flags (MuMsgSQLite *row)
163
 
{
164
 
        return sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_FLAGS);
165
 
}
166
 
 
167
 
 
168
 
MuMsgPriority
169
 
mu_msg_sqlite_get_priority (MuMsgSQLite *row)
170
 
{
171
 
        return sqlite3_column_int(row->_stmt, MU_EXPR_TYPE_PRIORITY);
172
 
 
173
 
}
174
 
 
175
 
static const char*
176
 
str_field_s (MuMsgSQLite *row, char fieldchar)
177
 
{
178
 
        const char* str;
179
 
        static char buf[2] = { '\0', '\0' };
180
 
        
181
 
        switch (fieldchar) {
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;
192
 
        default:
193
 
                buf[0] = fieldchar;
194
 
                return buf;
195
 
        }
196
 
        
197
 
        return str ? str : "";
198
 
}
199
 
 
200
 
 
201
 
gchar* 
202
 
mu_msg_sqlite_to_string (MuMsgSQLite *row, const char* rowformat)
203
 
{
204
 
        GString *str;
205
 
        const char* c;
206
 
 
207
 
        g_return_val_if_fail (row, NULL);
208
 
 
209
 
        if (!rowformat)
210
 
                return NULL;
211
 
 
212
 
        str = g_string_sized_new (512);
213
 
 
214
 
        c = rowformat;
215
 
        while (c[0]) {
216
 
                g_string_append (str, str_field_s (row, c[0]));
217
 
                ++c;
218
 
        }
219
 
        
220
 
        return g_string_free (str, FALSE);
221
 
}