dcDbUpdate ; 
 ; ----------------------------------------------------------------------------
 ; #
 ; #  DivConq
 ; #
 ; #  http://divconq.com/
 ; #
 ; #  Copyright:
 ; #    Copyright 2012 eTimeline, LLC. All rights reserved.
 ; #
 ; #  License:
 ; #    See the dcLicense.txt file in the project's top-level directory for details.
 ; #
 ; #  Authors:
 ; #    * Andy White
 ; #
 ; ----------------------------------------------------------------------------
 ;
insertRec n id,fields
 ;
 d runFilter("Insert") quit:Errors  ; if any violations in filter then do not proceed
 ;
 m fields=Params("Fields")
 d set^dcDb(Params("Table"),.id,.fields) quit:Errors
 ;
 s Params("Id")=id 
 ;
 w StartRec
 w Field_"Id"_ScalarStr_id
 w EndRec
 ;
 quit 
 ;
 ;
updateRec n id,fields s id=Params("Id") i (id="") d err^dcConn(50007) quit
 ;
 d runFilter("Update") quit:Errors  ; if any violations in filter then do not proceed
 ;
 m fields=Params("Fields")
 d set^dcDb(Params("Table"),id,.fields) quit:Errors
 ;
 quit 
 ;
 ; --------------------------------------
 ;
 ;	^Params(Pid,"Table") = [table]
 ;	^Params(Pid,"Id") = [id]
 ;	^Params(Pid,"Field") = [name]
 ;	^Params(Pid,"Filter") = [name]
 ;	^Params(Pid,"Extra") = [struct]
 ;
 ; StaticScalar handling
 ;   ^Params(Pid,"Content","Data",0) = [value]
 ;   ^Params(Pid,"Content","Tags") = [value]
 ;
 ; StaticList handling
 ;   ^Params(Pid,"Content",sid,"Data",0) = [value]
 ;   ^Params(Pid,"Content",sid,"Tags") = [value]			|value1|value2|etc...
 ;
 ; DynamicScalar handling
 ;   ^Params(Pid,"Content",sid,"Data",0) = [value]
 ;   ^Params(Pid,"Content",sid,"From") = [value]			null means always was
 ;   ^Params(Pid,"Content",sid,"Tags") = [value]
 ;
 ; DynamicList handling
 ;   ^Params(Pid,"Content",sid,"Data",0) = [value]
 ;   ^Params(Pid,"Content",sid,"From") = [value]			null means always was
 ;   ^Params(Pid,"Content",sid,"To") = [value]				null means always will be
 ;   ^Params(Pid,"Content",sid,"Tags") = [value]
 ; --------------------------------------
 ;
updateFld n id,field,table,fschema,sid s id=^Params(Pid,"Id"),table=^Params(Pid,"Table"),field=^Params(Pid,"Field")
 i (id="")!(field="")!(table="") d err^dcConn(50011) quit
 ;
 d runFilter("Update") quit:Errors  ; if any violations in filter then do not proceed
 ;
 ; works with replication, only counts once
 ;
 i $d(^Record(table,id))=0 d
 . l +^RecordMeta(table,"Count")
 . s ^RecordMeta(table,"Count")=^RecordMeta(table,"Count")+1
 . l -^RecordMeta(table,"Count")
 ;
 l +^Record(table,id)
 ;
 ; if marked for delete then skip, records cannot be undeleted and id cannot be reused
 i $$isDeleted^dcDb(table,id) l -^Record(table,id) d err^dcConn(50009) quit			
 ;
 i $d(^Schema(table,"Fields",field))=0 l -^Record(table,id) d err^dcConn(50010) quit
 ;
 m fschema=^Schema(table,"Fields",field)
 ;
 i 'fschema("List")&'fschema("Dynamic") d  i 1
 . i Audit=1 k ^Record(table,id,field,Stamp)
 . m ^Record(table,id,field,Stamp)=^Params(Pid,"Content") 
 e  d
 . s sid=$o(^Params(Pid,"Content","")) q:sid=""  
 . i Audit=1 k ^Record(table,id,field,sid,Stamp)
 . m ^Record(table,id,field,sid,Stamp)=^Params(Pid,"Content",sid)
 ;
 ; --------------------------------------
 ; auditing TODO
 ; --------------------------------------
 ;
 ;i TaskId'="" s ^RecordTasks(table,id,TaskId)=1
 ;
 l -^Record(table,id)
 ;
 quit 
 ;
 ;
retireRec n id,table s id=Params("Id"),table=Params("Table")
 i (table="") d err^dcConn(50003) quit
 i (id="") d err^dcConn(50004) quit
 ;
 d runFilter("Retire") quit:Errors  ; if any violations in filter then do not proceed
 ;
 d set1^dcDb(table,id,"Retired",1)  
 ;
 quit
 ;
 ;
reviveRec n id,table s id=Params("Id"),table=Params("Table")
 i (table="") d err^dcConn(50003) quit
 i (id="") d err^dcConn(50004) quit
 ;
 d runFilter("Retire") quit:Errors  ; if any violations in filter then do not proceed
 ;
 d set1^dcDb(table,id,"Retired",0)  
 ;
 quit
 ;
 ;
runFilter(mode) n filter,table,id s filter=Params("Filter"),table=Params("Table"),id=Params("Id")
 i table="" s filter=^Params(Pid,"Filter"),table=^Params(Pid,"Table"),id=^Params(Pid,"Id")
 i table="" d err^dcConn(50005) quit	
 i (filter'="")&(^Filter1(filter)'="") x "d "_^Filter1(filter)
 quit
 ;
 ;

