#  Name of this package
package JS;

#  Packages required by this package
use strict;
no strict 'subs'; 				#  Allow bare words, so &ade_err_error() calls look nicer.
use warnings;
use lib substr `ade-config ade_include_prefix`,0,-1;
use ADE;
use File::Find;
use Data::Dumper;

#  Declare exports
BEGIN {
    use Exporter   ();
    our (@ISA, @EXPORT);

    @ISA         = qw(Exporter);
    @EXPORT      = qw(js_sql_select js_sql_execute);
}

END { 
}

#  Instantiate exported data
#our($CT_DELETED, $CT_ADDED, $CT_TYPE, $CT_OWNER, ...);

#  Initialise exported data
#($CT_DELETED, $CT_ADDED, $CT_TYPE, $CT_OWNER, ...) = qw(d a t u ...);

#  Instantiate private data
my($app_svnid, %js_defined_errors);

#  Initialise private data
$app_svnid = '$HeadURL$ $LastChangedRevision$';
#  Error codes (this will be registered with ADE's error handling routines)
%js_defined_errors = (
        js_err_misc  => { fmt => "%s" },
    );

##############################################################################
#
#  PUBLIC FUNCTIONS
#
##############################################################################

sub js_sql_execute
{
    my($errstack_ref, $dbh, $sql_command) = @_;

    &ade_err_debug($errstack_ref, 10, "js_sql_execute: running \"$sql_command\" ...");
    if (!defined($dbh->do($sql_command))) {
        &ade_err_debug($errstack_ref, 10, "js_sql_execute: failed");

        &ade_err_error($errstack_ref, jsq_err_misc, $DBI::errstr);
        return($ade_err_fail);
    }

    return($ade_err_ok);
}

sub js_sql_select
{
    my($errstack_ref, $dbh, $select_results_arrayref, $sql_command) = @_;
    my($sth, @row);

    @{$select_results_arrayref} = ();
    $sth = $dbh->prepare($sql_command);
    $sth->execute();
    while (@row = $sth->fetchrow_array) {
         #  We can't push a reference to @row onto the caller's array, because
         #  @row will shortly be reset and that means the data in that is lost.
         #  (Oddly this doesn't result in multiple entries of the same anonymous
         #  array, but rather in empty arrays for all but the last element.)
         push @{$select_results_arrayref}, [@row];
    }

    return($ade_err_ok);
}

##############################################################################
#
#  PRIVATE FUNCTIONS
#
##############################################################################

sub _js_complex_initialisations
{
    my ($errstack_ref) = @_; shift;

    &ade_err_registerdefderrs(\%js_defined_errors);

    return($ade_err_ok);
}

##############################################################################
#
#  CODE
#
##############################################################################

&_js_complex_initialisations(undef);

##############################################################################
#
#  NON-ZERO MODULE EXIT CODE
#
##############################################################################

1;  # don't forget to return a true value from the file
