<commit_before>(ns clj-money.models.storage.sql-storage
  (:require [clojure.tools.logging :as log]
            [clojure.spec :as s]
            [clojure.string :as string]
            [clojure.java.jdbc :as jdbc]
            [clojure.pprint :refer [pprint]]
            #_[clj-time.jdbc]
            [clj-time.core :as t]
            [clj-time.coerce :refer [to-sql-date
                                     to-sql-time
                                     to-local-date]]
            [honeysql.core :as sql]
            [honeysql.helpers :as h]
            [clj-postgresql.types]
            [clj-money.util :refer [pprint-and-return]]
            [clj-money.partitioning :refer [table-name
                                            tables-for-range]]
            [clj-money.models.storage :refer [Storage]])
  (:import [java.sql BatchUpdateException
                     Date]
           org.joda.time.LocalDate
           org.postgresql.util.PSQLException))

(defn- first-key-present
  "Accepts a list of keys and a map, returning the first
  key from the list that is present in the map.

  (first-key-present :a :b :c {:b 42})
  ;; => :b"
  [& args]
  (let [m (last args)
        k (take (- (count args) 1) args)]
    (-> m
        keys
        set
        (some k))))

(extend-protocol jdbc/IResultSetReadColumn
  Date
  (result-set-read-column [v _ _]
    (to-local-date v)))

(extend-protocol jdbc/ISQLValue
  LocalDate
  (sql-value [v]
    (to-sql-date v)))

(extend-protocol jdbc/ISQLValue
  org.joda.time.DateTime
  (sql-value [v]
    (to-sql-time v)))

(defn- id-criteria?
  [value]
  (or (integer? value)
      (uuid? value)
      (and (coll? value)
           (seq value)
           (every? integer? value))))

(s/def ::user-id integer?)
(s/def ::entity-id id-criteria?)
(s/def ::lot-id integer?)
(s/def ::account-id integer?)
(s/def ::commodity-id integer?)
(s/def ::transaction-id uuid?)
(s/def ::date (partial instance? LocalDate))
(s/def ::nilable-date #(or (instance? LocalDate %) (nil? %)))
<commit_msg>automatically convert keywords to strings for sql<commit_after>(ns clj-money.models.storage.sql-storage
  (:require [clojure.tools.logging :as log]
            [clojure.spec :as s]
            [clojure.string :as string]
            [clojure.java.jdbc :as jdbc]
            [clojure.pprint :refer [pprint]]
            #_[clj-time.jdbc]
            [clj-time.core :as t]
            [clj-time.coerce :refer [to-sql-date
                                     to-sql-time
                                     to-local-date]]
            [honeysql.core :as sql]
            [honeysql.helpers :as h]
            [clj-postgresql.types]
            [clj-money.util :refer [pprint-and-return]]
            [clj-money.partitioning :refer [table-name
                                            tables-for-range]]
            [clj-money.models.storage :refer [Storage]])
  (:import [java.sql BatchUpdateException
                     Date]
           org.joda.time.LocalDate
           org.postgresql.util.PSQLException))

(defn- first-key-present
  "Accepts a list of keys and a map, returning the first
  key from the list that is present in the map.

  (first-key-present :a :b :c {:b 42})
  ;; => :b"
  [& args]
  (let [m (last args)
        k (take (- (count args) 1) args)]
    (-> m
        keys
        set
        (some k))))

(extend-protocol jdbc/IResultSetReadColumn
  Date
  (result-set-read-column [v _ _]
    (to-local-date v)))

(extend-protocol jdbc/ISQLValue
  LocalDate
  (sql-value [v]
    (to-sql-date v)))

(extend-protocol jdbc/ISQLValue
  org.joda.time.DateTime
  (sql-value [v]
    (to-sql-time v)))

(extend-protocol jdbc/ISQLValue
  clojure.lang.Keyword
  (sql-value [v]
    (name v)))

(defn- id-criteria?
  [value]
  (or (integer? value)
      (uuid? value)
      (and (coll? value)
           (seq value)
           (every? integer? value))))

(s/def ::user-id integer?)
(s/def ::entity-id id-criteria?)
(s/def ::lot-id integer?)
(s/def ::account-id integer?)
(s/def ::commodity-id integer?)
(s/def ::transaction-id uuid?)
(s/def ::date (partial instance? LocalDate))
(s/def ::nilable-date #(or (instance? LocalDate %) (nil? %)))

{'fraction_non_alphanumeric': 0.14439655172413793, 'fraction_numerical': 0.0014367816091954023, 'mean_word_length': 1.916899441340782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '360672', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1434, 'n_words': 337}