App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Database.pm  view on Meta::CPAN

  my %hash = ();
  @hash{@$aref} = 1;
  return \%hash;
}

sub symbols_list {
  # my ($class) = @_;
  my $dbh = App::Chart::DBI->instance;
  my $sth = $dbh->prepare_cached('SELECT symbol FROM info');
  my $aref = $dbh->selectcol_arrayref ($sth);
  $sth->finish();
  return @$aref;
}

sub symbol_is_historical {
  my ($class, $symbol) = @_;
  my $dbh = App::Chart::DBI->instance;
  my $sth = $dbh->prepare_cached('SELECT historical FROM info WHERE symbol=?');
  my $aref = $dbh->selectrow_arrayref ($sth, undef, $symbol);
  return ($aref && $aref->[0]);
}

sub symbol_name {
  my ($class, $symbol) = @_;
  return read_single ('SELECT name FROM info WHERE symbol=?', $symbol);
}

sub symbol_decimals {
  my ($class, $symbol) = @_;
  return (read_single ('SELECT decimals FROM info WHERE symbol=?', $symbol)
          || 0);
}

sub write_extra {
  my ($class, $symbol, $key, $value) = @_;
  if (! defined $key) { croak 'write_extra() key cannot be undef'; }

  my $dbh = App::Chart::DBI->instance;
  if (defined $value) {
    my $sth = $dbh->prepare_cached
      ('INSERT OR REPLACE INTO extra (symbol, key, value) VALUES (?,?,?)');
    $sth->execute ($symbol, $key, $value);
    $sth->finish;
  } else {
    $dbh->do ('DELETE FROM extra WHERE symbol=? AND key=?',
              undef,
              $symbol, $key);
  }
}

sub read_extra {
  my ($class, $symbol, $key) = @_;
  return read_single ('SELECT value FROM extra WHERE symbol=? AND key=?',
                      $symbol, $key);
}

# An eval isn't backtrace friendly, but a __DIE__ handler would be reached
# by possible normal errors caught by a handler in $subr.
#
# rollback() can get errors too, like database gone away.  They end up
# thrown in preference to the original error.
#
sub call_with_transaction {
  my ($dbh, $subr) = @_;
  my $hold = App::Chart::chart_dirbroadcast()->hold;

  if ($dbh->{AutoCommit}) {
    my $ret;
    $dbh->begin_work;
    if (eval { $ret = $subr->(); 1 }) {
      $dbh->commit;
      return $ret;
    } else {
      my $err = $@;
      $dbh->rollback;
      die $err;
    }

  } else {
    $subr->();
  }
}

sub preference_get {
  my ($class, $key, $default) = @_;
  my $value = read_notes_single
    ('SELECT value FROM preference WHERE key=?', $key);
  if (defined $value) {
    return $value;
  } else {
    return $default;
  }
}


1;
__END__

=for stopwords delisted

=head1 NAME

App::Chart::Database -- database functions

=head1 FUNCTIONS

=over 4

=item C<< App::Chart::Database->add_symbol ($symbol) >>

Add C<$symbol> to the database.  If C<$symbol> is already in the database
then remove its "historical" marker.

=item C<< App::Chart::Database->delete_symbol ($symbol, $notes_too) >>

Delete all data relating to C<$symbol> from the database.  If C<$notes_too>
is given and it's true then delete user notes and annotations too.

=back

=head2 Symbol Info



( run in 0.690 second using v1.01-cache-2.11-cpan-39bf76dae61 )