%META:TOPICINFO{author="ChrisBartram" date="1149893669" format="1.1" version="1.1"}% %META:TOPICPARENT{name="Hp3000MpexXeqs"}%
parm diskfile="?", database="?", dataset="?"
#  Author:   Paul H. Christidis       phchristidis@west.raytheon.com
#
#     This is an MPEX command file can be used to load back into
#     an IMAGE dataset the contents of a flat file.  It is assumed
#     that the file was created with the save command of QUERY
#     from the same dataset.
#
setvar _db_bell chr(7)
setvar _db_diskfile UPS("!diskfile")
setvar _db_database UPS("!database")
setvar _db_dataset  UPS("!dataset" )
if _db_diskfile = "?" OR _db_database = "?" OR _db_dataset = "?" then
  echo !_db_bell
  echo Usage: ![finfo(hpfile,"fname")] Diskfile Database Dataset
  echo
  echo  Diskfile - Name of flat file containing records to be loaded
  echo        into the database.  Usually this file would have been
  echo        generated using the QUERY 'save' command.
  echo
  echo  Database - Name of IMAGE database.  It is assumed that the
  echo        user of this command file is the database's creator or
  echo        that the user has knowledge of the password that gives
  echo        write/update access to the database.
  echo
  echo  Dataset  - Name of dataset, within the above database, into
  echo        which the contents of the above disk file will be loaded.
  echo !_db_bell
  return
endif
#
#  Detrmine if we are within MPEX and invoke it if not.
#
setjcw insidempex=0
setvar _db_mpex "main.pub.vesoft"
if insidempex = 0 then
  if finfo("!_db_mpex","exists") then
    run !_db_mpex; &
      info="xeq !hpfile '!_db_diskfile','!_db_database','!_db_dataset'"
    return
  else
    echo !_db_bell This command file requires 'MPEX' to execute...
    deletevar _db_@
    escape 52
  endif
endif
#
echo (PHC) DISK2DB using MPEX  98.05.14 !hpdatef !hptimef
echo
setvar _db_err 0
if NOT FEXISTS(_db_diskfile) then
   echo !_db_bell **Unable to locate file: !_db_diskfile**
   setvar _db_err _db_err + 1
else
   if vefinfo(_db_diskfile).recsize > 255 then
     echo !_db_bell **Record size of !_db_diskfile is too large
     setvar _db_err _db_err + 1
   endif
endif
if NOT FEXISTS(_db_database) then
   echo !_db_bell **Unable to locate database: !_db_database**
   setvar _db_err _db_err + 1
elseif vefinfo(_db_database).intcode <> -400 then
   echo !_db_bell **File (!_db_database) is NOT an IMAGE database...
   setvar _db_err _db_err + 1
else
   setvar _db_owner vefinfo(_db_database).owner = "!hpuser.!hpaccount"
endif
if _db_err <> 0 then
  return
endif
#
if _db_owner then
   setvar _db_pass ";"
else
   echo ** You are NOT the creator of the database ** !_db_bell
   input _db_pass;prompt="Enter DB's Password: "
endif
#
setvar _db_cntr 0
setvar _db_dbid  VEDBOPEN("!_db_database,'!_db_pass',1")
echo 1. Opened database:  ![VEFINFO(_db_database).FULLNAME]
setvar _db_ffnum VEFOPEN("!_db_diskfile,old")
echo 2. Opened disk file: ![VEFINFO(_db_diskfile).FULLNAME]
echo 3. Starting to load records
while VEFREADV("_db_ftext",_db_ffnum) do
  IF VEDBPUTL (_db_dbid,_db_dataset,"@;",_db_ftext) then
    setvar _db_cntr _db_cntr + 1
  else
    echo !_db_bell Error adding records to dataset..
    escape
  endif
  if _db_cntr mod 100 = 0 then
    echo     !_db_cntr Records were processed....
  endif
endwhile
echo !_db_cntr Records were added to the database !_db_bell
echo 4. Closing database and disk file...
if NOT VEDBCLOSE(_db_dbid,"",1) then
  echo !_db_bell *Error while closing the database
endif
calc VEFCLOSE(_db_ffnum)
#
#Clean Up
#
if BOUND(traceon) then
  showvar _db_@
else
  deletevar _db_@
endif

-- Main.ChrisBartram - 09 Jun 2006