<commit_before>(ns modus.system.db-connection
  (:require [com.stuartsierra.component :as component]
            [clojure.tools.logging :as log]
            [modus.misc.config :refer [int-system-property]]
            [modus.misc.util :refer [->kebab-case]]
            [modus.misc.db :as db-util]
            [cheshire.core :as json]
            [clj-time.coerce :as time-coerce]
            [clojure.java.jdbc :as jdbc])
  (:import (com.zaxxer.hikari HikariDataSource)
           (org.postgresql.util PGobject)
           (java.sql Timestamp Date Array PreparedStatement)
           (clojure.lang IPersistentVector IPersistentMap)
           (org.joda.time DateTime)))

(defn db-pool [{:keys [url username password max-pool-size]}]
  (let [ds (doto (HikariDataSource.)
             (.setJdbcUrl url)
             (.setUsername username)
             (.setPassword password)
             (.setMaximumPoolSize max-pool-size))]
    {:datasource ds}))

(defn psql-db-properties
  []
  {:url           (str "jdbc:postgresql:" (System/getProperty "DB_CONNSTR" "//localhost:5432/modus"))
   :username      (System/getProperty "DB_USERNAME" (System/getProperty "user.name"))
   :password      (System/getProperty "DB_PASSWORD" "")
   :max-pool-size (int-system-property "DB_MAX_POOL_SIZE" 10)})

(defn datasource [db] (:singleton-datasource db))

(defrecord DBConnections [singleton-datasource]
  component/Lifecycle

  (start [component]
    (->> (psql-db-properties)
         db-pool
         (assoc component :singleton-datasource)))

  (stop [component]
    (when singleton-datasource
      (try
        (.close (:datasource singleton-datasource))
        (catch Exception e
          (log/error e "exception when closing DB connections"))))
    (assoc component :singleton-datasource nil)))

(extend-type IPersistentVector
  jdbc/ISQLParameter
  (set-parameter [v ^PreparedStatement stmt ^long idx]
    (let [conn (.getConnection stmt)
          meta (.getParameterMetaData stmt)
          type-name (.getParameterTypeName meta idx)]
      (if-let [elem-type (when (= (first type-name) \_) (apply str (rest type-name)))]
        (.setObject stmt idx (.createArrayOf conn elem-type (to-array v)))<commit_msg> fix db-properties<commit_after>(ns modus.system.db-connection
  (:require [com.stuartsierra.component :as component]
            [clojure.tools.logging :as log]
            [modus.misc.config :refer [int-system-property]]
            [modus.misc.util :refer [->kebab-case]]
            [modus.misc.db :as db-util]
            [cheshire.core :as json]
            [clj-time.coerce :as time-coerce]
            [clojure.java.jdbc :as jdbc])
  (:import (com.zaxxer.hikari HikariDataSource)
           (org.postgresql.util PGobject)
           (java.sql Timestamp Date Array PreparedStatement)
           (clojure.lang IPersistentVector IPersistentMap)
           (org.joda.time DateTime)))

(defn db-pool [{:keys [url username password max-pool-size]}]
  (let [ds (doto (HikariDataSource.)
             (.setJdbcUrl url)
             (.setUsername username)
             (.setPassword password)
             (.setMaximumPoolSize max-pool-size))]
    {:datasource ds}))

(defn psql-db-properties
  []
  {:url           (str "jdbc:postgresql://" (System/getProperty "DB_CONNSTR" "localhost:5432/modus"))
   :username      (System/getProperty "DB_USERNAME" (System/getProperty "user.name"))
   :password      (System/getProperty "DB_PASSWORD" "")
   :max-pool-size (int-system-property "DB_MAX_POOL_SIZE" 10)})

(defn datasource [db] (:singleton-datasource db))

(defrecord DBConnections [singleton-datasource]
  component/Lifecycle

  (start [component]
    (->> (psql-db-properties)
         db-pool
         (assoc component :singleton-datasource)))

  (stop [component]
    (when singleton-datasource
      (try
        (.close (:datasource singleton-datasource))
        (catch Exception e
          (log/error e "exception when closing DB connections"))))
    (assoc component :singleton-datasource nil)))

(extend-type IPersistentVector
  jdbc/ISQLParameter
  (set-parameter [v ^PreparedStatement stmt ^long idx]
    (let [conn (.getConnection stmt)
          meta (.getParameterMetaData stmt)
          type-name (.getParameterTypeName meta idx)]
      (if-let [elem-type (when (= (first type-name) \_) (apply str (rest type-name)))]
        (.setObject stmt idx (.createArrayOf conn elem-type (to-array v)))
{'fraction_non_alphanumeric': 0.1292732623952909, 'fraction_numerical': 0.002716776092370387, 'mean_word_length': 2.7504244482173177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4720175', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1379, 'n_words': 317}