 
 
 
+
+
+
+
 (defn make-todo-table []
   (let [coils-admin-tables      (korma.core/exec-raw
                                  [" select * from pg_tables where schemaname='public' and tablename='coils_todo_items'" []]




(defn get-results [& {:keys [db-table where order start end params]}]
(sql
    (str
        "select id from "
        db-table " "
        (if (-> where count pos?) (str "where " where " "))
        (if (-> order count pos?) (str "order by " order " "))
        (cond
            (= *database-type* "postgres" )
            (str
                (if start (str " offset " (- start 1) " "))
                (if end (str " limit " (+ 1 (- end start)) " ") "limit 2")
                )

            (= *database-type* "oracle" )
            ""
            )

        ;questions_answered_count is not null
        ;order by
        ;questions_answered_count desc"
        ) params)
        )






(defn create-realtime-trigger [& {:keys [:table-name]}]
  (let [coils-trigger      (korma.core/exec-raw
                            [" select * from coils_triggers where table_name = ?" [table-name]]
                            :results)

        coils-trigger-exists   (pos? (count coils-trigger))

        sql-to-drop-trigger    (str "DROP TRIGGER IF EXISTS trigger_afterinsert ON " table-name ";")
        sql-to-insert-trigger-row "
        INSERT INTO coils_triggers
        (
        table_name,version
        ) values (?,?);
        "

        sql-to-create-insert-trigger (str "CREATE TRIGGER trigger_afterInsert AFTER INSERT ON " table-name " FOR EACH ROW EXECUTE PROCEDURE trigger_function_afterInsert();")
        sql-to-create-update-trigger (str "CREATE TRIGGER trigger_afterUpdate AFTER UPDATE ON " table-name " FOR EACH ROW EXECUTE PROCEDURE trigger_function_afterUpdate();")
        sql-to-create-delete-trigger (str "CREATE TRIGGER trigger_afterDelete AFTER DELETE ON " table-name " FOR EACH ROW EXECUTE PROCEDURE trigger_function_afterDelete();")
        ]
    ;(println "Coils trigger table exists: " coils-trigger-exists)


    (if (not coils-trigger-exists )
      (do
        (korma.core/exec-raw   [sql-to-insert-trigger-row [table-name  coils-tables-trigger-version]]   [])
        (korma.core/exec-raw   [sql-to-drop-trigger []]   [])
        (korma.core/exec-raw   [sql-to-create-insert-trigger []]   [])
        (korma.core/exec-raw   [sql-to-create-update-trigger []]   [])
        (korma.core/exec-raw   [sql-to-create-delete-trigger []]   [])

        )
      nil)))



