sub initialize {
$log = 'OpenSRF::Utils::Logger';
+
+ $sc = OpenSRF::Utils::SettingsClient->new;
+
+ my $dbfile = $sc->config_value( apps => persist => app_settings => 'dbfile');
+ unless ($dbfile) {
+ throw OpenSRF::EX::PANIC ("Can't find my dbfile for SQLite!");
+ }
+
+ my $init_dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
+ $init_dbh->{AutoCommit} = 1;
+ $init_dbh->{RaiseError} = 0;
+
+ $init_dbh->do( <<" SQL" );
+ CREATE TABLE storage (
+ id INTEGER PRIMARY KEY,
+ name_id INTEGER,
+ value TEXT
+ );
+ SQL
+
+ $init_dbh->do( <<" SQL" );
+ CREATE TABLE store_name (
+ id INTEGER PRIMARY KEY,
+ name TEXT UNIQUE
+ );
+ SQL
+
}
sub child_init {
$dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
$dbh->{AutoCommit} = 1;
- $dbh->{RaiseError} = 1;
-
- eval {
- $dbh->do( <<" SQL" );
- CREATE TABLE storage (
- id INT PRIMARY KEY,
- name_id INT,
- value TEXT
- );
- SQL
-
- $dbh->do( <<" SQL" );
- CREATE TABLE store_name (
- id INT PRIMARY KEY,
- name TEXT UNIQUE
- );
- SQL
- };
+ $dbh->{RaiseError} = 0;
+
}
sub create_store {
my $name = shift || '';
- eval {
- my $sth = $dbh->prepare("INSERT INTO store_name (name) VALUES (?)");
- $sth->execute($name);
- $sth->finish;
+
+ my $continue = 0;
+ try {
+ _get_name_id($name);
+
+ } catch Error with {
+ $continue++;
};
- if ($@) {
- throw OpenSRF::EX::WARN ("Duplicate key: object name [$name] already exists! " . $dbh->errstr);
- }
+
+ throw OpenSRF::EX::WARN ("Duplicate key: object name [$name] already exists! " . $dbh->errstr)
+ unless ($continue);
+
+ my $sth = $dbh->prepare("INSERT INTO store_name (name) VALUES (?);");
+ $sth->execute($name);
+ $sth->finish;
unless ($name) {
- my $last_id = $dbh->last_insert_id();
+ my $last_id = $dbh->last_insert_id(undef, undef, 'store_name', 'id');
$name = 'AUTOGENERATED!!'.$last_id;
$dbh->do("UPDATE store_name SET name = '$name' WHERE id = '$last_id';");
}
my $self = shift;
my $client = shift;
- my $name = shift or throw OpenSRF::EX::WARN ("No queue name specified!");
+ my $name = shift or do {
+ throw OpenSRF::EX::WARN ("No name specified!");
+ };
my $value = shift || '';
my $name_id = _get_name_id($name);
if ($self->api_name =~ /object/) {
- $dbh->do('DELETE FROM storage WHERE name_id = ?', {}, $name_id);
+ $dbh->do('DELETE FROM storage WHERE name_id = ?;', {}, $name_id);
}
$dbh->do('INSERT INTO storage (name_id,value) VALUES (?,?);', {}, $name_id, JSON->perl2JSON($value));
);
sub _get_name_id {
- my $name = shift or throw OpenSRF::EX::WARN ("No queue name specified!");
+ my $name = shift or do {
+ throw OpenSRF::EX::WARN ("No queue name specified!");
+ };
- my $name_id = $dbh->selectcol_arrayref("SELECT id FROM store_name WHERE name = ?", {}, $name)->[0];
- unless ($name_id) {
+ my $name_id = $dbh->selectrow_arrayref("SELECT id FROM store_name WHERE name = ?;", {}, $name);
+
+ if (!ref($name_id) || !defined($name_id->[0])) {
throw OpenSRF::EX::WARN ("Object name [$name] does not exist!");
}
- return $name_id;
+ return $name_id->[0];
}
sub destroy_store {
my $name_id = _get_name_id($name);
- $dbh->do("DELETE FROM storage WHERE name_id = ?", {}, $name_id);
- $dbh->do("DELETE FROM store_name WHERE id = ?", {}, $name_id);
+ $dbh->do("DELETE FROM storage WHERE name_id = ?;", {}, $name_id);
+ $dbh->do("DELETE FROM store_name WHERE id = ?;", {}, $name_id);
+
}
__PACKAGE__->register_method(
api_name => 'opensrf.persist.slot.destroy',
sub _flush_by_name {
my $name = shift;
if ($name =~ /^AUTOGENERATED!!/) {
- my $count = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM storage WHERE name = ?", {}, $name);
+ my $name_id = _get_name_id($name);
+ my $count = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM storage WHERE name_id = ?;", {}, $name_id);
if (!ref($count) || $$count[0] == 0) {
- $dbh->do("DELETE FROM store_name WHERE name = ?", {}, $name);
+ $dbh->do("DELETE FROM store_name WHERE name = ?;", {}, $name);
}
}
}
my $self = shift;
my $client = shift;
- my $name = shift or throw OpenSRF::EX::WARN ("No queue name specified!");
+ my $name = shift or do {
+ throw OpenSRF::EX::WARN ("No queue name specified!");
+ };
my $name_id = _get_name_id($name);
- my $value = $dbh->selectrow_arrayref('SELECT id, value FROM storage WHERE name_id = ? ORDER BY id ASC LIMIT 1', {}, $name_id);
- $dbh->do('DELETE FROM storage WHERE id = ?',{}, $value->[0]);
+ my $value = $dbh->selectrow_arrayref('SELECT id, value FROM storage WHERE name_id = ? ORDER BY id ASC LIMIT 1;', {}, $name_id);
+ $dbh->do('DELETE FROM storage WHERE id = ?;',{}, $value->[0]);
_flush_by_name($name);
+
return JSON->JSON2perl( $value->[1] );
}
__PACKAGE__->register_method(
my $self = shift;
my $client = shift;
- my $name = shift or throw OpenSRF::EX::WARN ("No queue name specified!");
+ my $name = shift or do {
+ throw OpenSRF::EX::WARN ("No queue name specified!");
+ };
my $name_id = _get_name_id($name);
- my $value = $dbh->selectrow_arrayref('SELECT id, value FROM storage WHERE name_id = ? ORDER BY id DESC LIMIT 1', {}, $name_id);
- $dbh->do('DELETE FROM storage WHERE id = ?',{}, $value->[0]);
+ my $value = $dbh->selectrow_arrayref('SELECT id, value FROM storage WHERE name_id = ? ORDER BY id DESC LIMIT 1;', {}, $name_id);
+ $dbh->do('DELETE FROM storage WHERE id = ?;',{}, $value->[0]);
_flush_by_name($name);
+
return JSON->JSON2perl( $value->[1] );
}
__PACKAGE__->register_method(
my $self = shift;
my $client = shift;
- my $name = shift or throw OpenSRF::EX::WARN ("No queue name specified!");
+ my $name = shift or do {
+ throw OpenSRF::EX::WARN ("No object name specified!");
+ };
+
my $name_id = _get_name_id($name);
- my $value = $dbh->selectrow_arrayref('SELECT name_id, value FROM storage WHERE name_id = ? ORDER BY id DESC LIMIT 1', {}, $name_id);
+ my $value = $dbh->selectrow_arrayref('SELECT name_id, value FROM storage WHERE name_id = ? ORDER BY id DESC LIMIT 1;', {}, $name_id);
$dbh->do('DELETE FROM storage WHERE name_id = ?',{}, $value->[0]);
_flush_by_name($name);
+
return JSON->JSON2perl( $value->[1] );
}
__PACKAGE__->register_method(