& 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.

Table rules: See section on Transactions between Tables.
The only exception is that tables are not cleared, and the open-ended ranges rule does not apply for the & assignment operator. The ^ suffix has no impact on table-to-table transactions.

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, .. ] &<== [ animals2: Name, Cat, ]; // sharp claws will not be copied due to &-Rules
  [ animals1: {Name,Leg count}, 3 ]  &<== [ animals2: Name, Bee, { Weapon, Name } ]; // Only name will be transferred: Bee
  [ animals2: Name, 2 ] &<== [ animals1: Special ability, 1 ]; // No transaction due to mismatching headers

  table list( animals1 ); // Cat with sensitive nose, relatively smart bee
  table list( animals2 ); // No changes.  Gnu (row 1) stays a gnu.
    0 : Name | Leg count | Special ability  
    1 : Owl  | 2         | Night vision     
    2 : Cat  | 4         | Empathy to humans
    3 : Bee  | 2 or 4    | Relatively smart

    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_AND_Transactions_between_tables.b4p in B4P_Examples.zip. Decompress before use.
Move 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, Gnu, .. ] &<<= [ animals1: Name, Dog, Special ability ];  // Gnu with sensitive nose
  [ animals1: Name, Owl,    ] &<<= [ animals2: Name, Bee, .. ]; // Bee without sting

  table list( animals1 );  // Without dog
  table list( animals2 );  // Without bee
    0 : Name | Surface  | Leg count | Special ability
    1 : Bee  | Feathers | 6         | Makes honey     
    2 : Ape  | Hair     | 2 or 4    | Relatively smart

    0 : Name | Leg count | Special ability   | Weapon     
    1 : Gnu  | 4         | Sensitive nose    | horns      
    2 : Cat  | 4         | Empathy to humans | sharp claws

Try it yourself: Open LAN_Features_AND_Transactions_between_tables_01.b4p in B4P_Examples.zip. Decompress before use.
Swap Transaction Example
  table initialize( animals1,
      { { Name,  Leg count, Special ability, Strange habit  },
        { Owl,   2,         Night vision, night time noise in the forest     },
        { Dog,   4,         Sensitive nose, "Man's best friend"   },
        { Ape,   2 or 4,    Relatively smart, often funny } } );

  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
  
  table list( animals1 ); // No additional column created
  table list( animals2 ); // No additional column created
    0 : Name | Leg count | Special ability   | Strange habit                 
    1 : Bee  | 6         | Makes honey       | night time noise in the forest
    2 : Cat  | 4         | Empathy to humans | Man's best friend             
    3 : Ape  | 2 or 4    | Relatively smart  | often funny                   

    0 : Name | Leg count | Special ability | Weapon     
    1 : Owl  | 2         | Night vision    | sting      
    2 : Gnu  | 4         | Knows UNIX      | horns      
    3 : Dog  | 4         | Sensitive nose  | sharp claws

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