blob: ab45b1fea9db08f1e2cae039ad2e63e465a563a0 [file] [log] [blame]
drhdc04c582002-02-24 01:55:15 +00001/*
2** 2002 February 23
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains the C functions that implement various SQL
13** functions of SQLite.
14**
15** There is only one exported symbol in this file - the function
16** sqliteRegisterBuildinFunctions() found at the bottom of the file.
17** All other code has file scope.
18**
drhd3a149e2002-02-24 17:12:53 +000019** $Id: func.c,v 1.2 2002/02/24 17:12:54 drh Exp $
drhdc04c582002-02-24 01:55:15 +000020*/
21#include <ctype.h>
drhd3a149e2002-02-24 17:12:53 +000022#include <math.h>
23#include <stdlib.h>
drhdc04c582002-02-24 01:55:15 +000024#include "sqlite.h"
25
26/*
27** Implementation of the upper() and lower() SQL functions.
28*/
29static void upperFunc(void *context, int argc, const char **argv){
30 char *z;
31 int i;
32 if( argc<1 || argv[0]==0 ) return;
33 z = sqlite_set_result_string(context, argv[0], -1);
34 if( z==0 ) return;
35 for(i=0; z[i]; i++){
36 if( islower(z[i]) ) z[i] = toupper(z[i]);
37 }
38}
39static void lowerFunc(void *context, int argc, const char **argv){
40 char *z;
41 int i;
42 if( argc<1 || argv[0]==0 ) return;
43 z = sqlite_set_result_string(context, argv[0], -1);
44 if( z==0 ) return;
45 for(i=0; z[i]; i++){
46 if( isupper(z[i]) ) z[i] = tolower(z[i]);
47 }
48}
49
50/*
drhd3a149e2002-02-24 17:12:53 +000051** An instance of the following structure holds the context of a
52** standard deviation computation.
53*/
54typedef struct StdDevCtx StdDevCtx;
55struct StdDevCtx {
56 double sum; /* Sum of terms */
57 double sum2; /* Sum of the squares of terms */
58 int n; /* Number of terms seen so far */
59};
60
61/*
62** Routines used to compute the standard deviation as an aggregate.
63*/
64static void *stdDevStep(void *stddev, int argc, char **argv){
65 StdDevCtx *p;
66 double x;
67 if( argc<1 ) return 0;
68 if( stddev==0 ){
69 p = malloc( sizeof(*p) );
70 p->n = 0;
71 p->sum = 0.0;
72 p->sum2 = 0.0;
73 }else{
74 p = (StdDevCtx*)stddev;
75 }
76 x = atof(argv[0]);
77 p->sum += x;
78 p->sum2 += x*x;
79 p->n++;
80 return p;
81}
82static void stdDevFinalize(void *stddev, void *context){
83 StdDevCtx *p = (StdDevCtx*)stddev;
84 if( context && p && p->n>1 ){
85 double rN = p->n;
86 sqlite_set_result_double(context,
87 sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
88 }
89 if( stddev ) free(stddev);
90}
91
92/*
drhdc04c582002-02-24 01:55:15 +000093** This file registered all of the above C functions as SQL
94** functions.
95*/
96void sqliteRegisterBuildinFunctions(sqlite *db){
97 sqlite_create_function(db, "upper", 1, upperFunc);
98 sqlite_create_function(db, "lower", 1, lowerFunc);
drhd3a149e2002-02-24 17:12:53 +000099 sqlite_create_aggregate(db, "stddev", 1, stdDevStep, stdDevFinalize);
drhdc04c582002-02-24 01:55:15 +0000100}