<commit_before>;;; emacsql-pg.el --- Emacsql back-end for PostgreSQL via pg  -*- lexical-binding:t -*-

;; This is free and unencumbered software released into the public domain.

;; Author: <NAME> <<EMAIL>>
;; Maintainer: <NAME> <<EMAIL>>
;; Homepage: https://github.com/magit/emacsql
;; Version: 3.0.0-git
;; Package-Requires: ((emacs "25.1") (emacsql "2.0.0") (pg "0"))
;; SPDX-License-Identifier: Unlicense

;;; Commentary:

;; Unlike emacsql-psql, this connection type uses Eric Marsden's pg.el
;; to connect to PostgreSQL. It speaks directly to the database, so
;; unlike the other EmacSQL connection types, this one requires no
;; external command line programs.

;; The only pg functions required are pg:connect, pg:disconnect,
;; pg:exec, and pg:result. Unfortunately, since pg.el is synchronous
;; it will not be fully compliant once EmacSQL supports asynchronous
;; queries. But, on the plus side, this means the implementation below
;; is dead simple.

;;; Code:

(require 'pg)
(require 'eieio)
(require 'cl-lib)
(require 'cl-generic)
(require 'emacsql)
(require 'emacsql-psql)  ; for reserved words

(defclass emacsql-pg-connection (emacsql-connection)
  ((pgcon :reader emacsql-pg-pgcon :initarg :pgcon)
   (dbname :reader emacsql-pg-dbname :initarg :dbname)
   (result :accessor emacsql-pg-result)
   (types :allocation :class
          :reader emacsql-types
          :initform '((integer "BIGINT")
                      (float "DOUBLE PRECISION")
                      (object "TEXT")
                      (nil "TEXT"))))
  (:documentation "A connection to a PostgreSQL database via pg.el."))

(cl-defun emacsql-pg (dbname user &key
                             (host "localhost") (password "") (port 5432) debug)
  "Connect to a PostgreSQL server using pg.el."
  (let* ((pgcon (pg:connect dbname user password host port))
         (connection (make-instance 'emacsql-pg-connection
                                    :process (pgcon-process pgcon)
                                    :pgcon pgcon
                                    :dbname dbname)))
    (when debug (emacsql-enable-debugging connection))
    (emacsql connection [:set (= default-transaction-isolation 'SERIALIZABLE)])<commit_msg>No longer require emacsql-psql in emacsql-pg<commit_after>;;; emacsql-pg.el --- Emacsql back-end for PostgreSQL via pg  -*- lexical-binding:t -*-

;; This is free and unencumbered software released into the public domain.

;; Author: <NAME> <<EMAIL>>
;; Maintainer: <NAME> <<EMAIL>>
;; Homepage: https://github.com/magit/emacsql
;; Version: 3.0.0-git
;; Package-Requires: ((emacs "25.1") (emacsql "2.0.0") (pg "0"))
;; SPDX-License-Identifier: Unlicense

;;; Commentary:

;; Unlike emacsql-psql, this connection type uses Eric Marsden's pg.el
;; to connect to PostgreSQL. It speaks directly to the database, so
;; unlike the other EmacSQL connection types, this one requires no
;; external command line programs.

;; The only pg functions required are pg:connect, pg:disconnect,
;; pg:exec, and pg:result. Unfortunately, since pg.el is synchronous
;; it will not be fully compliant once EmacSQL supports asynchronous
;; queries. But, on the plus side, this means the implementation below
;; is dead simple.

;;; Code:

(require 'pg)
(require 'eieio)
(require 'cl-lib)
(require 'cl-generic)
(require 'emacsql)

(defclass emacsql-pg-connection (emacsql-connection)
  ((pgcon :reader emacsql-pg-pgcon :initarg :pgcon)
   (dbname :reader emacsql-pg-dbname :initarg :dbname)
   (result :accessor emacsql-pg-result)
   (types :allocation :class
          :reader emacsql-types
          :initform '((integer "BIGINT")
                      (float "DOUBLE PRECISION")
                      (object "TEXT")
                      (nil "TEXT"))))
  (:documentation "A connection to a PostgreSQL database via pg.el."))

(cl-defun emacsql-pg (dbname user &key
                             (host "localhost") (password "") (port 5432) debug)
  "Connect to a PostgreSQL server using pg.el."
  (let* ((pgcon (pg:connect dbname user password host port))
         (connection (make-instance 'emacsql-pg-connection
                                    :process (pgcon-process pgcon)
                                    :pgcon pgcon
                                    :dbname dbname)))
    (when debug (emacsql-enable-debugging connection))
    (emacsql connection [:set (= default-transaction-isolation 'SERIALIZABLE)])
{'fraction_non_alphanumeric': 0.12293411817976002, 'fraction_numerical': 0.006339144215530904, 'mean_word_length': 2.8959435626102294, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2429106', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1326, 'n_words': 429}