2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
4
* Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6
6
* The contents of this file are subject to the terms of either the GNU Lesser
7
7
* General Public License Version 2.1 only ("LGPL") or the Common Development and
8
8
* Distribution License ("CDDL")(collectively, the "License"). You may not use this
9
9
* file except in compliance with the License. You can obtain a copy of the CDDL at
10
10
* http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11
* http://www.opensource.org/licenses/lgpl-license.php. See the License for the
11
* http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12
12
* specific language governing permissions and limitations under the License. When
13
13
* distributing the software, include this License Header Notice in each file and
14
14
* include the full text of the License in the License file as well as the
15
15
* following notice:
17
17
* NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19
19
* For Covered Software in this distribution, this License shall be governed by the
32
32
* Version 2.1, or to extend the choice of license to its licensees as provided
33
33
* above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34
34
* Version 2 license, then the option applies only if the new code is made subject
35
* to such option by the copyright holder.
35
* to such option by the copyright holder.
38
38
#include <assert.h>
39
40
#include "userdict.h"
42
bool CUserDict::load(const char *fname)
44
int rc = sqlite3_open(fname, &m_db);
44
CUserDict::load(const char *fname)
46
int rc = sqlite3_open(":memory:", &m_db);
46
48
if (rc != SQLITE_OK) {
47
49
sqlite3_close(m_db);
53
m_fname = strdup(fname);
51
56
return _createTable() && _createIndexes();
55
void CUserDict::free()
58
70
sqlite3_close(m_db);
64
unsigned CUserDict::addWord (CSyllables &syllables, const wstring& word)
77
CUserDict::addWord(CSyllables &syllables, const wstring& word)
66
79
assert(m_db != NULL);
67
assert (syllables.size() >= 2 && syllables.size() <= MAX_USRDEF_WORD_LEN);
80
assert(syllables.size() >= 2 && syllables.size() <= MAX_USRDEF_WORD_LEN);
69
82
sqlite3_stmt *stmt;
71
84
"INSERT INTO dict (len, i0, f0, t0, i1, f1, t1, i2, f2, t2, i3, f3, t3, i4, f4, t4, i5, f5, t5, utf8str) \
72
85
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
75
sqlite3_prepare (m_db, sql_str, strlen(sql_str), &stmt, &tail);
88
sqlite3_prepare(m_db, sql_str, strlen(sql_str), &stmt, &tail);
78
sqlite3_bind_int (stmt, i++, syllables.size());
91
sqlite3_bind_int(stmt, i++, syllables.size());
80
CSyllables::iterator it = syllables.begin();
93
CSyllables::iterator it = syllables.begin();
81
94
CSyllables::iterator ite = syllables.end();
82
95
for (; it != ite; ++it) {
83
sqlite3_bind_int (stmt, i++, it->initial);
84
sqlite3_bind_int (stmt, i++, it->final);
85
sqlite3_bind_int (stmt, i++, it->tone);
96
sqlite3_bind_int(stmt, i++, it->initial);
97
sqlite3_bind_int(stmt, i++, it->final);
98
sqlite3_bind_int(stmt, i++, it->tone);
88
while (i <= MAX_USRDEF_WORD_LEN*3 + 1)
89
sqlite3_bind_int (stmt, i++, 0);
101
while (i <= MAX_USRDEF_WORD_LEN * 3 + 1)
102
sqlite3_bind_int(stmt, i++, 0);
91
char buf[MAX_USRDEF_WORD_LEN*6+1];
104
char buf[MAX_USRDEF_WORD_LEN * 6 + 1];
92
105
WCSTOMBS(buf, word.c_str(), sizeof(buf) - 1);
93
sqlite3_bind_text (stmt, i, (const char *)buf, strlen(buf), NULL);
106
sqlite3_bind_text(stmt, i, (const char*)buf, strlen(buf), NULL);
95
unsigned ret = (SQLITE_DONE == sqlite3_step (stmt))?
96
INI_USRDEF_WID + sqlite3_last_insert_rowid (m_db):
108
unsigned ret = (SQLITE_DONE == sqlite3_step(stmt)) ?
109
INI_USRDEF_WID + sqlite3_last_insert_rowid(m_db) :
99
sqlite3_finalize (stmt);
112
sqlite3_finalize(stmt);
104
void CUserDict::removeWord (unsigned wid)
118
CUserDict::removeWord(unsigned wid)
106
assert (m_db != NULL);
120
assert(m_db != NULL);
107
121
char *zErr = NULL;
108
122
char sql[256] = "DELETE FROM dict WHERE id=";
110
124
if (wid > INI_USRDEF_WID) {
111
125
sprintf(sql, "%s%d;", sql, (wid - INI_USRDEF_WID));
112
126
sqlite3_exec(m_db, sql, NULL, NULL, &zErr);
114
m_dict.erase (m_dict.find(wid - INI_USRDEF_WID));
128
m_dict.erase(m_dict.find(wid - INI_USRDEF_WID));
119
void CUserDict::getWords (CSyllables &syllables,
120
std::vector<CPinyinTrie::TWordIdInfo> &result)
134
CUserDict::getWords(CSyllables &syllables,
135
std::vector<CPinyinTrie::TWordIdInfo> &result)
122
assert (m_db != NULL);
137
assert(m_db != NULL);
125
140
const char *tail;
132
147
if (length > MAX_USRDEF_WORD_LEN)
135
for (int i=0; i<length; i++) {
136
sprintf (buf, " and i%d=%d", i, syllables[i].initial);
150
for (int i = 0; i < length; i++) {
151
sprintf(buf, " and i%d=%d", i, syllables[i].initial);
137
152
i_conditions += buf;
139
154
if (!syllables[i].final)
142
sprintf (buf, " and f%i=%i", i, syllables[i].final);
157
sprintf(buf, " and f%i=%i", i, syllables[i].final);
143
158
f_conditions += buf;
145
160
if (!syllables[i].tone)
148
sprintf (buf, " and t%i=%i", i, syllables[i].tone);
163
sprintf(buf, " and t%i=%i", i, syllables[i].tone);
149
164
t_conditions += buf;
152
sql_str = sqlite3_mprintf("SELECT id, utf8str FROM dict WHERE len=%i%q%q%q;",
153
length, i_conditions.c_str(), f_conditions.c_str(), t_conditions.c_str());
155
rc = sqlite3_prepare(m_db, sql_str, strlen (sql_str), &stmt, &tail);
156
if(rc != SQLITE_OK) {
167
sql_str = sqlite3_mprintf(
168
"SELECT id, utf8str FROM dict WHERE len=%i%q%q%q;",
170
i_conditions.c_str(),
171
f_conditions.c_str(),
172
t_conditions.c_str());
174
rc = sqlite3_prepare(m_db, sql_str, strlen(sql_str), &stmt, &tail);
175
if (rc != SQLITE_OK) {
157
176
sqlite3_free(sql_str);
158
177
fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(m_db));
219
239
utf8str = sqlite3_column_text(stmt, 0);
220
240
MBSTOWCS(cwstr, (const char*)utf8str, MAX_USRDEF_WORD_LEN);
221
241
wstring wstr(cwstr);
222
m_dict.insert (std::make_pair (wid, wstr));
242
m_dict.insert(std::make_pair(wid, wstr));
223
243
ret = wstr.c_str();
226
sqlite3_finalize (stmt);
246
sqlite3_finalize(stmt);
231
bool CUserDict::_createTable()
251
CUserDict::_copyDb(DBCopyDirection direction)
254
int rc = sqlite3_open(m_fname, &disk_db);
256
if (rc == SQLITE_OK) {
257
sqlite3 *dst = direction == Load ? m_db : disk_db;
258
sqlite3 *src = direction == Save ? m_db : disk_db;
259
sqlite3_backup *backup = sqlite3_backup_init(dst, "main", src, "main");
261
sqlite3_backup_step(backup, -1);
262
sqlite3_backup_finish(backup);
264
rc = sqlite3_errcode(dst);
267
sqlite3_close(disk_db);
272
CUserDict::_createTable()
233
274
assert(m_db != NULL);
235
276
char *zErr = NULL;
237
const char *sql_str =
278
const char *sql_str =
238
279
"CREATE TABLE IF NOT EXISTS dict( \
239
280
id INTEGER PRIMARY KEY, len INTEGER, \
240
281
i0 INTEGER, i1 INTEGER, i2 INTEGER, i3 INTEGER, i4 INTEGER, i5 INTEGER, \