                      PrintWriter    BufferedWriter OutputStreamWriter
                      DataInputStream  BufferedInputStream
                      DataOutputStream BufferedOutputStream)))
-
-(defmacro- safe [& body]
-  `(try ~@body (catch Exception e# e#)))
 
 (defn- embedded-query [dba q]
   (let [[q-type q-opts] q]

    (if (sequential? result) (vec result) result)))

(defn- process-query [dba q]
  (safe
    (or (embedded-query dba q)
        (core-query dba q))))

(defn- text-read-query [#^PushbackReader in eof-val]
  (safe
    (binding [*read-eval* false]
      (read in false eof-val))))

(defn- text-write-exception [#^PrintWriter out e]
  (if (raised? e)
    (.println out (str e))
    (stacktrace/pst-on out false e))
  (.println out)
  (.flush out))

(defn- text-write-result [#^PrintWriter out result]
  (.println out (prn-str result))
  (.flush out))

(defn- text-handler [dba #^Socket socket]
  (try

                in     (DataInputStream.  (BufferedInputStream.  (.getInputStream  socket)))]
      (.setKeepAlive socket true)
      (loop []
        (try
          (let [query  (io/dis-read in io/eof)
                result (process-query dba query)]
            (io/dos-write out [0 result]))
          (catch Exception e
            (io/dos-write out [1 (stacktrace/pst-str e)])))
        (recur)))
    (catch Exception e
      (stacktrace/pst-on System/err false e)
      (.println System/err))))
