package Common::DB::Item::TableState;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::DB::ItemCollection;
use Common::DB::Item;
use base 'Common::DB::Item';

use constant kTable => 'table_state';
use constant kDB    => Common::DB::Item::kCommonDB();

sub MarkTableAsChanged {
    my ( $class, $tableName ) = @_;

    # I need to be able to update the token atomically.
    # This means also being able to insert into the table.
    # I might be forced to use a lock... which would suck.
    #

    # !!! Basically, I am not going to try and be tricky and create a row
    # !!! for this table if there isn't one.
    # !!! If we want to track state for a table, we will need to seed the
    # !!! table_state table with a row for that table....
    #
    my $dbo = Common::RSApp::GetCommonDB();

    my $sql = "UPDATE " . kTable . " SET state_token=(state_token+1) WHERE table_name=" . $dbo->DBQuote($tableName);
    $dbo->DoCmd($sql);
}

sub GetTokenForTable {
    my ( $class, $tableName ) = @_;

    my $obj = Common::DB::Item::TableState->Lookup( table_name => $tableName );
    return undef unless $obj;
    return $obj->state_token;
}

1;
