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,453 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and zsv contributors. All rights reserved.
3
+ *
4
+ * This file is part of zsv/lib, distributed under the MIT license as defined at
5
+ * https://opensource.org/licenses/MIT
6
+ */
7
+
8
+ #include <stdlib.h>
9
+ #include <stdio.h>
10
+ #include <string.h>
11
+ #include <sys/stat.h>
12
+ // #include <sqlite3.h>
13
+ #include "external/sqlite3/sqlite3.h"
14
+ #include "external/sqlite3/sqlite3_csv_vtab-mem.h"
15
+
16
+ #define ZSV_COMMAND sql
17
+ #include "zsv_command.h"
18
+
19
+ #include <zsv/utils/writer.h>
20
+ #include <zsv/utils/file.h>
21
+ #include <zsv/utils/string.h>
22
+ #include <zsv/utils/sql.h>
23
+ #include "sql_internal.h"
24
+
25
+ #include <unistd.h> // unlink
26
+
27
+ #ifndef STRING_LIST
28
+ #define STRING_LIST
29
+ struct string_list {
30
+ struct string_list *next;
31
+ char *value;
32
+ };
33
+ #endif
34
+
35
+ const char *zsv_sql_usage_msg[] = {
36
+ APPNAME ": run ad hoc sql on a CSV file",
37
+ " or join multiple CSV files on one or more common column(s)",
38
+ "",
39
+ #ifdef NO_STDIN
40
+ "Usage: " APPNAME " <filename> [filename ...] <sql | @file.sql>",
41
+ #else
42
+ "Usage: " APPNAME " [filename, or - for stdin] [filename ...] <sql | @file.sql | --join-indexes <N,...>>",
43
+ #endif
44
+ " e.g. " APPNAME " file.csv \"select * from data\"",
45
+ " e.g. " APPNAME " file1.csv file2.csv \"select * from data inner join data2\"",
46
+ " e.g. " APPNAME " file1.csv file2.csv --join-indexes 1,2",
47
+ "",
48
+ "Loads your CSV file into a table named 'data', then runs your sql, which must start with 'select '.",
49
+ "If multiple files are specified, tables will be named data, data2, data3, ...",
50
+ "",
51
+ "Options:",
52
+ " --join-indexes <n1...>: specify one or more column names to join multiple files by",
53
+ " each n is treated as an index in the first input file that determines a column",
54
+ " of the join. For example, if joining two files that, respectively, have columns",
55
+ " A,B,C,D and X,B,C,A,Y then `--join-indexes 1,3` will join on columns A and C.",
56
+ " When using this option, do not include an sql statement",
57
+ " -b : output with BOM",
58
+ " -C,--max-cols <n> : change the maximum allowable columns. must be > 0 and < 2000",
59
+ " -o <filename> : filename to save output to",
60
+ " --memory : use in-memory instead of temporary db (see https://www.sqlite.org/inmemorydb.html)",
61
+ NULL,
62
+ };
63
+
64
+ static int zsv_sql_usage(FILE *f) {
65
+ for (size_t i = 0; zsv_sql_usage_msg[i]; i++)
66
+ fprintf(f, "%s\n", zsv_sql_usage_msg[i]);
67
+ return f == stderr ? 1 : 0;
68
+ }
69
+
70
+ struct zsv_sql_data {
71
+ FILE *in;
72
+ const char *input_filename;
73
+ struct string_list *more_input_filenames;
74
+ char *sql_dynamic; // will hold contents of sql file, if any
75
+ char *join_indexes; // will hold contents of join_indexes arg, prefixed and suffixed with a comma
76
+ struct string_list *join_column_names;
77
+ unsigned char in_memory : 1;
78
+ unsigned char _ : 7;
79
+ };
80
+
81
+ static void zsv_sql_finalize(struct zsv_sql_data *data) {
82
+ (void)data;
83
+ }
84
+
85
+ static void zsv_sql_cleanup(struct zsv_sql_data *data) {
86
+ if (data->in && data->in != stdin)
87
+ fclose(data->in);
88
+ free(data->sql_dynamic);
89
+ free(data->join_indexes);
90
+ if (data->join_column_names) {
91
+ struct string_list *next;
92
+ for (struct string_list *tmp = data->join_column_names; tmp; tmp = next) {
93
+ next = tmp->next;
94
+ free(tmp->value);
95
+ free(tmp);
96
+ }
97
+ }
98
+
99
+ if (data->more_input_filenames) {
100
+ struct string_list *next;
101
+ for (struct string_list *tmp = data->more_input_filenames; tmp; tmp = next) {
102
+ next = tmp->next;
103
+ free(tmp);
104
+ }
105
+ }
106
+ (void)data;
107
+ }
108
+
109
+ static char is_select_sql(const char *s) {
110
+ return strlen(s) > strlen("select ") && !zsv_strincmp((const unsigned char *)"select ", strlen("select "),
111
+ (const unsigned char *)s, strlen("select "));
112
+ }
113
+
114
+ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *opts,
115
+ struct zsv_prop_handler *custom_prop_handler) {
116
+ /**
117
+ * We need to pass the following data to the sqlite3 virtual table code:
118
+ * a. zsv parser options indicated in the cmd line
119
+ * b. input file path
120
+ * c. cmd-line options used in (a), so that we can print warnings in case of
121
+ * conflict between (a) and properties of (b)
122
+ */
123
+ int err = 0;
124
+ if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
125
+ err = zsv_sql_usage(argc < 2 ? stderr : stdout);
126
+ else {
127
+ struct zsv_sql_data data = {0};
128
+ const char *my_sql = NULL;
129
+ struct string_list **next_input_filename = &data.more_input_filenames;
130
+
131
+ struct zsv_csv_writer_options writer_opts = zsv_writer_get_default_opts();
132
+ for (int arg_i = 1; !err && arg_i < argc; arg_i++) {
133
+ const char *arg = argv[arg_i];
134
+ if (!strcmp(arg, "--join-indexes")) {
135
+ if (data.join_indexes) {
136
+ fprintf(stderr, "%s specified more than once\n", arg);
137
+ err = 1;
138
+ } else if (!(++arg_i < argc)) {
139
+ fprintf(stderr, "%s option requires a value\n", arg);
140
+ err = 1;
141
+ } else {
142
+ arg = argv[arg_i];
143
+ int have = 0;
144
+ for (const char *s = arg; *s; s++) {
145
+ if (*s == ',')
146
+ ;
147
+ else if ((*s >= '0' && *s <= '9'))
148
+ have = 1;
149
+ else
150
+ err = 1;
151
+ }
152
+ if (!have || err)
153
+ fprintf(stderr,
154
+ "Invalid --join-indexes value (%s): must be a comma-separated"
155
+ " list of indexes with no spaces or other characters\n",
156
+ arg),
157
+ err = 1;
158
+ else {
159
+ asprintf(&data.join_indexes, ",%s,", arg);
160
+ if (data.join_indexes && strstr(data.join_indexes, ",0,"))
161
+ fprintf(stderr, "--join-indexes index values must be greater than zero\n"), err = 1;
162
+ }
163
+ }
164
+ } else if (!my_sql && ((*arg == '@' && arg[1]) || is_select_sql(arg))) {
165
+ if (is_select_sql(arg))
166
+ my_sql = arg;
167
+ else {
168
+ struct stat st;
169
+ if (stat(arg + 1, &st) == 0 && st.st_size > 0) {
170
+ FILE *f = fopen(arg + 1, "rb");
171
+ if (f) {
172
+ data.sql_dynamic = malloc(st.st_size + 1);
173
+ if (data.sql_dynamic) {
174
+ fread(data.sql_dynamic, 1, st.st_size, f);
175
+ data.sql_dynamic[st.st_size] = '\0';
176
+ if (is_select_sql(data.sql_dynamic))
177
+ my_sql = (const char *)data.sql_dynamic;
178
+ else {
179
+ fprintf(stderr, "File %s contents must be a sql SELECT statement\n", arg + 1);
180
+ err = 1;
181
+ }
182
+ }
183
+ fclose(f);
184
+ }
185
+ }
186
+ if (!data.sql_dynamic && !err) {
187
+ fprintf(stderr, "File %s empty or not readable\n", arg + 1);
188
+ err = 1;
189
+ }
190
+ }
191
+ } else if (!strcmp(arg, "-o") || !strcmp(arg, "--output"))
192
+ writer_opts.output_path = zsv_next_arg(++arg_i, argc, argv, &err);
193
+ else if (!strcmp(arg, "--memory"))
194
+ data.in_memory = 1;
195
+ else if (!strcmp(arg, "-b"))
196
+ writer_opts.with_bom = 1;
197
+ else if (*arg != '-') {
198
+ if (!data.input_filename) {
199
+ data.input_filename = arg;
200
+ if (!(data.in = fopen(arg, "rb"))) {
201
+ fprintf(stderr, "Unable to open for reading: %s\n", arg);
202
+ err = 1;
203
+ }
204
+ } else { // another input file
205
+ FILE *tmp_f;
206
+ if (!(tmp_f = fopen(arg, "rb"))) {
207
+ fprintf(stderr, "Unable to open %s for reading\n", arg);
208
+ err = 1;
209
+ } else {
210
+ fclose(tmp_f);
211
+ struct string_list *tmp = calloc(1, sizeof(**next_input_filename));
212
+ if (!tmp)
213
+ fprintf(stderr, "Out of memory!\n");
214
+ else {
215
+ tmp->value = (char *)arg;
216
+ *next_input_filename = tmp;
217
+ next_input_filename = &tmp->next;
218
+ }
219
+ }
220
+ }
221
+ } else {
222
+ err = 1;
223
+ fprintf(stderr, "Unrecognized option: %s\n", arg);
224
+ }
225
+ }
226
+
227
+ if (!data.in || !data.input_filename) {
228
+ #ifdef NO_STDIN
229
+ fprintf(stderr, "Please specify an input file\n");
230
+ err = 1;
231
+ #else
232
+ data.in = stdin;
233
+ #endif
234
+ }
235
+
236
+ if (!my_sql && !data.join_indexes) {
237
+ fprintf(stderr, "No sql command specified\n");
238
+ err = 1;
239
+ }
240
+
241
+ if (err) {
242
+ zsv_sql_cleanup(&data);
243
+ return 1;
244
+ }
245
+
246
+ if (data.in != stdin) {
247
+ fclose(data.in);
248
+ data.in = NULL;
249
+ }
250
+
251
+ FILE *f = NULL;
252
+ char *tmpfn = NULL;
253
+ if (data.input_filename) {
254
+ f = fopen(data.input_filename, "rb");
255
+ if (!f)
256
+ fprintf(stderr, "Unable to open %s for reading\n", data.input_filename);
257
+ } else
258
+ f = stdin;
259
+
260
+ if (f == stdin) {
261
+ tmpfn = zsv_get_temp_filename("zsv_sql_XXXXXXXX");
262
+ if (!tmpfn) {
263
+ fprintf(stderr, "Unable to create temp file name\n");
264
+ } else {
265
+ FILE *tmpf = fopen(tmpfn, "wb");
266
+ if (!tmpf)
267
+ fprintf(stderr, "Unable to open temp file %s\n", tmpfn);
268
+ else {
269
+ char rbuff[1024];
270
+ size_t bytes_read;
271
+ while ((bytes_read = fread(rbuff, 1, sizeof(rbuff), stdin)))
272
+ fwrite(rbuff, 1, bytes_read, tmpf);
273
+ f = tmpf;
274
+ }
275
+ }
276
+ }
277
+
278
+ zsv_csv_writer cw = zsv_writer_new(&writer_opts);
279
+ if (f && cw) {
280
+ fclose(f); // to do: don't open in the first place
281
+ f = NULL;
282
+
283
+ unsigned char cw_buff[1024];
284
+ zsv_writer_set_temp_buff(cw, cw_buff, sizeof(cw_buff));
285
+
286
+ struct zsv_sqlite3_dbopts dbopts = {
287
+ .in_memory = data.in_memory,
288
+ };
289
+ struct zsv_sqlite3_db *zdb = zsv_sqlite3_db_new(&dbopts);
290
+ if (zdb && zdb->rc == SQLITE_OK) {
291
+ const char *csv_filename = tmpfn ? (const char *)tmpfn : data.input_filename;
292
+
293
+ // for simplicity, we assume the same opts and custom_prop_handler for every input
294
+ // it may be desirable later to make this customizable for each input
295
+ if (zsv_sqlite3_add_csv(zdb, csv_filename, opts, custom_prop_handler) == SQLITE_OK) {
296
+ for (struct string_list *sl = data.more_input_filenames; sl; sl = sl->next)
297
+ if (zsv_sqlite3_add_csv(zdb, sl->value, opts, custom_prop_handler) != SQLITE_OK)
298
+ break;
299
+ }
300
+
301
+ if (zdb->rc == SQLITE_OK && data.join_indexes) { // get column names, and construct the sql
302
+ // sql template:
303
+ // select t1.*, t2.*, t3.* from t1 left join (select * from t2 group by a) t2 left join (select * from t3
304
+ // group by a) t3 using(a);
305
+ sqlite3_stmt *stmt = NULL;
306
+ const char *prefix_search = NULL;
307
+ const char *prefix_end = NULL;
308
+ if (my_sql) {
309
+ prefix_search = " from data ";
310
+ prefix_end = strstr(my_sql, prefix_search);
311
+ if (!prefix_end) {
312
+ prefix_search = " from data";
313
+ prefix_end = strstr(my_sql, prefix_search);
314
+ if (prefix_end && (prefix_end + strlen(prefix_search) != my_sql + strlen(my_sql)))
315
+ prefix_end = NULL;
316
+ }
317
+ if (!prefix_end || !prefix_search) {
318
+ err = 1;
319
+ fprintf(stderr, "Invalid sql: must contain 'from data'");
320
+ }
321
+ }
322
+
323
+ if (!err) {
324
+ zdb->rc = sqlite3_prepare_v2(zdb->db, "select * from data", -1, &stmt, NULL);
325
+ if (zdb->rc != SQLITE_OK) {
326
+ fprintf(stderr, "%s:\n %s\n (or bad CSV/utf8 input)\n\n", sqlite3_errstr(err), "select * from data");
327
+ err = 1;
328
+ }
329
+ }
330
+ if (!err && stmt) {
331
+ struct string_list **next_joined_column_name = &data.join_column_names;
332
+ int col_count = sqlite3_column_count(stmt);
333
+ for (char *ix_str = data.join_indexes; !err && ix_str && *ix_str && *(++ix_str);
334
+ ix_str = strchr(ix_str + 1, ',')) {
335
+ unsigned int next_ix;
336
+ if (sscanf(ix_str, "%u,", &next_ix) == 1) {
337
+ if (next_ix == 0)
338
+ fprintf(stderr, "--join-indexes index must be greater than zero\n");
339
+ else if (next_ix > (unsigned)col_count)
340
+ fprintf(stderr, "Column %u out of range; input has only %i columns\n", next_ix, col_count), err = 1;
341
+ else if (!sqlite3_column_name(stmt, next_ix - 1))
342
+ fprintf(stderr, "Column %u unexpectedly missing name\n", next_ix);
343
+ else {
344
+ struct string_list *tmp = calloc(1, sizeof(**next_joined_column_name));
345
+ if (!tmp)
346
+ fprintf(stderr, "Out of memory!\n"), err = 1;
347
+ else {
348
+ tmp->value = strdup(sqlite3_column_name(stmt, next_ix - 1));
349
+ *next_joined_column_name = tmp;
350
+ next_joined_column_name = &tmp->next;
351
+ }
352
+ }
353
+ }
354
+ }
355
+
356
+ if (!data.more_input_filenames)
357
+ fprintf(stderr, "--join-indexes requires more than one input\n"), err = 1;
358
+ else if (!err) { // now build the join select
359
+ sqlite3_str *select_clause = sqlite3_str_new(zdb->db);
360
+ sqlite3_str *from_clause = sqlite3_str_new(zdb->db);
361
+ sqlite3_str *group_by_clause = sqlite3_str_new(zdb->db);
362
+
363
+ sqlite3_str_appendf(select_clause, "data.*");
364
+ sqlite3_str_appendf(from_clause, "data");
365
+
366
+ for (struct string_list *sl = data.join_column_names; sl; sl = sl->next) {
367
+ if (sl != data.join_column_names)
368
+ sqlite3_str_appendf(group_by_clause, ",");
369
+ sqlite3_str_appendf(group_by_clause, "\"%w\"", sl->value);
370
+ }
371
+
372
+ int i = 2;
373
+ for (struct string_list *sl = data.more_input_filenames; sl; sl = sl->next, i++) {
374
+ sqlite3_str_appendf(select_clause, ", data%i.*", i);
375
+ // left join (select * from t2 group by a) t2 using(x,...)
376
+ sqlite3_str_appendf(from_clause, " left join (select * from data%i group by %s) data%i", i,
377
+ sqlite3_str_value(group_by_clause), i);
378
+ sqlite3_str_appendf(from_clause, " using (%s)", sqlite3_str_value(group_by_clause));
379
+ }
380
+
381
+ if (!prefix_end || !prefix_search)
382
+ asprintf(&data.sql_dynamic, "select %s from %s", sqlite3_str_value(select_clause),
383
+ sqlite3_str_value(from_clause));
384
+ else {
385
+ asprintf(&data.sql_dynamic, "%.*s from %s%s%s", (int)(prefix_end - my_sql), my_sql,
386
+ sqlite3_str_value(from_clause), strlen(prefix_end + strlen(prefix_search)) ? " " : "",
387
+ strlen(prefix_end + strlen(prefix_search)) ? prefix_end + strlen(prefix_search) : "");
388
+ }
389
+
390
+ my_sql = data.sql_dynamic;
391
+ if (opts->verbose)
392
+ fprintf(stderr, "Join sql:\n%s\n", my_sql);
393
+ sqlite3_free(sqlite3_str_finish(select_clause));
394
+ sqlite3_free(sqlite3_str_finish(from_clause));
395
+ sqlite3_free(sqlite3_str_finish(group_by_clause));
396
+ }
397
+ }
398
+ if (stmt)
399
+ sqlite3_finalize(stmt);
400
+ }
401
+
402
+ if (zdb->rc == SQLITE_OK && !err && my_sql) {
403
+ sqlite3_stmt *stmt;
404
+ err = sqlite3_prepare_v2(zdb->db, my_sql, -1, &stmt, NULL);
405
+ if (err != SQLITE_OK)
406
+ fprintf(stderr, "%s:\n %s\n (or bad CSV/utf8 input)\n\n", sqlite3_errstr(err), my_sql);
407
+ else {
408
+
409
+ int col_count = sqlite3_column_count(stmt);
410
+
411
+ // write header row
412
+ for (int i = 0; i < col_count; i++) {
413
+ const char *colname = sqlite3_column_name(stmt, i);
414
+ zsv_writer_cell(cw, !i, (const unsigned char *)colname, colname ? strlen(colname) : 0, 1);
415
+ }
416
+
417
+ // write sql results
418
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
419
+ for (int i = 0; i < col_count; i++) {
420
+ const unsigned char *text = sqlite3_column_text(stmt, i);
421
+ int len = text ? sqlite3_column_bytes(stmt, i) : 0;
422
+ zsv_writer_cell(cw, !i, text, len, 1);
423
+ }
424
+ }
425
+ sqlite3_finalize(stmt);
426
+ }
427
+ }
428
+ err = 1;
429
+ if (zdb->err_msg)
430
+ fprintf(stderr, "Error: %s\n", zdb->err_msg);
431
+ else if (!zdb->db)
432
+ fprintf(stderr, "Error (unable to open db, code %i): %s\n", zdb->rc, sqlite3_errstr(zdb->rc));
433
+ else if (zdb->rc != SQLITE_OK)
434
+ fprintf(stderr, "Error (code %i): %s\n", zdb->rc, sqlite3_errstr(zdb->rc));
435
+ else
436
+ err = 0;
437
+
438
+ zsv_sqlite3_db_delete(zdb);
439
+ }
440
+ }
441
+ if (f)
442
+ fclose(f);
443
+ zsv_sql_finalize(&data);
444
+ zsv_sql_cleanup(&data);
445
+ zsv_writer_delete(cw);
446
+
447
+ if (tmpfn) {
448
+ unlink(tmpfn);
449
+ free(tmpfn);
450
+ }
451
+ }
452
+ return err;
453
+ }
@@ -0,0 +1,101 @@
1
+ #include "sql_internal.h"
2
+
3
+ struct zsv_sqlite3_db *zsv_sqlite3_db_new(struct zsv_sqlite3_dbopts *dbopts) {
4
+ struct zsv_sqlite3_db *zdb = calloc(1, sizeof(*zdb));
5
+ if (!zdb) {
6
+ perror(NULL);
7
+ return NULL;
8
+ }
9
+ const char *db_url = dbopts && dbopts->in_memory ? "file::memory:" : "";
10
+ int flags = SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE;
11
+ zdb->rc = sqlite3_open_v2(db_url, &zdb->db, flags, NULL);
12
+ if (zdb->rc == SQLITE_OK && zdb->db)
13
+ zdb->rc = sqlite3_create_module(zdb->db, "csv", &CsvModule, 0);
14
+ if (zdb->rc != SQLITE_OK && !zdb->err_msg)
15
+ zdb->err_msg = strdup(sqlite3_errstr(zdb->rc));
16
+ return zdb;
17
+ }
18
+
19
+ void zsv_sqlite3_db_delete(struct zsv_sqlite3_db *zdb) {
20
+ if (zdb) {
21
+ for (struct zsv_sqlite3_csv_file *next, *zcf = zdb->csv_files; zcf; zcf = next) {
22
+ next = zcf->next;
23
+ if (zcf->added)
24
+ sqlite3_zsv_list_remove(zcf->path);
25
+ free(zcf->path);
26
+ free(zcf);
27
+ }
28
+ if (zdb->db)
29
+ sqlite3_close(zdb->db);
30
+ free(zdb);
31
+ }
32
+ }
33
+
34
+ static int create_virtual_csv_table(const char *fname, sqlite3 *db, // int max_columns,
35
+ char **err_msgp, int table_ix) {
36
+ // TO DO: set customizable maximum number of columns to prevent
37
+ // runaway in case no line ends found
38
+ char *sql = NULL;
39
+ char table_name_suffix[64];
40
+
41
+ if (table_ix == 0)
42
+ *table_name_suffix = '\0';
43
+ else if (table_ix < 0 || table_ix > 1000)
44
+ return -1;
45
+ else
46
+ snprintf(table_name_suffix, sizeof(table_name_suffix), "%i", table_ix + 1);
47
+
48
+ sql = sqlite3_mprintf("CREATE VIRTUAL TABLE data%s USING csv(filename=%Q)", table_name_suffix, fname);
49
+
50
+ char *err_msg_tmp;
51
+ int rc = sqlite3_exec(db, sql, NULL, NULL, &err_msg_tmp);
52
+ if (err_msg_tmp) {
53
+ *err_msgp = strdup(err_msg_tmp);
54
+ sqlite3_free(err_msg_tmp);
55
+ }
56
+ sqlite3_free(sql);
57
+ return rc;
58
+ }
59
+
60
+ int zsv_sqlite3_add_csv(struct zsv_sqlite3_db *zdb, const char *csv_filename, struct zsv_opts *opts,
61
+ struct zsv_prop_handler *custom_prop_handler) {
62
+ struct zsv_sqlite3_csv_file *zcf = calloc(1, sizeof(*zcf));
63
+ if (!zcf || !(zcf->path = strdup(csv_filename)))
64
+ zdb->rc = SQLITE_ERROR;
65
+ else {
66
+ int err = 0;
67
+ if (opts) {
68
+ err = sqlite3_zsv_list_add(csv_filename, opts, custom_prop_handler);
69
+ if (!err)
70
+ zcf->added = 1;
71
+ }
72
+ if (err)
73
+ zdb->rc = SQLITE_ERROR;
74
+ else {
75
+ zcf->next = zdb->csv_files;
76
+ zdb->csv_files = zcf;
77
+ zdb->rc = create_virtual_csv_table(csv_filename, zdb->db, &zdb->err_msg, zdb->table_count);
78
+ if (zdb->rc == SQLITE_OK) {
79
+ zdb->table_count++;
80
+ return SQLITE_OK;
81
+ }
82
+ }
83
+ }
84
+ if (zcf) {
85
+ free(zcf->path);
86
+ free(zcf);
87
+ }
88
+ return zdb->rc;
89
+ }
90
+
91
+ /**
92
+ * zsv_sqlite3_add_csv_no_dq is the same as zsv_sqlite3_add_csv() but also executes
93
+ * sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 0, (void*)0);
94
+ */
95
+ int zsv_sqlite3_add_csv_no_dq(struct zsv_sqlite3_db *zdb, const char *csv_filename, struct zsv_opts *opts,
96
+ struct zsv_prop_handler *custom_prop_handler) {
97
+ int rc = zsv_sqlite3_add_csv(zdb, csv_filename, opts, custom_prop_handler);
98
+ if (rc == SQLITE_OK)
99
+ sqlite3_db_config(zdb->db, SQLITE_DBCONFIG_DQS_DDL, 0, (void *)0);
100
+ return rc;
101
+ }
@@ -0,0 +1,49 @@
1
+ #ifndef SQL_INTERNAL_H
2
+ #define SQL_INTERNAL_H
3
+
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+ #include "external/sqlite3/sqlite3.h"
7
+ #include "external/sqlite3/sqlite3_csv_vtab-mem.h"
8
+ #include <zsv/utils/prop.h>
9
+ #include <zsv/utils/string.h>
10
+
11
+ extern sqlite3_module CsvModule;
12
+
13
+ struct zsv_sqlite3_csv_file {
14
+ struct zsv_sqlite3_csv_file *next;
15
+ char *path;
16
+ unsigned char added : 1;
17
+ unsigned char _ : 7;
18
+ };
19
+
20
+ struct zsv_sqlite3_db {
21
+ sqlite3 *db;
22
+ int table_count;
23
+ char *err_msg;
24
+ struct zsv_sqlite3_csv_file *csv_files;
25
+ int rc;
26
+ };
27
+
28
+ #include "../include/zsv/utils/sql.h"
29
+
30
+ struct zsv_sqlite3_db *zsv_sqlite3_db_new(struct zsv_sqlite3_dbopts *dbopts);
31
+ void zsv_sqlite3_db_delete(struct zsv_sqlite3_db *zdb);
32
+
33
+ /**
34
+ * @param opts: if non-null, opts and custom_prop_handler will be saved for use by the sqlite3 csvModule
35
+ * and the saved entry will be removed by zsv_sqlite3_db_delete()
36
+ * if NULL, csvModule will rely on any previously saved opts/custom_prop_handler
37
+ */
38
+ int zsv_sqlite3_add_csv(struct zsv_sqlite3_db *zdb, const char *csv_filename, struct zsv_opts *opts,
39
+ struct zsv_prop_handler *custom_prop_handler);
40
+
41
+ /**
42
+ * zsv_sqlite3_add_csv_no_dq is the same as zsv_sqlite3_add_csv() but also disables double-quoted
43
+ * values from being interpreted as literals by executing the below:
44
+ * sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 0, (void*)0);
45
+ */
46
+ int zsv_sqlite3_add_csv_no_dq(struct zsv_sqlite3_db *zdb, const char *csv_filename, struct zsv_opts *opts,
47
+ struct zsv_prop_handler *custom_prop_handler);
48
+
49
+ #endif