App-Chart

 view release on metacpan or  search on metacpan

devel/convert-scm.pl  view on Meta::CPAN

      if ($key eq 'annotation') {
        my ($key, $date, $str) = @$form;
        $date = App::Chart::tdate_to_iso ($date);
        my $id = $annotation_id++;
        my $sth = $nbh->prepare_cached
          ('INSERT INTO annotation (symbol, id, date, note) VALUES (?,?,?,?)');
        $sth->execute ($symbol, $id, $date, $str);
        $sth->finish();

      } elsif ($key eq 'line' || $key eq 'hline') {
        my ($key, $date1, $price1, $date2, $price2) = @$form;
        my $id = $line_id++;
        $date1 = App::Chart::tdate_to_iso ($date1);
        $date2 = App::Chart::tdate_to_iso ($date2);
        $price1  = insert_decimal ($price1, $decimals);
        $price2  = insert_decimal ($price2, $decimals);
        my $sth = $nbh->prepare_cached
          ('INSERT INTO line (symbol, id, date1, price1, date2, price2, horizontal)
            VALUES (?,?,?,?,?,?,?)');
        $sth->execute ($symbol, $id, $date1, $price1, $date2, $price2,
                       ($key eq 'hline' ? 1 : 0));
        $sth->finish();

      } elsif ($key eq 'alert-above' || $key eq 'alert-below') {
        my ($key, $price) = @$form;
        my $id = $alert_id++;
        $price  = insert_decimal ($price, $decimals);
        my $sth = $nbh->prepare_cached
          ('INSERT INTO alert (symbol, id, price, above) VALUES (?,?,?,?)');
        $sth->execute ($symbol, $id, $price, ($key eq 'alert-above' ? 1 : 0));
        $sth->finish();

      } else {
        print "unrecognised note: ", Dumper(\$form);
      }
    }
  }
}

sub convert_prefs {
  print "prefs\n";

  my $filename = "$ENV{HOME}/Chart/prefs.scm";
  my $content = File::Slurp::slurp ($filename);

  $content =~ s/\#f/""/g;
  my $forms = Lisp::Reader::lisp_read($content);

  foreach my $form (@$forms) {
    my $key = $form->[0];
    if ($key eq 'favourites') {
      my ($key, $list) = @$form;
      my $sth = $nbh->prepare_cached
        ('INSERT INTO symlist_content (key, pos, symbol) VALUES (?,?,?)');
      foreach my $pos (0 .. $#$list) {
        $sth->execute ('favourites', $pos, $list->[$pos]);
        $sth->finish();
      }

    } elsif ($key eq 'lme-username'
             || $key eq 'lme-password'
             || $key eq 'yahoo-quote-host'
             || $key eq 'yahoo-quote-timezone'
             || $key eq 'commsec-data-url'
             || $key eq 'commsec-enabled') {
      my ($key, $value) = @$form;
      if ($key eq 'commsec-enabled') {
        $key = 'commsec-enable';
        if ($value eq 'yes') { $value = 1; }
        if ($value eq 'no') { $value = 0; }
      }
      my $sth = $nbh->prepare_cached
        ('INSERT INTO preference (key, value) VALUES (?,?)');
      $sth->execute ($key, $value);
      $sth->finish();

    } else {
      print "(skip $key)\n";
    }
  }
}

sub convert_historical {
  require App::Chart::Gtk2::Symlist::Historical;
  require App::Chart::Gtk2::Symlist::All;
  my $historical_symlist = App::Chart::Gtk2::Symlist::Historical->instance;
  my $all_symlist = App::Chart::Gtk2::Symlist::All->instance;

  my $filename = "$ENV{HOME}/Chart/cache/historical-symbols";
  if (! -e $filename) { return; }
  my $content = File::Slurp::slurp ($filename);
  $content =~ s/\#f/""/g;
  my $forms = Lisp::Reader::lisp_read($content);

  my $sth = $dbh->prepare_cached
    ('UPDATE info SET historical=1 WHERE symbol=?');
  foreach my $symbol (@{$forms->[0]}) {
    if (App::Chart::Database->symbol_exists ($symbol)) {
      print "historical $symbol\n";
      $sth->execute ($symbol);
      $sth->finish;

      $historical_symlist->insert_symbol ($symbol);
    }
  }

  foreach my $symbol (App::Chart::Database->symbols_list()) {
    if (! App::Chart::Database->symbol_is_historical ($symbol)) {
      $all_symlist->insert_symbol ($symbol);
    }
  }
}

$dbh->begin_work;
$nbh->begin_work;
$dbh->do ('PRAGMA synchronous = OFF');
$dbh->do ('PRAGMA cache_size = 20000'); # of 1.5k pages, is 30Mb

convert_prefs();

#convert_notes ('BHP.AX','BBW.AX','ERA.AX','TEL.NZ');



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