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 20, 2024
1 parent a5d9538 commit 6a2c1bb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
26 changes: 9 additions & 17 deletions duckdb-api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -331,28 +331,20 @@
(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)))))
(unwind-protect (progn ,@body)
(unless (null-pointer-p ,chunk-var)
(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
38 changes: 23 additions & 15 deletions duckdb.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
,@body))

(define-condition duckdb-error (simple-error)
((database :initarg :database)
(statement :initarg :statement)
(appender :initarg :appender)
(arrow :initarg :arrow)
((database :initarg :database :accessor database)
(statement :initarg :statement :accessor statement)
(appender :initarg :appender :accessor appender)
(arrow :initarg :arrow :accessor arrow)
(error-message :initarg :error-message
:accessor error-message))
(:report (lambda (condition stream)
Expand Down Expand Up @@ -241,8 +241,8 @@ The connection and the database are cleaned up after BODY is evaluated."
;;; Queries

(defclass result ()
((connection :initarg :connection)
(statement :initarg :statement)
((connection :accessor connection :initarg :connection)
(statement :accessor statement :initarg :statement)
(handle :accessor handle :initarg :handle)))

(defun make-result (connection statement p-result)
Expand Down Expand Up @@ -425,22 +425,30 @@ 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)
(if (null-pointer-p chunk)
(alexandria:when-let (error-message (duckdb-api:duckdb-result-error
p-result))
(destroy-result result)
(error 'duckdb-error
:database (database (connection result))
:statement (statement result)
:error-message error-message))
(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
8 changes: 7 additions & 1 deletion package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@
(defpackage #:duckdb
(:nicknames #:ddb)
(:use #:cl #:cffi)
(:export #:*connection*
(:export #:duckdb-error
#:database
#:statement
#:appender
#:arrow
#:error-message
#:*connection*
#:*threads*
#:*default-thread-count*
#:*sql-null-return-value*
Expand Down

0 comments on commit 6a2c1bb

Please sign in to comment.