Skip to content

Commit 8337d23

Browse files
authored
docs(prqlc-c): replace stale Go cgo snippet, fix linker flags (#5956)
1 parent af75a8c commit 8337d23

1 file changed

Lines changed: 16 additions & 74 deletions

File tree

prqlc/bindings/prqlc-c/README.md

Lines changed: 16 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,28 @@ allows embedding in languages that support FFI — for example, Golang.
77

88
## Linking
99

10-
See [examples/minimal-c/Makefile](examples/minimal-c/Makefile).
10+
See [examples/minimal-c/Makefile](examples/minimal-c/Makefile) for the canonical
11+
link flags used to build the bundled C example.
1112

12-
Copy the `.a` and `.so` files in a convenient place and add the following
13-
compile flags to Go (cgo):
13+
To link against the static library from another build system, point the linker
14+
at the directory containing `libprqlc_c.a` and add `prqlc_c` plus its system
15+
dependencies, for example:
1416

15-
`CGO_LDFLAGS="-L/path/to/libprqlc_c.a -lprqlc -pthread -ldl" go build`
17+
`CGO_LDFLAGS="-L/path/to/target/release -lprqlc_c -pthread -ldl -lm" go build`
1618

17-
## Examples
18-
19-
For a minimal example, see
20-
[examples/minimal-c/main.c](examples/minimal-c/main.c).
21-
22-
Below is an example from an actual application that is using PRQL in Go.
23-
24-
```go
25-
package prql
26-
27-
/*
28-
29-
30-
#include <stdlib.h>
31-
32-
int to_sql(char *prql_query, char *sql_query);
33-
int to_json(char *prql_query, char *json_query);
34-
35-
*/
36-
import "C"
19+
(On macOS, also add `-framework CoreFoundation`.)
3720

38-
import (
39-
"errors"
40-
"strings"
41-
"unsafe"
42-
)
43-
44-
// ToSQL converts a PRQL query to SQL
45-
func ToSQL(prql string) (string, error) {
46-
// buffer length should not be less than 1K because we may get an error
47-
// from the PRQL compiler with a very short query
48-
cStringBufferLength := 1024
49-
50-
// allocate a buffer 3 times the length of the PRQL query to store the
51-
// generated SQL query
52-
if len(prql)*3 > cStringBufferLength {
53-
cStringBufferLength = len(prql) * 3
54-
}
55-
56-
// preallocate the buffer
57-
cstr := C.CString(strings.Repeat(" ", cStringBufferLength))
58-
defer C.free(unsafe.Pointer(cstr))
59-
60-
// convert the PRQL query to SQL
61-
res := C.to_sql(C.CString(prql), cstr)
62-
if res == 0 {
63-
return C.GoString(cstr), nil
64-
}
65-
66-
return "", errors.New(C.GoString(cstr))
67-
}
68-
69-
// ToJSON converts a PRQL query to JSON
70-
func ToJSON(prql string) (string, error) {
71-
// buffer length should not be less than 1K because we may get an error
72-
cStringBufferLength := 1024
73-
if len(prql)*3 > cStringBufferLength {
74-
cStringBufferLength = len(prql) * 10
75-
}
76-
77-
// preallocate the buffer
78-
cstr := C.CString(strings.Repeat(" ", cStringBufferLength))
79-
defer C.free(unsafe.Pointer(cstr))
21+
## Examples
8022

81-
// convert the PRQL query to SQL
82-
res := C.to_json(C.CString(prql), cstr)
83-
if res == 0 {
84-
return C.GoString(cstr), nil
85-
}
23+
- [examples/minimal-c/main.c](examples/minimal-c/main.c) — minimal C example
24+
covering `compile`, custom `Options`, error handling, and the staged
25+
`prql_to_pl` / `pl_to_rq` entry points.
26+
- [examples/minimal-cpp](examples/minimal-cpp) — the same flow using the
27+
generated C++ header.
28+
- [examples/minimal-zig](examples/minimal-zig) — a Zig example using `@cImport`
29+
against `prqlc.h`.
8630

87-
return "", errors.New(C.GoString(cstr))
88-
}
89-
```
31+
The full FFI surface is documented inline in [prqlc.h](prqlc.h).
9032

9133
## Development
9234

0 commit comments

Comments
 (0)