Skip to content

Commit

Permalink
add ADV, KoerberOneCore, and REPOSITORY
Browse files Browse the repository at this point in the history
  • Loading branch information
derekvance21 committed Nov 4, 2024
1 parent 1c021e5 commit 65b0db5
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 103 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
# Koerber Snippets

This is a project to generate useful SQL snippets for Koerber development, specifically for AAD database.
This is a project to generate useful SQL snippets for Koerber development.

![Snippets Demo](demo.gif)

## Install

Go to the [release page](https://github.com/derekvance21/koerber-snippets/releases) and download the appropriate file for your IDE (SSMS or VSCode/Azure Data Studio).
Go to the [release page](https://github.com/derekvance21/koerber-snippets/releases) and download the appropriate file for your IDE. The `*.snippet` file is for SSMS and the `sql.json` file is for VSCode/Azure Data Studio.

### VSCode/Azure Data Studio
### [VSCode](https://code.visualstudio.com/docs/editor/userdefinedsnippets)/[Azure Data Studio](https://learn.microsoft.com/en-us/azure-data-studio/code-snippets#creating-sql-code-snippets)

[docs](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets)

1. Open the Command Palette with `Ctrl+Shift+P` and search for and select `Snippets: Configure Snippets`, then `MS SQL`
1. Open the Command Palette with `Ctrl+Shift+P` and search for and select `Snippets: Configure Snippets`, then `SQL`
2. Copy the downloaded `sql.json` file and paste it into this opened buffer and save.
3. Alternatively, move the downloaded `sql.json` file to `%APPDATA%\Code\User\snippets` for VSCode or `%APPDATA%\azuredatastudio\User\snippets` for Azure Data Studio.
4. Open a new buffer, change the language to SQL, type `stopkd`, then hit `Enter` to test snippet expansion.

### SSMS

[docs](https://learn.microsoft.com/en-us/sql/ssms/scripting/add-transact-sql-snippets?view=sql-server-ver16)
### [SSMS](https://learn.microsoft.com/en-us/sql/ssms/scripting/add-transact-sql-snippets?view=sql-server-ver16)

1. Open the Code Snippets Manager with `Ctrl+K, Ctrl+B` or clicking `Tools > Code Snippets Manager`.
2. Click `Add` and add a folder where you're going to put the downloaded `.snippet` file. I would name it something that would show up first in an alphabetical list (like `AAD` or `.Koerber`) because of the way snippets work in SSMS.
2. Click `Add` and add a folder at `%USERPROFILE%\Documents\SQL Server Management Studio\Code Snippets\SQL` where you're going to put the downloaded `.snippet` file. I would name it something that would show up first in an alphabetical list (like `AAD` or `.Koerber`) because of the way snippets work in SSMS.
3. Click `Import` and select the downloaded `.snippet` file.
4. Select the folder you created and click `Finish` (this may take a few moments) and then hit `OK`.
5. To open the available snippets, use the shortcut `Ctrl+K, Ctrl+X`. Select (by pressing `Enter`) the folder you added (you want it to be first in the list so that you don't have to search for it!), type `stopkd`, then hit `Enter`. The `stopkd` `JOIN` snippet expansion should work correctly.
Expand Down Expand Up @@ -104,3 +100,4 @@ The following generates two snippet files in the `out/` folder: `sql.json` (for
```sh
mkdir -p out; clj -M -m snippets.core
```

2 changes: 1 addition & 1 deletion src/snippets/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

(defn join-from-subset
[[start & dests]]
(let [edges (g/edges-to-destinations g/aad-schema start dests)]
(let [edges (g/edges-to-destinations g/schema start dests)]
(when-not (empty? edges)
{:start start
:dests dests
Expand Down
44 changes: 25 additions & 19 deletions src/snippets/generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@


(defn clause->str
[src dest [f1 f2]]
(let [f1-coll? (coll? f1)]
(str (if f1-coll?
(value->str dest f2)
(value->str src f1))
(if (some coll? [f1 f2])
" IN "
" = ")
(if f1-coll?
(value->str src f1)
(value->str dest f2)))))
([src dest fs]
(clause->str src dest fs nil))
([src dest [f1 f2] collate?]
(let [f1-coll? (coll? f1)]
(str (if f1-coll?
(value->str dest f2)
(value->str src f1))
(when collate?
(str " COLLATE DATABASE_DEFAULT"))
(if (some coll? [f1 f2])
" IN "
" = ")
(if f1-coll?
(value->str src f1)
(value->str dest f2))))))


(comment
Expand All @@ -45,14 +49,16 @@


(defn edge->body
[src [dest {:keys [db table]}] join-map]
(let [dest (if (= src dest)
(keyword (str (name dest) "2"))
dest)]
(concat [(str "JOIN " (table-source db table) " " (name dest) " WITH (NOLOCK)")]
(map str
(cons "\tON " (repeat "\tAND "))
(map #(clause->str src dest %) join-map)))))
([src dest join-map]
(edge->body src dest join-map))
([src [dest {:keys [db table]}] join-map collate?]
(let [dest (if (= src dest)
(keyword (str (name dest) "2"))
dest)]
(concat [(str "JOIN " (table-source db table) " " (name dest) " WITH (NOLOCK)")]
(map str
(cons "\tON " (repeat "\tAND "))
(map #(clause->str src dest % collate?) join-map))))))


(comment
Expand Down
197 changes: 124 additions & 73 deletions src/snippets/graph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,13 @@
[ubergraph.alg :as alg]))


;; KoerberOneCore tables...
;; subgraph of g with only nodes
#_(defn induced
[g nodes]
())


(def repository-nodes
[[:mnu {:table "t_menu"}]
[:pob {:db "REPOSITORY" :table "t_app_process_object"}]
[:pobd {:db "REPOSITORY" :table "t_app_process_object_detail"}]
[:clc {:db "REPOSITORY" :table "t_act_calculate"}]
[:clcd {:db "REPOSITORY" :table "t_act_calculate_detail"}]
[:db {:db "REPOSITORY" :table "t_act_database"}]
[:dbd {:db "REPOSITORY" :table "t_act_database_detail"}]
[:rsc {:db "REPOSITORY" :table "t_resource"}]
[:rscd {:db "REPOSITORY" :table "t_resource_detail"}]
[:app {:db "REPOSITORY" :table "t_application_development"}]])

(def repository-edges
[;; pob
[:pob :app {:application_id :application_id}]
;; pobd
[:pobd :pob {:id :id :version :version}]
#_[:pobd :pob {:action_type "1"
:action_id :id}]
[:pobd :clc {:action_type "3"
:action_id :id}]
[:pobd :db {:action_type "5"
:action_id :id}]
;; clc
[:clc :app {:application_id :application_id}]
;; clcd
[:clcd :clc {:id :id :version :version}]
[:clcd :rsc {[:operand1_id :operand2_id] :id}]
;; db
[:db :app {:application_id :application_id}]
;; dbd
[:dbd :db {:id :id :version :version}]
;; rsc
[:rsc :app {:application_id :application_id}]
;; rscd
[:rscd :rsc {:id :id :version :version}]
;; mnu
[:mnu :pob {:process :name}]])

(def aad-nodes
[[:sto {:table "t_stored_item"}]
Expand Down Expand Up @@ -80,8 +47,8 @@
[:ppm {:table "t_pick_put_master"}]
[:ppr {:table "t_pick_put_rules"}]
[:ppd {:table "t_pick_put_detail"}]
[:eil {:table "t_emp_input_log"}] ;; FK to rules and master
])
[:eil {:table "t_emp_input_log"}]
[:mnu {:table "t_menu"}]])

;; lkp should be a directed edge
(def aad-edges
Expand Down Expand Up @@ -249,32 +216,111 @@
[:trl :trn {:tran_type :tran_type} {:weight 1.1}]
[:trl :orm {:wh_id :wh_id
:outbound_order_number :order_number} {:weight 1.1}]
[:trl :pkd {:pick_id :pick_id} {:weight 1.1}]
;; eil
[:eil :emp {:id :id}]
[:eil :loc {:wh_id :wh_id
:fork_id :location_id}]])


(defn assoc-db
[db nodes]
(mapv
(fn [[n attrs]]
[n (assoc attrs :db db)])
nodes))


(def adv-nodes
(assoc-db
"ADV"
[[:app {:table "t_application"}]
[:lgm {:table "t_log_message"}]]))


;; KoerberOneCore tables...
(def koerber-one-core-nodes
(assoc-db
"KoerberOneCore"
[[:usr {:table "[User]"}]
[:uic {:table "UserIdentityClaim"}]
[:icl {:table "IdentityClaim"}]
[:rol {:table "[Role]"}]
[:url {:table "UserRoles"}]
[:ric {:table "RoleIdentityClaim"}]]))

(def koerber-one-core-edges
[[:uic :usr {:UserId :Id}]
[:uic :icl {:IdentityClaimId :Id}]
[:url :usr {:UserId :Id}]
[:url :rol {:RoleId :Id}]
[:ric :rol {:RoleId :Id}]
[:ric :icl {:IdentityClaimId :Id}]
[:usr :emp {:LogOnName :id} {:collate? true}]])

(def repository-nodes
(assoc-db
"REPOSITORY"
[[:pob {:table "t_app_process_object"}]
[:pobd {:table "t_app_process_object_detail"}]
[:clc {:table "t_act_calculate"}]
[:clcd {:table "t_act_calculate_detail"}]
[:db {:table "t_act_database"}]
[:dbd {:table "t_act_database_detail"}]
[:rsc {:table "t_resource"}]
[:rscd {:table "t_resource_detail"}]
[:apd {:table "t_application_development"}]]))

(def repository-edges
[;; pob
[:pob :apd {:application_id :application_id}]
;; pobd
[:pobd :pob {:id :id :version :version}]
#_[:pobd :pob {:action_type "1"
:action_id :id}]
[:pobd :clc {:action_type "3"
:action_id :id}]
[:pobd :db {:action_type "5"
:action_id :id}]
;; clc
[:clc :apd {:application_id :application_id}]
;; clcd
[:clcd :clc {:id :id :version :version}]
[:clcd :rsc {[:operand1_id :operand2_id] :id}]
;; db
[:db :apd {:application_id :application_id}]
;; dbd
[:dbd :db {:id :id :version :version}]
;; rsc
[:rsc :apd {:application_id :application_id}]
;; rscd
[:rscd :rsc {:id :id :version :version}]
;; mnu
[:mnu :pob {:process :name}]])


(defn edge->init
[[src dest join attrs]]
[src dest (merge {:weight 1} attrs {:join join})])


;; TODO - rename to schema or aad-schema or something
(def aad-schema
(let [inits (into aad-nodes (map edge->init) aad-edges)]
(def schema
(let [nodes (into [] cat [adv-nodes koerber-one-core-nodes repository-nodes aad-nodes])
edges (into [] (comp cat (map edge->init)) [koerber-one-core-edges repository-edges aad-edges])
inits (into nodes edges)]
(apply uber/graph inits)))


(defn edge-description
[g e]
(let [[src dest {:keys [join]}] (uber/edge-with-attrs g e)
(let [[src dest {:keys [join collate?]}] (uber/edge-with-attrs g e)
mirror? (uber/mirror-edge? e)]
[src
(uber/node-with-attrs g dest)
(if mirror?
(set/map-invert join)
join)]))
join)
collate?]))


(defn shortest-paths-to-destinations
Expand Down Expand Up @@ -305,7 +351,8 @@
;; all the paths to each dest in dests need to be under the max-jumps
;; TODO maybe also the sum of the path costs needs to be under max-jumps?
;; but really that's something I need to communicate with the shortest-path calculation...
(when (every? #(<= (:cost %) max-jumps) paths)
(when (every? #(when-let [cost (:cost %)]
(<= cost max-jumps)) paths)
paths))))


Expand All @@ -323,49 +370,52 @@


(comment
(shortest-paths-to-destinations aad-schema :sto [:loc :orm])
(edges-to-destinations aad-schema :sto [:loc :orm])
(shortest-paths-to-destinations schema :sto [:loc :orm])
(shortest-paths-to-destinations schema :uic [:usr])

(edges-to-destinations schema :sto [:loc :orm])
(edges-to-destinations schema :sto [:loc :orm])

(edges-to-destinations aad-schema :hum [:pkd :wkq])
(shortest-paths-to-destinations aad-schema :hum [:pkd :wkq])
(shortest-paths-to-destinations aad-schema :zon [:loc :alo])
(shortest-paths-to-destinations aad-schema :hld [:sto :car])
(shortest-paths-to-destinations aad-schema :hld [:sto :itm])
(edges-to-destinations aad-schema :zon [:alo])
(edges-to-destinations aad-schema :zon [:loc :alo :sto :pkd :itm :ppm])
(edges-to-destinations aad-schema :zon [:loc :alo :ppm])
(edges-to-destinations aad-schema :ppm [:hld])
(edges-to-destinations aad-schema :sto [:emp])
(edges-to-destinations aad-schema :sto [:loc :emp])
(edges-to-destinations aad-schema :pkd [:emp :loc :ppm])
(edges-to-destinations aad-schema :pkd [:loc :ord :orm])
(edges-to-destinations aad-schema :sto [:orm :loc :emp])
(edges-to-destinations aad-schema :sto [:pkd :hum])
(edges-to-destinations aad-schema :alo [:orm :emp])
(edges-to-destinations aad-schema :alo [:orm :emp])
(edges-to-destinations schema :hum [:pkd :wkq])
(shortest-paths-to-destinations schema :hum [:pkd :wkq])
(shortest-paths-to-destinations schema :zon [:loc :alo])
(shortest-paths-to-destinations schema :hld [:sto :car])
(shortest-paths-to-destinations schema :hld [:sto :itm])
(edges-to-destinations schema :zon [:alo])
(edges-to-destinations schema :zon [:loc :alo :sto :pkd :itm :ppm])
(edges-to-destinations schema :zon [:loc :alo :ppm])
(edges-to-destinations schema :ppm [:hld])
(edges-to-destinations schema :sto [:emp])
(edges-to-destinations schema :sto [:loc :emp])
(edges-to-destinations schema :pkd [:emp :loc :ppm])
(edges-to-destinations schema :pkd [:loc :ord :orm])
(edges-to-destinations schema :sto [:orm :loc :emp])
(edges-to-destinations schema :sto [:pkd :hum])
(edges-to-destinations schema :alo [:orm :emp])
(edges-to-destinations schema :alo [:orm :emp])
)


(def schema-nodes
(uber/nodes aad-schema))
(uber/nodes schema))


(def node-descriptions
(sequence
(map #(uber/node-with-attrs aad-schema %))
(uber/nodes aad-schema)))
(map #(uber/node-with-attrs schema %))
(uber/nodes schema)))


(defn viz-graph
([]
(viz-graph [:dot :neato :fdp :sfdp :twopi :circo]))
([layouts]
(let [repository-nodes [] #_[:pob :pobd :clc :clcd :db :dbd :rsc :rscd :app]
g (apply uber/remove-nodes aad-schema repository-nodes)
(let [repository-nodes [] #_[:pob :pobd :clc :clcd :db :dbd :rsc :rscd :apd]
g (apply uber/remove-nodes schema repository-nodes)
vg (reduce (fn [g node]
(let [{:keys [_db table]} (uber/attrs g node)]
(let [{:keys [db table]} (uber/attrs g node)]
(uber/add-attrs
g node {:label (str "{" (name node) #_(when db (str "|" db)) "|" table "}")
g node {:label (str "{" (name node) (when db (str "|" db)) "|" table "}")
:shape :Mrecord})))
g (uber/nodes g))]
(doseq [layout layouts]
Expand All @@ -377,6 +427,7 @@


(comment
(viz-graph)
(viz-graph [:dot])

)

0 comments on commit 65b0db5

Please sign in to comment.