Tim Bunce wrote:
On Fri, Mar 25, 2005 at 04:37:29PM -0500, Stas Bekman wrote:

Should it be something like (and replace ??):

SV *dbd_take_imp_data(SV *h, imp_xxh_t *imp_xxh, ?? ??)

It can return a plain int as all it's doing is 'preparing' for the DBIs default take_imp_data to do the actual brain surgery.

Now I've lost you, Tim, how can the DBIs default take_imp_data do the surgery w/o knowing the details of the driver?


Because it removes (disconnects and returns) the whole imp_xxh structure. A simple and clean cut. I've appended the code. Hopefully the comments
will help explain the context.
[...]
thanks Tim, I'm trying to wrap my head around it.

One question: I'm trying to write first a test that checks that the new perl connection uses the same C connection. Any idea how to check that? Is there a way to get the connection ID? e.g. in mysql:

mysql> SHOW FULL PROCESSLIST;
+-----+------+-----------+-------+---------+------+------------------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------+-------+---------+------+------------------+-----------------------+
| 15 | root | localhost | mysql | Query | 0 | NULL | SHOW FULL PROCESSLIST |
| 106 | stas | localhost | test | Connect | 2 | Reading from net | NULL |
+-----+------+-----------+-------+---------+------+------------------+-----------------------+
2 rows in set (0.00 sec)


I could probably use that id 106, but I'm not sure how to get it. Any ideas how to write this test?

Mysql dbd has strange non-standard way to write tests, not sure why it doesn't use Test::More. Here is what I've so far:

Index: dbdimp.c
===================================================================
RCS file: /home/stas/cvs/modules/DBD-mysql/dbdimp.c,v
retrieving revision 1.1
diff -u -r1.1 dbdimp.c
--- dbdimp.c    23 Mar 2005 23:07:00 -0000      1.1
+++ dbdimp.c    28 Mar 2005 18:54:04 -0000
@@ -2455,4 +2455,18 @@
     return result;
 }

+SV *mysql_take_imp_data(SV *h, imp_xxh_t *imp_xxh, void *foo)
+{
+#ifdef dTHR
+    dTHR;
+#endif
+    D_imp_dbh(h);
+
+    DBIc_ACTIVE_off(imp_dbh);
+    if (dbis->debug >= 2)
+        PerlIO_printf(DBILOGFP, "&imp_dbh->mysql: %lx\n",
+                     (long) &imp_dbh->mysql);
+
+    return &PL_sv_yes;
+}

Index: dbdimp.h
===================================================================
RCS file: /home/stas/cvs/modules/DBD-mysql/dbdimp.h,v
retrieving revision 1.1
diff -u -r1.1 dbdimp.h
--- dbdimp.h    23 Mar 2005 23:07:00 -0000      1.1
+++ dbdimp.h    28 Mar 2005 18:54:04 -0000
@@ -189,6 +189,7 @@
 #define do_error               mysql_dr_error
 #define dbd_db_type_info_all    mysql_db_type_info_all
 #define dbd_db_quote            mysql_db_quote
+#define dbd_take_imp_data       mysql_take_imp_data

 #include <dbd_xsh.h>
 void    do_error (SV* h, int rc, const char *what);


and the test, partially adopted from DBI's 03handle.t:



#!/usr/local/bin/perl
#
#   $Id: 70takeimp.t,v 1.1 2005/03/23 23:07:04 stas Exp $
#
#   This is a skeleton test. For writing new tests, take this file
#   and modify/extend it.
#

use strict;
use vars qw($test_dsn $test_user $test_password $mdriver $dbdriver);
use DBI ();

#
#   Include lib.pl
#
$mdriver = "";
my $file;
foreach $file ("lib.pl", "t/lib.pl") {
    do $file; if ($@) { print STDERR "Error while executing lib.pl: [EMAIL 
PROTECTED]";
                           exit 10;
                      }
    if ($mdriver ne '') {
        last;
    }
}

my $drh = DBI->install_driver($mdriver);

sub ServerError() {
    print STDERR ("Cannot connect: ", $DBI::errstr, "\n",
        "\tEither your server is not up and running or you have no\n",
        "\tpermissions for acessing the DSN $test_dsn.\n",
        "\tThis test requires a running server and write permissions.\n",
        "\tPlease make sure your server is running and you have\n",
        "\tpermissions, then retry.\n");
    exit 10;
}

#DBI->trace(2);

#
# Main loop; leave this untouched, put tests into the loop
#
use vars qw($state);
while (Testing()) {
#
# Connect to the database
my $dbh = DBI->connect($test_dsn, $test_user, $test_password);
# Test($state or $dbh = DBI->connect($test_dsn, $test_user, $test_password))
# or ServerError();


    use Data::Dumper;
    print Dumper $dbh;



    read_write_test($dbh);

    my $drh = $dbh->{Driver};

    my $imp_data;
    Test($state or $imp_data = $dbh->take_imp_data)
        or ErrMsg("didn't get imp_data");

Test($state or length($imp_data) >= 80)
or ErrMsg('test that our imp_data is greater than or equal to 80, this is reasonable');


Test($state or $drh->{Kids} == 0)
or ErrMsg('our Driver should have 0 Kid(s) after calling take_imp_data');


{
my $warn;
local $SIG{__WARN__} = sub { ++$warn if $_[0] =~ /after take_imp_data/ };


        my $drh = $dbh->{Driver};
        Test($state or !defined $drh)
            or ErrMsg('... our Driver should be undefined');

        my $trace_level = $dbh->{TraceLevel};
        Test($state or !defined $trace_level)
            or ErrMsg('our TraceLevel should be undefined');

        Test($state or !defined $dbh->disconnect)
            or ErrMsg('disconnect should return undef');

        Test($state or !defined $dbh->quote(42))
            or ErrMsg('quote should return undefined');

        Test($state or $warn == 4)
            or ErrMsg('we should have gotten 4 warnings');
    }

    # XXX: how can we test that the connection wasn't actually dropped?

    #use Data::Dumper;
    #print "GOT $imp_data\n";

my $dbh2 = DBI->connect($test_dsn, $test_user, $test_password, { dbi_imp_data => $imp_data });
# my $dbh2 = DBI->connect($test_dsn, $test_user, $test_password);


    # XXX: how can we test that the same connection is used?

    my $drh2;
    Test($state or $drh2 = $dbh2->{Driver})
        or ErrMsg("can't get the driver\n");

    Test($state or $dbh2->isa("DBI::db"))
         or ErrMsg('isa test');
    # need a way to test dbi_imp_data has been used

Test($state or $drh2->{Kids} == 1)
or ErrMsg("our Driver should have 1 Kid(s) again: having " . $drh2->{Kids} . "\n");


    read_write_test($dbh2);

    #
    #   Finally disconnect.
    #
    Test($state or $dbh2->disconnect())
           or DbiError($dbh2->err, $dbh2->errstr);

}

sub read_write_test {
    my $dbh = shift;

    # now the actual test:

    #   Find a possible new table name
    #
    my $table;
    Test($state or $table = FindNewTable($dbh))
           or DbiError($dbh->err, $dbh->errstr);

    #
    #   Create a new table
    #
    my $def;
    if (!$state) {
        ($def = TableDefinition($table,
                                ["id",   "INTEGER",  4, 0],
                                ["name", "CHAR",    64, 0]));
        print "Creating table:\n$def\n";
    }
    Test($state or $dbh->do($def))
        or DbiError($dbh->err, $dbh->errstr);

    #
    #   ... and drop it.
    #
    Test($state or $dbh->do("DROP TABLE $table"))
           or DbiError($dbh->err, $dbh->errstr);

}



--
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Reply via email to