PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libUnihan-0.5.3-Source/src/Unihan_SQL_gen.c

#
C | 1107 lines | 960 code | 97 blank | 50 comment | 98 complexity | bf2d85e2f5ee9c5d46b02b4e9cf9f262 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. /**
  2. * @file Unihan_SQL_gen.c
  3. * @brief SQL statement generation function and house keeping functions.
  4. *
  5. */
  6. /*
  7. * Copyright Š 2008 Red Hat, Inc. All rights reserved.
  8. * Copyright Š 2008 Ding-Yi Chen <dchen at redhat dot com>
  9. *
  10. * This file is part of the libUnihan Project.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this program; if not, write to the
  24. * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  25. * Boston, MA 02111-1307 USA
  26. */
  27. /*=================================
  28. * Select generating functions.
  29. */
  30. static void unihan_generate_select_field_clause(GString *strBuf, UnihanField givenField,UnihanField queryField,UnihanQueryOption qOption){
  31. UnihanTable fromTable=unihanField_get_table(queryField);
  32. UnihanTable givenTable=unihanField_get_table(givenField);
  33. gboolean need_right_parenthesis=FALSE;
  34. if (unihanField_is_lowercase(queryField)){
  35. g_string_append(strBuf,"TO_LOWERCASE(");
  36. need_right_parenthesis=TRUE;
  37. }else if (!unihanField_is_case_no_change(queryField)){
  38. if (!unihanField_is_mandarin(queryField)){
  39. g_string_append(strBuf,"TO_UPPERCASE(");
  40. need_right_parenthesis=TRUE;
  41. }
  42. }
  43. switch(queryField){
  44. case UNIHAN_FIELD_CODE:
  45. if (qOption & UNIHAN_QUERY_OPTION_SCALAR_STRING){
  46. g_string_append_printf(strBuf,"TO_SCALAR_STRING(%s.%s)%c AS code",
  47. UNIHAN_TABLE_NAMES[givenTable],
  48. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_CODE],
  49. (need_right_parenthesis)? ')' : ' '
  50. );
  51. need_right_parenthesis=FALSE;
  52. }else{
  53. g_string_append_printf(strBuf,"%s.%s",UNIHAN_TABLE_NAMES[givenTable],UNIHAN_FIELD_NAMES[UNIHAN_FIELD_CODE]);
  54. }
  55. break;
  56. case UNIHAN_FIELD_KCOMPATIBILITYVARIANT:
  57. case UNIHAN_FIELD_KSIMPLIFIEDVARIANT:
  58. case UNIHAN_FIELD_KTRADITIONALVARIANT:
  59. case UNIHAN_FIELD_VARIANT_CODE:
  60. if (qOption & UNIHAN_QUERY_OPTION_SCALAR_STRING){
  61. g_string_append_printf(strBuf,"TO_SCALAR_STRING(%s.%s)%c AS VariantCodePoint",
  62. UNIHAN_TABLE_NAMES[fromTable],
  63. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  64. (need_right_parenthesis)? ')' : ' '
  65. );
  66. need_right_parenthesis=FALSE;
  67. }else{
  68. g_string_append_printf(strBuf,"%s.%s",
  69. UNIHAN_TABLE_NAMES[fromTable],
  70. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE]);
  71. }
  72. break;
  73. case UNIHAN_FIELD_KIRG_HSOURCE:
  74. case UNIHAN_FIELD_KIRG_USOURCE:
  75. g_string_append_printf(strBuf,"%s.%s",
  76. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  77. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_MAPPING]);
  78. break;
  79. case UNIHAN_FIELD_KIRG_GSOURCE:
  80. case UNIHAN_FIELD_KIRG_JSOURCE:
  81. case UNIHAN_FIELD_KIRG_KPSOURCE:
  82. case UNIHAN_FIELD_KIRG_KSOURCE:
  83. case UNIHAN_FIELD_KIRG_TSOURCE:
  84. case UNIHAN_FIELD_KIRG_VSOURCE:
  85. g_string_append_printf(strBuf,"IRG_SOURCE_VALUE_CONCAT(%s.%s,%s.%s)%c AS %s",
  86. UNIHAN_TABLE_NAMES[fromTable],
  87. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME],
  88. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  89. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_MAPPING],
  90. (need_right_parenthesis)? ')' : ' ',
  91. UNIHAN_FIELD_NAMES[queryField]);
  92. need_right_parenthesis=FALSE;
  93. break;
  94. case UNIHAN_FIELD_KIRGKANGXI:
  95. case UNIHAN_FIELD_KKANGXI:
  96. g_string_append_printf(strBuf,"KANGXI_VALUE_CONCAT(%s, %s, %s)%c AS %s",
  97. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KANGXI_PAGE],
  98. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KANGXI_CHARNUM],
  99. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KANGXI_VIRTUAL],
  100. (need_right_parenthesis)? ')' : ' ',
  101. UNIHAN_FIELD_NAMES[queryField]);
  102. need_right_parenthesis=FALSE;
  103. break;
  104. case UNIHAN_FIELD_KHANYUPINLU:
  105. g_string_append_printf(strBuf,"HANYU_PINLU_VALUE_CONCAT(%s, %s, %d)%c AS %s",
  106. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_PINYIN],
  107. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_PINYIN_FREQ],
  108. qOption,
  109. (need_right_parenthesis)? ')' : ' ',
  110. UNIHAN_FIELD_NAMES[queryField]);
  111. need_right_parenthesis=FALSE;
  112. break;
  113. case UNIHAN_FIELD_KRSADOBE_JAPAN1_6:
  114. g_string_append_printf(strBuf,"ADOBE_RADICAL_STROKE_VALUE_CONCAT(%s, %s, %s, %s, %s)%c AS %s",
  115. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID_CV],
  116. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID],
  117. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_RADICAL_INDEX],
  118. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID_RADICAL_STROKE_COUNT],
  119. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADDITIONAL_STROKE_COUNT],
  120. (need_right_parenthesis)? ')' : ' ',
  121. UNIHAN_FIELD_NAMES[queryField]);
  122. need_right_parenthesis=FALSE;
  123. break;
  124. case UNIHAN_FIELD_KRSJAPANESE:
  125. case UNIHAN_FIELD_KRSKANGXI:
  126. case UNIHAN_FIELD_KRSKANWA:
  127. case UNIHAN_FIELD_KRSKOREAN:
  128. case UNIHAN_FIELD_KRSUNICODE:
  129. g_string_append_printf(strBuf,"RADICAL_STROKE_VALUE_CONCAT(%s, %s)%c AS %s",
  130. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_RADICAL_INDEX],
  131. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADDITIONAL_STROKE_COUNT],
  132. (need_right_parenthesis)? ')' : ' ',
  133. UNIHAN_FIELD_NAMES[queryField]);
  134. need_right_parenthesis=FALSE;
  135. break;
  136. case UNIHAN_FIELD_KSEMANTICVARIANT:
  137. case UNIHAN_FIELD_KSPECIALIZEDSEMANTICVARIANT:
  138. g_string_append_printf(strBuf,"SEMANTIC_VARIANT_VALUE_CONCAT(%s.%s, %s, %s, %s, %s)%c AS %s",
  139. UNIHAN_TABLE_NAMES[fromTable],
  140. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  141. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_FROM_DICT],
  142. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_SEMANTIC_T],
  143. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_SEMANTIC_B],
  144. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_SEMANTIC_Z],
  145. (need_right_parenthesis)? ')' : ' ',
  146. UNIHAN_FIELD_NAMES[queryField]);
  147. need_right_parenthesis=FALSE;
  148. break;
  149. case UNIHAN_FIELD_KZVARIANT:
  150. g_string_append_printf(strBuf,"Z_VARIANT_VALUE_CONCAT(%s.%s, %s)%c AS %s",
  151. UNIHAN_TABLE_NAMES[fromTable],
  152. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  153. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ZVARIANT_SOURCE],
  154. (need_right_parenthesis)? ')' : ' ',
  155. UNIHAN_FIELD_NAMES[queryField]);
  156. need_right_parenthesis=FALSE;
  157. break;
  158. default:
  159. if (unihanField_is_mandarin(queryField)){
  160. if (queryField==UNIHAN_FIELD_ZHUYIN || (qOption & UNIHAN_QUERY_OPTION_ZHUYIN_FORCE_DISPLAY)){
  161. g_string_append_printf(strBuf,"PINYIN_TO_ZHUYIN(%s.%s, %d)",
  162. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_KMANDARIN],
  163. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KMANDARIN],
  164. UNIHAN_QUERY_OPTION_GET_ZHUYIN_FORMAT(qOption));
  165. }else{
  166. g_string_append_printf(strBuf,
  167. "PINYIN_CONVERT_ACCENT_FORMAT(%s.%s,%d,%d)",
  168. UNIHAN_TABLE_NAMES[fromTable],
  169. UNIHAN_FIELD_NAMES[queryField],
  170. UNIHAN_QUERY_OPTION_GET_PINYIN_FORMAT(qOption),
  171. ((qOption & UNIHAN_QUERY_OPTION_PINYIN_TONE_ACCENT) ? 0: 1));
  172. }
  173. g_string_append_printf(strBuf," AS %s",
  174. UNIHAN_FIELD_NAMES[queryField]);
  175. need_right_parenthesis=FALSE;
  176. }else{
  177. g_string_append_printf(strBuf,"%s.%s",UNIHAN_TABLE_NAMES[fromTable],UNIHAN_FIELD_NAMES[queryField]);
  178. }
  179. break;
  180. }
  181. if (need_right_parenthesis){
  182. g_string_append(strBuf,")");
  183. need_right_parenthesis=FALSE;
  184. }
  185. }
  186. static char *unihan_generate_select_clause(UnihanField givenField,UnihanField queryField,UnihanQueryOption qOption){
  187. GString *strBuf=g_string_new(NULL);
  188. if (qOption & UNIHAN_QUERY_OPTION_SHOW_GIVEN_FIELD){
  189. unihan_generate_select_field_clause(strBuf, queryField, givenField,qOption);
  190. g_string_append_printf(strBuf," ,");
  191. }
  192. unihan_generate_select_field_clause(strBuf, givenField, queryField,qOption);
  193. return g_string_free(strBuf,FALSE);
  194. }
  195. /*=================================
  196. * FROM generating functions.
  197. */
  198. static char *unihan_generate_from_clause(UnihanField givenField,UnihanField queryField){
  199. GString *strBuf=g_string_new(NULL);
  200. UnihanTable fromTable,givenTable,extraTable;
  201. gboolean first=TRUE;
  202. if (queryField!=UNIHAN_FIELD_CODE ){
  203. fromTable=unihanField_get_table(queryField);
  204. g_string_append(strBuf,UNIHAN_TABLE_NAMES[fromTable]);
  205. if (unihanField_is_IRG_Source(queryField)){
  206. g_string_append_printf(strBuf," LEFT JOIN %s ON %s.code=%s.code AND %s.%s=%s.%s",
  207. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  208. UNIHAN_TABLE_NAMES[fromTable],
  209. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  210. UNIHAN_TABLE_NAMES[fromTable],
  211. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME],
  212. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  213. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME]);
  214. }else{
  215. switch(queryField){
  216. case UNIHAN_FIELD_KSEMANTICVARIANT:
  217. case UNIHAN_FIELD_KSPECIALIZEDSEMANTICVARIANT:
  218. extraTable=unihanField_get_extra_table(queryField);
  219. g_string_append_printf(strBuf," LEFT JOIN %s ON %s.code=%s.code AND %s.%s=%s.%s ",
  220. UNIHAN_TABLE_NAMES[extraTable],
  221. UNIHAN_TABLE_NAMES[fromTable],
  222. UNIHAN_TABLE_NAMES[extraTable],
  223. UNIHAN_TABLE_NAMES[fromTable],
  224. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  225. UNIHAN_TABLE_NAMES[extraTable],
  226. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE]
  227. );
  228. break;
  229. case UNIHAN_FIELD_KZVARIANT:
  230. extraTable=unihanField_get_extra_table(queryField);
  231. g_string_append_printf(strBuf," LEFT JOIN %s ON %s.code=%s.code AND %s.%s=%s.%s",
  232. UNIHAN_TABLE_NAMES[extraTable],
  233. UNIHAN_TABLE_NAMES[fromTable],
  234. UNIHAN_TABLE_NAMES[extraTable],
  235. UNIHAN_TABLE_NAMES[fromTable],
  236. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  237. UNIHAN_TABLE_NAMES[extraTable],
  238. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE]);
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. first=FALSE;
  245. }
  246. if (givenField!=UNIHAN_FIELD_CODE ){
  247. givenTable=unihanField_get_table(givenField);
  248. if (givenTable!=fromTable){
  249. if (!first){
  250. g_string_append_c(strBuf,',');
  251. }
  252. g_string_append(strBuf,UNIHAN_TABLE_NAMES[givenTable]);
  253. if (unihanField_is_IRG_Source(givenField)){
  254. g_string_append_printf(strBuf," LEFT JOIN %s ON %s.code=%s.code AND %s.%s=%s.%s",
  255. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  256. UNIHAN_TABLE_NAMES[givenTable],
  257. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  258. UNIHAN_TABLE_NAMES[givenTable],
  259. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME],
  260. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE_MAPPING],
  261. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME]);
  262. }else{
  263. switch(givenField){
  264. case UNIHAN_FIELD_KSEMANTICVARIANT:
  265. case UNIHAN_FIELD_KSPECIALIZEDSEMANTICVARIANT:
  266. extraTable=unihanField_get_extra_table(givenField);
  267. g_string_append_printf(strBuf," LEFT JOIN %s ON %s.code=%s.code AND %s.%s=%s.%s",
  268. UNIHAN_TABLE_NAMES[extraTable],
  269. UNIHAN_TABLE_NAMES[givenTable],
  270. UNIHAN_TABLE_NAMES[extraTable],
  271. UNIHAN_TABLE_NAMES[givenTable],
  272. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  273. UNIHAN_TABLE_NAMES[extraTable],
  274. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE]
  275. );
  276. break;
  277. case UNIHAN_FIELD_KZVARIANT:
  278. extraTable=unihanField_get_extra_table(givenField);
  279. g_string_append_printf(strBuf," LEFT JOIN %s ON %s.code=%s.code AND %s.%s=%s.%s",
  280. UNIHAN_TABLE_NAMES[extraTable],
  281. UNIHAN_TABLE_NAMES[givenTable],
  282. UNIHAN_TABLE_NAMES[extraTable],
  283. UNIHAN_TABLE_NAMES[givenTable],
  284. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  285. UNIHAN_TABLE_NAMES[extraTable],
  286. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE]);
  287. break;
  288. default:
  289. break;
  290. }
  291. }
  292. }
  293. }
  294. g_string_append_c(strBuf,' ');
  295. return g_string_free(strBuf,FALSE);
  296. }
  297. /*=================================
  298. * WHERE generating functions.
  299. */
  300. static char *unihan_append_radical_stroke_where_clause(GString *strBuf, UnihanField field, const char *value){
  301. int i=0,len=strlen(value);
  302. g_string_append_printf(strBuf,"%s='",
  303. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_RADICAL_INDEX]);
  304. while (i<len && value[i]!='.'){
  305. g_string_append_c(strBuf,value[i]);
  306. if (value[i]=='\''){
  307. g_string_append_c(strBuf,value[i]);
  308. }
  309. i++;
  310. }
  311. if (value[i]=='.'){
  312. /* additional strokes count */
  313. g_string_append_printf(strBuf,"' AND %s='",UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADDITIONAL_STROKE_COUNT]);
  314. i++;
  315. }else{
  316. verboseMsg_print(VERBOSE_MSG_ERROR,"Invalid format!");
  317. return NULL;
  318. }
  319. while (i<len){
  320. g_string_append_c(strBuf,value[i]);
  321. if (value[i]=='\''){
  322. g_string_append_c(strBuf,value[i]);
  323. }
  324. i++;
  325. }
  326. g_string_append(strBuf,"' ") ;
  327. return strBuf->str;
  328. }
  329. static char *unihan_append_adobe_japan1_6_where_clause(GString *strBuf ,const char *value){
  330. int i=0,len=strlen(value);
  331. if (value[i]=='C'){
  332. g_string_append_printf(strBuf," %s='C'",UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID_CV]);
  333. i++;
  334. }
  335. if (value[i]=='V'){
  336. g_string_append_printf(strBuf," %s='V'",UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID_CV]);
  337. i++;
  338. }
  339. if (value[i]=='+'){
  340. /* CID field */
  341. g_string_append_printf(strBuf," AND %s='",UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID]);
  342. i++;
  343. }else{
  344. verboseMsg_print(VERBOSE_MSG_ERROR,"Invalid format!");
  345. g_string_free(strBuf,TRUE);
  346. return NULL;
  347. }
  348. while (i<len && value[i]!='+'){
  349. g_string_append_c(strBuf,value[i]);
  350. if (value[i]=='\''){
  351. g_string_append_c(strBuf,value[i]);
  352. }
  353. i++;
  354. }
  355. if (value[i]=='+'){
  356. /* Radical field */
  357. g_string_append_printf(strBuf,"' AND %s='",
  358. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_RADICAL_INDEX]);
  359. i++;
  360. }else{
  361. verboseMsg_print(VERBOSE_MSG_ERROR,"Invalid format!");
  362. g_string_free(strBuf,TRUE);
  363. exit(-1);
  364. return NULL;
  365. }
  366. while (i<len && value[i]!='.'){
  367. g_string_append_c(strBuf,value[i]);
  368. if (value[i]=='\''){
  369. g_string_append_c(strBuf,value[i]);
  370. }
  371. i++;
  372. }
  373. if (value[i]=='.'){
  374. /* Radical strokes count */
  375. g_string_append_printf(strBuf,"' AND %s='",UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADOBE_CID_RADICAL_STROKE_COUNT]);
  376. i++;
  377. }else{
  378. verboseMsg_print(VERBOSE_MSG_ERROR,"Invalid format!");
  379. g_string_free(strBuf,TRUE);
  380. return NULL;
  381. }
  382. while (i<len && value[i]!='.'){
  383. g_string_append_c(strBuf,value[i]);
  384. if (value[i]=='\''){
  385. g_string_append_c(strBuf,value[i]);
  386. }
  387. i++;
  388. }
  389. if (value[i]=='.'){
  390. /* additional strokes count */
  391. g_string_append_printf(strBuf,"' AND %s='",UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ADDITIONAL_STROKE_COUNT]);
  392. i++;
  393. }else{
  394. verboseMsg_print(VERBOSE_MSG_ERROR,"Invalid format!");
  395. return NULL;
  396. }
  397. while (i<len){
  398. g_string_append_c(strBuf,value[i]);
  399. if (value[i]=='\''){
  400. g_string_append_c(strBuf,value[i]);
  401. }
  402. i++;
  403. }
  404. g_string_append_printf(strBuf,"' ");
  405. return strBuf->str;
  406. }
  407. static void unihan_append_IRG_sources_query_where_clause(GString *strBuf, UnihanIRG_Source source){
  408. g_string_append_printf(strBuf," AND %s.%s LIKE ",
  409. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE],
  410. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME]);
  411. switch(source){
  412. case UNIHAN_IRG_SOURCE_G:
  413. g_string_append(strBuf,"'G%%'");
  414. break;
  415. case UNIHAN_IRG_SOURCE_H:
  416. g_string_append(strBuf,"'H'");
  417. break;
  418. case UNIHAN_IRG_SOURCE_J:
  419. g_string_append(strBuf,"'J%%'");
  420. break;
  421. case UNIHAN_IRG_SOURCE_KP:
  422. g_string_append(strBuf,"'KP_'");
  423. break;
  424. case UNIHAN_IRG_SOURCE_K:
  425. g_string_append(strBuf,"'K_'");
  426. break;
  427. case UNIHAN_IRG_SOURCE_T:
  428. g_string_append(strBuf,"'T%%'");
  429. break;
  430. case UNIHAN_IRG_SOURCE_U:
  431. g_string_append(strBuf,"'U'");
  432. break;
  433. case UNIHAN_IRG_SOURCE_V:
  434. g_string_append(strBuf,"'V_'");
  435. break;
  436. default:
  437. break;
  438. }
  439. }
  440. static void unihan_append_kangXi_where_clause(GString *strBuf, UnihanField field,const char* composite_value){
  441. UnihanTable table=unihanField_get_table(field);
  442. GArray *gArray=kangXiRec_parse(composite_value);
  443. int i=0;
  444. KangXiRec *rec=NULL;
  445. char *strTmp=NULL;
  446. g_assert(table>=0);
  447. if (gArray->len>0){
  448. rec=&g_array_index(gArray,KangXiRec,i);
  449. strTmp=sqlite3_mprintf(" %s.%s=%Q AND %s.%s=%Q",
  450. UNIHAN_TABLE_NAMES[table],
  451. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KANGXI_PAGE],
  452. rec->page,
  453. UNIHAN_TABLE_NAMES[table],
  454. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KANGXI_CHARNUM],
  455. rec->charNum);
  456. g_string_append_printf(strBuf,"%s AND %s.%s=%d",
  457. strTmp,
  458. UNIHAN_TABLE_NAMES[table],
  459. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KANGXI_VIRTUAL],
  460. rec->virtual);
  461. sqlite3_free(strTmp);
  462. }
  463. g_array_free(gArray,TRUE);
  464. }
  465. static void unihan_append_pinYinFreq_where_clause(GString *strBuf, UnihanField field,const char* composite_value){
  466. UnihanTable table=unihanField_get_table(field);
  467. GArray *gArray=pinYinFreqRec_parse(composite_value);
  468. char *strTmp=NULL;
  469. int i=0;
  470. PinYinFreqRec *rec=NULL;
  471. g_assert(table>=0);
  472. if (gArray->len>0){
  473. rec=&g_array_index(gArray,PinYinFreqRec,i);
  474. strTmp=sqlite3_mprintf(" %s.%s=%Q",
  475. UNIHAN_TABLE_NAMES[table],
  476. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_PINYIN],
  477. rec->pinYin);
  478. g_string_append_printf(strBuf,"%s AND %s.%s=%d",
  479. strTmp,
  480. UNIHAN_TABLE_NAMES[table],
  481. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_PINYIN_FREQ],
  482. rec->freq);
  483. sqlite3_free(strTmp);
  484. }
  485. g_array_free(gArray,TRUE);
  486. }
  487. static void unihan_append_semantic_where_clause(GString *strBuf, UnihanField field, const char* composite_value){
  488. GArray *gArray=semanticDictRec_parse(composite_value);
  489. char *strTmp=NULL;
  490. int i=0;
  491. SemanticDictRec *rec=NULL;
  492. UnihanTable table=unihanField_get_table(field);
  493. g_assert(table>=0);
  494. if (gArray->len>0){
  495. rec=&g_array_index(gArray,SemanticDictRec,i);
  496. g_string_append_printf(strBuf," %s.%s=%d",
  497. UNIHAN_TABLE_NAMES[table],
  498. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  499. rec->variantCode);
  500. if (!isEmptyString(rec->fromDict)){
  501. strTmp=sqlite3_mprintf(" AND %s=%Q",
  502. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_FROM_DICT],
  503. rec->fromDict);
  504. g_string_append_printf(strBuf,"%s AND semanticT=%d AND semanticB=%d AND semanticZ=%d",
  505. strTmp,rec->T,rec->B,rec->Z);
  506. sqlite3_free(strTmp);
  507. }
  508. }
  509. g_array_free(gArray,TRUE);
  510. }
  511. static void unihan_append_zvariant_where_clause(GString *strBuf, UnihanField field, const char* composite_value){
  512. char **subFieldArray=g_strsplit(composite_value,":",-1);
  513. char *strTmp=NULL;
  514. g_assert(subFieldArray[0]);
  515. gunichar variantCode=unihanChar_parse(subFieldArray[0]);
  516. g_string_append_printf(strBuf," %s.%s=%d",
  517. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_KZVARIANT],
  518. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  519. variantCode);
  520. if (!isEmptyString(subFieldArray[1])){
  521. strTmp=sqlite3_mprintf(" AND %s=%Q",
  522. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_ZVARIANT_SOURCE],
  523. subFieldArray[1]);
  524. g_string_append(strBuf,strTmp);
  525. sqlite3_free(strTmp);
  526. }
  527. g_strfreev(subFieldArray);
  528. }
  529. static void unihan_append_where_clause_join_table(GString *strBuf,
  530. UnihanField givenField, UnihanField queryField){
  531. UnihanTable givenTable,queryTable;
  532. if (givenField==UNIHAN_FIELD_CODE ){
  533. return;
  534. }else if (queryField==UNIHAN_FIELD_CODE){
  535. return;
  536. }else{
  537. queryTable=unihanField_get_table(queryField);
  538. givenTable=unihanField_get_table(givenField);
  539. g_string_append_printf(strBuf, " AND %s.code=%s.code",
  540. UNIHAN_TABLE_NAMES[queryTable],
  541. UNIHAN_TABLE_NAMES[givenTable]);
  542. }
  543. }
  544. static char *unihan_generate_where_clause(UnihanField givenField, const char *value,
  545. UnihanTable fromTable, UnihanField queryField, UnihanQueryOption qOption){
  546. GString *strBuf=g_string_new(NULL);
  547. UnihanIRG_SourceRec *rec=NULL;
  548. char *valueTmp= NULL;
  549. if (unihanField_is_case_no_change(givenField)){
  550. valueTmp=g_strdup(value);
  551. }else if (unihanField_is_lowercase(givenField)){
  552. valueTmp=g_utf8_strdown(value,-1);
  553. }else{
  554. valueTmp=g_utf8_strup(value,-1);
  555. }
  556. if (unihanField_is_IRG_Source(givenField)){
  557. rec=unihanIRG_SourceRec_parse(givenField,valueTmp);
  558. }
  559. char *relStr=(qOption & UNIHAN_QUERY_OPTION_LIKE)? "LIKE": "=";
  560. char *strTmp=NULL;
  561. char *pinYinTmp=NULL;
  562. if (rec!=NULL){
  563. strTmp=sqlite3_mprintf(" %s.%s=%Q",
  564. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_IRG_SOURCE],
  565. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_SHORT_NAME],
  566. UNIHAN_IRG_SOURCES[rec->sourceId].name);
  567. if (strTmp){
  568. g_string_append(strBuf,strTmp);
  569. sqlite3_free(strTmp);
  570. }
  571. if (!unihanIRG_Source_has_no_mapping(rec->sourceId)){
  572. strTmp=sqlite3_mprintf(" AND %s %s %Q",
  573. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_IRG_SOURCE_MAPPING],
  574. relStr,
  575. rec->sourceMapping);
  576. if (strTmp){
  577. g_string_append(strBuf,strTmp);
  578. sqlite3_free(strTmp);
  579. }
  580. }
  581. unihanIRG_SourceRec_free(rec);
  582. }else{
  583. switch(givenField){
  584. case UNIHAN_FIELD_CODE:
  585. if (valueTmp[0]=='U'){
  586. strTmp=sqlite3_mprintf("TO_SCALAR_STRING(%s.%s) %s %Q",
  587. UNIHAN_TABLE_NAMES[fromTable],
  588. UNIHAN_FIELD_NAMES[givenField],
  589. relStr, valueTmp);
  590. }else{
  591. strTmp=sqlite3_mprintf("%s.%s %s %Q",
  592. UNIHAN_TABLE_NAMES[fromTable],
  593. UNIHAN_FIELD_NAMES[givenField],
  594. relStr, valueTmp);
  595. }
  596. break;
  597. case UNIHAN_FIELD_KIRGKANGXI:
  598. case UNIHAN_FIELD_KKANGXI:
  599. unihan_append_kangXi_where_clause(strBuf,givenField,valueTmp);
  600. break;
  601. case UNIHAN_FIELD_KHANYUPINLU:
  602. unihan_append_pinYinFreq_where_clause(strBuf,givenField,valueTmp);
  603. break;
  604. case UNIHAN_FIELD_KRSADOBE_JAPAN1_6:
  605. unihan_append_adobe_japan1_6_where_clause(strBuf,valueTmp);
  606. break;
  607. case UNIHAN_FIELD_KRSJAPANESE:
  608. case UNIHAN_FIELD_KRSKANGXI:
  609. case UNIHAN_FIELD_KRSKANWA:
  610. case UNIHAN_FIELD_KRSKOREAN:
  611. case UNIHAN_FIELD_KRSUNICODE:
  612. unihan_append_radical_stroke_where_clause(strBuf, givenField, valueTmp);
  613. break;
  614. case UNIHAN_FIELD_KSEMANTICVARIANT:
  615. case UNIHAN_FIELD_KSPECIALIZEDSEMANTICVARIANT:
  616. unihan_append_semantic_where_clause(strBuf, givenField, valueTmp);
  617. break;
  618. case UNIHAN_FIELD_KCOMPATIBILITYVARIANT:
  619. case UNIHAN_FIELD_KSIMPLIFIEDVARIANT:
  620. case UNIHAN_FIELD_KTRADITIONALVARIANT:
  621. strTmp=sqlite3_mprintf("%s.%s=SCALAR_STRING_PARSE(%Q)",
  622. UNIHAN_TABLE_NAMES[fromTable],
  623. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE],
  624. valueTmp);
  625. break;
  626. case UNIHAN_FIELD_KZVARIANT:
  627. unihan_append_zvariant_where_clause(strBuf, givenField, valueTmp);
  628. break;
  629. case UNIHAN_FIELD_ZHUYIN:
  630. pinYinTmp=zhuYin_to_pinYin(valueTmp,PINYIN_ACCENT_UNIHAN,TRUE);
  631. strTmp=sqlite3_mprintf("%s.%s %s %Q",
  632. UNIHAN_TABLE_NAMES[UNIHAN_TABLE_KMANDARIN],
  633. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_KMANDARIN],
  634. relStr,
  635. pinYinTmp,
  636. valueTmp);
  637. g_free(pinYinTmp);
  638. break;
  639. default:
  640. strTmp=sqlite3_mprintf("%s.%s %s %Q",
  641. UNIHAN_TABLE_NAMES[fromTable],
  642. UNIHAN_FIELD_NAMES[givenField],
  643. relStr,valueTmp);
  644. break;
  645. }
  646. if (strTmp){
  647. g_string_append(strBuf,strTmp);
  648. sqlite3_free(strTmp);
  649. }
  650. }
  651. unihan_append_where_clause_join_table(strBuf, givenField, queryField);
  652. /* Below is needed if queryField is pseudo */
  653. UnihanIRG_Source source=unihanField_get_IRG_source(queryField);
  654. if (source>=0){
  655. unihan_append_IRG_sources_query_where_clause(strBuf, source);
  656. }
  657. g_free(valueTmp);
  658. return g_string_free(strBuf,FALSE);
  659. }
  660. SQL_Result *unihan_find_all_matched(UnihanField givenField, const char *givenValue,
  661. UnihanField queryField, UnihanQueryOption qOption){
  662. char *selectStr=NULL;
  663. char *fromStr=NULL;
  664. char *whereStr=NULL;
  665. int i;
  666. GString *strBuf=g_string_new(NULL);
  667. UnihanTable table;
  668. UnihanTable *tableArray=(givenField!=UNIHAN_FIELD_CODE) ?
  669. unihanField_get_all_tables(givenField): unihanField_get_all_tables(queryField);
  670. for(i=0;tableArray[i]>=0;i++){
  671. if (i>0){
  672. g_string_append(strBuf," UNION ");
  673. }
  674. selectStr=unihan_generate_select_clause(givenField,queryField,qOption);
  675. fromStr=unihan_generate_from_clause(givenField,queryField);
  676. whereStr=unihan_generate_where_clause(givenField,givenValue,tableArray[i],queryField,qOption);
  677. g_string_append_printf(strBuf,"SELECT %s FROM %s WHERE %s",selectStr,fromStr, whereStr);
  678. /* Group by */
  679. switch(queryField){
  680. case UNIHAN_FIELD_KSEMANTICVARIANT:
  681. case UNIHAN_FIELD_KSPECIALIZEDSEMANTICVARIANT:
  682. table=unihanField_get_table(queryField);
  683. g_string_append_printf(strBuf," GROUP BY %s.%s",
  684. UNIHAN_TABLE_NAMES[table],
  685. UNIHAN_FIELD_NAMES[UNIHAN_FIELD_VARIANT_CODE]);
  686. break;
  687. default:
  688. break;
  689. }
  690. }
  691. g_string_append(strBuf,";");
  692. verboseMsg_print(VERBOSE_MSG_INFO1,"%s\n",strBuf->str);
  693. SQL_Result *sResult=unihanSql_get_sql_result(strBuf->str);
  694. verboseMsg_print(VERBOSE_MSG_INFO4," 2 sResult->colCount=%d \n",sResult->colCount);
  695. g_free(selectStr);
  696. g_free(fromStr);
  697. g_free(whereStr);
  698. g_string_free(strBuf,TRUE);
  699. g_free(tableArray);
  700. return sResult;
  701. }
  702. char* unihan_find_first_matched(UnihanField givenField, const char* givenValue,
  703. UnihanField queryField, UnihanQueryOption qOption){
  704. SQL_Result *sResult=unihan_find_all_matched(givenField, givenValue, queryField, qOption);
  705. if (sResult->execResult){
  706. verboseMsg_print(VERBOSE_MSG_ERROR,"Database error: %s\n", sResult->errMsg);
  707. sql_result_free(sResult, TRUE);
  708. return NULL;
  709. }
  710. char *result=g_strdup(stringList_index(sResult->resultList,0));
  711. sql_result_free(sResult,TRUE);
  712. return result;
  713. }
  714. int unihan_count_matched_record(UnihanTable table, StringList *valueList){
  715. int i;
  716. if (table<0){
  717. return -1;
  718. }
  719. UnihanField* fields=unihanTable_get_fields(table);
  720. GString *strBuf=g_string_new("SELECT * FROM");
  721. g_string_append_printf(strBuf," %s WHERE", UNIHAN_TABLE_NAMES[table]);
  722. for(i=0;i<valueList->len;i++){
  723. if (fields[i]<0){
  724. g_error("unihan_count_matched_record(): too many values! %d value required, but %d are given!\n",i,valueList->len);
  725. }
  726. if (i>0){
  727. g_string_append(strBuf," AND");
  728. }
  729. if (unihanField_is_integer(fields[i])){
  730. g_string_append_printf(strBuf," %s=%s",UNIHAN_FIELD_NAMES[fields[i]],stringList_index(valueList,i));
  731. }else{
  732. g_string_append_printf(strBuf," %s=\"%s\"",UNIHAN_FIELD_NAMES[fields[i]],stringList_index(valueList,i));
  733. }
  734. }
  735. while(fields[i]>=0){
  736. i++;
  737. }
  738. if (i!=valueList->len){
  739. g_error("unihan_count_matched_record(): too few values! %d value required, but %d are given!\n",i,valueList->len);
  740. }
  741. g_string_append(strBuf,";");
  742. verboseMsg_print(VERBOSE_MSG_INFO2,"unihan_count_matched_record(): %s\n",strBuf->str);
  743. char *errMsg=NULL;
  744. int ret=unihanSql_count_matches(strBuf->str,&errMsg);
  745. verboseMsg_print(VERBOSE_MSG_INFO2,"unihan_count_matched_record(): %d records founds\n",ret);
  746. g_string_free(strBuf,TRUE);
  747. g_free(fields);
  748. return ret;
  749. }
  750. static int latest_db_result(int result,int newResult){
  751. if ((result<=0) && (newResult>0)){
  752. /* newResult overrides result, indicates there is database error */
  753. result=newResult;
  754. }else if ((result==0) && (newResult<0)){
  755. /* newResult overrides result, indicates at least one insert instruction is skipped. */
  756. result=newResult;
  757. }else{
  758. /* no need to change result */
  759. }
  760. return result;
  761. }
  762. static int insert_semanticVariant(gunichar code, UnihanField field, const char *composite_value){
  763. StringList *sList=NULL;
  764. GArray *gArray=semanticDictRec_parse(composite_value);
  765. SemanticDictRec *sRec=NULL;
  766. int i;
  767. char codeBuf[20], variantBuf[20];
  768. const char oneStr[]="1", zeroStr[]="0";
  769. int result=0,newResult=0;
  770. UnihanTable table=unihanField_get_table(field),
  771. extraTable=unihanField_get_extra_table(field);
  772. g_snprintf(codeBuf,20,"%d",code);
  773. for(i=0;i<gArray->len;i++){
  774. sList=stringList_new();
  775. sRec=&g_array_index(gArray,SemanticDictRec,i);
  776. g_snprintf(variantBuf,20,"%d",sRec->variantCode);
  777. /* Insert to k*SementicVariantTable */
  778. stringList_insert(sList,codeBuf);
  779. stringList_insert(sList,variantBuf);
  780. newResult=unihan_insert_no_duplicate(table,sList);
  781. result=latest_db_result(result,newResult);
  782. stringList_free(sList);
  783. if (isEmptyString(sRec->fromDict)){
  784. continue;
  785. }
  786. sList=stringList_new();
  787. /* Insert to SemanticDictTable */
  788. stringList_insert(sList,codeBuf);
  789. stringList_insert(sList,variantBuf);
  790. stringList_insert(sList,sRec->fromDict);
  791. stringList_insert(sList,(sRec->T)? oneStr: zeroStr);
  792. stringList_insert(sList,(sRec->B)? oneStr: zeroStr);
  793. stringList_insert(sList,(sRec->Z)? oneStr: zeroStr);
  794. newResult=unihan_insert_no_duplicate(extraTable,sList);
  795. result=latest_db_result(result,newResult);
  796. stringList_free(sList);
  797. }
  798. g_array_free(gArray,TRUE);
  799. return result;
  800. }
  801. static int insert_zVariant(gunichar code, UnihanField field, const char *composite_value){
  802. char **subFieldArray=g_strsplit(composite_value,":",-1);
  803. g_assert(subFieldArray[0]);
  804. gunichar variantCode=unihanChar_parse(subFieldArray[0]);
  805. char codeBuf[20], variantBuf[20];
  806. StringList *sList=stringList_new();
  807. int result=0,newResult=0;
  808. UnihanTable table=unihanField_get_table(field),
  809. extraTable=unihanField_get_extra_table(field);
  810. g_snprintf(codeBuf,20,"%d",code);
  811. g_snprintf(variantBuf,20,"%d",variantCode);
  812. stringList_insert(sList,codeBuf);
  813. stringList_insert(sList,variantBuf);
  814. newResult=unihan_insert_no_duplicate(table, sList);
  815. result=latest_db_result(result,newResult);
  816. if (!isEmptyString(subFieldArray[1])){
  817. stringList_clear(sList);
  818. stringList_insert(sList,codeBuf);
  819. stringList_insert(sList,variantBuf);
  820. stringList_insert(sList,subFieldArray[1]);
  821. newResult=unihan_insert_no_duplicate(extraTable,sList);
  822. result=latest_db_result(result,newResult);
  823. }
  824. stringList_free(sList);
  825. g_strfreev(subFieldArray);
  826. return result;
  827. }
  828. int unihan_insert(UnihanTable table, StringList *valueList){
  829. int i;
  830. char *errmsg=NULL;
  831. if (table<0){
  832. return -1;
  833. }
  834. UnihanField* fields=unihanTable_get_fields(table);
  835. GString *strBuf=g_string_new("INSERT INTO");
  836. g_string_append_printf(strBuf," %s VALUES (", UNIHAN_TABLE_NAMES[table]);
  837. for(i=0;i<valueList->len;i++){
  838. if (fields[i]<0){
  839. g_error("unihan_insert(): too many values! %d value required, but %d are given!\n",i,valueList->len);
  840. }
  841. if (i>0){
  842. g_string_append_c(strBuf,',');
  843. }
  844. if (unihanField_is_integer(fields[i])){
  845. g_string_append_printf(strBuf," %s",stringList_index(valueList,i));
  846. }else{
  847. g_string_append_printf(strBuf," \"%s\"",stringList_index(valueList,i));
  848. }
  849. }
  850. while(fields[i]>=0){
  851. i++;
  852. }
  853. if (i!=valueList->len){
  854. g_error("unihan_insert(): too few values! %d value required, but %d are given!\n",i,valueList->len);
  855. }
  856. g_string_append(strBuf,");");
  857. verboseMsg_print(VERBOSE_MSG_INFO1,"%s\n",strBuf->str);
  858. int ret=unihanSql_exec(strBuf->str,NULL,NULL,&errmsg);
  859. if (ret) {
  860. verboseMsg_print(VERBOSE_MSG_ERROR, "unihan_insert(): Cannot insert. :%s\n", errmsg);
  861. }
  862. g_string_free(strBuf,TRUE);
  863. g_free(fields);
  864. return ret;
  865. }
  866. int unihan_insert_value(gunichar code, UnihanField field, const char *value){
  867. char buf[20];
  868. UnihanTable table=unihanField_get_table(field);
  869. StringList *sList=stringList_new();
  870. char **subFieldArray=NULL;
  871. gunichar variantCode;
  872. static gunichar lastCode=0;
  873. int result=0,newResult=0;
  874. static int freqRank=1;
  875. UnihanIRG_SourceRec *rec=NULL;
  876. if (unihanField_is_IRG_Source(field)){
  877. rec=unihanIRG_SourceRec_parse(field,value);
  878. }
  879. if (rec!=NULL){
  880. const UnihanIRG_SourceData *srcData=unihanIRG_SourceData_get(rec->sourceId);
  881. if (srcData==NULL){
  882. g_error("Unknown source!");
  883. }
  884. g_snprintf(buf,20,"%d",code);
  885. if (!unihanIRG_Source_has_no_mapping(rec->sourceId)){
  886. stringList_insert(sList,buf);
  887. stringList_insert(sList,srcData->name);
  888. stringList_insert(sList,rec->sourceMapping);
  889. newResult=unihan_insert_no_duplicate(UNIHAN_TABLE_IRG_SOURCE_MAPPING,sList);
  890. result=latest_db_result(result,newResult);
  891. stringList_clear(sList);
  892. }
  893. stringList_insert(sList,buf);
  894. stringList_insert(sList,srcData->name);
  895. unihanIRG_SourceRec_free(rec);
  896. }else if (unihanField_array_index(field,UNIHAN_KVARIANT_FIELDS)>=0){
  897. switch(field){
  898. case UNIHAN_FIELD_KSEMANTICVARIANT:
  899. case UNIHAN_FIELD_KSPECIALIZEDSEMANTICVARIANT:
  900. return insert_semanticVariant(code, field, value);
  901. case UNIHAN_FIELD_KZVARIANT:
  902. return insert_zVariant(code, field, value);
  903. default:
  904. variantCode=unihanChar_parse(value);
  905. g_snprintf(buf,20,"%d",code);
  906. stringList_insert(sList,buf);
  907. g_snprintf(buf,20,"%d",variantCode);
  908. stringList_insert(sList,buf);
  909. break;
  910. }
  911. }else if (unihanField_array_index(field,UNIHAN_KRS_FIELDS)>=0){
  912. switch(field){
  913. case UNIHAN_FIELD_KRSADOBE_JAPAN1_6:
  914. subFieldArray=g_strsplit_set(value,".+",-1);
  915. g_snprintf(buf,20,"%d",code);
  916. stringList_insert(sList,buf);
  917. stringList_insert(sList,subFieldArray[0]);
  918. stringList_insert(sList,subFieldArray[1]);
  919. stringList_insert(sList,subFieldArray[2]);
  920. stringList_insert(sList,subFieldArray[3]);
  921. stringList_insert(sList,subFieldArray[4]);
  922. break;
  923. default:
  924. subFieldArray=g_strsplit(value,".",-1);
  925. g_snprintf(buf,20,"%d",code);
  926. stringList_insert(sList,buf);
  927. stringList_insert(sList,subFieldArray[0]);
  928. stringList_insert(sList,subFieldArray[1]);
  929. break;
  930. }
  931. g_assert(subFieldArray);
  932. g_free(subFieldArray);
  933. }else if (field==UNIHAN_FIELD_KMANDARIN){
  934. if (lastCode!=code){
  935. freqRank=1;
  936. lastCode=code;
  937. }
  938. g_snprintf(buf,20,"%d",code);
  939. stringList_insert(sList,buf);
  940. stringList_insert(sList,value);
  941. g_snprintf(buf,20,"%d",freqRank);
  942. stringList_insert(sList,buf);
  943. freqRank++;
  944. }else if (field==UNIHAN_FIELD_KHANYUPINLU){
  945. g_snprintf(buf,20,"%d",code);
  946. stringList_insert(sList,buf);
  947. GArray *pfArray=pinYinFreqRec_parse(value);
  948. if (pfArray->len>0){
  949. PinYinFreqRec *rec=&g_array_index(pfArray,PinYinFreqRec,0);
  950. stringList_insert(sList,rec->pinYin);
  951. g_snprintf(buf,20,"%d",rec->freq);
  952. stringList_insert(sList,buf);
  953. }
  954. g_array_free(pfArray,TRUE);
  955. }else if (field==UNIHAN_FIELD_KIRGKANGXI || field==UNIHAN_FIELD_KKANGXI){
  956. g_snprintf(buf,20,"%d",code);
  957. stringList_insert(sList,buf);
  958. GArray *kArray=kangXiRec_parse(value);
  959. if (kArray->len>0){
  960. KangXiRec *rec=&g_array_index(kArray,KangXiRec,0);
  961. stringList_insert(sList,rec->page);
  962. stringList_insert(sList,rec->charNum);
  963. g_snprintf(buf,20,"%d",(rec->virtual)? 1:0);
  964. stringList_insert(sList,buf);
  965. }
  966. g_array_free(kArray,TRUE);
  967. }else{
  968. g_snprintf(buf,20,"%d",code);
  969. stringList_insert(sList,buf);
  970. stringList_insert(sList,value);
  971. }
  972. newResult=unihan_insert_no_duplicate(table,sList);
  973. result=latest_db_result(result,newResult);
  974. stringList_free(sList);
  975. return result;
  976. }
  977. int unihan_insert_no_duplicate(UnihanTable table, StringList *valueList){
  978. if (unihan_count_matched_record(table,valueList)>0){
  979. if (table==UNIHAN_TABLE_KSEMANTICVARIANT || table == UNIHAN_TABLE_KSPECIALIZEDSEMANTICVARIANT){
  980. verboseMsg_print(VERBOSE_MSG_WARNING,"code %s is already inserted in table %s, no need to insert again.\n",
  981. stringList_index(valueList,0),UNIHAN_TABLE_NAMES[table]);
  982. }else{
  983. verboseMsg_print(VERBOSE_MSG_WARNING,"Record duplicate in table %s on code=%s, skip!\n",
  984. UNIHAN_TABLE_NAMES[table],stringList_index(valueList,0));
  985. }
  986. return -1;
  987. }
  988. int ret=unihan_insert(table,valueList);
  989. if (ret>0){
  990. return ret;
  991. }
  992. return 0;
  993. }
  994. /*=================================
  995. * SQL wrapper functions.
  996. */
  997. int unihanSql_exec(char *sqlClause, UnihanCallback callback,
  998. void *callbackOption, char **errMsg_ptr){
  999. return sqlite3_exec(unihanDb, sqlClause, callback, callbackOption, errMsg_ptr);
  1000. }
  1001. int unihanSql_count_matches(const char * sqlClause, char **errMsg_ptr){
  1002. int ret=sqlite_count_matches(unihanDb,sqlClause,errMsg_ptr);
  1003. if (ret<0){
  1004. verboseMsg_print(VERBOSE_MSG_ERROR, "Database error: %s\n", *errMsg_ptr);
  1005. }
  1006. return ret;
  1007. }
  1008. SQL_Result *unihanSql_get_sql_result(const char *sqlClause){
  1009. return sqlite_get_sql_result(unihanDb, sqlClause);
  1010. }