DEFINE BUTTONBAR oBar OF oWnd SIZE 80,32
@ 50,20 XBROWSE oBrw SIZE -20,-20 PIXEL OF oWnd ;
DATASOURCE oRs AUTOCOLS AUTOSORT ;
CELL LINES NOBORDER FASTEDIT
WITH OBJECT oBrw
:nEditTypes := EDIT_GET
:bDataRow := < |brw,cFieldList,lNew|
local oRec := TDataRow():New( oRs, cFieldList, lNew )
oRec:bSave := { |oRec| MySavePQQ( oRec ) }
return oRec
>
:CreateFromCode()
END
DEFINE BUTTON OF oBar PROMPT "AddNew" ACTION oBrw:EditSourc( .t. )
DEFINE BUTTON OF oBar PROMPT "Edit" ACTION oBrw:EditSource()
ACTIVATE WINDOW oWnd
//----------------------------------------------------------------------------//
function MySavePQQ( Self )
local lAppend := ( ::RecNo == 0 )
local oQry := ::uSource
local aModi := ::aModiData
local aKey := {}
local aSave := {}
local cSeq, nCurVal
local n, v, nFld, cSql, uVal, cWhere, nRows, nRow
local lRefresh := .f.
for n := 1 to Len( ::aStructPG )
if ::aStructPG[ n, 10 ] == "PRI" .and. ;
oQry:TableName == ::aStructPG[ n, 7 ]
if cSeq == nil .and. ::aStructPG[ n, 2 ] == '+'
cSeq := ::aStructPG[ n, 9 ]
endif
if ValType( ::aData[ n, 2 ] ) == 'C'
AAdd( aKey, { n, ::aStructPG[ n, 8 ], TRIM( ::aOrg[ n, 2 ] ), TRIM( ::aData[ n, 2 ] ) } )
else
AAdd( aKey, { n, ::aStructPG[ n, 8 ], ::aOrg[ n, 2 ], ::aData[ n, 2 ] } )
endif
endif
next
if Empty( aKey )
return .t.
endif
for n := 1 to Len ( ::aModiData )
nFld := ::aModiData[ n, 3 ]
uVal := ::aModiData[ n, 2 ]
if ValType( uVal ) == 'C'
uVal := Trim( uVal )
endif
AAdd( aSave, { ::aStructPG[ nFld, 8 ], uVal } )
next
if Empty( aSave )
return .t.
endif
if lAppend
cSql := "INSERT INTO " + oQry:Schema + "." + oQry:TableName + " ( "
cSql += FW_ArrayAsList( ArrTranspose( aSave )[ 1 ] )
cSql += " ) VALUES "
cSql += FW_ValToSQL( ArrTranspose( aSave )[ 2 ], nil, "PG" )
else
cSql := "UPDATE " + oQry:Schema + "." + oQry:TableName
for n := 1 to Len( aSave )
if n == 1
cSql += " SET "
else
cSql += ", "
endif
cSql += aSave[ n, 1 ] + " = " + FW_ValToSQL( aSave[ n, 2 ],, "PG" )
next
for n := 1 to Len( aKey )
if n == 1
cSql += " WHERE "
else
cSql += " AND "
endif
cSql += aKey[ n, 2 ] + " = " + FW_ValToSQL( aKey[ n, 3 ] )
next
endif
FWPG_Execute( oQry, cSql )
if lAppend .and. !Empty( cSeq )
nCurVal := FWPG_Execute( oQry, "SELECT currval( '" + cSeq + "' )" )[ 1, 1 ]
endif
oQry:Refresh()
// Reposition record
if lAppend
n := aKey[ 1, 1 ]
if !Empty( nCurVal )
v := nCurVal
else
v := aKey[ 1, 4 ]
endif
oQry:GoTo( oQry:LastRec() )
FWPG_GoToKeyVal( oQry, n, v )
lRefresh := .t.
else
n := aKey[ 1, 1 ]
v := aKey[ 1, 4 ]
if ! ( oQry:FieldGet( n ) == v )
FWPG_GoToKeyVal( oQry, n, v )
lRefresh := .t.
endif
endif
::ReadPQQ( nil, .f., .t. )
if ::oBrw != nil
if lRefresh
::oBrw:Refresh()
else
::oBrw:RefreshCurrent()
endif
endif
return .t.
//----------------------------------------------------------------------------//