(import postgresql (chicken string) (chicken format) srfi-13)

(define conn (connect '((dbname . test))))
(define raw-conn (connect '((dbname . test)) '() '()))

(print "--- Connecting/disconnecting ---")
(print "Full connection")
(time (do ((i 0 (add1 i)))
          ((= i 1000))
        (disconnect (connect '((dbname . test))))))

(print "Raw connection")
(time (do ((i 0 (add1 i)))
          ((= i 1000))
        (disconnect (connect '((dbname . test)) '() '()))))

(let ((str (make-string 1000 #\x)))
  (newline)
  (print "---  Quotation ---")
  (print "Escaping many strings")
  (time (do ((i 0 (add1 i)))
            ((= i 100000))
          (escape-string conn str)))

  (print "Quoting many identifiers")
  (time (do ((i 0 (add1 i)))
            ((= i 100000))
          (quote-identifier conn str)))

  (print "Escaping many byteas")
  (time (do ((i 0 (add1 i)))
            ((= i 100000))
          (escape-bytea conn str)))

  (print "Unescaping many byteas")
  (let ((bytea (escape-bytea conn str)))
    (time (do ((i 0 (add1 i)))
              ((= i 100000))
            (unescape-bytea bytea)))))

(begin (newline)
       (print "---  Reading data ---")
       (print "Raw query with no result processing (postgres/libpq benchmark)")
       (time (query conn "SELECT generate_series(0, 100000)"))

       (newline)
       (print "Fetching all values, no type conversion (string)")
       (time (row-for-each (lambda (values) #f)
                           (query raw-conn "SELECT generate_series(0, 1000000)::text")))
       (print "Fetching all values, with type conversion (string)")
       (time (row-for-each (lambda (values) #f)
                           (query conn "SELECT generate_series(0, 1000000)::text")))

       (newline)
       (print "Fetching all values, no type conversion (integer)")
       (time (row-for-each (lambda (values) #f)
                           (query raw-conn "SELECT generate_series(0, 1000000)")))
       (print "Fetching all values, with type conversion (integer)")
       (time (row-for-each (lambda (values) #f)
                           (query conn "SELECT generate_series(0, 1000000)")))

       (newline)
       (print "COPY TO")
       (time (copy-query-for-each (lambda (values) #f)
                                  raw-conn "COPY (SELECT generate_series(0, 1000000)) TO STDOUT")))

(begin (newline)
       (print "--- Inserting data ---")
       (query raw-conn "CREATE TEMPORARY TABLE foo (bar int)")
       (print "INSERT in a loop, no type conversion")
       (time (do ((i 0 (add1 i)))
                 ((= i 65535))
               (query raw-conn "INSERT INTO foo VALUES ($1)" (->string i))))
       (query raw-conn "TRUNCATE foo")

       (query conn "CREATE TEMPORARY TABLE foo (bar int)")
       (print "INSERT in a loop, with type conversion")
       (time (do ((i 0 (add1 i)))
                 ((= i 65535))
               (query conn "INSERT INTO foo VALUES ($1)" i)))
       (query conn "TRUNCATE foo")

       (newline)
       
       (print "Parameterized INSERT statement, no type conversion")
       (time (do ((i 0 (add1 i))
                  (values '() (cons (->string i) values))
                  (values-string '() (cons (sprintf "($~A)" (add1 i)) values-string)))
                 ((= i 65535)
                  (query* raw-conn (sprintf "INSERT INTO foo VALUES ~A"
                                     (string-join values-string ","))
                          values))))
       (query raw-conn "TRUNCATE foo")

       (print "Parameterized INSERT statement, with type conversion")
       (time (do ((i 0 (add1 i))
                  (values '() (cons i values))
                  (values-string '() (cons (sprintf "($~A)" (add1 i)) values-string)))
                 ((= i 65535)
                  (query* conn (sprintf "INSERT INTO foo VALUES ~A"
                                 (string-join values-string ","))
                          values))))
       (query conn "TRUNCATE foo")

       (newline)

       (print "Unparameterized INSERT statement")
       (time (do ((i 0 (add1 i))
                  (values-string '() (cons (sprintf "(~A)" i) values-string)))
                 ((= i 65535)
                  (query raw-conn (sprintf "INSERT INTO foo VALUES ~A"
                                    (string-join values-string ","))))))
       (query raw-conn "TRUNCATE foo")

       (newline)
       (print "COPY FROM")
       (time (call-with-output-copy-query
              (lambda (p)
                (do ((i 0 (add1 i)))
                    ((= i 65535))
                  (display i p)
                  (newline p)))
              raw-conn "COPY foo (bar) FROM STDIN"))
       (query raw-conn "TRUNCATE foo"))

(disconnect raw-conn)
(disconnect conn)