Ignore:
Timestamp:
2010-12-07T16:55:10+13:00 (13 years ago)
Author:
max
Message:

Added (for GDBM and Sqlite) set_infodb_entry to directly change one value in the database. Print statements added to other versions of function, alterting that they are not implemented.

Also added read_infodb_rawentry. Plus, more efficient way of accessing a single record from Sqlite.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/dbutil/sqlite.pm

    r23166 r23399  
    3737{
    3838  my $infodb_file_path = shift(@_);
    39 
     39  my $opt_append = shift(@_);
     40 
    4041  my $sqlite3_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "sqlite3" . &util::get_os_exe());
    4142  my $infodb_handle = undef;
     
    5657  binmode($infodb_handle,":utf8");
    5758
     59  if (!(defined $opt_append) || ($opt_append ne "append")) {
     60    print $infodb_handle "DROP TABLE IF EXISTS data;\n";
     61    print $infodb_handle "DROP TABLE IF EXISTS document_metadata;\n";
     62  }
     63   
    5864  print $infodb_handle "CREATE TABLE IF NOT EXISTS data (key TEXT PRIMARY KEY, value TEXT);\n";
    5965  print $infodb_handle "CREATE TABLE IF NOT EXISTS document_metadata (id INTEGER PRIMARY KEY, docOID TEXT, element TEXT, value TEXT);\n";
     
    161167      $infodb_map->{$key} = 1;
    162168  }
     169}
     170
     171sub read_infodb_rawentry
     172{
     173  my $infodb_file_path = shift(@_);
     174  my $infodb_key = shift(@_);
     175
     176 
     177  my $key_safe = &sqlite_safe($infodb_key);
     178  my $select_val_cmd = "SELECT value FROM data WHERE key='$key_safe';";
     179
     180  my $val_str = read_infodb_cmd($infodb_file_path,$select_val_cmd);
     181
     182  return $val_str
     183}
     184
     185sub read_infodb_entry
     186{
     187  my $infodb_file_path = shift(@_);
     188  my $infodb_key = shift(@_);
     189
     190  my $val_str = read_infodb_rawentry($infodb_file_path,$infodb_key);
     191 
     192  my $rec_hash = &dbutil::convert_infodb_string_to_hash($val_str);
     193
     194  return $rec_hash;
    163195}
    164196
     
    216248}
    217249
    218 
    219 
    220250sub write_infodb_rawentry
    221251{
     
    227257  print $infodb_handle "INSERT OR REPLACE INTO data (key, value) VALUES ('" . $safe_infodb_key . "', '" . &sqlite_safe($infodb_val) . "');\n";
    228258}
     259
     260
     261sub set_infodb_entry
     262{
     263    my $infodb_file_path = shift(@_);
     264    my $infodb_key = shift(@_);
     265    my $infodb_map = shift(@_);
     266   
     267  my $sqlite3_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "sqlite3" . &util::get_os_exe());
     268  my $infodb_handle = undef;
     269  my $cmd = "\"$sqlite3_exe\" \"$infodb_file_path\"";
     270
     271  if (!-e "$sqlite3_exe" || !open($infodb_handle, "|$cmd"))
     272  {
     273      print STDERR "Unable to execute: $cmd\n";
     274      print STDERR "$!\n";
     275      return -1;
     276  }
     277  else {
     278
     279      binmode($infodb_handle, ":utf8");
     280      write_infodb_entry($infodb_handle,$infodb_key,$infodb_map);
     281      close($infodb_handle);
     282  }
     283
     284  # Not currently checking for errors on write to DB
     285  return 0;
     286 
     287 
     288    }
     289   
    229290
    230291
Note: See TracChangeset for help on using the changeset viewer.