Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In addition to executing SQL statements directly, Iguana offers another method for writing to databases using a Database Schema File (*.vdb), db.tables{} and the conn:merge{} function.

...

Info

conn:merge{} does not support spaces in column names. If your database schema contains spaces in column names, you’ll need to modify the column names to remove or replace spaces before using conn:merge{} or see https://help.interfaceware.com/v6/customize-custom_merge-lua-for-table-and-field-names-with-spaces.

Expand
titleError Handling Merges

The merge process all happens within a single database transaction. If it fails, the transaction is rolled back and nothing is altered in the database. This behavior can be optionally turned off if you pass in transaction=false as an argument.

Just like with conn:execute{}, you can use pcall() to catch and gracefully handle any errors that arise from conn:merge{} operations:

Code Block
languagelua
function doMerge(conn, T) 
  local R = conn:merge{data=T, live=true}
end 

function main()
  -- create empty table
  local T = db.tables{vdb='report.vdb, name ='Patients'} 
  
  -- map vdb table values
  T.Patients[1].FirstName = 'John' 
  T.Patients[1].LastName = 'Doe' 
  T.Patients[1].ID = 1
  
  -- call merge function and handle response
  local Success, R = pcall(doMerge, conn, T) 
  if not Success then 
    iguana.log('Error during merge: ', R.message) 
  end
end 

...