}
}
+/*
+** EXPERIMENTAL - This is not an official function. The interface may
+** change. This function may disappear. Do not write code that depends
+** on this function.
+**
+** Implementation of the TOINTEGER() function. This function takes a
+** single argument. If the argument is an integer or is a double that
+** can be losslessly converted to an integer, the return value is the
+** same as the argument. If the argument is a double that cannot be
+** losslessly represented as an integer, the return value is undefined.
+** If the argument is NULL, the return value is NULL. Otherwise, an
+** attempt is made to convert the argument to an integer. If the
+** conversion is successful, the integer value is returned; otherwise,
+** NULL is returned.
+*/
+static void tointegerFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ assert( argc==1 );
+ UNUSED_PARAMETER(argc);
+ switch( sqlite3_value_type(argv[0]) ){
+ case SQLITE_FLOAT:
+ case SQLITE_INTEGER: {
+ sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
+ break;
+ }
+ case SQLITE_BLOB:
+ case SQLITE_TEXT: {
+ const unsigned char *zStr = sqlite3_value_text(argv[0]);
+ if( zStr ){
+ int nStr = sqlite3_value_bytes(argv[0]);
+ if( nStr ){
+ i64 iVal;
+ if( !sqlite3Atoi64(zStr, &iVal, nStr, SQLITE_UTF8) ){
+ sqlite3_result_int64(context, iVal);
+ return;
+ }
+ }
+ }
+ sqlite3_result_null(context);
+ break;
+ }
+ default: {
+ assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
+ sqlite3_result_null(context);
+ break;
+ }
+ }
+}
+
+/*
+** EXPERIMENTAL - This is not an official function. The interface may
+** change. This function may disappear. Do not write code that depends
+** on this function.
+**
+** Implementation of the TODOUBLE() function. This function takes a
+** single argument. If the argument is a double or is an integer that
+** can be losslessly converted to a double, the return value is the
+** same as the argument. If the argument is an integer that cannot be
+** losslessly represented as a double, the return value is undefined.
+** If the argument is NULL, the return value is NULL. Otherwise, an
+** attempt is made to convert the argument to a double. If the
+** conversion is successful, the double value is returned; otherwise,
+** NULL is returned.
+*/
+#ifndef SQLITE_OMIT_FLOATING_POINT
+static void todoubleFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ assert( argc==1 );
+ UNUSED_PARAMETER(argc);
+ switch( sqlite3_value_type(argv[0]) ){
+ case SQLITE_FLOAT:
+ case SQLITE_INTEGER: {
+ sqlite3_result_double(context, sqlite3_value_double(argv[0]));
+ break;
+ }
+ case SQLITE_BLOB:
+ case SQLITE_TEXT: {
+ const unsigned char *zStr = sqlite3_value_text(argv[0]);
+ if( zStr ){
+ int nStr = sqlite3_value_bytes(argv[0]);
+ if( nStr ){
+ double rVal;
+ if( sqlite3AtoF(zStr, &rVal, nStr, SQLITE_UTF8) ){
+ sqlite3_result_double(context, rVal);
+ return;
+ }
+ }
+ }
+ sqlite3_result_null(context);
+ break;
+ }
+ default: {
+ assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
+ sqlite3_result_null(context);
+ break;
+ }
+ }
+}
+#endif
+
/*
** The unicode() function. Return the integer unicode code-point value
** for the first character of the input string.
FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
FUNCTION(quote, 1, 0, 0, quoteFunc ),
+ FUNCTION(tointeger, 1, 0, 0, tointegerFunc ),
+#ifndef SQLITE_OMIT_FLOATING_POINT
+ FUNCTION(todouble, 1, 0, 0, todoubleFunc ),
+#endif
FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
FUNCTION(changes, 0, 0, 0, changes ),
FUNCTION(total_changes, 0, 0, 0, total_changes ),
--- /dev/null
+# 2013 March 10
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements regression tests for SQLite library. The
+# focus of this file is testing the TOINTEGER() and TODOUBLE()
+# functions.
+#
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+set i 0
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(NULL);
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(' ');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('1234');
+} {1234}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(' 1234');
+} {1234}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('bad');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('0xBAD');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('123BAD');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('0x123BAD');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('123NO');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('0x123NO');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('-0x1');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('-0x0');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('0x0');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger('0x1');
+} {{}}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-1);
+} {-1}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-0);
+} {0}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(0);
+} {0}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(1);
+} {1}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-1.79769313486232e308 - 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-1.79769313486232e308);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-1.79769313486232e308 + 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-9223372036854775808 - 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-9223372036854775808);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-9223372036854775808 + 1);
+} {-9223372036854775807}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-2147483648 - 1);
+} {-2147483649}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-2147483648);
+} {-2147483648}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(-2147483648 + 1);
+} {-2147483647}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(2147483647 - 1);
+} {2147483646}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(2147483647);
+} {2147483647}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(2147483647 + 1);
+} {2147483648}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9223372036854775807 - 1);
+} {9223372036854775806}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9223372036854775807);
+} {9223372036854775807}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9223372036854775807 + 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(1.79769313486232e308 - 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(1.79769313486232e308);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(1.79769313486232e308 + 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(4503599627370496 - 1);
+} {4503599627370495}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(4503599627370496);
+} {4503599627370496}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(4503599627370496 + 1);
+} {4503599627370497}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9007199254740992 - 1);
+} {9007199254740991}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9007199254740992);
+} {9007199254740992}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9007199254740992 + 1);
+} {9007199254740993}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9223372036854775808 - 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9223372036854775808);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(9223372036854775808 + 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(18446744073709551616 - 1);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(18446744073709551616);
+} {-9223372036854775808}
+do_execsql_test func4-1.[incr i] {
+ SELECT tointeger(18446744073709551616 + 1);
+} {-9223372036854775808}
+
+ifcapable floatingpoint {
+ set i 0
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(NULL);
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(' ');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('1234');
+ } {1234.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(' 1234');
+ } {1234.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('bad');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('0xBAD');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('123BAD');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('0x123BAD');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('123NO');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('0x123NO');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('-0x1');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('-0x0');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('0x0');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble('0x1');
+ } {{}}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-1);
+ } {-1.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-0);
+ } {0.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(0);
+ } {0.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(1);
+ } {1.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-1.79769313486232e308 - 1);
+ } {-Inf}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-1.79769313486232e308);
+ } {-Inf}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-1.79769313486232e308 + 1);
+ } {-Inf}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-9223372036854775808 - 1);
+ } {-9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-9223372036854775808);
+ } {-9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-9223372036854775808 + 1);
+ } {-9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-2147483648 - 1);
+ } {-2147483649.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-2147483648);
+ } {-2147483648.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(-2147483648 + 1);
+ } {-2147483647.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(2147483647 - 1);
+ } {2147483646.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(2147483647);
+ } {2147483647.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(2147483647 + 1);
+ } {2147483648.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9223372036854775807 - 1);
+ } {9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9223372036854775807);
+ } {9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9223372036854775807 + 1);
+ } {9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(1.79769313486232e308 - 1);
+ } {Inf}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(1.79769313486232e308);
+ } {Inf}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(1.79769313486232e308 + 1);
+ } {Inf}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(4503599627370496 - 1);
+ } {4503599627370500.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(4503599627370496);
+ } {4503599627370500.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(4503599627370496 + 1);
+ } {4503599627370500.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9007199254740992 - 1);
+ } {9007199254740990.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9007199254740992);
+ } {9007199254740990.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9007199254740992 + 1);
+ } {9007199254740990.0}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9223372036854775808 - 1);
+ } {9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9223372036854775808);
+ } {9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(9223372036854775808 + 1);
+ } {9.22337203685478e+18}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(18446744073709551616 - 1);
+ } {1.84467440737096e+19}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(18446744073709551616);
+ } {1.84467440737096e+19}
+ do_execsql_test func4-2.[incr i] {
+ SELECT todouble(18446744073709551616 + 1);
+ } {1.84467440737096e+19}
+}
+
+finish_test