Skip to content

Commit

Permalink
Use newer API for fetching data chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
ak-coram committed Aug 16, 2024
1 parent a5d9538 commit 4c7e7c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
27 changes: 10 additions & 17 deletions duckdb-api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -331,28 +331,21 @@
(result p-duckdb-result)
(col idx))

(defcfun duckdb-result-chunk-count idx
(defcfun duckdb-fetch-chunk (duckdb-data-chunk)
(result (:struct duckdb-result)))

(defun result-chunk-count (p-result)
(duckdb-result-chunk-count (mem-ref p-result '(:struct duckdb-result))))

(defcfun duckdb-result-get-chunk (duckdb-data-chunk)
(result (:struct duckdb-result))
(chunk-index idx))

(defcfun duckdb-destroy-data-chunk :void
(chunk (:pointer duckdb-data-chunk)))

(defmacro with-data-chunk ((chunk-var result chunk-index) &body body)
`(let ((,chunk-var (duckdb-result-get-chunk
(mem-ref ,result '(:struct duckdb-result))
,chunk-index)))
(unwind-protect
(progn ,@body)
(with-foreign-object (p-chunk '(:pointer duckdb-data-chunk))
(setf (mem-ref p-chunk 'duckdb-data-chunk) ,chunk-var)
(duckdb-destroy-data-chunk p-chunk)))))
(defmacro with-data-chunk ((chunk-var result) &body body)
`(let ((,chunk-var (duckdb-fetch-chunk
(mem-ref ,result '(:struct duckdb-result)))))
(if (null-pointer-p ,chunk-var)
(let ((,chunk-var nil)) (progn ,@body))
(unwind-protect (progn ,@body)
(with-foreign-object (p-chunk '(:pointer duckdb-data-chunk))
(setf (mem-ref p-chunk 'duckdb-data-chunk) ,chunk-var)
(duckdb-destroy-data-chunk p-chunk))))))

(defun result-get-chunk (p-result i)
(duckdb-result-get-chunk (mem-ref p-result '(:struct duckdb-result)) i))
Expand Down
18 changes: 9 additions & 9 deletions duckdb.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -425,22 +425,22 @@ cleanup."

(defun translate-result (result &optional sql-null-return-value)
(let* ((p-result (handle result))
(chunk-count (duckdb-api:result-chunk-count p-result))
(column-count (duckdb-api:duckdb-column-count p-result))
(result-alist
(loop :for column-index :below column-count
:collect (cons (duckdb-api:duckdb-column-name p-result column-index)
(make-array '(0) :adjustable t :fill-pointer 0))))
column-types
(row-count
(loop :for chunk-index :below chunk-count
:sum (duckdb-api:with-data-chunk (chunk p-result chunk-index)
(multiple-value-bind (types chunk-size)
(translate-chunk result-alist
chunk
sql-null-return-value)
(setf column-types types)
chunk-size)))))
(loop :for n := (duckdb-api:with-data-chunk (chunk p-result)
(when chunk
(multiple-value-bind (types chunk-size)
(translate-chunk result-alist
chunk
sql-null-return-value)
(setf column-types types)
chunk-size)))
:while n :sum n)))
(values result-alist column-types row-count)))

(defun assert-parameter-count (statement values)
Expand Down

0 comments on commit 4c7e7c9

Please sign in to comment.