--- PApp-SQL/SQL.xs 2001/04/22 17:03:28 1.6 +++ PApp-SQL/SQL.xs 2002/08/02 03:30:01 1.14 @@ -8,10 +8,12 @@ # define call_sv perl_call_sv #endif -#if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 5)) +#if (PERL_VERSION > 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION >= 6)) # define CAN_UTF8 1 #endif +#define MAX_CACHED_STATEMENT_SIZE 8192 + static SV * sql_upgrade_utf8 (SV *sv) { @@ -23,17 +25,17 @@ } static SV * -sql_force_utf8 (SV *sv) +mortalcopy_and_maybe_force_utf8(int utf8, SV *sv) { + sv = sv_mortalcopy (sv); #if CAN_UTF8 - if (SvPOK (sv)) + if (utf8 && SvPOK (sv)) SvUTF8_on (sv); #endif return sv; } #define maybe_upgrade_utf8(utf8,sv) ((utf8) ? sql_upgrade_utf8 (sv) : (sv)) -#define maybe_force_utf8(utf8,sv) ((utf8) ? sql_force_utf8 (sv) : (sv)) #define is_dbh(sv) ((sv) && sv_isobject (sv) && sv_derived_from ((sv), "DBI::db")) @@ -81,8 +83,6 @@ lru_hash; - /*fprintf (stderr, "F: %08lx %s\n", hash, SvPV_nolen (sql));/*D*/ - n = &lru_list; do { n = n->next; @@ -114,8 +114,6 @@ lru_list.prev = n->prev; n->prev->next = &lru_list; - /*fprintf (stderr, "N: %s\n", SvPV_nolen (n->sql));/*D*/ - SvREFCNT_dec (n->dbh); SvREFCNT_dec (n->sql); SvREFCNT_dec (n->sth); @@ -137,8 +135,6 @@ lru_hash; - /*fprintf (stderr, "S: %08lx %s\n", hash, SvPV_nolen (sql));/*D*/ - lru_size++; if (lru_size > lru_maxsize) lru_nukeone (); @@ -233,7 +229,7 @@ SV *sth; SV *sql; SV *execute; - STRLEN dc; + STRLEN dc, dd; /* dummy */ /* save our arguments against destruction through function calls */ SP += items; @@ -241,13 +237,15 @@ /* first check wether we should use an explicit db handle */ if (!is_dbh (dbh)) { - dbh = get_sv ("DBH", FALSE); + /* the next line doesn't work - check why later maybe */ + /* dbh = get_sv ("DBH", FALSE); if (!is_dbh (dbh)) - { + {*/ dbh = GvSV(DBH); if (!is_dbh (dbh)) - croak ("sql_exec: no $DBH found in current package or in PApp::SQL::"); - } + croak ("sql_exec: no $DBH argument and no fallback in $PApp::SQL::DBH"); + /*croak ("sql_exec: no $DBH found in current package or in PApp::SQL::"); + }*/ } else arg++; /* we consumed one argument */ @@ -274,7 +272,7 @@ sv_catsv (neu, sql); sv_catpv (neu, " limit 1"); sql = neu; - ix -= 6; /* sql_fetch */ + ix -= 4; /* sql_fetch */ } /* check cache for existing statement handle */ @@ -292,11 +290,12 @@ if (count != 1) croak ("sql_exec: unable to prepare() statement '%s': %s", SvPV (sql, dc), - SvPV (get_sv ("DBI::errstr", TRUE), dc)); + SvPV (get_sv ("DBI::errstr", TRUE), dd)); sth = POPs; - lru_store (dbh, sql, sth); + if (SvLEN (sql) < MAX_CACHED_STATEMENT_SIZE) + lru_store (dbh, sql, sth); } PUSHMARK (SP); @@ -319,14 +318,14 @@ if (count != 1) croak ("sql_exec: execute() didn't return any value ('%s'): %s", SvPV (sql, dc), - SvPV (get_sv ("DBI::errstr", TRUE), dc)); + SvPV (get_sv ("DBI::errstr", TRUE), dd)); execute = POPs; if (!SvTRUE (execute)) croak ("sql_exec: unable to execute statement '%s' (%s)", SvPV (sql, dc), - SvPV (get_sv ("DBI::errstr", TRUE), dc)); + SvPV (get_sv ("DBI::errstr", TRUE), dd)); sv_setsv (GvSV(sql_exec), execute); @@ -347,12 +346,12 @@ if (count != 1) croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", SvPV (sql, dc), - SvPV (get_sv ("DBI::errstr", TRUE), dc)); + SvPV (get_sv ("DBI::errstr", TRUE), dd)); if (!SvOK (POPs)) croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", SvPV (sql, dc), - SvPV (get_sv ("DBI::errstr", TRUE), dc)); + SvPV (get_sv ("DBI::errstr", TRUE), dd)); } /* free our arguments from the stack */ @@ -384,14 +383,14 @@ break; case G_SCALAR: /* the first element */ - XPUSHs (maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); + XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); break; case G_ARRAY: av = (AV *)SvRV (row); count = AvFILL (av) + 1; EXTEND (SP, count); for (arg = 0; arg < count; arg++) - PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); + PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); break; default: @@ -426,10 +425,10 @@ EXTEND (SP, count); if (columns == 1) for (arg = 0; arg < count; arg++) - PUSHs (maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); + PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); else for (arg = 0; arg < count; arg++) - PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); + PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); } } }