Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,16 @@ Column names with spaces work — quote them in SQL:
$ cat report.csv | sql-pipe 'SELECT "first name", "last name" FROM t WHERE "dept id" = "42"'
```

Use a custom input delimiter with `-d` / `--delimiter` (single character), or `--tsv` for tab-separated files:
Use a custom input delimiter with `-d` / `--delimiter` (1–8 characters), or `--tsv` for tab-separated files:

```sh
$ cat data.psv | sql-pipe -d '|' 'SELECT * FROM t'
$ cat data.tsv | sql-pipe --tsv 'SELECT * FROM t'
# equivalent:
$ cat data.tsv | sql-pipe --delimiter '\t' 'SELECT * FROM t'
# multi-character delimiters:
$ cat data.psv | sql-pipe -d '||' 'SELECT * FROM t'
$ cat report.txt | sql-pipe --delimiter ' ' 'SELECT * FROM t' # two spaces
```

Output results as a JSON array of objects with `--json`:
Expand Down
34 changes: 34 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,40 @@ pub fn build(b: *std.Build) void {
test_sample_fewer_rows.step.dependOn(b.getInstallStep());
test_step.dependOn(&test_sample_fewer_rows.step);

// Integration test 95: 2-char delimiter (||) splits fields correctly
const test_delimiter_double_pipe = b.addSystemCommand(&.{
"bash", "-c",
\\printf 'name||age\nAlice||30\nBob||25\n' | ./zig-out/bin/sql-pipe --delimiter '||' 'SELECT name, age FROM t ORDER BY age' | diff - <(printf 'Bob,25\nAlice,30\n')
});
test_delimiter_double_pipe.step.dependOn(b.getInstallStep());
test_step.dependOn(&test_delimiter_double_pipe.step);

// Integration test 96: 3-char delimiter (;;;) splits fields correctly
const test_delimiter_three_char = b.addSystemCommand(&.{
"bash", "-c",
\\printf 'name;;;age\nAlice;;;30\nBob;;;25\n' | ./zig-out/bin/sql-pipe --delimiter ';;;' 'SELECT name, age FROM t ORDER BY age' | diff - <(printf 'Bob,25\nAlice,30\n')
});
test_delimiter_three_char.step.dependOn(b.getInstallStep());
test_step.dependOn(&test_delimiter_three_char.step);

// Integration test 97: empty delimiter string exits 1 with error
const test_delimiter_empty_error = b.addSystemCommand(&.{
"bash", "-c",
\\msg=$(printf 'a,b\n1,2\n' | ./zig-out/bin/sql-pipe -d '' 'SELECT * FROM t' 2>&1 >/dev/null; echo "EXIT:$?")
\\echo "$msg" | grep -q 'EXIT:1'
});
test_delimiter_empty_error.step.dependOn(b.getInstallStep());
test_step.dependOn(&test_delimiter_empty_error.step);

// Integration test 98: delimiter longer than 8 chars exits 1 with error
const test_delimiter_too_long_error = b.addSystemCommand(&.{
"bash", "-c",
\\msg=$(printf 'a,b\n1,2\n' | ./zig-out/bin/sql-pipe -d '123456789' 'SELECT * FROM t' 2>&1 >/dev/null; echo "EXIT:$?")
\\echo "$msg" | grep -q 'EXIT:1'
});
test_delimiter_too_long_error.step.dependOn(b.getInstallStep());
test_step.dependOn(&test_delimiter_too_long_error.step);

// Unit tests for the RFC 4180 CSV parser (src/csv.zig)
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
Expand Down
9 changes: 5 additions & 4 deletions docs/sql-pipe.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ DESCRIPTION
behavior and treat all columns as TEXT.

By default, input fields are parsed as comma-separated values. Use
*--delimiter* (or *-d*) to parse other single-character delimiters, or *--tsv*
*--delimiter* (or *-d*) to parse other delimiters (1–8 characters), or *--tsv*
for tab-separated input.

Exit codes follow a structured convention for scripting integration (see EXIT CODES
section).

OPTIONS
*-d, --delimiter* <char>
Use a custom single-character input field delimiter instead of comma.
*-d, --delimiter* <string>
Use a custom input field delimiter (1–8 bytes) instead of comma.
Examples: *-d '|'* for pipe-separated files, *--delimiter '\\t'* for
tab-separated files.
tab-separated files, *-d '||'* for double-pipe-separated files.
Produces a usage error if the value is empty or longer than 8 bytes.

*--tsv*
Alias for *--delimiter '\\t'*. Parses tab-separated input.
Expand Down
Loading
Loading