# Copyright (c) 2004  Joshua Hoblitt
#
# $Id: SQL.pm,v 1.1.1.1 2004-12-22 02:16:24 jhoblitt Exp $

package PS::IPP::IData::Server::SQL;

use strict;

our $VERSION = '0.01';

use base qw( Class::Accessor::Fast );

my %sql = (
    set_transaction_model   => qq{
        SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
    },
    update_instance_uri => qq{
        UPDATE instance SET uri = ? WHERE ins_id = ?
    },
    last_insert_id      => qq{
        SELECT LAST_INSERT_ID()
    },
    new_object          => qq{
        INSERT INTO storage_object
        VALUES (NULL, ?, ?, ?, 0, NULL, NULL)
    },
    delete_object       => qq{
        DELETE FROM storage_object
        WHERE so_id = ?
    },
    new_object_instance  => qq{
        INSERT INTO instance 
        VALUES (NULL, LAST_INSERT_ID(), 'error', NULL, NULL, NULL, NULL)
    },
    new_instance        => qq{
        INSERT INTO instance
        VALUES (NULL, ?, 'error', NULL,NULL, NULL, NULL)
    },
    get_object          => qq{
        SELECT *
        FROM storage_object
        WHERE ext_id = ?
    },
    get_object_locks    => qq{
        SELECT so_id, read_lock, write_lock 
        FROM storage_object
        WHERE ext_id = ?
        FOR UPDATE
    },
    set_write_lock      => qq{
        UPDATE storage_object
        SET write_lock = 'write'
        WHERE ext_id = ?
    },
    delete_write_lock   => qq{
        UPDATE storage_object
        SET write_lock = NULL
        WHERE ext_id = ?
    },
    increment_read_lock => qq{
        UPDATE storage_object
        SET read_lock = read_lock+1
        WHERE ext_id = ?
    },
    decrement_read_lock => qq{
        UPDATE storage_object
        SET read_lock = read_lock-1
        WHERE ext_id = ?
    },
    delete_instance     => qq{
        DELETE FROM instance
        WHERE uri = ?
    },
    get_object_from_uri   => qq{
        SELECT so_id
        FROM instance
        WHERE uri = ?
    },
    get_instance_count   => qq{
        SELECT count(ins_id)
        FROM instance
        WHERE so_id = ?
    },
    get_object_instances    => qq{
        SELECT storage_object.so_id, uri, assigned_location
        FROM storage_object
        LEFT JOIN instance
        USING (so_id)
        WHERE storage_object.ext_id = ?
    },
);

__PACKAGE__->mk_ro_accessors( keys %sql );

sub new {
    my $class = shift;

    my $self = \%sql;

    bless $self, $class || ref $class;

    return $self;
}

1;

__END__
