--- PApp-SQL/SQL.xs 2002/04/11 01:02:10 1.12 +++ PApp-SQL/SQL.xs 2008/01/19 07:38:52 1.19 @@ -12,6 +12,8 @@ # 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")) @@ -62,7 +64,7 @@ /* this is primitive, yet effective */ /* the returned value must never be zero (or bad things will happen) */ #define lru_hash do { \ - hash = (((U32)dbh)>>2); \ + hash = (((U32)(long)dbh)>>2); \ hash += *statement;\ hash += len; \ } while (0) @@ -292,7 +294,8 @@ sth = POPs; - lru_store (dbh, sql, sth); + if (SvLEN (sql) < MAX_CACHED_STATEMENT_SIZE) + lru_store (dbh, sql, sth); } PUSHMARK (SP); @@ -300,7 +303,11 @@ PUSHs (sth); while (items > arg) { - PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg))); + SV *sv = ST(arg); + /* we sv_mortalcopy magical values since DBI seems to have a memory + * leak when magical values are passed into execute(). + */ + PUSHs (maybe_upgrade_utf8 (ix & 1, SvMAGICAL(sv) ? sv_mortalcopy(sv) : sv)); arg++; } @@ -332,6 +339,10 @@ EXTEND (SP, bind_last - bind_first + 2); PUSHs (sth); do { +#if CAN_UTF8 + if (ix & 1) + SvUTF8_on (SvRV(ST(bind_first))); +#endif PUSHs (ST(bind_first)); bind_first++; } while (bind_first != bind_last); @@ -345,13 +356,15 @@ SvPV (sql, dc), SvPV (get_sv ("DBI::errstr", TRUE), dd)); - if (!SvOK (POPs)) + if (!SvOK (TOPs)) croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", SvPV (sql, dc), SvPV (get_sv ("DBI::errstr", TRUE), dd)); + + POPs; } - /* free our arguments from the stack */ + /* restore our arguments again */ SP -= items; if ((ix & ~1) == 2) @@ -380,14 +393,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: @@ -422,10 +435,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])); } } }