Transactions between Tables

Prev Next

Introduction

Transactions between table rows in the same or two different tables are also supported. As long the transaction operator has no assignment operator (described in the next sections further below), then the contents in the destination row will be cleared entirely before writing. Following rules apply:

  • Only simple and horizontal table accesses are allowed. Vertical table accesses and matrix table accesses will be rejected and cause exceptions.
  • Use '..' or keep blank to specify all column entries in the specified row, e.g. [t:..,1] or [t:,1]. Otherwise only the specified columns will be written.
  • Partial table specifications are allowed, provided the context information for the table and row number is available (e.g. inside the function parameters of the table process() call)
  • The transactions uses the header names and not physical column positions for orientation. Make sure you are using unique header names. Otherwise, the first matching column from the left will be used.
  • Additional table columns with header names are only added if open-ended ranges are specified for the table row. Open-ended ranges apply in following cases:
    • [t:..,1] - All columns under existing headers selected. Further columns may be added
    • [t:,1] - All columns in the current row are selected. Further columns may be added
    • [t:3..,1] - All columns beginning with column 3 (example value) till end of row.
  • The ^ suffix behind the transaction operator is irrelvant because no base variables are involved.

Attention: In case no open-ended ranges have been specified, then only the columns with header names found in both source and destination tables will be transferred.

The following table applies to transactions between table rows (example: table names 't' and 'u', row 1 and 2, all columns) to a variable.

Transaction Symbol Explanation Description
Copy <== [t:..,1] <== [u:,2] Step 1: Checks that exactly one table row and one or more or all columns are specified.
Step 2: Clears all contents in the specified table row
Step 3: Data transfer from variable to table as described above.
Note: The base variable will not be copied.
Move <<= [t:..,1] <<= [u:,2]; Steps 1-3: Like the copy-transaction
Step 4: The specified row in the table will be deleted. The rows below will shift upwards.
Swap <=> [t:..,1] <=> [u:,2]; The contents in the member variable and the table row will be exchanged.

Copy Transaction Example
  table initialize( animals1,
      { { Name,  Leg count, Special ability  },
        { Owl,   2,         Night vision     },
        { Dog,   4,         Sensitive nose   },
        { Ape,   2 or 4,    Relatively smart } } );

  table initialize( animals2,
      { { Name,  Leg count, Special ability, Weapon  },
        { Bee,   6,         Makes honey,     sting   },
        { Gnu,   4,         Knows UNIX,      horns   },
        { Cat,   4,         Empathy to humans, sharp claws } } );

  [ animals1: Name, Dog, Name..Special ability ] <== [ animals2: Name, Cat, ]; // sharp claws will not be copied.
  [ animals1: Name, Owl, .. ] <== [ animals2: Name, Bee, { Weapon, Name } ];

  echo("Dog -> Cat without claws");
  table list( animals1 ); // No additional column created
  
  echo("Unchanged");
  table list( animals2 ); // Additional column created
Dog -> Cat without claws
    0 : Name | Leg count | Special ability   | Weapon
    1 : Bee  |           |                   | sting
    2 : Cat  | 4         | Empathy to humans |       
    3 : Ape  | 2 or 4    | Relatively smart  |       

Unchanged
    0 : Name | Leg count | Special ability   | Weapon     
    1 : Bee  | 6         | Makes honey       | sting      
    2 : Gnu  | 4         | Knows UNIX        | horns      
    3 : Cat  | 4         | Empathy to humans | sharp claws

Try it yourself: Open LAN_Features_Transactions_between_tables.b4p in B4P_Examples.zip. Decompress before use.
Move Transaction Example
  table initialize( animals1,
      { { Name,  Leg count, Special ability  },
        { Owl,   2,         Night vision     },
        { Dog,   4,         Sensitive nose   },
        { Ape,   2 or 4,    Relatively smart } } );

  table initialize( animals2,
      { { Name,  Leg count, Special ability, Weapon  },
        { Bee,   6,         Makes honey,     sting   },
        { Gnu,   4,         Knows UNIX,      horns   },
        { Cat,   4,         Empathy to humans, sharp claws } } );

  [ animals2: Name, Gnu, .. ] <<= [ animals1: Name, Dog, ];  // Dog overwrites Gnu, Dog erased from animals 1
  [ animals1: Name, Owl,    ] <<= [ animals2: Name, Bee, .. ]; // Open ended items, also copy the weapon

  echo("Contains the bee, owl and dog removed");
  table list( animals1 ); // No additional column created
  
  echo("Contains the dog, gnu and bee removed");
  table list( animals2 ); // Additional column created
Contains the bee, owl and dog removed
    0 : Name | Leg count | Special ability  | Weapon
    1 : Bee  | 6         | Makes honey      | sting
    2 : Ape  | 2 or 4    | Relatively smart |       

Contains the dog, gnu and bee removed
    0 : Name | Leg count | Special ability   | Weapon     
    1 : Dog  | 4         | Sensitive nose    |            
    2 : Cat  | 4         | Empathy to humans | sharp claws

Try it yourself: Open LAN_Features_Transactions_between_tables_01.b4p in B4P_Examples.zip. Decompress before use.
Swap Transaction Example
  table initialize( animals1,
      { { Name,  Surface,   Leg count, Special ability  },
        { Owl,   Feathers,  2,         Night vision     },
        { Dog,   Fur,       4,         Sensitive nose   },
        { Ape,   Hair,      2 or 4,    Relatively smart } } );

  table initialize( animals2,
      { { Name,  Leg count, Special ability, Weapon  },
        { Bee,   6,         Makes honey,     sting   },
        { Gnu,   4,         Knows UNIX,      horns   },
        { Cat,   4,         Empathy to humans, sharp claws } } );

  [ animals2: Name, Cat, .. ] <=> [ animals1: Name, Dog,  ];
  [ animals2: Name, Bee, Name..Special ability ] <=> [ animals1: Name, Owl, ]; // Excludes the weapon
  
  echo("Contains Bee, Cat and Ape");
  table list( animals1 ); // No additional column created
  
  echo("Contains Owl, Gnu and Dog");
  table list( animals2 ); // Additional column created
Contains Bee, Cat and Ape
    0 : Name | Surface | Leg count | Special ability   | Weapon     
    1 : Bee  |         | 6         | Makes honey       |            
    2 : Cat  |         | 4         | Empathy to humans | sharp claws
    3 : Ape  | Hair    | 2 or 4    | Relatively smart  |            

Contains Owl, Gnu and Dog
    0 : Name | Leg count | Special ability | Weapon | Surface
    1 : Owl  | 2         | Night vision    |        |        
    2 : Gnu  | 4         | Knows UNIX      | horns  |        
    3 : Dog  | 4         | Sensitive nose  |        | Fur    

Try it yourself: Open LAN_Features_Transactions_between_tables_02.b4p in B4P_Examples.zip. Decompress before use.