Commit 95b8f0b6 authored by Yuanle Song's avatar Yuanle Song

v0.100.1 code review for Database.cc

- add error handling for all db operations
- refactoring Database.cc to make it more readable.
  add doc for Database.cc
  extract method copyDB(), initUserDB(), setPragmaOnMainDB()
- fix old timeoutCallback(), now named cb_saveUserDB()
  saveUserDB() should not be in the condition, because save can fail endlessly
  if file system have problem.
- no longer require main db to be a regular file. symlink should work fine.
- drop the dreaded overly reused class variable m_buffer. use meaningful names
  in current context.
- escape string in SQL statements
- update meson.build
  add prefix in PKGDATADIR
  define glib logging domain
  disable glib asserts for release build
- I will use so file version in git log.
parent 43004d6f
This diff is collapsed.
......@@ -2,6 +2,7 @@
*
* libpyzy - The Chinese PinYin and Bopomofo conversion library.
*
* Copyright (c) 2019 Yuanle Song <[email protected]>
* Copyright (c) 2008-2010 Peng Huang <[email protected]>
*
* This library is free software; you can redistribute it and/or
......@@ -86,24 +87,28 @@ public:
}
private:
bool setPragmaOnMainDB (void);
bool open (void);
bool initUserDB (sqlite3* userdb);
bool copyDB (sqlite3* dest, const char* dest_dbname,
sqlite3* src, const char* src_dbname);
bool loadUserDB (void);
bool saveUserDB (void);
void prefetch (void);
void phraseSql (const Phrase & p, String & sql);
void phraseWhereSql (const Phrase & p, String & sql);
bool executeSQL (const char *sql, sqlite3 *db = NULL);
bool executeSQL (const char* sql, sqlite3* db = NULL);
static gboolean cb_saveUserDB (gpointer user_data);
void modified (void);
static gboolean timeoutCallback (void * data);
private:
sqlite3 *m_db; /* sqlite3 database */
String m_sql; /* sql stmt */
String m_buffer; /* temp buffer */
unsigned int m_timeout_id;
GTimer *m_timer;
String m_user_data_dir;
String m_user_db_file; /* user db file name with full path */
private:
static std::unique_ptr<Database> m_instance;
......
#include <glib.h>
#include <sqlite3.h>
static void
test_string_escape ()
{
const char* phrase1 = "nihao";
char* quoted = sqlite3_mprintf("%Q", phrase1);
g_assert_cmpstr (quoted, ==, "'nihao'");
sqlite3_free (quoted);
const char* phrase2 = "ni'hao";
quoted = sqlite3_mprintf("%Q", phrase2);
g_assert_cmpstr (quoted, ==, "'ni''hao'");
sqlite3_free (quoted);
const char* phrase3 = "'nihao'";
quoted = sqlite3_mprintf("%Q", phrase3);
g_assert_cmpstr (quoted, ==, "'''nihao'''");
sqlite3_free (quoted);
const char* phrase4 = "英国";
quoted = sqlite3_mprintf("%Q", phrase4);
g_assert_cmpstr (quoted, ==, "'英国'");
sqlite3_free (quoted);
const char* phrase5 = "英';drop国";
quoted = sqlite3_mprintf("%Q", phrase5);
g_assert_cmpstr (quoted, ==, "'英'';drop国'");
sqlite3_free (quoted);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/Database/string-escape",
test_string_escape);
return g_test_run ();
}
......@@ -2,6 +2,7 @@
*
* libpyzy - The Chinese PinYin and Bopomofo conversion library.
*
* Copyright (c) 2019 Yuanle Song <[email protected]>
* Copyright (c) 2008-2010 Peng Huang <[email protected]>
*
* This library is free software; you can redistribute it and/or
......@@ -149,6 +150,9 @@ PhraseEditor::updateTheFirstCandidate (void)
end - begin,
m_config.option);
ret = query.fill (m_candidate_0_phrases, 1);
if (ret != 1) {
g_warning ("expect query.fill() result be 1, found %d", ret);
}
g_assert (ret == 1);
begin += m_candidate_0_phrases.back ().len;
}
......
......@@ -10,15 +10,20 @@ project('pyzy', 'cpp',
'strip=true',
'b_ndebug=if-release'])
pkgdatadir = join_paths(get_option('datadir'), 'pyzy')
pkgdatadir = join_paths(get_option('prefix'), get_option('datadir'), 'pyzy')
add_project_arguments(
'-Wno-unused-parameter',
'-Wno-missing-field-initializers',
'-DHAVE_LIBUUID',
'-std=c++0x',
'-DG_LOG_DOMAIN="libpyzy"',
'-DPKGDATADIR="' + pkgdatadir + '"',
language: 'cpp')
if get_option('buildtype').startswith('release')
add_project_arguments('-DG_DISABLE_ASSERT', language: 'cpp')
endif
glib = dependency('glib-2.0')
sqlite3 = dependency('sqlite3')
uuid = dependency('uuid')
......@@ -43,6 +48,9 @@ lib_src = [
shared_library('pyzy-1.0',
lib_src,
soversion: '0',
version: '0.100.0',
version: '0.100.1',
dependencies: shared_dep,
install: true)
test('libpyzy-test',
executable('dbtest', 'Database_test.cc', dependencies: shared_dep))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment