Adapya – how to read, store and update data in severals files in one transaction.
I’m starting with python and adapya.
I’d be greatful for an example/some tips how in one transaction read data from two different files, store data into two another files and update one of reading file.
I tried to adjust the serarch.py example with searching using datamaps.
I have two maps:
emptel.py (from example from documentation),
emp2.py (new map),
search_2.py (adjusted search.py from script from documentaton)
emptel.py: -----------------------------------------------------------
from adapya.base.datamap import Datamap, String
from adapya.adabas.metadata import metamap, Metadata
empTelFormat = ‘AA,AC,AD,AE,AH,8,U,AL,AN,AM,AO,AP.’
emp = Datamap(‘EmplTel’,
String(‘personnel_id’, 8),
String(‘firstname’, 20),
String(‘m_initial’, 20),
String(‘lastname’, 20),
String(‘birth’, 8),
String(‘country’, 3),
String(‘areacode’, 6),
String(‘phone’, 15),
String(‘department’, 6),
String(‘jobtitle’, 25)
)
metadata = Metadata(dbid=10,fnr=11,fb=empTelFormat,dmap=emp)
emp2.py: -----------------------------------------------------------
from adapya.base.datamap import Datamap, String
from adapya.adabas.metadata import metamap, Metadata
emp2Format = ‘AA,AC,AE,AO,AP.’
emp2 = Datamap(‘EmplTel’,
String(‘personnel_id’, 8),
String(‘firstname’, 20),
String(‘lastname’, 20),
String(‘department’, 6),
String(‘jobtitle’, 25)
)
metadata = Metadata(dbid=10,fnr=11,fb=emp2Format,dmap=emp2)
search_2.py:
from future import print_function
from adapya.adabas.API import Adabas, Adabasx, archit2str
from adapya.adabas.API import DataEnd, DatabaseError, InterfaceError, adaSetTimeout
from adapya.adabas.API import setsaf
from adapya.adabas.fields import readfdt
from adapya.base.defs import log,LOGBEFORE,LOGCMD,LOGCB,LOGRB,LOGRSP,LOGFB,
LOGSB,LOGIB,LOGMB,LOGVB,LOGSP,evals
from adapya.base.conv import str2ebc
from adapya.base.datamap import funpack, Datamap, String, T_VAR0
from adapya.base import datamap
import datetime
import getopt
import sys
import os
PY3 = True
getinput = input
debug=0
arc=None
acode=0
acbx=0
dbid=0
dmap = None # datamap from metadata
dprint = None # detail print function from metadata
descending=0
fnr=0
format=‘’
histo=‘’
islstr=‘’
isnlist=0
isnlower=0
toisn = 0 # test
multifetch=1
metadata = None
password=‘’
noclose=0
quantum = 8
readop=‘’
replytimeout=0
safuid=safpw=safnew=‘’
search=‘’
sortfields=‘’
superc=‘’
value=‘’
verbose=0
wcode=0
timezone=‘’
readop=‘AO.’
value=evals(‘SALE04’)
quantity = 1
metafile = ‘empltel’
import importlib
memo = importlib.import_module(metafile)
metadata = memo.metadata
read another field by another superdesc. -------------------------- SECOND DATAMAP
readop=‘AO.’
value=evals(‘SALESMAN’)
quantity = 1
metafile2 = ‘emp2’
memo2 = importlib.import_module(metafile2)
metadata2 = memo2.metadata
---------------------------------------------------------------------END VAR. FOR SECOND DATAMAP
readmod=3
dbid = metadata.dbid
fnr = metadata.fnr
dmap = metadata.dmap
rbl1 = dmap.getsize() # if dmap else 4096
rblm = min(rbl1multifetch, 215-1 if not acbx else 220-1) # limit rbl
mbl = 4+16multifetch
fbl = len(metadata.fb) # if metadata else len(format)
c1=Adabas(fbl=fbl,rbl=rblm,sbl=32,vbl=128,ibl=max(isnlist*4,mbl),
multifetch=multifetch,password=password)
c1.dbid=dbid
c1.cb.fnr=fnr
c1.cb.cid=‘SSPY’
c1.fb.write(metadata.fb) # if metadata else format) # tu jest metadata
LOGL3=LOGS1=LOGRD=LOG0=LOGRSP
try:
log(LOG0)
c1.open(arc=arc,acode=acode,wcode=wcode,tz=timezone)
# c1.printopsys()
desc=readop[0:2] # extract descriptor
c1.sb.write(readop) # set selection criterion
c1.vb.write(value)
log(LOGL3)
c1.cb.cid='SSP2'
seq = readop[0:2]
dm = Datamap( 'record',String('vrec' , 0, opt=T_VAR0, sizefunc=lambda: dm.dmlen ))
nr = 0
c1.cb.rsv1=b'\xd5' # rsv1=N no option check
row = memo.emp
row.buffer = c1.rb
row.offset = 0
for isn,_ in c1.read(seq=seq,dmap=dm,descending=descending,startisn=isnlower,toisn=toisn):
nr += 1
dm.prepare() # adjust variable field
print(' ' + dm.vrec)
c1.cb.cmdt=0 # no further time for additional records in multifetch
if seq not in ('I') and nr == c1.cb.isq:
break
if nr>5: # not more than 5 rows
break
datamap.debug=0
----------------------------------------------------------------------
# HERE I'D LIKE TO READ DATA FROM ANOTHER FILE
# OR THE SAME FILE BUT ANOTHER LIST OF FIELDS AND BY ANOTHER DESC. - FOR EXAMPLE FOR DMAP EMP2
# ....
# AND LATER STORE DATA TO ANOTHER TWO FILES AND UPDATE DATA IN THE FIRST FILE
# ...
# AT THE END : ET()
# -----------------------------------------------------------------------
except DatabaseError as e:
print( ‘DatabaseError Exception on database %d\n\t%s’ % (dbid,e.value))
print( ‘Terminating due to database error’)
sys.exit(8)
except InterfaceError as e:
print( ‘Database %d –\n\t%s’ % (dbid,e.value))
sys.exit(16)
except Exception as e:
print( e)
from traceback import print_exc
print_exc()
sys.exit(12)
finally:
if not noclose:
log(LOG0)
c1.close()
# print(‘Adabas session closed. I/Os=%d, calls=%d, cpu=%4.3f ms’ % (
# c1.cb.isn, c1.cb.isl, c1.cb.isq/4096000. if acbx else c1.cb.isq * 1048.576))
else:
print( ‘noclose: Skipping close to database %d’ % dbid)