anchor.diff:  (cl:defpackage #:sykosomatic.db
  -  (:use :cl :alexandria :postmodern)
  -  (:export :id :defdao :init-db :with-db :dblog :rebuild-table
  +  (:use :cl :alexandria :postmodern
  +        :sykosomatic.util)
  +  (:export :id :defdao :init-db
  +           :with-db
  +           :get-connection
  +           :done-with-connection
  +           :dblog :rebuild-table
              :rebuild :drop-table :drop-all-tables
              :assert-validation :with-validation :assert-required))
   (cl:in-package #:sykosomatic.db)
  
positive.code:               when form
                collect form)))))
  
  (defmacro with-db (() &body body)
    `(let ((reusing-connection-p *database*)
           (*database* (or *database*
                           (apply #'connect (list *db-name* *db-user* *db-password* *db-host*)))))
       (unwind-protect (progn ,@body)
         (unless reusing-connection-p
           (disconnect *database*)))))
  
  (defun drop-table (symbol)
    (with-db ()
      (query (format nil "drop table if exists ~A" (sql-compile symbol)))))
  
negative.code: 
  (defgeneric id (dao))
  
  ;;;
  ;;; Connection pooling
  ;;;
  (defparameter *connection-pool-lock* (bt:make-lock))
  (defparameter *max-pooled-connections* 50)
  (defvar *connection-pool* (make-queue *max-pooled-connections*))
  
  (defun get-connection ()
    (or *database*
        (bt:with-lock-held (*connection-pool-lock*)
          (unless (queue-empty-p *connection-pool*)
            (dequeue *connection-pool*)))
        (apply #'connect (list *db-name* *db-user* *db-password* *db-host*))))
  
  (defun done-with-connection (connection)
    (bt:with-lock-held (*connection-pool-lock*)
      (if (queue-full-p *connection-pool*)
          (disconnect connection)
          (enqueue connection *connection-pool*))))
  
  (defun clear-pooled-connections ()
    (bt:with-lock-held (*connection-pool-lock*)
      (loop until (queue-empty-p *connection-pool*)
         for connection = (dequeue *connection-pool*)
         when (connected-p connection)
         do (disconnect connection))))
  
  (defmacro with-db ((&key (reusep t)) &body body)
    `(let* ((reusing-connection-p (and *database* ,reusep))
            (*database* (or (when reusing-connection-p *database*)
                            (get-connection))))
       (unwind-protect (progn ,@body)
         (unless reusing-connection-p
           (done-with-connection *database*)))))
  
  ;; Forms
  (defvar *validation-errors*)
  
  