     (cancel-timer emacsql-reap-timer)
     (setf emacsql-reap-timer nil)))
 
-(defun emacsql--log (emacsql &rest messages)
-  (let ((log (emacsql-log emacsql)))
+(defun emacsql--log (conn &rest messages)
+  (let ((log (emacsql-log conn)))
     (when log
       (with-current-buffer log
         (setf (point) (point-max))
         (mapc (lambda (s) (princ s log)) messages)))))
 
-(defun emacsql--send (emacsql string)
-  "Send STRING to EMACSQL, automatically appending newline."
-  (let ((process (emacsql-process emacsql)))
-    (emacsql--log emacsql string "\n")
+(defun emacsql--send (conn string)
+  "Send STRING to CONN, automatically appending newline."
+  (let ((process (emacsql-process conn)))
+    (emacsql--log conn string "\n")
     (process-send-string process string)
     (process-send-string process "\n")))
 
-(defun emacsql--clear (emacsql)
-  "Clear the process buffer for EMACSQL."
-  (with-current-buffer (emacsql-buffer emacsql)
+(defun emacsql--clear (conn)
+  "Clear the process buffer for CONN."
+  (with-current-buffer (emacsql-buffer conn)
     (erase-buffer)))
 
-(defun emacsql--complete-p (emacsql)
+(defun emacsql--complete-p (conn)
   "Return non-nil if receive buffer has finished filling."
-  (with-current-buffer (emacsql-buffer emacsql)
+  (with-current-buffer (emacsql-buffer conn)
     (cond ((= (buffer-size) 1) (string= "#" (buffer-string)))
           ((> (buffer-size) 1) (string= "\n#"
                                         (buffer-substring
                                          (- (point-max) 2) (point-max)))))))
 
-(defun emacsql--parse (emacsql &rest named)
+(defun emacsql--parse (conn &rest named)
   "Parse a query result into an s-expression.
 If NAMED is non-nil, don't include column names."
-  (with-current-buffer (emacsql-buffer emacsql)
+  (with-current-buffer (emacsql-buffer conn)
     (let ((standard-input (current-buffer)))
       (setf (point) (point-min))
       (cl-loop until (looking-at "#")

        (prin1-to-string value)
      (emacsql-escape (prin1-to-string value) t))))

(defun emacsql-insert (emacsql table &rest rows)
  "Insert ROWS into TABLE.
Each row must be a sequence of values to store into TABLE.

  (emacsql-insert db :table '(\"Chris\" 0) [\"Jeff\" 1])"
  (emacsql-with-errors emacsql
    (emacsql--send
     emacsql
     (format "INSERT INTO %s VALUES (%s);" (emacsql-escape table)
             (mapconcat (lambda (row)
                          (mapconcat #'emacsql-escape-value row ", "))
                        rows "), (")))))

(defun emacsql-select-raw (emacsql query)
  "Send a raw QUERY string to EMACSQL."
  (emacsql--clear emacsql)
  (emacsql--send emacsql query)
  (emacsql-wait emacsql)
  (emacsql--check-error emacsql)
  (emacsql--parse emacsql))

(provide 'emacsql)


    (setf (process-sentinel process) (lambda (_proc _) (kill-buffer buffer)))
    (process-send-string process ".prompt #\n")
    (process-send-string process ".mode line\n")
    (let ((conn (emacsql--create :process process :file file)))
      (when log
        (setf (emacsql-log conn) (generate-new-buffer "*emacsql-log*")))
      (prog1 conn
        (push (cons (copy-seq conn) (emacsql--ref conn))
              emacsql-connections)))))

(defun emacsql-close (conn)
  "Close connection to CONN database."
  (let ((process (emacsql-process conn)))
    (when (and process (process-live-p process))
      (process-send-string process ".exit\n"))))

(defun emacsql-buffer (conn)
  "Get proccess buffer for CONN."
  (process-buffer (emacsql-process conn)))

(defun emacsql-reap ()
  "Clean up after lost connections."
  (cl-loop for (conn-copy . ref) in emacsql-connections
           when (null (emacsql--deref ref))
           count (prog1 t (ignore-errors (emacsql-close emacsql-copy)))
           into total
           else collect (cons conn-copy ref) into connections
           finally (progn
                     (setf emacsql-connections connections)
                     (return total))))
