From da7f64c53df4b86cd108e8d89c9ec43589c11a08 Mon Sep 17 00:00:00 2001 From: Kenichi Ishigaki Date: Wed, 4 Sep 2013 16:05:32 +0900 Subject: [PATCH] introduced sqlite_prefer_numeric_type handle attribute --- dbdimp.c | 24 ++++++++++++++++++++---- dbdimp.h | 1 + t/27_metadata.t | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/dbdimp.c b/dbdimp.c index 38e974a..37dc294 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -455,6 +455,7 @@ sqlite_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *user, char *pa imp_dbh->extended_result_codes = extended; imp_dbh->stmt_list = NULL; imp_dbh->began_transaction = FALSE; + imp_dbh->prefer_numeric_type = FALSE; sqlite3_busy_timeout(imp_dbh->db, SQL_TIMEOUT); @@ -737,6 +738,10 @@ sqlite_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv) sqlite3_extended_result_codes(imp_dbh->db, imp_dbh->extended_result_codes); return TRUE; } + if (strEQ(key, "sqlite_prefer_numeric_type")) { + imp_dbh->prefer_numeric_type = !(! SvTRUE(valuesv)); + return TRUE; + } if (strEQ(key, "sqlite_unicode")) { #if PERL_UNICODE_DOES_NOT_WORK_WELL sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl.")); @@ -781,6 +786,9 @@ sqlite_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv) if (strEQ(key, "sqlite_extended_result_codes")) { return sv_2mortal(newSViv(imp_dbh->extended_result_codes ? 1 : 0)); } + if (strEQ(key, "sqlite_prefer_numeric_type")) { + return sv_2mortal(newSViv(imp_dbh->prefer_numeric_type ? 1 : 0)); + } if (strEQ(key, "sqlite_unicode")) { #if PERL_UNICODE_DOES_NOT_WORK_WELL sqlite_trace(dbh, imp_dbh, 3, "Unicode support is disabled for this version of perl."); @@ -1358,10 +1366,18 @@ sqlite_st_FETCH_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv) av_extend(av, i); retsv = sv_2mortal(newRV_noinc((SV*)av)); for (n = 0; n < i; n++) { - int type = sqlite3_column_type(imp_sth->stmt, n); - /* warn("got type: %d = %s\n", type, fieldtype); */ - type = sqlite_type_to_odbc_type(type); - av_store(av, n, newSViv(type)); + if (imp_dbh->prefer_numeric_type) { + int type = sqlite3_column_type(imp_sth->stmt, n); + /* warn("got type: %d = %s\n", type, fieldtype); */ + type = sqlite_type_to_odbc_type(type); + av_store(av, n, newSViv(type)); + } else { + const char *fieldtype = sqlite3_column_decltype(imp_sth->stmt, n); + if (fieldtype) + av_store(av, n, newSVpv(fieldtype, 0)); + else + av_store(av, n, newSVpv("VARCHAR", 0)); + } } } else if (strEQ(key, "NULLABLE")) { diff --git a/dbdimp.h b/dbdimp.h index b86f1c0..efe6828 100644 --- a/dbdimp.h +++ b/dbdimp.h @@ -53,6 +53,7 @@ struct imp_dbh_st { int extended_result_codes; stmt_list_s * stmt_list; bool began_transaction; + bool prefer_numeric_type; }; /* Statement Handle */ diff --git a/t/27_metadata.t b/t/27_metadata.t index 13833c1..3cd4a20 100644 --- a/t/27_metadata.t +++ b/t/27_metadata.t @@ -45,6 +45,9 @@ $dbh->do("INSERT INTO meta4 VALUES ('xyz', 'b')"); $sth = $dbh->prepare('SELECT * FROM meta4'); $sth->execute; $sth->fetch; + +$dbh->{sqlite_prefer_numeric_type} = 1; + my $types = $sth->{TYPE}; my $names = $sth->{NAME}; # diag "Types: @$types\nNames: @$names";