zsv 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +36 -0
  3. data/LICENSE +21 -0
  4. data/README.md +311 -0
  5. data/ext/zsv/common.h +34 -0
  6. data/ext/zsv/extconf.rb +137 -0
  7. data/ext/zsv/options.c +126 -0
  8. data/ext/zsv/options.h +31 -0
  9. data/ext/zsv/options_internal.h +8 -0
  10. data/ext/zsv/parser.c +300 -0
  11. data/ext/zsv/parser.h +62 -0
  12. data/ext/zsv/row.c +122 -0
  13. data/ext/zsv/row.h +39 -0
  14. data/ext/zsv/vendor/zsv-1.3.0/app/2db.c +756 -0
  15. data/ext/zsv/vendor/zsv-1.3.0/app/2json.c +381 -0
  16. data/ext/zsv/vendor/zsv-1.3.0/app/2tsv.c +228 -0
  17. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/help.c +123 -0
  18. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/license.c +39 -0
  19. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/register.c +104 -0
  20. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/thirdparty.c +41 -0
  21. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/unregister.c +1 -0
  22. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/version.c +14 -0
  23. data/ext/zsv/vendor/zsv-1.3.0/app/check/simdutf_wrapper.h +19 -0
  24. data/ext/zsv/vendor/zsv-1.3.0/app/check/utf8.c +116 -0
  25. data/ext/zsv/vendor/zsv-1.3.0/app/check.c +194 -0
  26. data/ext/zsv/vendor/zsv-1.3.0/app/cli.c +796 -0
  27. data/ext/zsv/vendor/zsv-1.3.0/app/cli_const.h +41 -0
  28. data/ext/zsv/vendor/zsv-1.3.0/app/cli_export.h +16 -0
  29. data/ext/zsv/vendor/zsv-1.3.0/app/cli_ini.c +280 -0
  30. data/ext/zsv/vendor/zsv-1.3.0/app/cli_internal.h +36 -0
  31. data/ext/zsv/vendor/zsv-1.3.0/app/compare.c +913 -0
  32. data/ext/zsv/vendor/zsv-1.3.0/app/compare.h +23 -0
  33. data/ext/zsv/vendor/zsv-1.3.0/app/compare_added_column.c +20 -0
  34. data/ext/zsv/vendor/zsv-1.3.0/app/compare_internal.h +140 -0
  35. data/ext/zsv/vendor/zsv-1.3.0/app/compare_sort.c +91 -0
  36. data/ext/zsv/vendor/zsv-1.3.0/app/compare_unique_colname.c +81 -0
  37. data/ext/zsv/vendor/zsv-1.3.0/app/count-pull.c +82 -0
  38. data/ext/zsv/vendor/zsv-1.3.0/app/count.c +404 -0
  39. data/ext/zsv/vendor/zsv-1.3.0/app/desc.c +569 -0
  40. data/ext/zsv/vendor/zsv-1.3.0/app/echo.c +365 -0
  41. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/my_extension.c +366 -0
  42. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/mysheet_extension.c +341 -0
  43. data/ext/zsv/vendor/zsv-1.3.0/app/ext_template/YOUR_EXTENSION_zsvext.c +263 -0
  44. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.c +298 -0
  45. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.h +157 -0
  46. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/json_numeric.c +177 -0
  47. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.c +444 -0
  48. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.h +145 -0
  49. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/utils.c +110 -0
  50. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/include/memfile.h +15 -0
  51. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/src/memfile.c +64 -0
  52. data/ext/zsv/vendor/zsv-1.3.0/app/external/sglib/sglib.h +1955 -0
  53. data/ext/zsv/vendor/zsv-1.3.0/app/external/simdutf/simdutf.h +6802 -0
  54. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.c +230517 -0
  55. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.h +12174 -0
  56. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_and_csv_vtab.c +2 -0
  57. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.c +142 -0
  58. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.h +49 -0
  59. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-zsv.c +485 -0
  60. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab.c +1015 -0
  61. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3ext.h +663 -0
  62. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/vtab_helper.c +85 -0
  63. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_common.h +75 -0
  64. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_gen.h +167 -0
  65. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_parse.h +228 -0
  66. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_tree.h +186 -0
  67. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_version.h +23 -0
  68. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_common.h +76 -0
  69. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_gen.h +167 -0
  70. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_parse.h +238 -0
  71. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_tree.h +186 -0
  72. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl.c +184 -0
  73. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.c +52 -0
  74. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.h +34 -0
  75. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.c +103 -0
  76. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.h +57 -0
  77. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_bytestack.h +69 -0
  78. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.c +220 -0
  79. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.h +34 -0
  80. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_gen.c +362 -0
  81. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.c +764 -0
  82. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.h +117 -0
  83. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.c +508 -0
  84. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.h +78 -0
  85. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_tree.c +505 -0
  86. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_version.c +7 -0
  87. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/json_value.h +59 -0
  88. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/yajl_helper.h +208 -0
  89. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper.c +795 -0
  90. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper_internal.h +28 -0
  91. data/ext/zsv/vendor/zsv-1.3.0/app/flatten.c +851 -0
  92. data/ext/zsv/vendor/zsv-1.3.0/app/jq.c +106 -0
  93. data/ext/zsv/vendor/zsv-1.3.0/app/jq.h +6 -0
  94. data/ext/zsv/vendor/zsv-1.3.0/app/mv.c +113 -0
  95. data/ext/zsv/vendor/zsv-1.3.0/app/noop.c +90 -0
  96. data/ext/zsv/vendor/zsv-1.3.0/app/overwrite.c +295 -0
  97. data/ext/zsv/vendor/zsv-1.3.0/app/paste.c +175 -0
  98. data/ext/zsv/vendor/zsv-1.3.0/app/pretty.c +693 -0
  99. data/ext/zsv/vendor/zsv-1.3.0/app/prop.c +980 -0
  100. data/ext/zsv/vendor/zsv-1.3.0/app/rm.c +131 -0
  101. data/ext/zsv/vendor/zsv-1.3.0/app/select/fixed.c +130 -0
  102. data/ext/zsv/vendor/zsv-1.3.0/app/select/internal.h +118 -0
  103. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.c +45 -0
  104. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.h +41 -0
  105. data/ext/zsv/vendor/zsv-1.3.0/app/select/processing.c +107 -0
  106. data/ext/zsv/vendor/zsv-1.3.0/app/select/rand.c +20 -0
  107. data/ext/zsv/vendor/zsv-1.3.0/app/select/regex.c +61 -0
  108. data/ext/zsv/vendor/zsv-1.3.0/app/select/search.c +14 -0
  109. data/ext/zsv/vendor/zsv-1.3.0/app/select/selection.c +192 -0
  110. data/ext/zsv/vendor/zsv-1.3.0/app/select/usage.c +72 -0
  111. data/ext/zsv/vendor/zsv-1.3.0/app/select-pull.c +812 -0
  112. data/ext/zsv/vendor/zsv-1.3.0/app/select.c +753 -0
  113. data/ext/zsv/vendor/zsv-1.3.0/app/serialize.c +372 -0
  114. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/curses.h +15 -0
  115. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/cursor.c +119 -0
  116. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/errors.c +45 -0
  117. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.c +63 -0
  118. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.h +12 -0
  119. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/filter.c +166 -0
  120. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers.c +214 -0
  121. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers_internal.h +128 -0
  122. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/help.c +43 -0
  123. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.c +81 -0
  124. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.h +25 -0
  125. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.c +325 -0
  126. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.h +73 -0
  127. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/lexer.c +203 -0
  128. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/newline_handler.c +7 -0
  129. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/pivot.c +318 -0
  130. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.c +134 -0
  131. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.h +119 -0
  132. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/read-data.c +322 -0
  133. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.c +203 -0
  134. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.h +36 -0
  135. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet-sql.c +167 -0
  136. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet_internal.h +36 -0
  137. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sqlfilter.c +153 -0
  138. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/terminfo.c +32 -0
  139. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.c +312 -0
  140. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.h +29 -0
  141. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/ui_buffer.c +266 -0
  142. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/usage.c +9 -0
  143. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/utf8-width.c +60 -0
  144. data/ext/zsv/vendor/zsv-1.3.0/app/sheet.c +1007 -0
  145. data/ext/zsv/vendor/zsv-1.3.0/app/sql.c +453 -0
  146. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.c +101 -0
  147. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.h +49 -0
  148. data/ext/zsv/vendor/zsv-1.3.0/app/stack.c +393 -0
  149. data/ext/zsv/vendor/zsv-1.3.0/app/utils/arg.c +322 -0
  150. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cache.c +228 -0
  151. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cat.c +91 -0
  152. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.c +240 -0
  153. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.h +63 -0
  154. data/ext/zsv/vendor/zsv-1.3.0/app/utils/clock.c +57 -0
  155. data/ext/zsv/vendor/zsv-1.3.0/app/utils/db.c +148 -0
  156. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs-no-jq.c +2 -0
  157. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs.c +427 -0
  158. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_from_json.c +253 -0
  159. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_to_json.c +121 -0
  160. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dl.c +20 -0
  161. data/ext/zsv/vendor/zsv-1.3.0/app/utils/emcc/fs_api.c +159 -0
  162. data/ext/zsv/vendor/zsv-1.3.0/app/utils/err.c +24 -0
  163. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file-mem.c +180 -0
  164. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file.c +256 -0
  165. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.c +197 -0
  166. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.h +49 -0
  167. data/ext/zsv/vendor/zsv-1.3.0/app/utils/jq.c +400 -0
  168. data/ext/zsv/vendor/zsv-1.3.0/app/utils/json.c +120 -0
  169. data/ext/zsv/vendor/zsv-1.3.0/app/utils/mem.c +18 -0
  170. data/ext/zsv/vendor/zsv-1.3.0/app/utils/memmem.c +132 -0
  171. data/ext/zsv/vendor/zsv-1.3.0/app/utils/os.c +178 -0
  172. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite.c +258 -0
  173. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite_writer.c +246 -0
  174. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8-test.c +123 -0
  175. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.c +153 -0
  176. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.h +54 -0
  177. data/ext/zsv/vendor/zsv-1.3.0/app/utils/prop.c +267 -0
  178. data/ext/zsv/vendor/zsv-1.3.0/app/utils/signal.c +53 -0
  179. data/ext/zsv/vendor/zsv-1.3.0/app/utils/string.c +357 -0
  180. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dir_exists_longpath.c +83 -0
  181. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dl.c +33 -0
  182. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/fopen_longpath.c +184 -0
  183. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/foreach_dirent_longpath.c +292 -0
  184. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.c +259 -0
  185. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.h +13 -0
  186. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/mkdir_longpath.c +255 -0
  187. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/remove_longpath.c +96 -0
  188. data/ext/zsv/vendor/zsv-1.3.0/app/utils/writer.c +361 -0
  189. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command.h +40 -0
  190. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command_standalone.c +16 -0
  191. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_main.h +44 -0
  192. data/ext/zsv/vendor/zsv-1.3.0/examples/js/zsv_parser_api_dummy.c +3 -0
  193. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/parse_by_chunk.c +100 -0
  194. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/print_my_column.c +143 -0
  195. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/pull.c +89 -0
  196. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/simple.c +123 -0
  197. data/ext/zsv/vendor/zsv-1.3.0/fuzz/fuzz.c +16 -0
  198. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/api.h +336 -0
  199. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/common.h +361 -0
  200. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation.h +62 -0
  201. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation_private.h +113 -0
  202. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/sheet.h +73 -0
  203. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext.h +329 -0
  204. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/arg.h +90 -0
  205. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/cache.h +49 -0
  206. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/clock.h +36 -0
  207. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/compiler.h +58 -0
  208. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/db.h +19 -0
  209. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dirs.h +147 -0
  210. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dl.h +22 -0
  211. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/emcc/fs_api.h +28 -0
  212. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/err.h +22 -0
  213. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file-mem.h +17 -0
  214. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file.h +99 -0
  215. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/jq.h +65 -0
  216. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/json.h +19 -0
  217. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/mem.h +19 -0
  218. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/memmem.h +13 -0
  219. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/os.h +54 -0
  220. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite.h +71 -0
  221. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite_writer.h +53 -0
  222. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/prop.h +107 -0
  223. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/signal.h +18 -0
  224. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/sql.h +11 -0
  225. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/string.h +148 -0
  226. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/utf8.h +41 -0
  227. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/win/dl.h +25 -0
  228. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/writer.h +101 -0
  229. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/zsv_export.h +33 -0
  230. data/ext/zsv/vendor/zsv-1.3.0/include/zsv.h +20 -0
  231. data/ext/zsv/vendor/zsv-1.3.0/src/vector_delim.c +60 -0
  232. data/ext/zsv/vendor/zsv-1.3.0/src/zsv.c +484 -0
  233. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_internal.c +731 -0
  234. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_delim.c +285 -0
  235. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_fixed.c +88 -0
  236. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_strencode.c +51 -0
  237. data/ext/zsv/zsv_ext.c +343 -0
  238. data/lib/zsv/version.rb +5 -0
  239. data/lib/zsv.rb +81 -0
  240. metadata +340 -0
@@ -0,0 +1,756 @@
1
+ /*
2
+ * Copyright (C) 2021-2022 Liquidaty and zsv contributors. All rights reserved.
3
+ * This file is part of zsv/lib, distributed under the license defined at
4
+ * https://opensource.org/licenses/MIT
5
+ */
6
+
7
+ #include <sys/stat.h>
8
+ #include <errno.h>
9
+ #include <unistd.h> // unlink
10
+ #include <stdio.h>
11
+ #include <string.h>
12
+ #include <stdlib.h>
13
+
14
+ #include <sqlite3.h>
15
+
16
+ #define ZSV_COMMAND 2db
17
+ #include "zsv_command.h"
18
+
19
+ #include <zsv/utils/mem.h>
20
+ #include <zsv/utils/string.h>
21
+ #include <zsv/utils/os.h>
22
+
23
+ #include <yajl_helper/yajl_helper.h>
24
+
25
+ #define ZSV_2DB_DEFAULT_TABLE_NAME "mytable"
26
+
27
+ enum zsv_2db_action {
28
+ zsv_2db_action_create = 1,
29
+ zsv_2db_action_append,
30
+ zsv_2db_action_index
31
+ };
32
+
33
+ enum zsv_2db_state {
34
+ zsv_2db_state_header = 1,
35
+ zsv_2db_state_data,
36
+ zsv_2db_state_done
37
+ };
38
+
39
+ #define LQ_2DB_MAX_INDEXES 32
40
+
41
+ struct zsv_2db_ix {
42
+ struct zsv_2db_ix *next;
43
+ char *name;
44
+ char *on;
45
+ char delete;
46
+ char unique;
47
+ };
48
+
49
+ struct zsv_2db_column {
50
+ struct zsv_2db_column *next;
51
+ char *name;
52
+ char *datatype;
53
+ char *collate;
54
+ };
55
+
56
+ struct zsv_2db_options {
57
+ char *table_name;
58
+ char *db_fn;
59
+ char verbose;
60
+ char overwrite; // overwrite old db if it exists
61
+ #define ZSV_2DB_DEFAULT_BATCH_SIZE 10000
62
+ size_t batch_size;
63
+ };
64
+
65
+ typedef struct zsv_2db_data *zsv_2db_handle;
66
+
67
+ struct zsv_2db_data {
68
+ struct zsv_2db_options opts;
69
+
70
+ char *db_fn_tmp;
71
+ sqlite3 *db;
72
+ char transaction_started;
73
+ char *connection_string;
74
+
75
+ struct {
76
+ yajl_helper_t yh;
77
+ yajl_status yajl_stat;
78
+ enum zsv_2db_state state;
79
+
80
+ unsigned int col_count;
81
+
82
+ struct zsv_2db_column *columns, **last_column;
83
+ struct zsv_2db_column current_column;
84
+ struct zsv_2db_ix *indexes, **last_index;
85
+ sqlite3_int64 index_sequence_num_max;
86
+ struct zsv_2db_ix current_index;
87
+ char have_row_data;
88
+
89
+ char **row_values;
90
+
91
+ sqlite3_stmt *insert_stmt;
92
+ unsigned stmt_colcount;
93
+
94
+ } json_parser;
95
+
96
+ size_t rows_processed;
97
+ size_t row_insert_attempts;
98
+ size_t rows_inserted;
99
+ #define ZSV_2DB_MSG_BATCH_SIZE 10000 // number of rows between each console update (if verbose)
100
+
101
+ int err;
102
+ };
103
+
104
+ static void zsv_2db_ix_free(struct zsv_2db_ix *e) {
105
+ free(e->name);
106
+ free(e->on);
107
+ }
108
+
109
+ static void zsv_2db_ixes_delete(struct zsv_2db_ix **p) {
110
+ if (p && *p) {
111
+ struct zsv_2db_ix *next;
112
+ for (struct zsv_2db_ix *e = *p; e; e = next) {
113
+ next = e->next;
114
+ zsv_2db_ix_free(e);
115
+ free(e);
116
+ }
117
+ *p = NULL;
118
+ }
119
+ }
120
+
121
+ static void zsv_2db_column_free(struct zsv_2db_column *e) {
122
+ free(e->name);
123
+ free(e->datatype);
124
+ free(e->collate);
125
+ }
126
+
127
+ static void zsv_2db_columns_delete(struct zsv_2db_column **p) {
128
+ struct zsv_2db_column *next;
129
+ if (p && *p) {
130
+ for (struct zsv_2db_column *e = *p; e; e = next) {
131
+ next = e->next;
132
+ zsv_2db_column_free(e);
133
+ free(e);
134
+ }
135
+ *p = NULL;
136
+ }
137
+ }
138
+
139
+ static void zsv_2db_delete(zsv_2db_handle data) {
140
+ if (!data)
141
+ return;
142
+
143
+ free(data->opts.table_name);
144
+ free(data->db_fn_tmp);
145
+ if (data->db)
146
+ sqlite3_close(data->db);
147
+
148
+ zsv_2db_columns_delete(&data->json_parser.columns);
149
+ zsv_2db_column_free(&data->json_parser.current_column);
150
+
151
+ zsv_2db_ixes_delete(&data->json_parser.indexes);
152
+ zsv_2db_ix_free(&data->json_parser.current_index);
153
+
154
+ free(data->json_parser.row_values);
155
+
156
+ yajl_helper_delete(data->json_parser.yh);
157
+
158
+ free(data);
159
+ }
160
+
161
+ /* sqlite3 helper functions */
162
+
163
+ static int zsv_2db_sqlite3_exec_2db(sqlite3 *db, const char *sql) {
164
+ char *err_msg = NULL;
165
+ int rc = sqlite3_exec(db, sql, NULL, NULL, &err_msg);
166
+ if (err_msg) {
167
+ fprintf(stderr, "Error executing '%s': %s\n", sql, err_msg);
168
+ sqlite3_free(err_msg);
169
+ } else if (rc == SQLITE_DONE || rc == SQLITE_OK)
170
+ return 0;
171
+ return 1;
172
+ }
173
+ // add_db_indexes: return 0 on success, else error code
174
+ static int zsv_2db_add_indexes(struct zsv_2db_data *data) {
175
+ int err = 0;
176
+ for (struct zsv_2db_ix *ix = data->json_parser.indexes; !err && ix; ix = ix->next) {
177
+ sqlite3_str *pStr = sqlite3_str_new(data->db);
178
+ sqlite3_str_appendf(pStr, "create%s index \"%w_%w\" on \"%w\"(%s)", ix->unique ? " unique" : "",
179
+ data->opts.table_name, ix->name, data->opts.table_name, ix->on);
180
+ err = zsv_2db_sqlite3_exec_2db(data->db, sqlite3_str_value(pStr));
181
+ if (!err)
182
+ data->json_parser.index_sequence_num_max++;
183
+ sqlite3_free(sqlite3_str_finish(pStr));
184
+ }
185
+ return err;
186
+ }
187
+
188
+ static void zsv_2db_start_transaction(struct zsv_2db_data *data) {
189
+ if (!data->transaction_started)
190
+ sqlite3_exec(data->db, "BEGIN TRANSACTION", NULL, NULL, NULL);
191
+ data->transaction_started = 1;
192
+ }
193
+
194
+ static void zsv_2db_end_transaction(struct zsv_2db_data *data) {
195
+ if (data->transaction_started)
196
+ sqlite3_exec(data->db, "COMMIT", NULL, NULL, NULL);
197
+ data->transaction_started = 0;
198
+ }
199
+
200
+ static sqlite3_str *build_create_table_statement(sqlite3 *db, const char *tname, const char *const *colnames,
201
+ const char *const *datatypes, const char *const *collates,
202
+ unsigned int col_count) {
203
+ int err = 0;
204
+ sqlite3_str *pStr = sqlite3_str_new(db);
205
+ sqlite3_str_appendf(pStr, "CREATE TABLE \"%w\" (\n ", tname ? tname : ZSV_2DB_DEFAULT_TABLE_NAME);
206
+ for (unsigned int i = 0; i < col_count; i++) {
207
+ if (i > 0)
208
+ sqlite3_str_appendf(pStr, ",\n ");
209
+ sqlite3_str_appendf(pStr, "\"%w\"", colnames[i]);
210
+
211
+ const char *datatype = datatypes ? datatypes[i] : NULL;
212
+ if (!datatype || !(!strcmp("int", datatype) || !strcmp("integer", datatype) || !strcmp("real", datatype) ||
213
+ !strcmp("text", datatype))) {
214
+ if (datatype)
215
+ fprintf(stderr, "Unrecognized datatype %s", datatype), err = 1;
216
+ else
217
+ datatype = "text";
218
+ }
219
+ if (!err) {
220
+ const char *collate = collates ? collates[i] : NULL;
221
+ if (collate && *collate) {
222
+ if (collate && !(!strcmp("binary", collate) || !strcmp("rtrim", collate) || !strcmp("nocase", collate))) {
223
+ fprintf(stderr, "Unrecognized collate: expected binary, rtrim or nocase, got %s", collate);
224
+ err = 1;
225
+ } else
226
+ sqlite3_str_appendf(pStr, " %s%s%s", datatype, collate ? " collate " : "", collate ? collate : "");
227
+ }
228
+ }
229
+ }
230
+ if (err) {
231
+ if (pStr)
232
+ sqlite3_free(sqlite3_str_finish(pStr));
233
+ pStr = NULL;
234
+ } else
235
+ sqlite3_str_appendf(pStr, ")\n");
236
+ return pStr;
237
+ }
238
+
239
+ // zsv_2db_finish_header: return 0 on error, 1 on success
240
+ static int zsv_2db_finish_header(struct zsv_2db_data *data) {
241
+ if (data->err)
242
+ return 0;
243
+ if (!data->json_parser.col_count) {
244
+ fprintf(stderr, "No columns found!\n");
245
+ return 0;
246
+ }
247
+
248
+ data->json_parser.state = zsv_2db_state_data;
249
+ if ((data->json_parser.row_values = calloc(data->json_parser.col_count, sizeof(*data->json_parser.row_values))))
250
+ return 1;
251
+
252
+ data->err = 1;
253
+ return 0;
254
+ }
255
+
256
+ /* json parser functions */
257
+
258
+ static sqlite3_stmt *create_insert_statement(sqlite3 *db, const char *tname, unsigned int col_count) {
259
+ sqlite3_stmt *insert_stmt = NULL;
260
+ sqlite3_str *insert_sql = sqlite3_str_new(db);
261
+ if (insert_sql) {
262
+ sqlite3_str_appendf(insert_sql, "insert into \"%w\" values(?", tname);
263
+ for (unsigned int i = 1; i < col_count; i++)
264
+ sqlite3_str_appendf(insert_sql, ", ?");
265
+ sqlite3_str_appendf(insert_sql, ")");
266
+ int status = sqlite3_prepare_v2(db, sqlite3_str_value(insert_sql), -1, &insert_stmt, NULL);
267
+ if (status != SQLITE_OK) {
268
+ fprintf(stderr, "Unable to prep (%s): %s\n", sqlite3_str_value(insert_sql), sqlite3_errmsg(db));
269
+ }
270
+ sqlite3_free(sqlite3_str_finish(insert_sql));
271
+ }
272
+ return insert_stmt;
273
+ }
274
+
275
+ // return error
276
+ static int zsv_2db_set_insert_stmt(struct zsv_2db_data *data) {
277
+ int err = 0;
278
+ if (!data->json_parser.col_count) {
279
+ fprintf(stderr, "insert statement called with no columns to insert");
280
+ err = 1;
281
+ } else {
282
+ const char **colnames = calloc(data->json_parser.col_count, sizeof(*colnames));
283
+ const char **datatypes = calloc(data->json_parser.col_count, sizeof(*datatypes));
284
+ const char **collates = calloc(data->json_parser.col_count, sizeof(*collates));
285
+ unsigned int i = 0;
286
+ for (struct zsv_2db_column *e = data->json_parser.columns; e; e = e->next, i++) {
287
+ colnames[i] = e->name;
288
+ datatypes[i] = e->datatype;
289
+ collates[i] = e->collate;
290
+ }
291
+
292
+ sqlite3_str *create_sql = build_create_table_statement(data->db, data->opts.table_name, colnames, datatypes,
293
+ collates, data->json_parser.col_count);
294
+ if (!create_sql)
295
+ err = 1;
296
+ else {
297
+ if (!(err = zsv_2db_sqlite3_exec_2db(data->db, sqlite3_str_value(create_sql))) &&
298
+ !(data->json_parser.insert_stmt =
299
+ create_insert_statement(data->db, data->opts.table_name, data->json_parser.col_count))) {
300
+ err = 1;
301
+ zsv_2db_start_transaction(data);
302
+ } else
303
+ data->json_parser.stmt_colcount = data->json_parser.col_count;
304
+ sqlite3_free(sqlite3_str_finish(create_sql));
305
+ }
306
+
307
+ free(colnames);
308
+ free(datatypes);
309
+ free(collates);
310
+ }
311
+ return err;
312
+ }
313
+
314
+ /*
315
+ add_local_db_row(): return sqlite3 error, or 0 on ok
316
+ */
317
+ static int zsv_2db_insert_row_values(sqlite3_stmt *stmt, unsigned stmt_colcount, char const *const *const values,
318
+ unsigned int values_count) {
319
+ if (!stmt)
320
+ return -1;
321
+
322
+ int status = 0;
323
+ unsigned int errors_printed = 0;
324
+ if (values_count > stmt_colcount)
325
+ values_count = stmt_colcount;
326
+
327
+ for (unsigned int i = 0; i < values_count; i++) {
328
+ const char *val = values[i];
329
+ if (val && *val)
330
+ sqlite3_bind_text(stmt, (int)i + 1, val, (int)strlen(val), SQLITE_STATIC);
331
+ else
332
+ // don't use sqlite3_bind_null, else x = ? will fail if value is ""/null
333
+ sqlite3_bind_text(stmt, (int)i + 1, "", 0, SQLITE_STATIC);
334
+ }
335
+
336
+ for (unsigned int i = values_count; i < stmt_colcount; i++)
337
+ sqlite3_bind_null(stmt, (int)i + 1);
338
+
339
+ status = sqlite3_step(stmt);
340
+ if (status == SQLITE_DONE)
341
+ status = 0;
342
+ else if (errors_printed < 10) {
343
+ errors_printed++;
344
+ fprintf(stderr, "Unable to insert: %s\n", sqlite3_errstr(status));
345
+ } else if (errors_printed != 100) {
346
+ errors_printed = 100;
347
+ fprintf(stderr, "Too many insert errors to print\n");
348
+ }
349
+
350
+ sqlite3_reset(stmt);
351
+
352
+ return status;
353
+ }
354
+
355
+ static int zsv_2db_insert_row(struct zsv_2db_data *data) {
356
+ if (!data->err) {
357
+ data->rows_processed++;
358
+ if (data->json_parser.have_row_data) {
359
+ if (!data->json_parser.insert_stmt)
360
+ data->err = zsv_2db_set_insert_stmt(data);
361
+
362
+ if (!data->db)
363
+ return 0;
364
+ int rc =
365
+ zsv_2db_insert_row_values(data->json_parser.insert_stmt, data->json_parser.stmt_colcount,
366
+ (char const *const *const)data->json_parser.row_values, data->json_parser.col_count);
367
+ data->row_insert_attempts++;
368
+ if (!rc) {
369
+ data->rows_inserted++;
370
+ if (data->opts.verbose && (data->rows_inserted % ZSV_2DB_MSG_BATCH_SIZE == 0))
371
+ fprintf(stderr, "%zu rows inserted\n", data->rows_inserted);
372
+ if (data->opts.batch_size && (data->rows_inserted % data->opts.batch_size == 0)) {
373
+ zsv_2db_end_transaction(data);
374
+ if (data->opts.verbose)
375
+ fprintf(stderr, "%zu rows committed\n", data->rows_inserted);
376
+ zsv_2db_start_transaction(data);
377
+ }
378
+ }
379
+ }
380
+ }
381
+
382
+ return 1;
383
+ }
384
+
385
+ static int json_start_map(yajl_helper_t yh) {
386
+ (void)(yh);
387
+ return 1;
388
+ }
389
+
390
+ static int json_end_map(yajl_helper_t yh) {
391
+ struct zsv_2db_data *data = yajl_helper_ctx(yh);
392
+ if (data->json_parser.state == zsv_2db_state_header &&
393
+ yajl_helper_got_path(yh, 3, "[{columns[")) { // exiting a column header
394
+ if (!data->json_parser.current_column.name) {
395
+ fprintf(stderr, "Name missing from column spec!\n");
396
+ return 0;
397
+ } else {
398
+ struct zsv_2db_column *e = calloc(1, sizeof(*e));
399
+ if (!e) {
400
+ fprintf(stderr, "Out of memory!");
401
+ return 0;
402
+ }
403
+ *e = data->json_parser.current_column;
404
+ *data->json_parser.last_column = e;
405
+ data->json_parser.last_column = &e->next;
406
+ data->json_parser.col_count++;
407
+ memset(&data->json_parser.current_column, 0, sizeof(data->json_parser.current_column));
408
+ }
409
+ } else if (data->json_parser.state == zsv_2db_state_header &&
410
+ yajl_helper_got_path(yh, 3, "[{indexes{")) { // exiting an index
411
+ if (!data->json_parser.current_index.name) {
412
+ fprintf(stderr, "Name missing from index spec\n");
413
+ return 0;
414
+ } else if (!(data->json_parser.current_index.on || data->json_parser.current_index.delete)) {
415
+ fprintf(stderr, "'on' or 'delete' missing from index spec\n");
416
+ return 0;
417
+ } else {
418
+ struct zsv_2db_ix *e = calloc(1, sizeof(*e));
419
+ if (!e) {
420
+ fprintf(stderr, "Out of memory!");
421
+ return 0;
422
+ }
423
+ *e = data->json_parser.current_index;
424
+ *data->json_parser.last_index = e;
425
+ data->json_parser.last_index = &e->next;
426
+ memset(&data->json_parser.current_index, 0, sizeof(data->json_parser.current_index));
427
+ }
428
+ }
429
+ return 1;
430
+ }
431
+
432
+ static int json_map_key(yajl_helper_t yh, const unsigned char *s, size_t len) {
433
+ struct zsv_2db_data *data = yajl_helper_ctx(yh);
434
+ if (data->json_parser.state == zsv_2db_state_header && yajl_helper_got_path(yh, 3, "[{indexes{")) {
435
+ free(data->json_parser.current_index.name);
436
+ if (len)
437
+ data->json_parser.current_index.name = zsv_memdup(s, len);
438
+ else
439
+ data->json_parser.current_index.name = NULL;
440
+ }
441
+ return 1;
442
+ }
443
+
444
+ static int json_start_array(yajl_helper_t yh) {
445
+ if (yajl_helper_level(yh) == 2) {
446
+ struct zsv_2db_data *data = yajl_helper_ctx(yh);
447
+ if (data->json_parser.state == zsv_2db_state_header && yajl_helper_got_path(yh, 2, "[[") &&
448
+ yajl_helper_array_index_plus_1(yh, 1) == 2)
449
+ return zsv_2db_finish_header(data);
450
+ }
451
+ return 1;
452
+ }
453
+
454
+ static void reset_row_values(struct zsv_2db_data *data) {
455
+ if (data->json_parser.row_values) {
456
+ for (unsigned int i = 0; i < data->json_parser.col_count; i++) {
457
+ free(data->json_parser.row_values[i]);
458
+ data->json_parser.row_values[i] = NULL;
459
+ }
460
+ }
461
+ data->json_parser.have_row_data = 0;
462
+ }
463
+
464
+ static int json_end_array(yajl_helper_t yh) {
465
+ if (yajl_helper_level(yh) == 2) {
466
+ struct zsv_2db_data *data = yajl_helper_ctx(yh);
467
+ if (data->json_parser.state == zsv_2db_state_data && yajl_helper_got_path(yh, 2, "[[")) { // finished a row of data
468
+ zsv_2db_insert_row(data);
469
+ reset_row_values(data);
470
+ }
471
+ }
472
+ return 1;
473
+ }
474
+
475
+ static int json_process_value(yajl_helper_t yh, struct json_value *value) {
476
+ const unsigned char *jsstr;
477
+ size_t len;
478
+ struct zsv_2db_data *data = yajl_helper_ctx(yh);
479
+ if (data->json_parser.state == zsv_2db_state_data) {
480
+ if (yajl_helper_got_path(yh, 3, "[[[")) {
481
+ json_value_default_string(value, &jsstr, &len);
482
+ if (jsstr && len) {
483
+ unsigned int j = yajl_helper_array_index_plus_1(yh, 0);
484
+ if (j && j - 1 < data->json_parser.col_count) {
485
+ data->json_parser.row_values[j - 1] = zsv_memdup(jsstr, len);
486
+ data->json_parser.have_row_data = 1;
487
+ }
488
+ }
489
+ }
490
+ } else if (yajl_helper_got_path(yh, 2, "[{name")) {
491
+ json_value_default_string(value, &jsstr, &len);
492
+ if (len) {
493
+ if (data->opts.table_name)
494
+ fprintf(stderr, "Table name specified twice; keeping %s, ignoring %.*s\n", data->opts.table_name, (int)len,
495
+ jsstr);
496
+ else
497
+ data->opts.table_name = zsv_memdup(jsstr, len);
498
+ }
499
+ } else if (yajl_helper_got_path(yh, 4, "[{columns[{name")) {
500
+ free(data->json_parser.current_column.name);
501
+ data->json_parser.current_column.name = NULL;
502
+ json_value_default_string(value, &jsstr, &len);
503
+ if (jsstr && len)
504
+ data->json_parser.current_column.name = zsv_memdup(jsstr, len);
505
+ } else if (yajl_helper_got_path(yh, 4, "[{columns[{datatype")) {
506
+ free(data->json_parser.current_column.datatype);
507
+ data->json_parser.current_column.datatype = NULL;
508
+ json_value_default_string(value, &jsstr, &len);
509
+ if (jsstr && len)
510
+ data->json_parser.current_column.datatype = zsv_memdup(jsstr, len);
511
+ } else if (yajl_helper_got_path(yh, 4, "[{columns[{collate")) {
512
+ free(data->json_parser.current_column.collate);
513
+ data->json_parser.current_column.collate = NULL;
514
+ json_value_default_string(value, &jsstr, &len);
515
+ if (jsstr && len)
516
+ data->json_parser.current_column.collate = zsv_memdup(jsstr, len);
517
+ } else if (yajl_helper_got_path(yh, 4, "[{indexes{*{delete")) {
518
+ data->json_parser.current_index.delete = json_value_truthy(value);
519
+ } else if (yajl_helper_got_path(yh, 4, "[{indexes{*{unique")) {
520
+ data->json_parser.current_index.unique = json_value_truthy(value);
521
+ } else if (yajl_helper_got_path(yh, 4, "[{indexes{*{on") || yajl_helper_got_path(yh, 5, "[{indexes{*{on[")) {
522
+ json_value_default_string(value, &jsstr, &len);
523
+ if (len) {
524
+ if (yajl_helper_level(yh) == 4 || !data->json_parser.current_index.on) {
525
+ free(data->json_parser.current_index.on);
526
+ data->json_parser.current_index.on = zsv_memdup(jsstr, len);
527
+ } else {
528
+ char *defn;
529
+ asprintf(&defn, "%s,%.*s", data->json_parser.current_index.on, (int)len, jsstr);
530
+ free(data->json_parser.current_index.on);
531
+ data->json_parser.current_index.on = defn;
532
+ }
533
+ }
534
+ }
535
+ return 1;
536
+ }
537
+
538
+ /* api functions */
539
+
540
+ // exportable
541
+ static zsv_2db_handle zsv_2db_new(struct zsv_2db_options *opts) {
542
+ int err = 0;
543
+ if (!opts->db_fn)
544
+ fprintf(stderr, "Please specify an output file\n"), err = 1;
545
+
546
+ struct stat stt = {0};
547
+ if (!err && !opts->overwrite && (!stat(opts->db_fn, &stt) || errno != ENOENT))
548
+ fprintf(stderr, "File %s already exists\n", opts->db_fn), err = 1;
549
+
550
+ if (err)
551
+ return NULL;
552
+
553
+ struct zsv_2db_data *data = calloc(1, sizeof(*data));
554
+ data->opts = *opts;
555
+
556
+ if (!(data->opts.batch_size))
557
+ data->opts.batch_size = ZSV_2DB_DEFAULT_BATCH_SIZE;
558
+
559
+ data->json_parser.last_column = &data->json_parser.columns;
560
+ data->json_parser.last_index = &data->json_parser.indexes;
561
+ data->json_parser.state = zsv_2db_state_header;
562
+ if (opts->table_name)
563
+ data->opts.table_name = strdup(opts->table_name);
564
+ asprintf(&data->db_fn_tmp, "%s.tmp", data->opts.db_fn);
565
+ if (!data->db_fn_tmp)
566
+ err = 1;
567
+ else {
568
+ int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
569
+ #ifndef NDEBUG
570
+ fprintf(stderr, "Opening: %s\n", data->db_fn_tmp);
571
+ #endif
572
+ unlink(data->db_fn_tmp);
573
+ int rc = sqlite3_open_v2(data->db_fn_tmp, &data->db, flags, NULL);
574
+ err = 1;
575
+ if (!data->db)
576
+ fprintf(stderr, "Unable to open db at %s\n", data->db_fn_tmp);
577
+ else if (rc != SQLITE_OK)
578
+ fprintf(stderr, "Unable to open db at %s: %s\n", data->db_fn_tmp, sqlite3_errmsg(data->db));
579
+ else {
580
+ err = 0;
581
+
582
+ // performance tweaks
583
+ sqlite3_exec(data->db, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
584
+ sqlite3_exec(data->db, "PRAGMA journal_mode = OFF", NULL, NULL, NULL);
585
+
586
+ // parse the input and create & populate the database table
587
+ if (!(data->json_parser.yh = yajl_helper_new(32, json_start_map, json_end_map, json_map_key, json_start_array,
588
+ json_end_array, json_process_value, data))) {
589
+ fprintf(stderr, "Unable to get yajl parser\n");
590
+ err = 1;
591
+ }
592
+ }
593
+ }
594
+
595
+ if (err) {
596
+ zsv_2db_delete(data);
597
+ data = NULL;
598
+ }
599
+
600
+ return data;
601
+ }
602
+
603
+ // exportable
604
+ static int zsv_2db_err(zsv_2db_handle h) {
605
+ return h->err;
606
+ }
607
+
608
+ // exportable
609
+ static int zsv_2db_finish(zsv_2db_handle data) {
610
+ // add indexes
611
+ int err = zsv_2db_add_indexes(data);
612
+ if (!err) {
613
+ if (data->db) {
614
+ zsv_2db_end_transaction(data);
615
+ if (data->json_parser.insert_stmt)
616
+ sqlite3_finalize(data->json_parser.insert_stmt);
617
+
618
+ sqlite3_close(data->db);
619
+ data->db = NULL;
620
+
621
+ // rename tmp to target
622
+ unlink(data->opts.db_fn);
623
+ if (zsv_replace_file(data->db_fn_tmp, data->opts.db_fn)) {
624
+ fprintf(stderr, "Unable to rename %s to %s\n", data->db_fn_tmp, data->opts.db_fn);
625
+ zsv_perror(NULL);
626
+ err = 1;
627
+ } else
628
+ fprintf(stderr, "Database %s created\n", data->opts.db_fn);
629
+ }
630
+ }
631
+ return err;
632
+ }
633
+
634
+ // exportable
635
+ static yajl_handle zsv_2db_yajl_handle(zsv_2db_handle data) {
636
+ return yajl_helper_yajl(data->json_parser.yh);
637
+ }
638
+
639
+ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *zsv_opts,
640
+ struct zsv_prop_handler *custom_prop_handler) {
641
+ (void)(zsv_opts);
642
+ (void)(custom_prop_handler);
643
+ FILE *f_in = NULL;
644
+ int err = 0;
645
+ struct zsv_2db_options opts = {0};
646
+ opts.verbose = zsv_get_default_opts().verbose;
647
+
648
+ const char *usage[] = {
649
+ APPNAME ": convert JSON to SQLite3 DB",
650
+ "",
651
+ "Usage: " APPNAME " -o <output path> [-t <table name>] [input.json]\n",
652
+ "",
653
+ "Options:",
654
+ " -h,--help : show usage",
655
+ " --table <table_name> : save as specified table name",
656
+ " --overwrite : overwrite existing database",
657
+ // TO DO:
658
+ // --sql to output sql statements
659
+ // --append: append to existing db
660
+ "",
661
+ // TO DO: add output examples and schema descriptions
662
+ "The input should conform to the schema defined at:",
663
+ " https://github.com/liquidaty/zsv/blob/main/app/schema/database-table.json",
664
+ "",
665
+ "Example:",
666
+ " [",
667
+ " {",
668
+ " \"columns\":[{\"name\":\"column 1\"}],",
669
+ " \"indexes\":{\"ix1\":{\"on\":\"[column 1]\",\"unique\":true}}",
670
+ " },",
671
+ " [",
672
+ " [\"row 1 cell 1\"],",
673
+ " [\"row 2 cell 1\"]",
674
+ " ]",
675
+ " ]",
676
+ NULL,
677
+ };
678
+
679
+ for (int i = 1; !err && i < argc; i++) {
680
+ if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
681
+ for (int j = 0; usage[j]; j++)
682
+ fprintf(stdout, "%s\n", usage[j]);
683
+ goto exit_2db;
684
+ } else if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output")) {
685
+ if (++i >= argc)
686
+ fprintf(stderr, "%s option requires a filename value\n", argv[i - 1]), err = 1;
687
+ else if (opts.db_fn)
688
+ fprintf(stderr, "Output file specified more than once (%s and %s)\n", opts.db_fn, argv[i]), err = 1;
689
+ else
690
+ opts.db_fn = (char *)argv[i]; // we won't free this
691
+ } else if (!strcmp(argv[i], "--overwrite")) {
692
+ opts.overwrite = 1;
693
+ } else if (!strcmp(argv[i], "--table")) {
694
+ if (++i >= argc)
695
+ fprintf(stderr, "%s option requires a filename value\n", argv[i - 1]), err = 1;
696
+ else if (opts.table_name)
697
+ fprintf(stderr, "Table name specified more than once (%s and %s)\n", opts.table_name, argv[i]), err = 1;
698
+ else
699
+ opts.table_name = (char *)argv[i]; // we won't free this
700
+ } else if (f_in)
701
+ fprintf(stderr, "Input file specified more than once\n"), err = 1;
702
+ else if (!(f_in = fopen(argv[i], "rb")))
703
+ fprintf(stderr, "Unable to open for reading: %s\n", argv[i]), err = 1;
704
+ else if (!(strlen(argv[i]) > 5 &&
705
+ !zsv_stricmp((const unsigned char *)argv[i] + strlen(argv[i]) - 5, (const unsigned char *)".json")))
706
+ fprintf(stderr, "Warning: input filename does not end with .json (%s)\n", argv[i]);
707
+ }
708
+
709
+ if (!f_in) {
710
+ #ifdef NO_STDIN
711
+ fprintf(stderr, "Please specify an input file\n");
712
+ err = 1;
713
+ #else
714
+ f_in = stdin;
715
+ #endif
716
+ }
717
+
718
+ if (!err) {
719
+ zsv_2db_handle data = zsv_2db_new(&opts);
720
+ if (!data)
721
+ err = 1;
722
+ else {
723
+ size_t chunk_size = 4096 * 16;
724
+ unsigned char *buff = malloc(chunk_size);
725
+ if (!buff)
726
+ err = 1;
727
+ else {
728
+ size_t bytes_read = 0;
729
+ yajl_handle y = zsv_2db_yajl_handle(data);
730
+ while (!err && !zsv_2db_err(data)) {
731
+ bytes_read = fread(buff, 1, chunk_size, f_in);
732
+ if (bytes_read == 0)
733
+ break;
734
+ yajl_status stat = yajl_parse(y, buff, bytes_read);
735
+ if (stat != yajl_status_ok)
736
+ err = yajl_helper_print_err(y, buff, bytes_read);
737
+ }
738
+
739
+ if (!err) {
740
+ if (yajl_complete_parse(y) != yajl_status_ok)
741
+ err = yajl_helper_print_err(y, buff, bytes_read);
742
+ else if (zsv_2db_err(data) || zsv_2db_finish(data))
743
+ err = 1;
744
+ }
745
+ free(buff);
746
+ }
747
+ zsv_2db_delete(data);
748
+ }
749
+ }
750
+
751
+ exit_2db:
752
+ if (f_in && f_in != stdin)
753
+ fclose(f_in);
754
+
755
+ return err;
756
+ }