From 6f1943313a0e503189dd9685483a9ecd1bd7cc6e Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Thu, 1 Oct 2015 01:05:11 +0200
Subject: introduced rhrd context


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index 894c03e..9155d8e 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -31,12 +31,12 @@ use DBI;
 use constant {
   DB_VERSION => 245,
   RD_CONFIG_FILE => '/etc/rd.conf',
-  DROPBOX_PSEUDO_STATION_NAME => 'import-dropbox',
   RD_MIN_CART => 1,
   RD_MAX_CART => 999999,
   RD_MIN_CUT => 1,
   RD_MAX_CUT => 999,
 
+  RHRD_DROPBOX_PSEUDO_STATION_NAME => 'import-dropbox',
   RHRD_SYSTEM_MACROS_GROUP => 'SYSTEM',
   RHRD_SHOW_MACROS_GROUP => 'SHOWS',
   RHRD_ALLSHOWS_GROUP => 'ALL_SHOWS',
@@ -44,7 +44,27 @@ use constant {
   RHRD_ALLJINGLES_GROUP => 'ALL_JINGLE',
 };
 
-###########################  connection handling  ###########################
+###########################  context handling  ###########################
+
+sub init
+{
+  my %ctx;
+
+  my ($dbh, $status, $errorstring) = opendb();
+  unless(defined $dbh) {
+    return ($dbh, $status, $errorstring);
+  }
+  $ctx{'dbh'} = $dbh;
+
+  return \%ctx;
+}
+
+sub destroy
+{
+  my ($ctx) = @_;
+
+  closedb($ctx->{'dbh'});
+}
 
 sub opendb
 {
@@ -91,11 +111,11 @@ sub closedb
 
 sub get_cart_range
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   my $sql = qq{select DEFAULT_LOW_CART, DEFAULT_HIGH_CART, DEFAULT_TITLE from GROUPS where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -110,13 +130,13 @@ sub get_cart_range
 
 sub get_next_free_slot
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my ($group_low_cart, $group_high_cart, $group_chunksize) = get_cart_range($dbh, $groupname);
+  my ($group_low_cart, $group_high_cart, $group_chunksize) = get_cart_range($ctx, $groupname);
 
   my $sql = qq{select NAME, DEFAULT_LOW_CART, DEFAULT_HIGH_CART from GROUPS where NAME != ? and DEFAULT_LOW_CART >= ? and DEFAULT_HIGH_CART <= ? order by DEFAULT_LOW_CART;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname, $group_low_cart, $group_high_cart)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -144,11 +164,11 @@ sub get_next_free_slot
 
 sub get_token
 {
-  my ($dbh, $username) = @_;
+  my ($ctx, $username) = @_;
 
   my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -164,14 +184,14 @@ sub get_token
 
 sub set_token
 {
-  my ($dbh, $username, $token) = @_;
+  my ($ctx, $username, $token) = @_;
   if(!defined $token || $token eq '') {
     return (undef, 'ERROR', "empty token is not allowed")
   }
 
   my $sql = qq{update USERS set PASSWORD = ? where LOGIN_NAME = ?;};
-  my $rows = $dbh->do($sql, undef, $token, $username)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $rows = $ctx->{'dbh'}->do($sql, undef, $token, $username)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   unless($rows == 1) {
     return (undef, 'ERROR', "user '" . $username . "' not known by rivendell")
@@ -181,14 +201,14 @@ sub set_token
 
 sub check_token
 {
-  my ($dbh, $username, $token) = @_;
+  my ($ctx, $username, $token) = @_;
   if(!defined $token || $token eq '') {
     return (undef, 'ERROR', "empty token is not allowed")
   }
 
   my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -210,7 +230,7 @@ sub check_token
 
 sub add_user
 {
-  my ($dbh, $username, $token, $fullname) = @_;
+  my ($ctx, $username, $token, $fullname) = @_;
   if(!defined $token || $token eq '') {
     return (undef, 'ERROR', "empty token is not allowed")
   }
@@ -219,8 +239,8 @@ sub add_user
   }
 
   my $sql = qq{insert into USERS (LOGIN_NAME, FULL_NAME, PHONE_NUMBER, DESCRIPTION, PASSWORD, ENABLE_WEB, ADMIN_USERS_PRIV, ADMIN_CONFIG_PRIV, CREATE_CARTS_PRIV, DELETE_CARTS_PRIV, MODIFY_CARTS_PRIV, EDIT_AUDIO_PRIV, ASSIGN_CART_PRIV, CREATE_LOG_PRIV, DELETE_LOG_PRIV, DELETE_REC_PRIV, PLAYOUT_LOG_PRIV, ARRANGE_LOG_PRIV, MODIFY_TEMPLATE_PRIV, ADDTO_LOG_PRIV, REMOVEFROM_LOG_PRIV, CONFIG_PANELS_PRIV, VOICETRACK_LOG_PRIV, EDIT_CATCHES_PRIV, ADD_PODCAST_PRIV, EDIT_PODCAST_PRIV, DELETE_PODCAST_PRIV) values ( ?, ?, "", "", ? , "N", "N", "N", "Y", "Y", "Y", "Y", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N");};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   my $cnt = $sth->execute($username, $fullname, $token)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -231,7 +251,7 @@ sub add_user
 
 sub remove_user
 {
-  my ($dbh, $username) = @_;
+  my ($ctx, $username) = @_;
 
   my @actions = ({
       # Delete RSS Feed Perms
@@ -256,8 +276,8 @@ sub remove_user
     });
 
   for my $href (@actions) {
-    my $sth = $dbh->prepare($href->{sql})
-      or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+    my $sth = $ctx->{'dbh'}->prepare($href->{sql})
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
     delete($href->{sql});
 
     $href->{cnt} = $sth->execute($username)
@@ -271,11 +291,11 @@ sub remove_user
 
 sub check_user
 {
-  my ($dbh, $username) = @_;
+  my ($ctx, $username) = @_;
 
   my $sql = qq{select count(*) from USERS where LOGIN_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -285,8 +305,8 @@ sub check_user
 
   if ($cnt != 0) {
     $sql = qq{select count(*) from STATIONS where DEFAULT_NAME = ?;};
-    my $sth = $dbh->prepare($sql)
-      or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+    my $sth = $ctx->{'dbh'}->prepare($sql)
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
     $sth->execute($username)
       or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -303,11 +323,11 @@ sub check_user
 
 sub get_fullname
 {
-  my ($dbh, $username) = @_;
+  my ($ctx, $username) = @_;
 
   my $sql = qq{select FULL_NAME from USERS where LOGIN_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -323,11 +343,11 @@ sub get_fullname
 
 sub list_users
 {
-  my ($dbh) = @_;
+  my ($ctx) = @_;
 
   my $sql = qq{select LOGIN_NAME from USERS order by LOGIN_NAME;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute()
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -345,14 +365,14 @@ sub list_users
 
 sub add_group
 {
-  my ($dbh, $groupname, $description) = @_;
+  my ($ctx, $groupname, $description) = @_;
   if(!defined $description) {
     $description = '';
   }
 
   my $sql = qq{insert into GROUPS (NAME, DESCRIPTION) values (?, ?);};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   my $cnt = $sth->execute($groupname, $description)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -363,7 +383,7 @@ sub add_group
 
 sub remove_group
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   my @actions = ({
       # Delete Member Carts
@@ -398,8 +418,8 @@ sub remove_group
     });
 
   for my $href (@actions) {
-    my $sth = $dbh->prepare($href->{sql})
-      or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+    my $sth = $ctx->{'dbh'}->prepare($href->{sql})
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
     delete($href->{sql});
 
     $href->{cnt} = $sth->execute($groupname)
@@ -413,11 +433,11 @@ sub remove_group
 
 sub check_group
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   my $sql = qq{select count(*) from GROUPS where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -427,8 +447,8 @@ sub check_group
 
   if ($cnt != 0) {
     $sql = qq{select count(*) from CART where GROUP_NAME = ?;};
-    my $sth = $dbh->prepare($sql)
-      or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+    my $sth = $ctx->{'dbh'}->prepare($sql)
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
     $sth->execute($groupname)
       or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -445,11 +465,11 @@ sub check_group
 
 sub get_group_carts
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   my $sql = qq{select DEFAULT_LOW_CART, DEFAULT_HIGH_CART, DEFAULT_CART_TYPE, ENFORCE_CART_RANGE from GROUPS where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -464,7 +484,7 @@ sub get_group_carts
 
 sub set_group_carts
 {
-  my ($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;
+  my ($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;
   if(!defined $low_cart) {
     $low_cart = 0;
   }
@@ -479,8 +499,8 @@ sub set_group_carts
   }
 
   my $sql = qq{update GROUPS set DEFAULT_LOW_CART = ?, DEFAULT_HIGH_CART = ?, DEFAULT_CART_TYPE  = ? , ENFORCE_CART_RANGE = ? where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   my $cnt = $sth->execute($low_cart, $high_cart, $cart_type, $enforce_cart_range, $groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -492,11 +512,11 @@ sub set_group_carts
 
 sub get_group_reports
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   my $sql = qq{select ENABLE_NOW_NEXT, REPORT_TFC, REPORT_MUS from GROUPS where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -511,7 +531,7 @@ sub get_group_reports
 
 sub set_group_reports
 {
-  my ($dbh, $groupname, $now_next, $traffic, $music) = @_;
+  my ($ctx, $groupname, $now_next, $traffic, $music) = @_;
   if(!defined $now_next) {
     $now_next = 'N';
   }
@@ -523,8 +543,8 @@ sub set_group_reports
   }
 
   my $sql = qq{update GROUPS set ENABLE_NOW_NEXT = ?, REPORT_TFC = ?, REPORT_MUS = ? where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   my $cnt = $sth->execute($now_next, $traffic, $music, $groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -536,11 +556,11 @@ sub set_group_reports
 
 sub get_group_members
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   my $sql = qq{select USER_NAME from USER_PERMS where GROUP_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -556,11 +576,11 @@ sub get_group_members
 
 sub is_group_member
 {
-  my ($dbh, $groupname, $username) = @_;
+  my ($ctx, $groupname, $username) = @_;
 
   my $sql = qq{select count(*) from USER_PERMS where GROUP_NAME = ? and USER_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($groupname, $username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -573,11 +593,11 @@ sub is_group_member
 
 sub add_group_member
 {
-  my ($dbh, $groupname, $username) = @_;
+  my ($ctx, $groupname, $username) = @_;
 
   my $sql = qq{select count(*) from USERS where LOGIN_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -588,14 +608,14 @@ sub add_group_member
     return (undef, 'ERROR', "user '" . $username . "' does not exist");
   }
 
-  ($cnt, my $result, my $errostring) = is_group_member($dbh, $groupname, $username);
+  ($cnt, my $result, my $errostring) = is_group_member($ctx, $groupname, $username);
   if($cnt > 0) {
     return (undef, 'ERROR', "already a member");
   }
 
   $sql = qq{insert into USER_PERMS (GROUP_NAME, USER_NAME) values (?, ?);};
-  $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $cnt = $sth->execute($groupname, $username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -606,11 +626,11 @@ sub add_group_member
 
 sub remove_group_member
 {
-  my ($dbh, $groupname, $username) = @_;
+  my ($ctx, $groupname, $username) = @_;
 
   my $sql = qq{delete from USER_PERMS where GROUP_NAME = ? and USER_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   my $cnt = $sth->execute($groupname, $username)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -621,11 +641,11 @@ sub remove_group_member
 
 sub list_groups
 {
-  my ($dbh) = @_;
+  my ($ctx) = @_;
 
   my $sql = qq{select NAME from GROUPS order by DEFAULT_LOW_CART;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute()
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -643,21 +663,21 @@ sub list_groups
 
 sub get_dropboxes
 {
-  my ($dbh, $username, $groupname, $type) = @_;
+  my ($ctx, $username, $groupname, $type) = @_;
 
   my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME = ? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME = ?;};
   if(defined $groupname) {
     $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME = ? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=? and GROUPS.NAME = ?;};
   }
 
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   if(defined $groupname) {
-    $sth->execute($username, DROPBOX_PSEUDO_STATION_NAME, $groupname)
+    $sth->execute($username, RHRD_DROPBOX_PSEUDO_STATION_NAME, $groupname)
       or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
   } else {
-    $sth->execute($username, DROPBOX_PSEUDO_STATION_NAME)
+    $sth->execute($username, RHRD_DROPBOX_PSEUDO_STATION_NAME)
       or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
   }
 
@@ -678,7 +698,7 @@ sub get_dropboxes
       $entry->{'TYPE'} = 'show';
       $entry->{'SHOWID'} = $to_cart;
 
-      my ($title, $log, $status, $errorstring) = get_show_title_and_log($dbh, $to_cart);
+      my ($title, $log, $status, $errorstring) = get_show_title_and_log($ctx, $to_cart);
       unless (defined $title && defined $log) {
         return (undef, $status, $errorstring);
       }
@@ -710,26 +730,26 @@ sub get_dropboxes
 
 sub get_shows_cart_range
 {
-  my ($dbh) = @_;
-  return get_cart_range($dbh, RHRD_ALLSHOWS_GROUP)
+  my ($ctx) = @_;
+  return get_cart_range($ctx, RHRD_ALLSHOWS_GROUP)
 }
 
 sub get_shows_next_free_slot
 {
-  my ($dbh) = @_;
-  return get_next_free_slot($dbh, RHRD_ALLSHOWS_GROUP)
+  my ($ctx) = @_;
+  return get_next_free_slot($ctx, RHRD_ALLSHOWS_GROUP)
 }
 
 sub list_shows
 {
-  my ($dbh) = @_;
+  my ($ctx) = @_;
 
   my $sql = qq{select TO_CART,SET_USER_DEFINED from DROPBOXES where STATION_NAME=?;};
 
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
-  $sth->execute(DROPBOX_PSEUDO_STATION_NAME)
+  $sth->execute(RHRD_DROPBOX_PSEUDO_STATION_NAME)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
 
   my @show_dbs;
@@ -739,7 +759,7 @@ sub list_shows
 
     my $entry = {};
     $entry->{'ID'} = $to_cart;
-    my ($title, $log, $status, $errorstring) = get_show_title_and_log($dbh, $to_cart);
+    my ($title, $log, $status, $errorstring) = get_show_title_and_log($ctx, $to_cart);
     unless (defined $title && defined $log) {
       return (undef, $status, $errorstring);
     }
@@ -761,13 +781,13 @@ sub list_shows
 
 sub get_show_group_carts
 {
-  my ($dbh, $showid) = @_;
+  my ($ctx, $showid) = @_;
 
   my $sql = qq{select GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART from DROPBOXES, GROUPS where DROPBOXES.TO_CART = ? and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
-  $sth->execute($showid, DROPBOX_PSEUDO_STATION_NAME)
+  $sth->execute($showid, RHRD_DROPBOX_PSEUDO_STATION_NAME)
     or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr);
 
   my ($group_low_cart, $group_high_cart) = $sth->fetchrow_array();
@@ -779,11 +799,11 @@ sub get_show_group_carts
 
 sub get_show_title_and_log
 {
-  my ($dbh, $showid) = @_;
+  my ($ctx, $showid) = @_;
 
   my $sql = qq{select TITLE,MACROS from CART where NUMBER = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($showid)
     or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -808,21 +828,21 @@ sub get_show_title_and_log
 
 sub get_show_carts
 {
-  my ($dbh, $showid) = @_;
+  my ($ctx, $showid) = @_;
 
-  my ($group_low_cart, $group_high_cart, $status, $errorstring) = get_show_group_carts($dbh, $showid);
+  my ($group_low_cart, $group_high_cart, $status, $errorstring) = get_show_group_carts($ctx, $showid);
   unless (defined $group_low_cart && defined $group_high_cart) {
     return (undef, $status, $errorstring);
   }
 
-  (undef, my $log, $status, $errorstring) = get_show_title_and_log($dbh, $showid);
+  (undef, my $log, $status, $errorstring) = get_show_title_and_log($ctx, $showid);
   unless (defined $log) {
     return (undef, $status, $errorstring);
   }
 
   my $sql = qq{select LOG_EXISTS from LOGS where NAME = ?;};
-  my $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute($log)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -835,11 +855,11 @@ sub get_show_carts
   }
 
   $log=~s/ /_/g;
-  $log = $dbh->quote_identifier($log . '_LOG');
+  $log = $ctx->{'dbh'}->quote_identifier($log . '_LOG');
   $sql = qq{select COUNT,CART_NUMBER from $log order by COUNT;};
 
-  $sth = $dbh->prepare($sql)
-    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+  $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   $sth->execute()
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
@@ -859,28 +879,28 @@ sub get_show_carts
 
 sub get_musicpools_cart_range
 {
-  my ($dbh) = @_;
-  return get_cart_range($dbh, RHRD_ALLMUSICPOOLS_GROUP)
+  my ($ctx) = @_;
+  return get_cart_range($ctx, RHRD_ALLMUSICPOOLS_GROUP)
 }
 
 sub get_musicpools_next_free_slot
 {
-  my ($dbh) = @_;
-  return get_next_free_slot($dbh, RHRD_ALLMUSICPOOLS_GROUP)
+  my ($ctx) = @_;
+  return get_next_free_slot($ctx, RHRD_ALLMUSICPOOLS_GROUP)
 }
 
 ###########################  JINGLES handling  ###########################
 
 sub get_jingles_cart_range
 {
-  my ($dbh) = @_;
-  return get_cart_range($dbh, RHRD_ALLJINGLES_GROUP)
+  my ($ctx) = @_;
+  return get_cart_range($ctx, RHRD_ALLJINGLES_GROUP)
 }
 
 sub get_jingles_next_free_slot
 {
-  my ($dbh) = @_;
-  return get_next_free_slot($dbh, RHRD_ALLJINGLES_GROUP)
+  my ($ctx) = @_;
+  return get_next_free_slot($ctx, RHRD_ALLJINGLES_GROUP)
 }
 
 #################################  END  ####################################
diff --git a/test/get-dropboxes b/test/get-dropboxes
index b8a5266..89a4935 100755
--- a/test/get-dropboxes
+++ b/test/get-dropboxes
@@ -33,15 +33,15 @@ my $type = $ARGV[2];
 
 
 my @dropboxes = ();
-my ($dbh, $status, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
-  @dropboxes = RHRD::rddb::get_dropboxes($dbh, $username, $groupname, $type);
+my ($ctx, $status, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
+  @dropboxes = RHRD::rddb::get_dropboxes($ctx, $username, $groupname, $type);
   if(!defined $dropboxes[0] && defined $dropboxes[1]) {
     print STDERR "$dropboxes[1]: $dropboxes[2]";
-    RHRD::rddb::closedb($dbh);
+    RHRD::rddb::destroy($ctx);
     exit 1;
   }
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print STDERR "$errorstring\n";
   exit 1;
diff --git a/test/get-lists b/test/get-lists
index 47d6dfd..2f7be2b 100755
--- a/test/get-lists
+++ b/test/get-lists
@@ -24,13 +24,13 @@ use strict;
 use lib "../lib/";
 use RHRD::rddb;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
+my ($ctx, undef, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
   print "Users:\n";
-  my @users = RHRD::rddb::list_users($dbh);
+  my @users = RHRD::rddb::list_users($ctx);
   if(!defined $users[0] && defined $users[1]) {
     print STDERR "$users[1]: $users[2]";
-    RHRD::rddb::closedb($dbh);
+    RHRD::rddb::destroy($ctx);
     exit 1;
   }
   for my $user (@users) {
@@ -38,17 +38,17 @@ if(defined $dbh) {
   }
 
   print "\nGroups:\n";
-  my @groups = RHRD::rddb::list_groups($dbh);
+  my @groups = RHRD::rddb::list_groups($ctx);
   if(!defined $groups[0] && defined $groups[1]) {
     print STDERR "$groups[1]: $groups[2]";
-    RHRD::rddb::closedb($dbh);
+    RHRD::rddb::destroy($ctx);
     exit 1;
   }
   for my $group (@groups) {
     print " " . $group . "\n";
   }
 
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print "$errorstring\n";
   exit 1;
diff --git a/test/get-range b/test/get-range
index 1027653..e0c0e36 100755
--- a/test/get-range
+++ b/test/get-range
@@ -24,16 +24,16 @@ use strict;
 use lib "../lib/";
 use RHRD::rddb;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
+my ($ctx, undef, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
   print "Shows:\n";
-  my ($low_cart, $high_cart, $chunk_size) = RHRD::rddb::get_shows_cart_range($dbh);
+  my ($low_cart, $high_cart, $chunk_size) = RHRD::rddb::get_shows_cart_range($ctx);
   if(!$low_cart) {
     print "$high_cart: $chunk_size\n";
   } else {
     print "Range: " . $low_cart  . " - " . $high_cart . ", chunk size: " . $chunk_size . "\n";
   }
-  my ($slot_low_cart, $slot_high_cart, $err) = RHRD::rddb::get_shows_next_free_slot($dbh);
+  my ($slot_low_cart, $slot_high_cart, $err) = RHRD::rddb::get_shows_next_free_slot($ctx);
   if(!$slot_low_cart) {
     print "$slot_high_cart: $err\n";
   } else {
@@ -41,13 +41,13 @@ if(defined $dbh) {
   }
 
   print "\nMusic Pools:\n";
-  ($low_cart, $high_cart, $chunk_size) = RHRD::rddb::get_musicpools_cart_range($dbh);
+  ($low_cart, $high_cart, $chunk_size) = RHRD::rddb::get_musicpools_cart_range($ctx);
   if(!$low_cart) {
     print "$high_cart: $chunk_size\n";
   } else {
     print "Range: " . $low_cart  . " - " . $high_cart . ", chunk size: " . $chunk_size . "\n";
   }
-  ($slot_low_cart, $slot_high_cart, $err) = RHRD::rddb::get_musicpools_next_free_slot($dbh);
+  ($slot_low_cart, $slot_high_cart, $err) = RHRD::rddb::get_musicpools_next_free_slot($ctx);
   if(!$slot_low_cart) {
     print "$slot_high_cart: $err\n";
   } else {
@@ -55,13 +55,13 @@ if(defined $dbh) {
   }
 
   print "\nJingles:\n";
-  ($low_cart, $high_cart, $chunk_size) = RHRD::rddb::get_jingles_cart_range($dbh);
+  ($low_cart, $high_cart, $chunk_size) = RHRD::rddb::get_jingles_cart_range($ctx);
   if(!$low_cart) {
     print "$high_cart: $chunk_size\n";
   } else {
     print "Range: " . $low_cart  . " - " . $high_cart . ", chunk size: " . $chunk_size . "\n";
   }
-  ($slot_low_cart, $slot_high_cart, $err) = RHRD::rddb::get_jingles_next_free_slot($dbh);
+  ($slot_low_cart, $slot_high_cart, $err) = RHRD::rddb::get_jingles_next_free_slot($ctx);
   if(!$slot_low_cart) {
     print "$slot_high_cart: $err\n";
   } else {
@@ -69,7 +69,7 @@ if(defined $dbh) {
   }
 
 
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print "$errorstring\n";
   exit 1;
diff --git a/utils/get-rd-token b/utils/get-rd-token
index b2b4dce..2c54ea2 100755
--- a/utils/get-rd-token
+++ b/utils/get-rd-token
@@ -46,14 +46,14 @@ if($num_args == 1) {
 my $token='';
 my $ret = 0;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
-  ($token, undef, $errorstring) = RHRD::rddb::get_token($dbh, $requsername);
+my ($ctx, undef, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
+  ($token, undef, $errorstring) = RHRD::rddb::get_token($ctx, $requsername);
   unless($token) {
     print STDERR "$errorstring\n";
     $ret = 1;
   }
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print STDERR "$errorstring\n";
   $ret = 1;
diff --git a/utils/rd-group b/utils/rd-group
index ecf10cb..3898b5e 100755
--- a/utils/rd-group
+++ b/utils/rd-group
@@ -41,9 +41,9 @@ sub print_usage
 
 sub list
 {
-  my ($dbh) = @_;
+  my ($ctx) = @_;
 
-  my @groups = RHRD::rddb::list_groups($dbh);
+  my @groups = RHRD::rddb::list_groups($ctx);
   if(!defined $groups[0] && defined $groups[1]) {
     print STDERR "$groups[1]: $groups[2]";
     return 1;
@@ -56,18 +56,18 @@ sub list
 
 sub check
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my ($result, $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname);
+  my ($result, $status, $errorstring) = RHRD::rddb::check_group($ctx, $groupname);
   print STDERR "$result, $status: $errorstring\n";
   return $result;
 }
 
 sub add
 {
-  my ($dbh, $groupname, $description) = @_;
+  my ($ctx, $groupname, $description) = @_;
 
-  my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $description);
+  my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($ctx, $groupname, $description);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -78,9 +78,9 @@ sub add
 
 sub remove
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my @results = RHRD::rddb::remove_group($dbh, $groupname);
+  my @results = RHRD::rddb::remove_group($ctx, $groupname);
   if(!defined $results[0] && defined $results[2]) {
     print STDERR "$results[2]\n";
     return 1;
@@ -93,9 +93,9 @@ sub remove
 
 sub get_members
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my @users = RHRD::rddb::get_group_members($dbh, $groupname);
+  my @users = RHRD::rddb::get_group_members($ctx, $groupname);
   if(!defined $users[0] && defined $users[1]) {
     print STDERR "$users[2]\n";
     return 1;
@@ -108,9 +108,9 @@ sub get_members
 
 sub add_member
 {
-  my ($dbh, $groupname, $username) = @_;
+  my ($ctx, $groupname, $username) = @_;
 
-  my ($cnt, undef, $errorstring) = RHRD::rddb::add_group_member($dbh, $groupname, $username);
+  my ($cnt, undef, $errorstring) = RHRD::rddb::add_group_member($ctx, $groupname, $username);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -121,9 +121,9 @@ sub add_member
 
 sub remove_member
 {
-  my ($dbh, $groupname, $username) = @_;
+  my ($ctx, $groupname, $username) = @_;
 
-  my ($cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($dbh, $groupname, $username);
+  my ($cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($ctx, $groupname, $username);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -134,9 +134,9 @@ sub remove_member
 
 sub is_member
 {
-  my ($dbh, $groupname, $username) = @_;
+  my ($ctx, $groupname, $username) = @_;
 
-  my ($cnt, undef, $errorstring) = RHRD::rddb::is_group_member($dbh, $groupname, $username);
+  my ($cnt, undef, $errorstring) = RHRD::rddb::is_group_member($ctx, $groupname, $username);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -147,9 +147,9 @@ sub is_member
 
 sub get_carts
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname);
+  my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($ctx, $groupname);
   unless(defined $low) {
     print STDERR "$type\n";
     return 1;
@@ -160,9 +160,9 @@ sub get_carts
 
 sub set_carts
 {
-  my ($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;
+  my ($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;
 
-  my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range);
+  my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -173,9 +173,9 @@ sub set_carts
 
 sub get_reports
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname);
+  my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $groupname);
   unless(defined $nownext) {
     print STDERR "$music\n";
     return 1;
@@ -186,9 +186,9 @@ sub get_reports
 
 sub set_reports
 {
-  my ($dbh, $groupname, $now_next, $traffic, $music) = @_;
+  my ($ctx, $groupname, $now_next, $traffic, $music) = @_;
 
-  my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $now_next, $traffic, $music);
+  my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, $now_next, $traffic, $music);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -208,14 +208,14 @@ my $cmd = $ARGV[0];
 my $groupname = $ARGV[1];
 my $ret = 0;
 
-my ($dbh, $status, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
+my ($ctx, $status, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
   if($cmd eq "list") {
     if($num_args != 1) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = list($dbh);
+      $ret = list($ctx);
     }
   }
   elsif($cmd eq "check") {
@@ -223,7 +223,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = check($dbh, $groupname);
+      $ret = check($ctx, $groupname);
     }
   }
   elsif($cmd eq "add") {
@@ -231,7 +231,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = add($dbh, $groupname, $ARGV[2]);
+      $ret = add($ctx, $groupname, $ARGV[2]);
     }
   }
   elsif($cmd eq "remove") {
@@ -239,7 +239,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = remove($dbh, $groupname);
+      $ret = remove($ctx, $groupname);
     }
   }
   elsif($cmd eq "get-members") {
@@ -247,7 +247,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = get_members($dbh, $groupname);
+      $ret = get_members($ctx, $groupname);
     }
   }
   elsif($cmd eq "add-member") {
@@ -255,7 +255,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = add_member($dbh, $groupname, $ARGV[2]);
+      $ret = add_member($ctx, $groupname, $ARGV[2]);
     }
   }
   elsif($cmd eq "remove-member") {
@@ -263,7 +263,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = remove_member($dbh, $groupname, $ARGV[2]);
+      $ret = remove_member($ctx, $groupname, $ARGV[2]);
     }
   }
   elsif($cmd eq "is-member") {
@@ -271,7 +271,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = is_member($dbh, $groupname, $ARGV[2]);
+      $ret = is_member($ctx, $groupname, $ARGV[2]);
     }
   }
   elsif($cmd eq "get-carts") {
@@ -279,7 +279,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = get_carts($dbh, $groupname);
+      $ret = get_carts($ctx, $groupname);
     }
   }
   elsif($cmd eq "set-carts") {
@@ -287,7 +287,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = set_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]);
+      $ret = set_carts($ctx, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]);
     }
   }
   elsif($cmd eq "get-reports") {
@@ -295,7 +295,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = get_reports($dbh, $groupname);
+      $ret = get_reports($ctx, $groupname);
     }
   }
   elsif($cmd eq "set-reports") {
@@ -303,15 +303,15 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = set_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]);
+      $ret = set_reports($ctx, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]);
     }
   }
   else {
-    print STDERR "unknown command\n";
+    print_usage();
     $ret = 1;
   }
 
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print STDERR "$errorstring\n";
   $ret = 1;
diff --git a/utils/rd-show b/utils/rd-show
index 17286e9..0e53e05 100755
--- a/utils/rd-show
+++ b/utils/rd-show
@@ -39,9 +39,9 @@ sub print_usage
 
 sub list
 {
-  my ($dbh) = @_;
+  my ($ctx) = @_;
 
-  my @shows = RHRD::rddb::list_shows($dbh);
+  my @shows = RHRD::rddb::list_shows($ctx);
   if(!defined $shows[0] && defined $shows[1]) {
     print STDERR "$shows[1]: $shows[2]";
     return 1;
@@ -54,14 +54,14 @@ sub list
 
 sub show
 {
-  my ($dbh, $show_id) = @_;
+  my ($ctx, $show_id) = @_;
 
-  my @carts = RHRD::rddb::get_show_carts($dbh, $show_id);
+  my @carts = RHRD::rddb::get_show_carts($ctx, $show_id);
   if(!defined $carts[0] && defined $carts[1]) {
     print STDERR "$carts[1]: $carts[2]\n";
     return 1;
   }
-  my ($title, undef, $status, $errorstring) = RHRD::rddb::get_show_title_and_log($dbh, $show_id);
+  my ($title, undef, $status, $errorstring) = RHRD::rddb::get_show_title_and_log($ctx, $show_id);
   unless (defined $title) {
     print STDERR "$errorstring\n";
     return 1;
@@ -77,24 +77,24 @@ sub show
 
 sub add__create_group
 {
-  my ($dbh, $groupname) = @_;
+  my ($ctx, $groupname) = @_;
 
   print "   > '" . $groupname . "' does not exist - creating it .. ";
-  my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($dbh, $groupname);
+  my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($ctx, $groupname);
   unless(defined $cnt) {
     print STDERR $status . ": " . $errorstring . "\n";
     return undef;
   }
   print int($cnt) . " rows affected\n";
 
-  (my $low_cart, my $high_cart, $errorstring) = RHRD::rddb::get_shows_next_free_slot($dbh);
+  (my $low_cart, my $high_cart, $errorstring) = RHRD::rddb::get_shows_next_free_slot($ctx);
   if(!$low_cart) {
     print $high_cart . ": " . $errorstring . "\n";
     return undef;
   }
   print "     using carts " . $low_cart . " - " . $high_cart . " for new group .. ";
 
-  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $low_cart, $high_cart, 1, 'Y');
+  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_carts($ctx, $groupname, $low_cart, $high_cart, 1, 'Y');
   unless(defined $cnt) {
     print STDERR $status . ": " . $errorstring . "\n";
     return undef;
@@ -102,7 +102,7 @@ sub add__create_group
   print int($cnt) . " rows affected\n";
 
   print "     enabling reports  .. ";
-  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, 'Y', 'Y', 'Y');
+  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, 'Y', 'Y', 'Y');
   unless(defined $cnt) {
     print STDERR $status . ": " . $errorstring . "\n";
     return undef;
@@ -114,9 +114,9 @@ sub add__create_group
 
 sub add__get_group_carts
 {
-  my ($dbh, $groupname, $num_carts) = @_;
+  my ($ctx, $groupname, $num_carts) = @_;
 
-  my ($result, $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname);
+  my ($result, $status, $errorstring) = RHRD::rddb::check_group($ctx, $groupname);
   unless(defined $result) {
     print STDERR $status . ": " . $errorstring . "\n";
     return undef;
@@ -129,7 +129,7 @@ sub add__get_group_carts
     #   find free range in group which is big enough for $num_carts carts
 
   } else {
-    $low_cart = add__create_group($dbh, $groupname);
+    $low_cart = add__create_group($ctx, $groupname);
   }
 
   return $low_cart;
@@ -137,11 +137,11 @@ sub add__get_group_carts
 
 sub add
 {
-  my ($dbh, $groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len) = @_;
+  my ($ctx, $groupname, $name, $title, $num_carts, $rhythm, $dow, $starttime, $len) = @_;
 
   print " * creating show: " . $title . " (" . $name . ") for group '" . $groupname . "'\n";
 
-  my $low_cart = add__get_group_carts($dbh, $groupname, $num_carts);
+  my $low_cart = add__get_group_carts($ctx, $groupname, $num_carts);
   return 1 unless defined($low_cart);
   my $high_cart = $low_cart + $num_carts - 1;
   print " * will be using carts: " . $low_cart . " - " . $high_cart . "\n";
@@ -158,7 +158,7 @@ sub add
 
 sub remove
 {
-  my ($dbh, $show_id) = @_;
+  my ($ctx, $show_id) = @_;
 
   print "removing show " . $show_id . ", not yet implemented!\n";
   return 0;
@@ -172,14 +172,14 @@ if($num_args < 1) {
 my $cmd = $ARGV[0];
 my $ret = 0;
 
-my ($dbh, $status, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
+my ($ctx, $status, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
   if($cmd eq "list") {
     if($num_args != 1) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = list($dbh)
+      $ret = list($ctx)
     }
   }
   elsif($cmd eq "show") {
@@ -187,7 +187,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = show($dbh, $ARGV[1])
+      $ret = show($ctx, $ARGV[1])
     }
   }
   elsif($cmd eq "remove") {
@@ -195,7 +195,7 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = remove($dbh, $ARGV[1]);
+      $ret = remove($ctx, $ARGV[1]);
     }
   }
   elsif($cmd eq "add") {
@@ -203,14 +203,14 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = add($dbh, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7], $ARGV[8]);
+      $ret = add($ctx, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7], $ARGV[8]);
     }
   }
   else {
     print_usage();
     $ret = 1;
   }
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print STDERR "$errorstring\n";
   $ret = 1;
diff --git a/utils/rd-user b/utils/rd-user
index 2a032dd..8689cb4 100755
--- a/utils/rd-user
+++ b/utils/rd-user
@@ -39,9 +39,9 @@ sub print_usage
 
 sub list
 {
-  my ($dbh) = @_;
+  my ($ctx) = @_;
 
-  my @users = RHRD::rddb::list_users($dbh);
+  my @users = RHRD::rddb::list_users($ctx);
   if(!defined $users[0] && defined $users[1]) {
     print STDERR "$users[1]: $users[2]";
     return 1;
@@ -54,19 +54,19 @@ sub list
 
 sub check
 {
-  my ($dbh, $username) = @_;
+  my ($ctx, $username) = @_;
 
-  my ($result, $status, $errorstring) = RHRD::rddb::check_user($dbh, $username);
+  my ($result, $status, $errorstring) = RHRD::rddb::check_user($ctx, $username);
   print STDERR "$result, $status: $errorstring\n";
   return $result;
 }
 
 sub add
 {
-  my ($dbh, $username, $fullname) = @_;
+  my ($ctx, $username, $fullname) = @_;
 
   my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 0);
-  my ($cnt, undef, $errorstring) = RHRD::rddb::add_user($dbh, $username, $token, $fullname);
+  my ($cnt, undef, $errorstring) = RHRD::rddb::add_user($ctx, $username, $token, $fullname);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
     return 1;
@@ -77,9 +77,9 @@ sub add
 
 sub remove
 {
-  my ($dbh, $username) = @_;
+  my ($ctx, $username) = @_;
 
-  my @results = RHRD::rddb::remove_user($dbh, $username);
+  my @results = RHRD::rddb::remove_user($ctx, $username);
   if(!defined $results[0] && defined $results[2]) {
     print STDERR "$results[2]\n";
     return 1;
@@ -100,14 +100,14 @@ my $cmd = $ARGV[0];
 my $username = $ARGV[1];
 my $ret = 0;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
+my ($ctx, undef, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
   if($cmd eq "list") {
     if($num_args != 1) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = list($dbh);
+      $ret = list($ctx);
     }
   }
   elsif($cmd eq "check") {
@@ -115,28 +115,28 @@ if(defined $dbh) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = check($dbh, $username);
+      $ret = check($ctx, $username);
     }
   } elsif($cmd eq "add") {
     if($num_args < 2 || $num_args > 3) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = add($dbh, $username, $ARGV[2]);
+      $ret = add($ctx, $username, $ARGV[2]);
     }
   } elsif($cmd eq "remove") {
     if($num_args != 2) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = remove($dbh, $username);
+      $ret = remove($ctx, $username);
     }
   } else {
-    print STDERR "unknown command\n";
+    print_usage();
     $ret = 1;
   }
 
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print STDERR "$errorstring\n";
   $ret = 1;
diff --git a/utils/rhrd-ws-login b/utils/rhrd-ws-login
index a3cee53..79eeac5 100755
--- a/utils/rhrd-ws-login
+++ b/utils/rhrd-ws-login
@@ -38,14 +38,14 @@ my $username = $ENV{'USER'};
 
 my $ret = 0;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
+my ($ctx, undef, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
   my $macro = '';
   if($cmd eq "logout") {
     $macro = "LO!";
   }
   elsif($cmd eq "login") {
-    (my $token, undef, $errorstring) = RHRD::rddb::get_token($dbh, $username);
+    (my $token, undef, $errorstring) = RHRD::rddb::get_token($ctx, $username);
     if($token) {
       $token =~ s/([ !'"\\])/\\$1/g;
       $macro = "LO $username $token!";
@@ -58,7 +58,7 @@ if(defined $dbh) {
     print_usage();
     $ret = 1;
   }
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
   unless ($macro eq '') {
     system('/usr/bin/rmlsend', $macro) == 0 or die "system(/usr/bin/rmlsend, $macro) failed: $?";
   }
diff --git a/utils/update-rd-tokens b/utils/update-rd-tokens
index 8983b17..4004e5d 100755
--- a/utils/update-rd-tokens
+++ b/utils/update-rd-tokens
@@ -39,9 +39,9 @@ my %EXCLUDED_USERS = map { $_ => 1 } @ARGV;
 $EXCLUDED_USERS{'admin'} = 1;
 my $ret = 0;
 
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
-if(defined $dbh) {
-  my @users = RHRD::rddb::list_users($dbh);
+my ($ctx, undef, $errorstring) = RHRD::rddb::init();
+if(defined $ctx) {
+  my @users = RHRD::rddb::list_users($ctx);
   if(!defined $users[0] && defined $users[2]) {
     print STDERR "$users[2]\n";
     $ret = 1;
@@ -49,12 +49,12 @@ if(defined $dbh) {
     foreach my $user (@users) {
       next if(exists($EXCLUDED_USERS{$user}));
       my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 0);
-      RHRD::rddb::set_token($dbh, $user, $token);
+      RHRD::rddb::set_token($ctx, $user, $token);
       print "$user -> $token\n";
     }
   }
 
-  RHRD::rddb::closedb($dbh);
+  RHRD::rddb::destroy($ctx);
 } else {
   print STDERR "$errorstring\n";
   $ret = 1;
-- 
cgit v0.10.2