SQLite

Check-in [affb0fa2e8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add built-in functions numeric(), text(), and blob() that coerce types. Ticket #1287. (CVS 2524)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: affb0fa2e8c5ff497838ba3c2994cdb1f6f50c68
User & Date: drh 2005-06-22 10:53:59.000
Context
2005-06-23
03:15
Make sure the String8 opcode always has a non-null P3 argument in the foreign_key_list pragma. Ticket #1297. (CVS 2525) (check-in: bcf62dc7a1 user: drh tags: trunk)
2005-06-22
10:53
Add built-in functions numeric(), text(), and blob() that coerce types. Ticket #1287. (CVS 2524) (check-in: affb0fa2e8 user: drh tags: trunk)
08:48
Allow parameters to be introduced by characters ':', '$' and '#'. This is an experimental change. (CVS 2523) (check-in: f3427a139c user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/func.c.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
** This file contains the C functions that implement various SQL
** functions of SQLite.  
**
** There is only one exported symbol in this file - the function
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.98 2005/05/24 12:01:02 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "vdbeInt.h"







|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
** This file contains the C functions that implement various SQL
** functions of SQLite.  
**
** There is only one exported symbol in this file - the function
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.99 2005/06/22 10:53:59 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "vdbeInt.h"
73
74
75
76
77
78
79























































































80
81
82
83
84
85
86
    case SQLITE_INTEGER: z = "integer"; break;
    case SQLITE_TEXT:    z = "text";    break;
    case SQLITE_FLOAT:   z = "real";    break;
    case SQLITE_BLOB:    z = "blob";    break;
  }
  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
}
























































































/*
** Implementation of the length() function
*/
static void lengthFunc(
  sqlite3_context *context,
  int argc,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    case SQLITE_INTEGER: z = "integer"; break;
    case SQLITE_TEXT:    z = "text";    break;
    case SQLITE_FLOAT:   z = "real";    break;
    case SQLITE_BLOB:    z = "blob";    break;
  }
  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
}

/*
** Convert the argument to a numeric type.
*/
static void numericFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *z = 0;
  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_NULL: {
      sqlite3_result_int(context, 0);
      break;
    }
    case SQLITE_INTEGER:
    case SQLITE_FLOAT: {
      sqlite3_result_value(context, argv[0]);
      break;
    }
    case SQLITE_TEXT:
    case SQLITE_BLOB: {
      z = sqlite3_value_text(argv[0]);
      while( *z && *z!='.' ){ z++; }
      if( *z ){
        sqlite3_result_double(context, sqlite3_value_double(argv[0]));
      }else{
        sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
      }
      break;
    }
  }
}

/*
** Convert the argument to TEXT
*/
static void textFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_NULL: {
      sqlite3_result_text(context, "", 0, SQLITE_STATIC);
      break;
    }
    case SQLITE_BLOB:
    case SQLITE_INTEGER:
    case SQLITE_FLOAT: {
      sqlite3_result_text(context, sqlite3_value_text(argv[0]),
          sqlite3_value_bytes(argv[0]), SQLITE_TRANSIENT);
      break;
    }
    case SQLITE_TEXT: {
      sqlite3_result_value(context, argv[0]);
      break;
    }
  }
}

/*
** Convert the argument to TEXT
*/
static void blobFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_NULL: {
      sqlite3_result_blob(context, "", 0, SQLITE_STATIC);
      break;
    }
    case SQLITE_TEXT:
    case SQLITE_INTEGER:
    case SQLITE_FLOAT: {
      sqlite3_result_blob(context, sqlite3_value_text(argv[0]),
          sqlite3_value_bytes(argv[0]), SQLITE_TRANSIENT);
      break;
    }
    case SQLITE_BLOB: {
      sqlite3_result_value(context, argv[0]);
      break;
    }
  }
}

/*
** Implementation of the length() function
*/
static void lengthFunc(
  sqlite3_context *context,
  int argc,
967
968
969
970
971
972
973



974
975
976
977
978
979
980
    { "glob",               2, 0, SQLITE_UTF8,    0, globFunc   },
    { "nullif",             2, 0, SQLITE_UTF8,    1, nullifFunc },
    { "sqlite_version",     0, 0, SQLITE_UTF8,    0, versionFunc},
    { "quote",              1, 0, SQLITE_UTF8,    0, quoteFunc  },
    { "last_insert_rowid",  0, 1, SQLITE_UTF8,    0, last_insert_rowid },
    { "changes",            0, 1, SQLITE_UTF8,    0, changes    },
    { "total_changes",      0, 1, SQLITE_UTF8,    0, total_changes },



#ifdef SQLITE_SOUNDEX
    { "soundex",            1, 0, SQLITE_UTF8, 0, soundexFunc},
#endif
#ifdef SQLITE_TEST
    { "randstr",               2, 0, SQLITE_UTF8, 0, randStr    },
    { "test_destructor",       1, 1, SQLITE_UTF8, 0, test_destructor},
    { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},







>
>
>







1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
    { "glob",               2, 0, SQLITE_UTF8,    0, globFunc   },
    { "nullif",             2, 0, SQLITE_UTF8,    1, nullifFunc },
    { "sqlite_version",     0, 0, SQLITE_UTF8,    0, versionFunc},
    { "quote",              1, 0, SQLITE_UTF8,    0, quoteFunc  },
    { "last_insert_rowid",  0, 1, SQLITE_UTF8,    0, last_insert_rowid },
    { "changes",            0, 1, SQLITE_UTF8,    0, changes    },
    { "total_changes",      0, 1, SQLITE_UTF8,    0, total_changes },
    { "text",               1, 0, SQLITE_UTF8,    0, textFunc      },
    { "numeric",            1, 0, SQLITE_UTF8,    0, numericFunc   },
    { "blob",               1, 0, SQLITE_UTF8,    0, blobFunc      },
#ifdef SQLITE_SOUNDEX
    { "soundex",            1, 0, SQLITE_UTF8, 0, soundexFunc},
#endif
#ifdef SQLITE_TEST
    { "randstr",               2, 0, SQLITE_UTF8, 0, randStr    },
    { "test_destructor",       1, 1, SQLITE_UTF8, 0, test_destructor},
    { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count},
Changes to src/vdbeapi.c.
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
*/
void sqlite3_result_blob(
  sqlite3_context *pCtx, 
  const void *z, 
  int n, 
  void (*xDel)(void *)
){
  assert( n>0 );
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
}
void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
}
void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
  pCtx->isError = 1;







|







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
*/
void sqlite3_result_blob(
  sqlite3_context *pCtx, 
  const void *z, 
  int n, 
  void (*xDel)(void *)
){
  assert( n>=0 );
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
}
void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
}
void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
  pCtx->isError = 1;
Changes to test/func.test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 2001 September 15
#
# 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 built-in functions.
#
# $Id: func.test,v 1.34 2005/03/29 03:11:00 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Create a table to work with.
#
do_test func-0.0 {













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 2001 September 15
#
# 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 built-in functions.
#
# $Id: func.test,v 1.35 2005/06/22 10:53:59 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Create a table to work with.
#
do_test func-0.0 {
481
482
483
484
485
486
487


488





























































































































489


  sqlite3_step $::STMT
  sqlite3_finalize $::STMT
  execsql {
    SELECT quote(a), quote(b) FROM tbl2;
  }
} {X'616263' NULL}



finish_test







































































































































>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
  sqlite3_step $::STMT
  sqlite3_finalize $::STMT
  execsql {
    SELECT quote(a), quote(b) FROM tbl2;
  }
} {X'616263' NULL}

# Tests for the blob(), text() and numeric() built-ins
#
do_test func-17.1 {
  execsql {SELECT x'616263'}
} abc
do_test func-17.2 {
  execsql {SELECT typeof(x'616263')}
} blob
do_test func-17.3 {
  execsql {SELECT text(x'616263')}
} abc
do_test func-17.4 {
  execsql {SELECT typeof(text(x'616263'))}
} text
do_test func-17.5 {
  execsql {SELECT numeric(x'616263')}
} 0
do_test func-17.6 {
  execsql {SELECT typeof(numeric(x'616263'))}
} integer
do_test func-17.7 {
  execsql {SELECT blob(x'616263')}
} abc
do_test func-17.8 {
  execsql {SELECT typeof(blob(x'616263'))}
} blob
do_test func-17.11 {
  execsql {SELECT null}
} {{}}
do_test func-17.12 {
  execsql {SELECT typeof(NULL)}
} null
do_test func-17.13 {
  execsql {SELECT text(NULL)}
} {{}}
do_test func-17.14 {
  execsql {SELECT typeof(text(NULL))}
} text
do_test func-17.15 {
  execsql {SELECT numeric(NULL)}
} 0
do_test func-17.16 {
  execsql {SELECT typeof(numeric(NULL))}
} integer
do_test func-17.17 {
  execsql {SELECT blob(NULL)}
} {{}}
do_test func-17.18 {
  execsql {SELECT typeof(blob(NULL))}
} blob
do_test func-17.21 {
  execsql {SELECT 123}
} {123}
do_test func-17.22 {
  execsql {SELECT typeof(123)}
} integer
do_test func-17.23 {
  execsql {SELECT text(123)}
} {123}
do_test func-17.24 {
  execsql {SELECT typeof(text(123))}
} text
do_test func-17.25 {
  execsql {SELECT numeric(123)}
} 123
do_test func-17.26 {
  execsql {SELECT typeof(numeric(123))}
} integer
do_test func-17.27 {
  execsql {SELECT blob(123)}
} {123}
do_test func-17.28 {
  execsql {SELECT typeof(blob(123))}
} blob
do_test func-17.31 {
  execsql {SELECT 123.456}
} {123.456}
do_test func-17.32 {
  execsql {SELECT typeof(123.456)}
} real
do_test func-17.33 {
  execsql {SELECT text(123.456)}
} {123.456}
do_test func-17.34 {
  execsql {SELECT typeof(text(123.456))}
} text
do_test func-17.35 {
  execsql {SELECT numeric(123.456)}
} 123.456
do_test func-17.36 {
  execsql {SELECT typeof(numeric(123.456))}
} real
do_test func-17.37 {
  execsql {SELECT blob(123.456)}
} {123.456}
do_test func-17.38 {
  execsql {SELECT typeof(blob(123.456))}
} blob
do_test func-17.41 {
  execsql {SELECT '123abc'}
} {123abc}
do_test func-17.42 {
  execsql {SELECT typeof('123abc')}
} text
do_test func-17.43 {
  execsql {SELECT text('123abc')}
} {123abc}
do_test func-17.44 {
  execsql {SELECT typeof(text('123abc'))}
} text
do_test func-17.45 {
  execsql {SELECT numeric('123abc')}
} 123
do_test func-17.46 {
  execsql {SELECT typeof(numeric('123abc'))}
} integer
do_test func-17.47 {
  execsql {SELECT blob('123abc')}
} {123abc}
do_test func-17.48 {
  execsql {SELECT typeof(blob('123abc'))}
} blob
do_test func-17.49 {
  execsql {SELECT numeric('123.5abc')}
} 123.5
do_test func-17.49b {
  execsql {SELECT typeof(numeric('123.5abc'))}
} real


finish_test