@@ -132,3 +132,46 @@ function _foreign_key_name(source, source_columns, target, target_columns)
132132 " $(source_table) ($(join(string.(source_columns), " , " )) ) → $(target_table) ($(join(string.(target_columns), " , " )) )"
133133 end
134134end
135+
136+ @funsql row_delta(table, keys = [$ (Symbol(" $(table) _id" ))], previous_table = $ (Symbol(" previous_$(table) " ))) = begin
137+ current => from($ table). define(is_present => true )
138+ join(
139+ previous => if_set($ previous_table, from($ previous_table), from($ table). filter(false )). define(is_present => true ),
140+ and(args = $ [@funsql(current.$ key == previous.$ key) for key in keys]),
141+ left = true ,
142+ right = true )
143+ frequency(state => is_null(previous. is_present) ? " added" : is_null(current. is_present) ? " dropped" : " retained" )
144+ end
145+
146+ function funsql_column_delta(table, keys = [Symbol(" $(table) _id" )], previous_table = Symbol(" previous_$(table) " ))
147+ function custom_resolve(n, ctx)
148+ tail′ = FunSQL. resolve(ctx)
149+ t = FunSQL. row_type(tail′)
150+ curr_t = t. fields[:current]
151+ prev_t = t. fields[:previous]
152+ fields = collect(Base. keys(curr_t. fields))
153+ qs = FunSQL. SQLQuery[]
154+ for field in fields
155+ if field in Base. keys(prev_t. fields)
156+ push!(qs, @funsql(count_if(previous.$ field != = current.$ field)))
157+ else
158+ push!(qs, @funsql(count(current.$ field)))
159+ end
160+ end
161+ @funsql begin
162+ $ tail′
163+ group()
164+ cross_join(summary_case => from(explode(sequence(1 , $ (length(fields)))), columns = [index]))
165+ define(column => $ (_summary_switch(string.(fields))))
166+ define(n_changed => $ (_summary_switch(qs)))
167+ define(pct_changed => floor(100 * n_changed / count(), 1 ))
168+ end
169+ end
170+ @funsql begin
171+ current => from($ table)
172+ join(
173+ previous => if_set($ previous_table, from($ previous_table), from($ table). filter(false )),
174+ and(args = $ [@funsql(current.$ key == previous.$ key) for key in keys]))
175+ $ (CustomResolve(custom_resolve))
176+ end
177+ end
0 commit comments