Uživatelské nástroje

Nástroje pro tento web


progs:fblutil

Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

Obě strany předchozí revizePředchozí verze
progs:fblutil [2020/01/17 09:00] Karel Petrůprogs:fblutil [2020/01/31 14:23] (aktuální) Karel Petrů
Řádek 1: Řádek 1:
 ====== FBLUtil ====== ====== FBLUtil ======
 +
 +===== Import ceniku z CSV =====
 +
 +<code LUA>
 +--../bin64/fblutil scripts/import_cenik.lua -debug 1 -- testcen
 +
 +local DBName = "localhost:/home/firebird/data.fdb"
 +local CenikName=arg[1]
 +local ImportFN = "/tmp/"..CenikName..".csv"
 +local IDDruh="x-"..CenikName
 +local Prefix=string.upper(string.sub(CenikName,1,2))
 +local PrefixNumChars=4
 +local ISC_PASSWORD=os.getenv("ISC_PASSWORD")
 +
 +print("Starting with", _VERSION)
 +print("Parameters after -- param: ", arg)
 +print("Connecting "..DBName)
 +local  db = FBLUtil.ConnectDB(DBName, "sysdba", ISC_PASSWORD)
 +
 +function CloneTable(db1, TableName, CloneTableName)
 +  local r=db1:GetTable(CloneTableName)
 +  if #r>0 then --tabulka existuje
 +    print("Drop "..CloneTableName)
 +    db1:Execute(string.format("DROP TABLE %s", CloneTableName))
 +    db1:Commit()
 +  end
 +  local t = db1:GetTable(TableName)
 +  local flds = ""
 +  for i, v in ipairs(t) do
 +    local s = string.format("%s %s", v.Field, v.Typ)
 +    if flds ~= "" then
 +      flds = flds .. ", "
 +    end
 +    flds = flds .. s
 +  end
 +  print("Create "..CloneTableName)
 +  db1:Execute(string.format("CREATE TABLE %s (%s)", CloneTableName, flds))
 +  db1:Commit()
 +  local t = db1:GetIndices(TableName)
 +  for i, v in ipairs(t) do
 +    db1:Execute(string.format("CREATE %s INDEX %s ON %s (%s)", v.Opts, "V_"..v.Name, CloneTableName, v.Fields))
 +    if v.Inactive then
 +      db1:Execute(string.format("ALTER INDEX %s INACTIVE", "V_"..v.Name))
 +    end
 +  end
 +  db1:Commit()
 +end
 +
 +function CloneCenik()
 +  CloneTable(db, "rozpocty_cenik", "v_rozpocty_cenik")
 +end
 +
 +function GetNextPrefixNum(NumChars)
 +  local r=db:SelectToTable(string.format("select first 1 IDCen from rozpocty_cenik where IDDruh='%s' order by IDCen desc", IDDruh))
 +  --print(r)
 +  local p=string.format("%%0%dd", NumChars)
 +  local n=0
 +  if #r.Rows>0 then
 +    local f=string.len(Prefix)+1
 +    local t=string.len(Prefix)+NumChars
 +    n=string.sub(r.Rows[1][1], f,t)+1
 +    --print(p,f,t,n)
 +  end
 +  return string.format(p, n)
 +end
 +
 +local PrefixNum=GetNextPrefixNum(PrefixNumChars)
 +
 +function Import_testcen()
 +  print("Import from "..ImportFN)
 +  local t = os.time()
 +  print(string.format("PrefixNum = %d", PrefixNum))
 +  local n = db:ImportTable(ImportFN, ";", "UTF-8", "base64", [[insert into v_rozpocty_cenik 
 +         (IDCen,               IDDruh,                      CisRozp, ObjCis, Popis, MJ, Naklady, Hmotnost, v_IDDod, DatumZmeny) 
 +  values (']]..Prefix..PrefixNum..[['||$COUNTER(%6.6d), ']]..IDDruh..[[', $(5), $(2), $(7), $(3), $(6),   $(4),    $(1), CURRENT_TIMESTAMP)]])
 +  db:Commit()
 +  print(string.format("Done %d rows in %ds", n, os.difftime(os.time(), t)))
 +end
 +
 +function UpdateCenik()
 +  print("Update "..IDDruh)
 +  local t = os.time()
 +  local n=db:ExecuteParams([[update rozpocty_cenik c set
 +    naklady=(select naklady from v_rozpocty_cenik v where v.v_iddod=c.v_iddod and c.iddruh=v.iddruh),
 +    DatumZmeny=CURRENT_TIMESTAMP
 +    where 
 +      exists (select * from v_rozpocty_cenik v where v.v_iddod=c.v_iddod)]],{})
 +  db:Commit()
 +  print(string.format("Update %d rown in %ds", n, os.difftime(os.time(), t)))
 +  t = os.time()
 +  local n=db:ExecuteParams([[insert into rozpocty_cenik select * from v_rozpocty_cenik v
 +  where not exists (select * from rozpocty_cenik c where c.v_iddod=v.v_iddod and c.iddruh=v.iddruh)
 +    ]],{})
 +  db:Commit()
 +  print(string.format("Insert %d rows in %ds", n, os.difftime(os.time(), t)))
 +end
 +
 +function CreateCenik()
 +  print("Create new "..IDDruh)
 +  local t = os.time()
 +  db:Execute(string.format([[update or insert into rozpocty_cdruh (IDDruh, Typ) values ('%s', 'd') matching (IDDruh)]], IDDruh))
 +  local n=db:ExecuteParams([[insert into rozpocty_cenik select * from v_rozpocty_cenik]],{})
 +  db:Commit()
 +  print(string.format("Insert %d rows in %ds", n, os.difftime(os.time(), t)))
 +end
 +
 +
 +CloneCenik()
 +
 +assert(load("Import_"..CenikName.."()"))()
 +
 +if tonumber(PrefixNum) == 0 then
 +  CreateCenik()
 +else
 +  UpdateCenik()
 +end
 +
 +db:Disconnect()
 +
 +</code>
  
 ===== Prenos dat mezi DB, Import, Export ===== ===== Prenos dat mezi DB, Import, Export =====
progs/fblutil.1579251649.txt.gz · Poslední úprava: 2020/01/17 09:00 autor: Karel Petrů