mirror of
https://github.com/bdnugget/php-src.git
synced 2026-11-20 15:36:37 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a400da70d3 |
No files matched your search
@@ -1,32 +0,0 @@
|
||||
BEGIN {
|
||||
mode=0
|
||||
sources=""
|
||||
}
|
||||
|
||||
mode == 0 && /^LTLIBRARY_SOURCES.*\\$/ {
|
||||
if (match($0, "[^=]*$")) {
|
||||
sources=substr($0, RSTART, RLENGTH-1)
|
||||
}
|
||||
mode=1
|
||||
next
|
||||
}
|
||||
|
||||
mode == 0 && /^LTLIBRARY_SOURCES.*/ {
|
||||
if (match($0, "[^=]*$")) {
|
||||
sources=substr($0, RSTART, RLENGTH)
|
||||
}
|
||||
}
|
||||
|
||||
mode == 1 && /.*\\$/ {
|
||||
sources=sources substr($0, 0, length - 1)
|
||||
next
|
||||
}
|
||||
|
||||
mode == 1 {
|
||||
sources=sources $0
|
||||
mode=0
|
||||
}
|
||||
|
||||
END {
|
||||
print sources
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
Bzip2
|
||||
Sterling Hughes
|
||||
-529
@@ -1,529 +0,0 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP version 4.0 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2003 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.02 of the PHP license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available at through the world-wide-web at |
|
||||
| http://www.php.net/license/2_02.txt. |
|
||||
| If you did not receive a copy of the PHP license and are unable to |
|
||||
| obtain it through the world-wide-web, please send a note to |
|
||||
| license@php.net so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Sterling Hughes <sterling@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "php.h"
|
||||
#include "php_bz2.h"
|
||||
|
||||
#if HAVE_BZ2
|
||||
|
||||
/* PHP Includes */
|
||||
#include "ext/standard/file.h"
|
||||
#include "ext/standard/info.h"
|
||||
#include "ext/standard/php_string.h"
|
||||
|
||||
/* for fileno() */
|
||||
#include <stdio.h>
|
||||
|
||||
/* Internal error constants */
|
||||
#define PHP_BZ_ERRNO 0
|
||||
#define PHP_BZ_ERRSTR 1
|
||||
#define PHP_BZ_ERRBOTH 2
|
||||
|
||||
/* Blocksize of the decompression buffer */
|
||||
#define PHP_BZ_DECOMPRESS_SIZE 4096
|
||||
|
||||
function_entry bz2_functions[] = {
|
||||
PHP_FE(bzopen, NULL)
|
||||
PHP_FE(bzread, NULL)
|
||||
PHP_FALIAS(bzwrite, fwrite, NULL)
|
||||
PHP_FALIAS(bzflush, fflush, NULL)
|
||||
PHP_FALIAS(bzclose, fclose, NULL)
|
||||
PHP_FE(bzerrno, NULL)
|
||||
PHP_FE(bzerrstr, NULL)
|
||||
PHP_FE(bzerror, NULL)
|
||||
PHP_FE(bzcompress, NULL)
|
||||
PHP_FE(bzdecompress, NULL)
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
zend_module_entry bz2_module_entry = {
|
||||
STANDARD_MODULE_HEADER,
|
||||
"bz2",
|
||||
bz2_functions,
|
||||
PHP_MINIT(bz2),
|
||||
PHP_MSHUTDOWN(bz2),
|
||||
NULL,
|
||||
NULL,
|
||||
PHP_MINFO(bz2),
|
||||
NO_VERSION_YET,
|
||||
STANDARD_MODULE_PROPERTIES
|
||||
};
|
||||
|
||||
#ifdef COMPILE_DL_BZ2
|
||||
ZEND_GET_MODULE(bz2)
|
||||
#endif
|
||||
|
||||
struct php_bz2_stream_data_t {
|
||||
BZFILE *bz_file;
|
||||
php_stream *stream;
|
||||
};
|
||||
|
||||
/* {{{ BZip2 stream implementation */
|
||||
|
||||
static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
|
||||
{
|
||||
struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *) stream->abstract;
|
||||
size_t ret;
|
||||
|
||||
ret = BZ2_bzread(self->bz_file, buf, count);
|
||||
|
||||
if (ret == 0) {
|
||||
stream->eof = 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
|
||||
{
|
||||
struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *) stream->abstract;
|
||||
|
||||
return BZ2_bzwrite(self->bz_file, (char*)buf, count);
|
||||
}
|
||||
|
||||
static int php_bz2iop_close(php_stream *stream, int close_handle TSRMLS_DC)
|
||||
{
|
||||
struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
|
||||
int ret = EOF;
|
||||
|
||||
if (close_handle) {
|
||||
BZ2_bzclose(self->bz_file);
|
||||
}
|
||||
|
||||
if (self->stream) {
|
||||
php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
|
||||
}
|
||||
|
||||
efree(self);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int php_bz2iop_flush(php_stream *stream TSRMLS_DC)
|
||||
{
|
||||
struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
|
||||
return BZ2_bzflush(self->bz_file);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
php_stream_ops php_stream_bz2io_ops = {
|
||||
php_bz2iop_write, php_bz2iop_read,
|
||||
php_bz2iop_close, php_bz2iop_flush,
|
||||
"BZip2",
|
||||
NULL, /* seek */
|
||||
NULL, /* cast */
|
||||
NULL, /* stat */
|
||||
NULL /* set_option */
|
||||
};
|
||||
|
||||
/* {{{ Bzip2 stream openers */
|
||||
PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz,
|
||||
char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC)
|
||||
{
|
||||
struct php_bz2_stream_data_t *self;
|
||||
|
||||
self = emalloc(sizeof(*self));
|
||||
|
||||
self->stream = innerstream;
|
||||
self->bz_file = bz;
|
||||
|
||||
return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
|
||||
}
|
||||
|
||||
PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
|
||||
char *path,
|
||||
char *mode,
|
||||
int options,
|
||||
char **opened_path,
|
||||
php_stream_context *context STREAMS_DC TSRMLS_DC)
|
||||
{
|
||||
php_stream *retstream = NULL, *stream = NULL;
|
||||
char *path_copy = NULL;
|
||||
BZFILE *bz_file = NULL;
|
||||
|
||||
if (strncasecmp("compress.bzip2://", path, 17) == 0) {
|
||||
path += 17;
|
||||
}
|
||||
|
||||
#ifdef VIRTUAL_DIR
|
||||
virtual_filepath(path, &path_copy TSRMLS_CC);
|
||||
#else
|
||||
path_copy = path;
|
||||
#endif
|
||||
|
||||
/* try and open it directly first */
|
||||
bz_file = BZ2_bzopen(path_copy, mode);
|
||||
|
||||
if (opened_path && bz_file) {
|
||||
*opened_path = estrdup(path_copy);
|
||||
}
|
||||
path_copy = NULL;
|
||||
|
||||
if (bz_file == NULL) {
|
||||
/* that didn't work, so try and get something from the network/wrapper */
|
||||
stream = php_stream_open_wrapper(path, mode, options, opened_path);
|
||||
|
||||
if (stream) {
|
||||
int fd;
|
||||
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
|
||||
bz_file = BZ2_bzdopen(fd, mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bz_file) {
|
||||
retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC TSRMLS_CC);
|
||||
if (retstream) {
|
||||
return retstream;
|
||||
}
|
||||
|
||||
BZ2_bzclose(bz_file);
|
||||
}
|
||||
|
||||
if (stream) {
|
||||
php_stream_close(stream);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
static php_stream_wrapper_ops bzip2_stream_wops = {
|
||||
_php_stream_bz2open,
|
||||
NULL, /* close */
|
||||
NULL, /* fstat */
|
||||
NULL, /* stat */
|
||||
NULL, /* opendir */
|
||||
"BZip2",
|
||||
NULL /* unlink */
|
||||
};
|
||||
|
||||
php_stream_wrapper php_stream_bzip2_wrapper = {
|
||||
&bzip2_stream_wops,
|
||||
NULL,
|
||||
0 /* is_url */
|
||||
};
|
||||
|
||||
static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int);
|
||||
|
||||
PHP_MINIT_FUNCTION(bz2)
|
||||
{
|
||||
php_register_url_stream_wrapper("compress.bzip2", &php_stream_bzip2_wrapper TSRMLS_CC);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
PHP_MSHUTDOWN_FUNCTION(bz2)
|
||||
{
|
||||
php_unregister_url_stream_wrapper("compress.bzip2" TSRMLS_CC);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
PHP_MINFO_FUNCTION(bz2)
|
||||
{
|
||||
php_info_print_table_start();
|
||||
php_info_print_table_row(2, "BZip2 Support", "Enabled");
|
||||
php_info_print_table_row(2, "BZip2 Version", (char *) BZ2_bzlibVersion());
|
||||
php_info_print_table_end();
|
||||
}
|
||||
|
||||
/* {{{ proto string bzread(int bz[, int length])
|
||||
Reads up to length bytes from a BZip2 stream, or 1024 bytes if length is not specified */
|
||||
PHP_FUNCTION(bzread)
|
||||
{
|
||||
zval *bz;
|
||||
long len = 1024;
|
||||
php_stream *stream;
|
||||
|
||||
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &bz, &len)) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
php_stream_from_zval(stream, &bz);
|
||||
|
||||
if (len < 0) {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "length may not be negative");
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
Z_STRVAL_P(return_value) = emalloc(len + 1);
|
||||
Z_STRLEN_P(return_value) = php_stream_read(stream, Z_STRVAL_P(return_value), len);
|
||||
|
||||
if (Z_STRLEN_P(return_value) < 0) {
|
||||
efree(Z_STRVAL_P(return_value));
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not read valid bz2 data from stream");
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
|
||||
|
||||
if (PG(magic_quotes_runtime)) {
|
||||
Z_STRVAL_P(return_value) = php_addslashes( Z_STRVAL_P(return_value),
|
||||
Z_STRLEN_P(return_value),
|
||||
&Z_STRLEN_P(return_value), 1 TSRMLS_CC);
|
||||
}
|
||||
|
||||
Z_TYPE_P(return_value) = IS_STRING;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto resource bzopen(string|int file|fp, string mode)
|
||||
Opens a new BZip2 stream */
|
||||
PHP_FUNCTION(bzopen)
|
||||
{
|
||||
zval **file, /* The file to open */
|
||||
**mode; /* The mode to open the stream with */
|
||||
BZFILE *bz; /* The compressed file stream */
|
||||
php_stream *stream = NULL;
|
||||
|
||||
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &file, &mode) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_string_ex(mode);
|
||||
|
||||
/* If it's not a resource its a string containing the filename to open */
|
||||
if (Z_TYPE_PP(file) != IS_RESOURCE) {
|
||||
convert_to_string_ex(file);
|
||||
stream = php_stream_bz2open(NULL,
|
||||
Z_STRVAL_PP(file),
|
||||
Z_STRVAL_PP(mode),
|
||||
ENFORCE_SAFE_MODE | REPORT_ERRORS,
|
||||
NULL);
|
||||
} else {
|
||||
/* If it is a resource, than its a stream resource */
|
||||
int fd;
|
||||
|
||||
php_stream_from_zval(stream, file);
|
||||
|
||||
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
bz = BZ2_bzdopen(fd, Z_STRVAL_PP(mode));
|
||||
|
||||
stream = php_stream_bz2open_from_BZFILE(bz, Z_STRVAL_PP(mode), stream);
|
||||
}
|
||||
|
||||
if (stream) {
|
||||
php_stream_to_zval(stream, return_value);
|
||||
} else {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto int bzerrno(resource bz)
|
||||
Returns the error number */
|
||||
PHP_FUNCTION(bzerrno)
|
||||
{
|
||||
php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string bzerrstr(resource bz)
|
||||
Returns the error string */
|
||||
PHP_FUNCTION(bzerrstr)
|
||||
{
|
||||
php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto array bzerror(resource bz)
|
||||
Returns the error number and error string in an associative array */
|
||||
PHP_FUNCTION(bzerror)
|
||||
{
|
||||
php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string bzcompress(string source [, int blocksize100k [, int workfactor]])
|
||||
Compresses a string into BZip2 encoded data */
|
||||
PHP_FUNCTION(bzcompress)
|
||||
{
|
||||
zval **source, /* Source data to compress */
|
||||
**zblock_size, /* Optional block size to use */
|
||||
**zwork_factor; /* Optional work factor to use */
|
||||
char *dest = NULL; /* Destination to place the compressed data into */
|
||||
int error, /* Error Container */
|
||||
block_size = 4, /* Block size for compression algorithm */
|
||||
work_factor = 0, /* Work factor for compression algorithm */
|
||||
argc; /* Argument count */
|
||||
unsigned int source_len, /* Length of the source data */
|
||||
dest_len; /* Length of the destination buffer */
|
||||
|
||||
argc = ZEND_NUM_ARGS();
|
||||
|
||||
if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &source, &zblock_size, &zwork_factor) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
|
||||
convert_to_string_ex(source);
|
||||
|
||||
/* Assign them to easy to use variables, dest_len is initially the length of the data
|
||||
+ .01 x length of data + 600 which is the largest size the results of the compression
|
||||
could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net
|
||||
for pointing this out). */
|
||||
source_len = Z_STRLEN_PP(source);
|
||||
dest_len = Z_STRLEN_PP(source) + (0.01 * Z_STRLEN_PP(source)) + 600;
|
||||
|
||||
/* Allocate the destination buffer */
|
||||
dest = emalloc(dest_len + 1);
|
||||
|
||||
/* Handle the optional arguments */
|
||||
if (argc > 1) {
|
||||
convert_to_long_ex(zblock_size);
|
||||
block_size = Z_LVAL_PP(zblock_size);
|
||||
}
|
||||
|
||||
if (argc > 2) {
|
||||
convert_to_long_ex(zwork_factor);
|
||||
work_factor = Z_LVAL_PP(zwork_factor);
|
||||
}
|
||||
|
||||
error = BZ2_bzBuffToBuffCompress(dest, &dest_len, Z_STRVAL_PP(source), source_len, block_size, 0, work_factor);
|
||||
if (error != BZ_OK) {
|
||||
efree(dest);
|
||||
RETURN_LONG(error);
|
||||
} else {
|
||||
/* Copy the buffer, we have perhaps allocate alot more than we need,
|
||||
so we erealloc() the buffer to the proper size */
|
||||
dest = erealloc(dest, dest_len + 1);
|
||||
dest[dest_len] = 0;
|
||||
RETURN_STRINGL(dest, dest_len, 0);
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string bzdecompress(string source [, int small])
|
||||
Decompresses BZip2 compressed data */
|
||||
PHP_FUNCTION(bzdecompress)
|
||||
{
|
||||
zval **source, /* Source data to decompress */
|
||||
**zsmall; /* (Optional) user specified small */
|
||||
char *dest; /* Destination buffer, initially allocated */
|
||||
int error, /* Error container */
|
||||
iter = 1, /* Iteration count for the compression loop */
|
||||
size, /* Current size to realloc the dest buffer to */
|
||||
dest_len = PHP_BZ_DECOMPRESS_SIZE, /* Size of the destination length */
|
||||
small = 0, /* The actual small */
|
||||
argc = ZEND_NUM_ARGS(); /* Argument count */
|
||||
|
||||
if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &source, &zsmall) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
|
||||
convert_to_string_ex(source);
|
||||
|
||||
/* optional small argument handling */
|
||||
if (argc > 1) {
|
||||
convert_to_long_ex(zsmall);
|
||||
small = Z_LVAL_PP(zsmall);
|
||||
}
|
||||
|
||||
/* Depending on the size of the source buffer, either allocate
|
||||
the length of the source buffer or the a default decompression
|
||||
size */
|
||||
dest = emalloc(PHP_BZ_DECOMPRESS_SIZE > Z_STRLEN_PP(source) ? PHP_BZ_DECOMPRESS_SIZE : Z_STRLEN_PP(source));
|
||||
|
||||
/* (de)Compression Loop */
|
||||
do {
|
||||
/* Handle the (re)allocation of the buffer */
|
||||
size = dest_len * iter;
|
||||
if (iter > 1) {
|
||||
dest = erealloc(dest, size);
|
||||
}
|
||||
++iter;
|
||||
|
||||
/* Perform the decompression */
|
||||
error = BZ2_bzBuffToBuffDecompress(dest, &size, Z_STRVAL_PP(source), Z_STRLEN_PP(source), small, 0);
|
||||
} while (error == BZ_OUTBUFF_FULL);
|
||||
|
||||
if (error != BZ_OK) {
|
||||
efree(dest);
|
||||
RETURN_LONG(error);
|
||||
} else {
|
||||
/* we might have allocated a little to much, so erealloc the buffer
|
||||
down to size, before returning it */
|
||||
dest = erealloc(dest, size + 1);
|
||||
dest[size] = 0;
|
||||
RETURN_STRINGL(dest, size, 0);
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ php_bz2_error()
|
||||
The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */
|
||||
static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
|
||||
{
|
||||
zval **bzp; /* BZip2 Resource Pointer */
|
||||
php_stream *stream;
|
||||
const char *errstr; /* Error string */
|
||||
int errnum; /* Error number */
|
||||
struct php_bz2_stream_data_t *self;
|
||||
|
||||
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &bzp) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
|
||||
php_stream_from_zval(stream, bzp);
|
||||
|
||||
if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
self = (struct php_bz2_stream_data_t *) stream->abstract;
|
||||
|
||||
/* Fetch the error information */
|
||||
errstr = BZ2_bzerror(self->bz_file, &errnum);
|
||||
|
||||
/* Determine what to return */
|
||||
switch (opt) {
|
||||
case PHP_BZ_ERRNO:
|
||||
RETURN_LONG(errnum);
|
||||
break;
|
||||
case PHP_BZ_ERRSTR:
|
||||
RETURN_STRING((char*)errstr, 1);
|
||||
break;
|
||||
case PHP_BZ_ERRBOTH:
|
||||
array_init(return_value);
|
||||
|
||||
add_assoc_long (return_value, "errno", errnum);
|
||||
add_assoc_string(return_value, "errstr", (char*)errstr, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: fdm=marker
|
||||
* vim: noet sw=4 ts=4
|
||||
*/
|
||||
-108
@@ -1,108 +0,0 @@
|
||||
# Microsoft Developer Studio Project File - Name="bz2" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=bz2 - Win32 Debug_TS
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "bz2.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "bz2.mak" CFG="bz2 - Win32 Debug_TS"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "bz2 - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "bz2 - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "bz2 - Win32 Release_TS"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release_TS"
|
||||
# PROP BASE Intermediate_Dir "Release_TS"
|
||||
# PROP BASE Ignore_Export_Lib 0
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release_TS"
|
||||
# PROP Intermediate_Dir "Release_TS"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BZ2_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\..\php_build\includes" /D ZEND_DEBUG=0 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "COMPILE_DL_BZ2" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_BZ2=1 /D "PHP_BZ2_EXPORTS" /FR /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x407 /d "NDEBUG"
|
||||
# ADD RSC /l 0x407 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 libbz2.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php4ts.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS/php_bz2.dll" /libpath:"..\..\Release_TS" /libpath:"..\..\Release_TS_Inline" /libpath:"..\..\..\php_build\release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "bz2 - Win32 Debug_TS"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug_TS"
|
||||
# PROP BASE Intermediate_Dir "Debug_TS"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug_TS"
|
||||
# PROP Intermediate_Dir "Debug_TS"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BZ2_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\..\php_build\includes" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "COMPILE_DL_BZ2" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_BZ2=1 /D "PHP_BZ2_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x407 /d "_DEBUG"
|
||||
# ADD RSC /l 0x407 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 php4ts_debug.lib libbz2.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"..\..\Debug_TS/php_bz2.dll" /pdbtype:sept /libpath:"..\..\Debug_TS" /libpath:"..\..\..\php_build\release"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "bz2 - Win32 Release_TS"
|
||||
# Name "bz2 - Win32 Debug_TS"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\bz2.c
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\php_bz2.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
@@ -1,33 +0,0 @@
|
||||
dnl
|
||||
dnl $Id$
|
||||
dnl
|
||||
|
||||
PHP_ARG_WITH(bz2, for BZip2 support,
|
||||
[ --with-bz2[=DIR] Include BZip2 support])
|
||||
|
||||
if test "$PHP_BZ2" != "no"; then
|
||||
if test -r $PHP_BZ2/include/bzlib.h; then
|
||||
BZIP_DIR=$PHP_BZ2
|
||||
else
|
||||
AC_MSG_CHECKING(for BZip2 in default path)
|
||||
for i in /usr/local /usr; do
|
||||
if test -r $i/include/bzlib.h; then
|
||||
BZIP_DIR=$i
|
||||
AC_MSG_RESULT(found in $i)
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
if test -z "$BZIP_DIR"; then
|
||||
AC_MSG_RESULT(not found)
|
||||
AC_MSG_ERROR(Please reinstall the BZip2 distribution)
|
||||
fi
|
||||
|
||||
PHP_ADD_INCLUDE($BZIP_DIR/include)
|
||||
|
||||
PHP_SUBST(BZ2_SHARED_LIBADD)
|
||||
PHP_ADD_LIBRARY_WITH_PATH(bz2, $BZIP_DIR/lib, BZ2_SHARED_LIBADD)
|
||||
AC_CHECK_LIB(bz2, BZ2_bzerror, [AC_DEFINE(HAVE_BZ2,1,[ ])], [AC_MSG_ERROR(bz2 module requires libbz2 >= 1.0.0)],)
|
||||
|
||||
PHP_NEW_EXTENSION(bz2, bz2.c, $ext_shared)
|
||||
fi
|
||||
@@ -1,35 +0,0 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
<!-- do not use the "Type" attribute here, that one is only for
|
||||
generated package.xml files -->
|
||||
<package>
|
||||
<name>bz2</name>
|
||||
<summary>A Bzip2 management extension</summary>
|
||||
<description>
|
||||
Bz2 is an extension to create and parse bzip2 compressed data.
|
||||
</description>
|
||||
<license>PHP License</license>
|
||||
<maintainers>
|
||||
<maintainer>
|
||||
<user>sterling</user>
|
||||
<name>Sterling Hughes</name>
|
||||
<email>sterling@php.net</email>
|
||||
</maintainer>
|
||||
</maintainers>
|
||||
<release>
|
||||
<version>1.0</version>
|
||||
<date>2003-05-17</date>
|
||||
<state>stable</state>
|
||||
<notes>
|
||||
Initial Release in PECL
|
||||
</notes>
|
||||
</release>
|
||||
<filelist>
|
||||
<dir role="src" name="/">
|
||||
<file role="doc">CREDITS</file>
|
||||
<file role="src">config.m4</file>
|
||||
<file role="doc">php_bz2.h</file>
|
||||
<file role="src">bz2.c</file>
|
||||
<file role="src">bz2.dsp</file>
|
||||
</dir>
|
||||
</filelist>
|
||||
</package>
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 4 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2003 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.02 of the PHP license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available at through the world-wide-web at |
|
||||
| http://www.php.net/license/2_02.txt. |
|
||||
| If you did not receive a copy of the PHP license and are unable to |
|
||||
| obtain it through the world-wide-web, please send a note to |
|
||||
| license@php.net so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Sterling Hughes <sterling@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef PHP_BZ2_H
|
||||
#define PHP_BZ2_H
|
||||
|
||||
#if HAVE_BZ2
|
||||
|
||||
extern zend_module_entry bz2_module_entry;
|
||||
#define phpext_bz2_ptr &bz2_module_entry
|
||||
|
||||
/* Bzip2 includes */
|
||||
#include <bzlib.h>
|
||||
|
||||
PHP_MINIT_FUNCTION(bz2);
|
||||
PHP_MSHUTDOWN_FUNCTION(bz2);
|
||||
PHP_MINFO_FUNCTION(bz2);
|
||||
PHP_FUNCTION(bzopen);
|
||||
PHP_FUNCTION(bzread);
|
||||
PHP_FUNCTION(bzerrno);
|
||||
PHP_FUNCTION(bzerrstr);
|
||||
PHP_FUNCTION(bzerror);
|
||||
PHP_FUNCTION(bzcompress);
|
||||
PHP_FUNCTION(bzdecompress);
|
||||
|
||||
#else
|
||||
#define phpext_bz2_ptr NULL
|
||||
#endif
|
||||
|
||||
#ifdef PHP_WIN32
|
||||
# ifdef PHP_BZ2_EXPORTS
|
||||
# define PHP_BZ2_API __declspec(dllexport)
|
||||
# else
|
||||
# define PHP_BZ2_API __declspec(dllimport)
|
||||
# endif
|
||||
#else
|
||||
# define PHP_BZ2_API
|
||||
#endif
|
||||
|
||||
PHP_BZ2_API PHPAPI php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
|
||||
PHP_BZ2_API PHPAPI php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC);
|
||||
|
||||
#define php_stream_bz2open_from_BZFILE(bz, mode, innerstream) _php_stream_bz2open_from_BZFILE((bz), (mode), (innerstream) STREAMS_CC TSRMLS_CC)
|
||||
#define php_stream_bz2open(wrapper, path, mode, options, opened_path) _php_stream_bz2open((wrapper), (path), (mode), (options), (opened_path), NULL STREAMS_CC TSRMLS_CC)
|
||||
|
||||
php_stream_ops php_stream_bz2io_ops;
|
||||
#define PHP_STREAM_IS_BZIP2 &php_stream_bz2io_ops
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
*/
|
||||
@@ -1,25 +0,0 @@
|
||||
--TEST--
|
||||
BZ2 with files
|
||||
--SKIPIF--
|
||||
<?php if (!extension_loaded("bz2")) print "skip"; ?>
|
||||
--POST--
|
||||
--GET--
|
||||
--FILE--
|
||||
<?php // $Id$
|
||||
|
||||
error_reporting(E_ALL);
|
||||
|
||||
$filename = "testfile.bz2";
|
||||
$str = "This is a test string.\n";
|
||||
$bz = bzopen($filename, "w");
|
||||
bzwrite($bz, $str);
|
||||
bzclose($bz);
|
||||
|
||||
$bz = bzopen($filename, "r");
|
||||
print bzread($bz, 10);
|
||||
print bzread($bz);
|
||||
bzclose($bz);
|
||||
unlink($filename);
|
||||
|
||||
--EXPECT--
|
||||
This is a test string.
|
||||
@@ -1,29 +0,0 @@
|
||||
--TEST--
|
||||
BZ2 with strings
|
||||
--SKIPIF--
|
||||
<?php if (!extension_loaded("bz2")) print "skip"; ?>
|
||||
--POST--
|
||||
--GET--
|
||||
--FILE--
|
||||
<?php // $Id$
|
||||
|
||||
error_reporting(E_ALL);
|
||||
|
||||
# This FAILS
|
||||
$blaat = <<<HEREDOC
|
||||
This is some random data
|
||||
HEREDOC;
|
||||
|
||||
# This Works: (so, is heredoc related)
|
||||
#$blaat= 'This is some random data';
|
||||
|
||||
$blaat2 = bzdecompress(bzcompress($blaat));
|
||||
|
||||
$tests = <<<TESTS
|
||||
\$blaat === \$blaat2
|
||||
TESTS;
|
||||
|
||||
include('tests/quicktester.inc');
|
||||
|
||||
--EXPECT--
|
||||
OK
|
||||
@@ -1,2 +0,0 @@
|
||||
sqlite
|
||||
Wez Furlong, Tal Peer, Marcus Börger
|
||||
@@ -1,29 +0,0 @@
|
||||
|
||||
# sqlite has some generated source files; this specifies how to
|
||||
# build them
|
||||
|
||||
# We avoid building these last three by bundling the generated versions
|
||||
# in our release packages
|
||||
|
||||
$(srcdir)/libsqlite/src/opcodes.c: $(srcdir)/libsqlite/src/opcodes.h $(srcdir)/libsqlite/src/sqlite.h
|
||||
echo '/* Automatically generated file. Do not edit */' >$@
|
||||
echo 'char *sqliteOpcodeNames[] = { "???", ' >>$@
|
||||
grep '^case OP_' $(srcdir)/libsqlite/src/vdbe.c | \
|
||||
sed -e 's/^.*OP_/ "/' -e 's/:.*$$/", /' >>$@
|
||||
echo '};' >>$@
|
||||
|
||||
$(srcdir)/libsqlite/src/opcodes.h: $(srcdir)/libsqlite/src/vdbe.c
|
||||
echo '/* Automatically generated file. Do not edit */' >$@
|
||||
grep '^case OP_' $(srcdir)/libsqlite/src/vdbe.c | \
|
||||
sed -e 's/://' | \
|
||||
awk '{printf "#define %-30s %3d\n", $$2, ++cnt}' >>$@
|
||||
|
||||
$(srcdir)/libsqlite/src/sqlite.h: $(srcdir)/libsqlite/src/sqlite.h.in
|
||||
sed -e s/--VERS--/$(SQLITE_VERSION)/ -e s/--ENCODING--/$(SQLITE_ENCODING)/ $(srcdir)/libsqlite/src/sqlite.h.in >$(srcdir)/libsqlite/src/sqlite.h
|
||||
|
||||
$(srcdir)/sqlite.c: $(srcdir)/libsqlite/src/sqlite.h
|
||||
|
||||
$(srcdir)/libsqlite/src/parse.c: $(srcdir)/libsqlite/src/parse.y
|
||||
$(LEMON) $(srcdir)/libsqlite/src/parse.y
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
This is an extension for the SQLite Embeddable SQL Database Engine.
|
||||
http://www.sqlite.org/
|
||||
|
||||
SQLite is a C library that implements an embeddable SQL database engine.
|
||||
Programs that link with the SQLite library can have SQL database access
|
||||
without running a separate RDBMS process.
|
||||
|
||||
SQLite is not a client library used to connect to a big database server.
|
||||
SQLite is the server. The SQLite library reads and writes directly to and from
|
||||
the database files on disk
|
||||
|
||||
|
||||
|
||||
vim:tw=78:et
|
||||
@@ -1,22 +0,0 @@
|
||||
- Transparent binary encoding of return values from PHP callback functions.
|
||||
|
||||
- Add user-space callback for the authorizer function (this is potentially
|
||||
very slow, so it needs to be implemented carefully).
|
||||
|
||||
- Add user-space callback to handle busy databases.
|
||||
|
||||
- Test-suite
|
||||
|
||||
o Test how robust we are when a user-space function is registered as
|
||||
a callback for a persistent connection in script A, then script B is
|
||||
called that doesn't register the callback but does make use of the
|
||||
function in an SQL query.
|
||||
--> Our test suite doesn't allow us to test persistent connections
|
||||
at this time :/
|
||||
|
||||
- If building a ZTS build, -DTHREADSAFE while compiling libsqlite
|
||||
|
||||
- If building a non-debug build, -DNDEBUG will disable the expensive
|
||||
asserts in the inner loop. (and double performance)
|
||||
|
||||
vim:tw=78
|
||||
@@ -1,106 +0,0 @@
|
||||
dnl $Id$
|
||||
dnl config.m4 for extension sqlite
|
||||
dnl vim:et:ts=2:sw=2
|
||||
|
||||
PHP_ARG_WITH(sqlite, for sqlite support,
|
||||
[ --with-sqlite Include sqlite support])
|
||||
|
||||
if test "$PHP_SQLITE" != "no"; then
|
||||
|
||||
if test "$PHP_SQLITE" != "yes"; then
|
||||
SEARCH_PATH="/usr/local /usr"
|
||||
SEARCH_FOR="/include/sqlite.h"
|
||||
if test -r $PHP_SQLITE/; then # path given as parameter
|
||||
SQLITE_DIR=$PHP_SQLITE
|
||||
else # search default path list
|
||||
AC_MSG_CHECKING([for sqlite files in default path])
|
||||
for i in $SEARCH_PATH ; do
|
||||
if test -r $i/$SEARCH_FOR; then
|
||||
SQLITE_DIR=$i
|
||||
AC_MSG_RESULT(found in $i)
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
if test -z "$SQLITE_DIR"; then
|
||||
AC_MSG_RESULT([not found])
|
||||
AC_MSG_ERROR([Please reinstall the sqlite distribution from http://www.sqlite.org])
|
||||
fi
|
||||
|
||||
PHP_ADD_INCLUDE($SQLITE_DIR/include)
|
||||
|
||||
LIBNAME=sqlite
|
||||
LIBSYMBOL=sqlite_open
|
||||
|
||||
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
|
||||
[
|
||||
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $SQLITE_DIR/lib, SQLITE_SHARED_LIBADD)
|
||||
AC_DEFINE(HAVE_SQLITELIB,1,[ ])
|
||||
],[
|
||||
AC_MSG_ERROR([wrong sqlite lib version or lib not found])
|
||||
],[
|
||||
-L$SQLITE_DIR/lib -lm -ldl
|
||||
])
|
||||
|
||||
PHP_SUBST(SQLITE_SHARED_LIBADD)
|
||||
PHP_NEW_EXTENSION(sqlite, sqlite.c libsqlite/src/encode.c, $ext_shared)
|
||||
else
|
||||
# use bundled library
|
||||
|
||||
PHP_SQLITE_CFLAGS="-I@ext_srcdir@/libsqlite/src"
|
||||
|
||||
sources="libsqlite/src/opcodes.c
|
||||
libsqlite/src/parse.c libsqlite/src/encode.c \
|
||||
libsqlite/src/auth.c libsqlite/src/btree.c libsqlite/src/build.c \
|
||||
libsqlite/src/delete.c libsqlite/src/expr.c libsqlite/src/func.c \
|
||||
libsqlite/src/hash.c libsqlite/src/insert.c libsqlite/src/main.c \
|
||||
libsqlite/src/os.c libsqlite/src/pager.c \
|
||||
libsqlite/src/printf.c libsqlite/src/random.c \
|
||||
libsqlite/src/select.c libsqlite/src/table.c libsqlite/src/tokenize.c \
|
||||
libsqlite/src/update.c libsqlite/src/util.c libsqlite/src/vdbe.c \
|
||||
libsqlite/src/attach.c libsqlite/src/btree_rb.c libsqlite/src/pragma.c \
|
||||
libsqlite/src/vacuum.c libsqlite/src/copy.c \
|
||||
libsqlite/src/where.c libsqlite/src/trigger.c"
|
||||
|
||||
PHP_NEW_EXTENSION(sqlite, sqlite.c $sources, $ext_shared,,$PHP_SQLITE_CFLAGS)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/libsqlite)
|
||||
PHP_ADD_BUILD_DIR($ext_builddir/libsqlite/src)
|
||||
AC_CHECK_SIZEOF(char *,4)
|
||||
AC_DEFINE(SQLITE_PTR_SZ, SIZEOF_CHAR_P, [Size of a pointer])
|
||||
AC_DEFINE(OS_UNIX, 1, [if this is unix])
|
||||
AC_DEFINE(OS_WIN, 0, [if this is windows])
|
||||
dnl use latin 1 for now; the utf-8 handling in funcs.c uses assert(),
|
||||
dnl which is a bit silly and something we want to avoid
|
||||
SQLITE_ENCODING="ISO8859"
|
||||
dnl SQLITE_ENCODING="UTF-8"
|
||||
dnl AC_DEFINE(SQLITE_UTF8,1,[if SQLite should use utf-8 encoding])
|
||||
PHP_SUBST(SQLITE_ENCODING)
|
||||
|
||||
AC_PATH_PROG(LEMON,lemon,no)
|
||||
PHP_SUBST(LEMON)
|
||||
|
||||
SQLITE_VERSION=`cat $ext_srcdir/libsqlite/VERSION`
|
||||
PHP_SUBST(SQLITE_VERSION)
|
||||
|
||||
if test "$ext_shared" = "no"; then
|
||||
echo '#include "php_config.h"' > $ext_srcdir/libsqlite/src/config.h
|
||||
else
|
||||
echo "#include \"$abs_builddir/config.h\"" > $ext_srcdir/libsqlite/src/config.h
|
||||
fi
|
||||
|
||||
cat >> $ext_srcdir/libsqlite/src/config.h <<EOF
|
||||
#if ZTS
|
||||
# define THREADSAFE 1
|
||||
#endif
|
||||
#if !ZEND_DEBUG
|
||||
# define NDEBUG
|
||||
#endif
|
||||
EOF
|
||||
|
||||
PHP_ADD_MAKEFILE_FRAGMENT
|
||||
|
||||
fi
|
||||
|
||||
AC_CHECK_FUNCS(usleep nanosleep)
|
||||
AC_CHECK_HEADERS(time.h)
|
||||
fi
|
||||
@@ -1,37 +0,0 @@
|
||||
This directory contains source code to
|
||||
|
||||
SQLite: An Embeddable SQL Database Engine
|
||||
|
||||
To compile the project, first create a directory in which to place
|
||||
the build products. It is recommended, but not required, that the
|
||||
build directory be separate from the source directory. Cd into the
|
||||
build directory and then from the build directory run the configure
|
||||
script found at the root of the source tree. Then run "make".
|
||||
|
||||
For example:
|
||||
|
||||
tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite"
|
||||
mkdir bld ;# Build will occur in a sibling directory
|
||||
cd bld ;# Change to the build directory
|
||||
../sqlite/configure ;# Run the configure script
|
||||
make ;# Run the makefile.
|
||||
|
||||
The configure script uses autoconf 2.50 and libtool. If the configure
|
||||
script does not work out for you, there is a generic makefile named
|
||||
"Makefile.linux-gcc" in the top directory of the source tree that you
|
||||
can copy and edit to suite your needs. Comments on the generic makefile
|
||||
show what changes are needed.
|
||||
|
||||
The linux binaries on the website are created using the generic makefile,
|
||||
not the configure script. The configure script is unmaintained. (You
|
||||
can volunteer to take over maintenance of the configure script, if you want!)
|
||||
The windows binaries on the website are created using MinGW32 configured
|
||||
as a cross-compiler running under Linux. For details, see the ./publish.sh
|
||||
script at the top-level of the source tree.
|
||||
|
||||
Contacts:
|
||||
|
||||
http://www.sqlite.org/
|
||||
http://www.hwaci.com/sw/sqlite/
|
||||
http://groups.yahoo.com/group/sqlite/
|
||||
drh@hwaci.com
|
||||
@@ -1 +0,0 @@
|
||||
2.8.3
|
||||
@@ -1,279 +0,0 @@
|
||||
/*
|
||||
** 2003 April 6
|
||||
**
|
||||
** 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 contains code used to implement the ATTACH and DETACH commands.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** This routine is called by the parser to process an ATTACH statement:
|
||||
**
|
||||
** ATTACH DATABASE filename AS dbname
|
||||
**
|
||||
** The pFilename and pDbname arguments are the tokens that define the
|
||||
** filename and dbname in the ATTACH statement.
|
||||
*/
|
||||
void sqliteAttach(Parse *pParse, Token *pFilename, Token *pDbname){
|
||||
Db *aNew;
|
||||
int rc, i;
|
||||
char *zFile, *zName;
|
||||
sqlite *db;
|
||||
|
||||
if( pParse->explain ) return;
|
||||
db = pParse->db;
|
||||
if( db->file_format<4 ){
|
||||
sqliteErrorMsg(pParse, "cannot attach auxiliary databases to an "
|
||||
"older format master database", 0);
|
||||
pParse->rc = SQLITE_ERROR;
|
||||
return;
|
||||
}
|
||||
if( db->nDb>=MAX_ATTACHED+2 ){
|
||||
sqliteErrorMsg(pParse, "too many attached databases - max %d",
|
||||
MAX_ATTACHED);
|
||||
pParse->rc = SQLITE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
zFile = 0;
|
||||
sqliteSetNString(&zFile, pFilename->z, pFilename->n, 0);
|
||||
if( zFile==0 ) return;
|
||||
sqliteDequote(zFile);
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
if( sqliteAuthCheck(pParse, SQLITE_ATTACH, zFile, 0, 0)!=SQLITE_OK ){
|
||||
sqliteFree(zFile);
|
||||
return;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_AUTHORIZATION */
|
||||
|
||||
zName = 0;
|
||||
sqliteSetNString(&zName, pDbname->z, pDbname->n, 0);
|
||||
if( zName==0 ) return;
|
||||
sqliteDequote(zName);
|
||||
for(i=0; i<db->nDb; i++){
|
||||
if( db->aDb[i].zName && sqliteStrICmp(db->aDb[i].zName, zName)==0 ){
|
||||
sqliteErrorMsg(pParse, "database %z is already in use", zName);
|
||||
pParse->rc = SQLITE_ERROR;
|
||||
sqliteFree(zFile);
|
||||
sqliteFree(zName);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if( db->aDb==db->aDbStatic ){
|
||||
aNew = sqliteMalloc( sizeof(db->aDb[0])*3 );
|
||||
if( aNew==0 ) return;
|
||||
memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
|
||||
}else{
|
||||
aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
|
||||
if( aNew==0 ) return;
|
||||
}
|
||||
db->aDb = aNew;
|
||||
aNew = &db->aDb[db->nDb++];
|
||||
memset(aNew, 0, sizeof(*aNew));
|
||||
sqliteHashInit(&aNew->tblHash, SQLITE_HASH_STRING, 0);
|
||||
sqliteHashInit(&aNew->idxHash, SQLITE_HASH_STRING, 0);
|
||||
sqliteHashInit(&aNew->trigHash, SQLITE_HASH_STRING, 0);
|
||||
sqliteHashInit(&aNew->aFKey, SQLITE_HASH_STRING, 1);
|
||||
aNew->zName = zName;
|
||||
rc = sqliteBtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt);
|
||||
if( rc ){
|
||||
sqliteErrorMsg(pParse, "unable to open database: %s", zFile);
|
||||
}
|
||||
sqliteFree(zFile);
|
||||
db->flags &= ~SQLITE_Initialized;
|
||||
if( pParse->nErr ) return;
|
||||
rc = sqliteInit(pParse->db, &pParse->zErrMsg);
|
||||
if( rc ){
|
||||
sqliteResetInternalSchema(db, 0);
|
||||
pParse->nErr++;
|
||||
pParse->rc = SQLITE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine is called by the parser to process a DETACH statement:
|
||||
**
|
||||
** DETACH DATABASE dbname
|
||||
**
|
||||
** The pDbname argument is the name of the database in the DETACH statement.
|
||||
*/
|
||||
void sqliteDetach(Parse *pParse, Token *pDbname){
|
||||
int i;
|
||||
sqlite *db;
|
||||
|
||||
if( pParse->explain ) return;
|
||||
db = pParse->db;
|
||||
for(i=0; i<db->nDb; i++){
|
||||
if( db->aDb[i].pBt==0 || db->aDb[i].zName==0 ) continue;
|
||||
if( strlen(db->aDb[i].zName)!=pDbname->n ) continue;
|
||||
if( sqliteStrNICmp(db->aDb[i].zName, pDbname->z, pDbname->n)==0 ) break;
|
||||
}
|
||||
if( i>=db->nDb ){
|
||||
sqliteErrorMsg(pParse, "no such database: %T", pDbname);
|
||||
return;
|
||||
}
|
||||
if( i<2 ){
|
||||
sqliteErrorMsg(pParse, "cannot detach database %T", pDbname);
|
||||
return;
|
||||
}
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
if( sqliteAuthCheck(pParse,SQLITE_DETACH,db->aDb[i].zName,0,0)!=SQLITE_OK ){
|
||||
return;
|
||||
}
|
||||
#endif /* SQLITE_OMIT_AUTHORIZATION */
|
||||
sqliteBtreeClose(db->aDb[i].pBt);
|
||||
db->aDb[i].pBt = 0;
|
||||
sqliteFree(db->aDb[i].zName);
|
||||
sqliteResetInternalSchema(db, i);
|
||||
db->nDb--;
|
||||
if( i<db->nDb ){
|
||||
db->aDb[i] = db->aDb[db->nDb];
|
||||
memset(&db->aDb[db->nDb], 0, sizeof(db->aDb[0]));
|
||||
sqliteResetInternalSchema(db, i);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Initialize a DbFixer structure. This routine must be called prior
|
||||
** to passing the structure to one of the sqliteFixAAAA() routines below.
|
||||
**
|
||||
** The return value indicates whether or not fixation is required. TRUE
|
||||
** means we do need to fix the database references, FALSE means we do not.
|
||||
*/
|
||||
int sqliteFixInit(
|
||||
DbFixer *pFix, /* The fixer to be initialized */
|
||||
Parse *pParse, /* Error messages will be written here */
|
||||
int iDb, /* This is the database that must must be used */
|
||||
const char *zType, /* "view", "trigger", or "index" */
|
||||
const Token *pName /* Name of the view, trigger, or index */
|
||||
){
|
||||
sqlite *db;
|
||||
|
||||
if( iDb<0 || iDb==1 ) return 0;
|
||||
db = pParse->db;
|
||||
assert( db->nDb>iDb );
|
||||
pFix->pParse = pParse;
|
||||
pFix->zDb = db->aDb[iDb].zName;
|
||||
pFix->zType = zType;
|
||||
pFix->pName = pName;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** The following set of routines walk through the parse tree and assign
|
||||
** a specific database to all table references where the database name
|
||||
** was left unspecified in the original SQL statement. The pFix structure
|
||||
** must have been initialized by a prior call to sqliteFixInit().
|
||||
**
|
||||
** These routines are used to make sure that an index, trigger, or
|
||||
** view in one database does not refer to objects in a different database.
|
||||
** (Exception: indices, triggers, and views in the TEMP database are
|
||||
** allowed to refer to anything.) If a reference is explicitly made
|
||||
** to an object in a different database, an error message is added to
|
||||
** pParse->zErrMsg and these routines return non-zero. If everything
|
||||
** checks out, these routines return 0.
|
||||
*/
|
||||
int sqliteFixSrcList(
|
||||
DbFixer *pFix, /* Context of the fixation */
|
||||
SrcList *pList /* The Source list to check and modify */
|
||||
){
|
||||
int i;
|
||||
const char *zDb;
|
||||
|
||||
if( pList==0 ) return 0;
|
||||
zDb = pFix->zDb;
|
||||
for(i=0; i<pList->nSrc; i++){
|
||||
if( pList->a[i].zDatabase==0 ){
|
||||
pList->a[i].zDatabase = sqliteStrDup(zDb);
|
||||
}else if( sqliteStrICmp(pList->a[i].zDatabase,zDb)!=0 ){
|
||||
sqliteErrorMsg(pFix->pParse,
|
||||
"%s %z cannot reference objects in database %s",
|
||||
pFix->zType, sqliteStrNDup(pFix->pName->z, pFix->pName->n),
|
||||
pList->a[i].zDatabase);
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixSelect(pFix, pList->a[i].pSelect) ) return 1;
|
||||
if( sqliteFixExpr(pFix, pList->a[i].pOn) ) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int sqliteFixSelect(
|
||||
DbFixer *pFix, /* Context of the fixation */
|
||||
Select *pSelect /* The SELECT statement to be fixed to one database */
|
||||
){
|
||||
while( pSelect ){
|
||||
if( sqliteFixExprList(pFix, pSelect->pEList) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixSrcList(pFix, pSelect->pSrc) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixExpr(pFix, pSelect->pWhere) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixExpr(pFix, pSelect->pHaving) ){
|
||||
return 1;
|
||||
}
|
||||
pSelect = pSelect->pPrior;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int sqliteFixExpr(
|
||||
DbFixer *pFix, /* Context of the fixation */
|
||||
Expr *pExpr /* The expression to be fixed to one database */
|
||||
){
|
||||
while( pExpr ){
|
||||
if( sqliteFixSelect(pFix, pExpr->pSelect) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixExprList(pFix, pExpr->pList) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixExpr(pFix, pExpr->pRight) ){
|
||||
return 1;
|
||||
}
|
||||
pExpr = pExpr->pLeft;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int sqliteFixExprList(
|
||||
DbFixer *pFix, /* Context of the fixation */
|
||||
ExprList *pList /* The expression to be fixed to one database */
|
||||
){
|
||||
int i;
|
||||
if( pList==0 ) return 0;
|
||||
for(i=0; i<pList->nExpr; i++){
|
||||
if( sqliteFixExpr(pFix, pList->a[i].pExpr) ){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int sqliteFixTriggerStep(
|
||||
DbFixer *pFix, /* Context of the fixation */
|
||||
TriggerStep *pStep /* The trigger step be fixed to one database */
|
||||
){
|
||||
while( pStep ){
|
||||
if( sqliteFixSelect(pFix, pStep->pSelect) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixExpr(pFix, pStep->pWhere) ){
|
||||
return 1;
|
||||
}
|
||||
if( sqliteFixExprList(pFix, pStep->pExprList) ){
|
||||
return 1;
|
||||
}
|
||||
pStep = pStep->pNext;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,225 +0,0 @@
|
||||
/*
|
||||
** 2003 January 11
|
||||
**
|
||||
** 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 contains code used to implement the sqlite_set_authorizer()
|
||||
** API. This facility is an optional feature of the library. Embedded
|
||||
** systems that do not need this facility may omit it by recompiling
|
||||
** the library with -DSQLITE_OMIT_AUTHORIZATION=1
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** All of the code in this file may be omitted by defining a single
|
||||
** macro.
|
||||
*/
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
|
||||
/*
|
||||
** Set or clear the access authorization function.
|
||||
**
|
||||
** The access authorization function is be called during the compilation
|
||||
** phase to verify that the user has read and/or write access permission on
|
||||
** various fields of the database. The first argument to the auth function
|
||||
** is a copy of the 3rd argument to this routine. The second argument
|
||||
** to the auth function is one of these constants:
|
||||
**
|
||||
** SQLITE_COPY
|
||||
** SQLITE_CREATE_INDEX
|
||||
** SQLITE_CREATE_TABLE
|
||||
** SQLITE_CREATE_TEMP_INDEX
|
||||
** SQLITE_CREATE_TEMP_TABLE
|
||||
** SQLITE_CREATE_TEMP_TRIGGER
|
||||
** SQLITE_CREATE_TEMP_VIEW
|
||||
** SQLITE_CREATE_TRIGGER
|
||||
** SQLITE_CREATE_VIEW
|
||||
** SQLITE_DELETE
|
||||
** SQLITE_DROP_INDEX
|
||||
** SQLITE_DROP_TABLE
|
||||
** SQLITE_DROP_TEMP_INDEX
|
||||
** SQLITE_DROP_TEMP_TABLE
|
||||
** SQLITE_DROP_TEMP_TRIGGER
|
||||
** SQLITE_DROP_TEMP_VIEW
|
||||
** SQLITE_DROP_TRIGGER
|
||||
** SQLITE_DROP_VIEW
|
||||
** SQLITE_INSERT
|
||||
** SQLITE_PRAGMA
|
||||
** SQLITE_READ
|
||||
** SQLITE_SELECT
|
||||
** SQLITE_TRANSACTION
|
||||
** SQLITE_UPDATE
|
||||
**
|
||||
** The third and fourth arguments to the auth function are the name of
|
||||
** the table and the column that are being accessed. The auth function
|
||||
** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
|
||||
** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
|
||||
** means that the SQL statement will never-run - the sqlite_exec() call
|
||||
** will return with an error. SQLITE_IGNORE means that the SQL statement
|
||||
** should run but attempts to read the specified column will return NULL
|
||||
** and attempts to write the column will be ignored.
|
||||
**
|
||||
** Setting the auth function to NULL disables this hook. The default
|
||||
** setting of the auth function is NULL.
|
||||
*/
|
||||
int sqlite_set_authorizer(
|
||||
sqlite *db,
|
||||
int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
|
||||
void *pArg
|
||||
){
|
||||
db->xAuth = xAuth;
|
||||
db->pAuthArg = pArg;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Write an error message into pParse->zErrMsg that explains that the
|
||||
** user-supplied authorization function returned an illegal value.
|
||||
*/
|
||||
static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
|
||||
char zBuf[20];
|
||||
sprintf(zBuf, "(%d)", rc);
|
||||
sqliteSetString(&pParse->zErrMsg, "illegal return value ", zBuf,
|
||||
" from the authorization function - should be SQLITE_OK, "
|
||||
"SQLITE_IGNORE, or SQLITE_DENY", 0);
|
||||
pParse->nErr++;
|
||||
pParse->rc = SQLITE_MISUSE;
|
||||
}
|
||||
|
||||
/*
|
||||
** The pExpr should be a TK_COLUMN expression. The table referred to
|
||||
** is in pTabList or else it is the NEW or OLD table of a trigger.
|
||||
** Check to see if it is OK to read this particular column.
|
||||
**
|
||||
** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
|
||||
** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
|
||||
** then generate an error.
|
||||
*/
|
||||
void sqliteAuthRead(
|
||||
Parse *pParse, /* The parser context */
|
||||
Expr *pExpr, /* The expression to check authorization on */
|
||||
SrcList *pTabList /* All table that pExpr might refer to */
|
||||
){
|
||||
sqlite *db = pParse->db;
|
||||
int rc;
|
||||
Table *pTab; /* The table being read */
|
||||
const char *zCol; /* Name of the column of the table */
|
||||
int iSrc; /* Index in pTabList->a[] of table being read */
|
||||
const char *zDBase; /* Name of database being accessed */
|
||||
|
||||
if( db->xAuth==0 ) return;
|
||||
assert( pExpr->op==TK_COLUMN );
|
||||
for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
|
||||
if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
|
||||
}
|
||||
if( iSrc>=0 && iSrc<pTabList->nSrc ){
|
||||
pTab = pTabList->a[iSrc].pTab;
|
||||
}else{
|
||||
/* This must be an attempt to read the NEW or OLD pseudo-tables
|
||||
** of a trigger.
|
||||
*/
|
||||
TriggerStack *pStack; /* The stack of current triggers */
|
||||
pStack = pParse->trigStack;
|
||||
assert( pStack!=0 );
|
||||
assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
|
||||
pTab = pStack->pTab;
|
||||
}
|
||||
if( pTab==0 ) return;
|
||||
if( pExpr->iColumn>=0 ){
|
||||
assert( pExpr->iColumn<pTab->nCol );
|
||||
zCol = pTab->aCol[pExpr->iColumn].zName;
|
||||
}else if( pTab->iPKey>=0 ){
|
||||
assert( pTab->iPKey<pTab->nCol );
|
||||
zCol = pTab->aCol[pTab->iPKey].zName;
|
||||
}else{
|
||||
zCol = "ROWID";
|
||||
}
|
||||
assert( pExpr->iDb<db->nDb );
|
||||
zDBase = db->aDb[pExpr->iDb].zName;
|
||||
rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase,
|
||||
pParse->zAuthContext);
|
||||
if( rc==SQLITE_IGNORE ){
|
||||
pExpr->op = TK_NULL;
|
||||
}else if( rc==SQLITE_DENY ){
|
||||
if( db->nDb>2 || pExpr->iDb!=0 ){
|
||||
sqliteSetString(&pParse->zErrMsg,"access to ", zDBase, ".",
|
||||
pTab->zName, ".", zCol, " is prohibited", 0);
|
||||
}else{
|
||||
sqliteSetString(&pParse->zErrMsg,"access to ", pTab->zName, ".",
|
||||
zCol, " is prohibited", 0);
|
||||
}
|
||||
pParse->nErr++;
|
||||
pParse->rc = SQLITE_AUTH;
|
||||
}else if( rc!=SQLITE_OK ){
|
||||
sqliteAuthBadReturnCode(pParse, rc);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Do an authorization check using the code and arguments given. Return
|
||||
** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
|
||||
** is returned, then the error count and error message in pParse are
|
||||
** modified appropriately.
|
||||
*/
|
||||
int sqliteAuthCheck(
|
||||
Parse *pParse,
|
||||
int code,
|
||||
const char *zArg1,
|
||||
const char *zArg2,
|
||||
const char *zArg3
|
||||
){
|
||||
sqlite *db = pParse->db;
|
||||
int rc;
|
||||
|
||||
if( db->xAuth==0 ){
|
||||
return SQLITE_OK;
|
||||
}
|
||||
rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
|
||||
if( rc==SQLITE_DENY ){
|
||||
sqliteSetString(&pParse->zErrMsg, "not authorized", 0);
|
||||
pParse->rc = SQLITE_AUTH;
|
||||
pParse->nErr++;
|
||||
}else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
|
||||
rc = SQLITE_DENY;
|
||||
sqliteAuthBadReturnCode(pParse, rc);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an authorization context. After this routine is called, the
|
||||
** zArg3 argument to authorization callbacks will be zContext until
|
||||
** popped. Or if pParse==0, this routine is a no-op.
|
||||
*/
|
||||
void sqliteAuthContextPush(
|
||||
Parse *pParse,
|
||||
AuthContext *pContext,
|
||||
const char *zContext
|
||||
){
|
||||
pContext->pParse = pParse;
|
||||
if( pParse ){
|
||||
pContext->zAuthContext = pParse->zAuthContext;
|
||||
pParse->zAuthContext = zContext;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Pop an authorization context that was previously pushed
|
||||
** by sqliteAuthContextPush
|
||||
*/
|
||||
void sqliteAuthContextPop(AuthContext *pContext){
|
||||
if( pContext->pParse ){
|
||||
pContext->pParse->zAuthContext = pContext->zAuthContext;
|
||||
pContext->pParse = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* SQLITE_OMIT_AUTHORIZATION */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
** 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 header file defines the interface that the sqlite B-Tree file
|
||||
** subsystem. See comments in the source code for a detailed description
|
||||
** of what each interface routine does.
|
||||
**
|
||||
** @(#) $Id$
|
||||
*/
|
||||
#ifndef _BTREE_H_
|
||||
#define _BTREE_H_
|
||||
|
||||
/*
|
||||
** Forward declarations of structure
|
||||
*/
|
||||
typedef struct Btree Btree;
|
||||
typedef struct BtCursor BtCursor;
|
||||
typedef struct BtOps BtOps;
|
||||
typedef struct BtCursorOps BtCursorOps;
|
||||
|
||||
|
||||
/*
|
||||
** An instance of the following structure contains pointers to all
|
||||
** methods against an open BTree. Alternative BTree implementations
|
||||
** (examples: file based versus in-memory) can be created by substituting
|
||||
** different methods. Users of the BTree cannot tell the difference.
|
||||
**
|
||||
** In C++ we could do this by defining a virtual base class and then
|
||||
** creating subclasses for each different implementation. But this is
|
||||
** C not C++ so we have to be a little more explicit.
|
||||
*/
|
||||
struct BtOps {
|
||||
int (*Close)(Btree*);
|
||||
int (*SetCacheSize)(Btree*, int);
|
||||
int (*SetSafetyLevel)(Btree*, int);
|
||||
int (*BeginTrans)(Btree*);
|
||||
int (*Commit)(Btree*);
|
||||
int (*Rollback)(Btree*);
|
||||
int (*BeginCkpt)(Btree*);
|
||||
int (*CommitCkpt)(Btree*);
|
||||
int (*RollbackCkpt)(Btree*);
|
||||
int (*CreateTable)(Btree*, int*);
|
||||
int (*CreateIndex)(Btree*, int*);
|
||||
int (*DropTable)(Btree*, int);
|
||||
int (*ClearTable)(Btree*, int);
|
||||
int (*Cursor)(Btree*, int iTable, int wrFlag, BtCursor **ppCur);
|
||||
int (*GetMeta)(Btree*, int*);
|
||||
int (*UpdateMeta)(Btree*, int*);
|
||||
char *(*IntegrityCheck)(Btree*, int*, int);
|
||||
const char *(*GetFilename)(Btree*);
|
||||
int (*Copyfile)(Btree*,Btree*);
|
||||
#ifdef SQLITE_TEST
|
||||
int (*PageDump)(Btree*, int, int);
|
||||
struct Pager *(*Pager)(Btree*);
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
** An instance of this structure defines all of the methods that can
|
||||
** be executed against a cursor.
|
||||
*/
|
||||
struct BtCursorOps {
|
||||
int (*Moveto)(BtCursor*, const void *pKey, int nKey, int *pRes);
|
||||
int (*Delete)(BtCursor*);
|
||||
int (*Insert)(BtCursor*, const void *pKey, int nKey,
|
||||
const void *pData, int nData);
|
||||
int (*First)(BtCursor*, int *pRes);
|
||||
int (*Last)(BtCursor*, int *pRes);
|
||||
int (*Next)(BtCursor*, int *pRes);
|
||||
int (*Previous)(BtCursor*, int *pRes);
|
||||
int (*KeySize)(BtCursor*, int *pSize);
|
||||
int (*Key)(BtCursor*, int offset, int amt, char *zBuf);
|
||||
int (*KeyCompare)(BtCursor*, const void *pKey, int nKey,
|
||||
int nIgnore, int *pRes);
|
||||
int (*DataSize)(BtCursor*, int *pSize);
|
||||
int (*Data)(BtCursor*, int offset, int amt, char *zBuf);
|
||||
int (*CloseCursor)(BtCursor*);
|
||||
#ifdef SQLITE_TEST
|
||||
int (*CursorDump)(BtCursor*, int*);
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
** The number of 4-byte "meta" values contained on the first page of each
|
||||
** database file.
|
||||
*/
|
||||
#define SQLITE_N_BTREE_META 10
|
||||
|
||||
int sqliteBtreeOpen(const char *zFilename, int mode, int nPg, Btree **ppBtree);
|
||||
int sqliteRbtreeOpen(const char *zFilename, int mode, int nPg, Btree **ppBtree);
|
||||
|
||||
#define btOps(pBt) (*((BtOps **)(pBt)))
|
||||
#define btCOps(pCur) (*((BtCursorOps **)(pCur)))
|
||||
|
||||
#define sqliteBtreeClose(pBt) (btOps(pBt)->Close(pBt))
|
||||
#define sqliteBtreeSetCacheSize(pBt, sz) (btOps(pBt)->SetCacheSize(pBt, sz))
|
||||
#define sqliteBtreeSetSafetyLevel(pBt, sl) (btOps(pBt)->SetSafetyLevel(pBt, sl))
|
||||
#define sqliteBtreeBeginTrans(pBt) (btOps(pBt)->BeginTrans(pBt))
|
||||
#define sqliteBtreeCommit(pBt) (btOps(pBt)->Commit(pBt))
|
||||
#define sqliteBtreeRollback(pBt) (btOps(pBt)->Rollback(pBt))
|
||||
#define sqliteBtreeBeginCkpt(pBt) (btOps(pBt)->BeginCkpt(pBt))
|
||||
#define sqliteBtreeCommitCkpt(pBt) (btOps(pBt)->CommitCkpt(pBt))
|
||||
#define sqliteBtreeRollbackCkpt(pBt) (btOps(pBt)->RollbackCkpt(pBt))
|
||||
#define sqliteBtreeCreateTable(pBt,piTable)\
|
||||
(btOps(pBt)->CreateTable(pBt,piTable))
|
||||
#define sqliteBtreeCreateIndex(pBt, piIndex)\
|
||||
(btOps(pBt)->CreateIndex(pBt, piIndex))
|
||||
#define sqliteBtreeDropTable(pBt, iTable) (btOps(pBt)->DropTable(pBt, iTable))
|
||||
#define sqliteBtreeClearTable(pBt, iTable)\
|
||||
(btOps(pBt)->ClearTable(pBt, iTable))
|
||||
#define sqliteBtreeCursor(pBt, iTable, wrFlag, ppCur)\
|
||||
(btOps(pBt)->Cursor(pBt, iTable, wrFlag, ppCur))
|
||||
#define sqliteBtreeMoveto(pCur, pKey, nKey, pRes)\
|
||||
(btCOps(pCur)->Moveto(pCur, pKey, nKey, pRes))
|
||||
#define sqliteBtreeDelete(pCur) (btCOps(pCur)->Delete(pCur))
|
||||
#define sqliteBtreeInsert(pCur, pKey, nKey, pData, nData) \
|
||||
(btCOps(pCur)->Insert(pCur, pKey, nKey, pData, nData))
|
||||
#define sqliteBtreeFirst(pCur, pRes) (btCOps(pCur)->First(pCur, pRes))
|
||||
#define sqliteBtreeLast(pCur, pRes) (btCOps(pCur)->Last(pCur, pRes))
|
||||
#define sqliteBtreeNext(pCur, pRes) (btCOps(pCur)->Next(pCur, pRes))
|
||||
#define sqliteBtreePrevious(pCur, pRes) (btCOps(pCur)->Previous(pCur, pRes))
|
||||
#define sqliteBtreeKeySize(pCur, pSize) (btCOps(pCur)->KeySize(pCur, pSize) )
|
||||
#define sqliteBtreeKey(pCur, offset, amt, zBuf)\
|
||||
(btCOps(pCur)->Key(pCur, offset, amt, zBuf))
|
||||
#define sqliteBtreeKeyCompare(pCur, pKey, nKey, nIgnore, pRes)\
|
||||
(btCOps(pCur)->KeyCompare(pCur, pKey, nKey, nIgnore, pRes))
|
||||
#define sqliteBtreeDataSize(pCur, pSize) (btCOps(pCur)->DataSize(pCur, pSize))
|
||||
#define sqliteBtreeData(pCur, offset, amt, zBuf)\
|
||||
(btCOps(pCur)->Data(pCur, offset, amt, zBuf))
|
||||
#define sqliteBtreeCloseCursor(pCur) (btCOps(pCur)->CloseCursor(pCur))
|
||||
#define sqliteBtreeGetMeta(pBt, aMeta) (btOps(pBt)->GetMeta(pBt, aMeta))
|
||||
#define sqliteBtreeUpdateMeta(pBt, aMeta) (btOps(pBt)->UpdateMeta(pBt, aMeta))
|
||||
#define sqliteBtreeIntegrityCheck(pBt, aRoot, nRoot)\
|
||||
(btOps(pBt)->IntegrityCheck(pBt, aRoot, nRoot))
|
||||
#define sqliteBtreeGetFilename(pBt) (btOps(pBt)->GetFilename(pBt))
|
||||
#define sqliteBtreeCopyFile(pBt1, pBt2) (btOps(pBt1)->Copyfile(pBt1, pBt2))
|
||||
|
||||
#ifdef SQLITE_TEST
|
||||
#define sqliteBtreePageDump(pBt, pgno, recursive)\
|
||||
(btOps(pBt)->PageDump(pBt, pgno, recursive))
|
||||
#define sqliteBtreeCursorDump(pCur, aResult)\
|
||||
(btCOps(pCur)->CursorDump(pCur, aResult))
|
||||
#define sqliteBtreePager(pBt) (btOps(pBt)->Pager(pBt))
|
||||
int btree_native_byte_order;
|
||||
#endif /* SQLITE_TEST */
|
||||
|
||||
|
||||
#endif /* _BTREE_H_ */
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
** 2003 April 6
|
||||
**
|
||||
** 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 contains code used to implement the COPY command.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** The COPY command is for compatibility with PostgreSQL and specificially
|
||||
** for the ability to read the output of pg_dump. The format is as
|
||||
** follows:
|
||||
**
|
||||
** COPY table FROM file [USING DELIMITERS string]
|
||||
**
|
||||
** "table" is an existing table name. We will read lines of code from
|
||||
** file to fill this table with data. File might be "stdin". The optional
|
||||
** delimiter string identifies the field separators. The default is a tab.
|
||||
*/
|
||||
void sqliteCopy(
|
||||
Parse *pParse, /* The parser context */
|
||||
SrcList *pTableName, /* The name of the table into which we will insert */
|
||||
Token *pFilename, /* The file from which to obtain information */
|
||||
Token *pDelimiter, /* Use this as the field delimiter */
|
||||
int onError /* What to do if a constraint fails */
|
||||
){
|
||||
Table *pTab;
|
||||
int i;
|
||||
Vdbe *v;
|
||||
int addr, end;
|
||||
Index *pIdx;
|
||||
char *zFile = 0;
|
||||
const char *zDb;
|
||||
sqlite *db = pParse->db;
|
||||
|
||||
|
||||
if( sqlite_malloc_failed ) goto copy_cleanup;
|
||||
assert( pTableName->nSrc==1 );
|
||||
pTab = sqliteSrcListLookup(pParse, pTableName);
|
||||
if( pTab==0 || sqliteIsReadOnly(pParse, pTab, 0) ) goto copy_cleanup;
|
||||
zFile = sqliteStrNDup(pFilename->z, pFilename->n);
|
||||
sqliteDequote(zFile);
|
||||
assert( pTab->iDb<db->nDb );
|
||||
zDb = db->aDb[pTab->iDb].zName;
|
||||
if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb)
|
||||
|| sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile, zDb) ){
|
||||
goto copy_cleanup;
|
||||
}
|
||||
v = sqliteGetVdbe(pParse);
|
||||
if( v ){
|
||||
sqliteBeginWriteOperation(pParse, 1, pTab->iDb);
|
||||
addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
|
||||
sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
|
||||
sqliteVdbeDequoteP3(v, addr);
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
|
||||
for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
|
||||
assert( pIdx->iDb==1 || pIdx->iDb==pTab->iDb );
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
|
||||
}
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
|
||||
}
|
||||
end = sqliteVdbeMakeLabel(v);
|
||||
addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
|
||||
if( pDelimiter ){
|
||||
sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
|
||||
sqliteVdbeDequoteP3(v, addr);
|
||||
}else{
|
||||
sqliteVdbeChangeP3(v, addr, "\t", 1);
|
||||
}
|
||||
if( pTab->iPKey>=0 ){
|
||||
sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
|
||||
sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
|
||||
}
|
||||
for(i=0; i<pTab->nCol; i++){
|
||||
if( i==pTab->iPKey ){
|
||||
/* The integer primary key column is filled with NULL since its
|
||||
** value is always pulled from the record number */
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
|
||||
}
|
||||
}
|
||||
sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, pTab->iPKey>=0,
|
||||
0, onError, addr);
|
||||
sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0, -1);
|
||||
if( (db->flags & SQLITE_CountRows)!=0 ){
|
||||
sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, addr);
|
||||
sqliteVdbeResolveLabel(v, end);
|
||||
sqliteVdbeAddOp(v, OP_Noop, 0, 0);
|
||||
sqliteEndWriteOperation(pParse);
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
copy_cleanup:
|
||||
sqliteSrcListDelete(pTableName);
|
||||
sqliteFree(zFile);
|
||||
return;
|
||||
}
|
||||
@@ -1,396 +0,0 @@
|
||||
/*
|
||||
** 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 contains C code routines that are called by the parser
|
||||
** to handle DELETE FROM statements.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** Look up every table that is named in pSrc. If any table is not found,
|
||||
** add an error message to pParse->zErrMsg and return NULL. If all tables
|
||||
** are found, return a pointer to the last table.
|
||||
*/
|
||||
Table *sqliteSrcListLookup(Parse *pParse, SrcList *pSrc){
|
||||
Table *pTab = 0;
|
||||
int i;
|
||||
for(i=0; i<pSrc->nSrc; i++){
|
||||
const char *zTab = pSrc->a[i].zName;
|
||||
const char *zDb = pSrc->a[i].zDatabase;
|
||||
pTab = sqliteLocateTable(pParse, zTab, zDb);
|
||||
pSrc->a[i].pTab = pTab;
|
||||
}
|
||||
return pTab;
|
||||
}
|
||||
|
||||
/*
|
||||
** Check to make sure the given table is writable. If it is not
|
||||
** writable, generate an error message and return 1. If it is
|
||||
** writable return 0;
|
||||
*/
|
||||
int sqliteIsReadOnly(Parse *pParse, Table *pTab, int viewOk){
|
||||
if( pTab->readOnly ){
|
||||
sqliteErrorMsg(pParse, "table %s may not be modified", pTab->zName);
|
||||
return 1;
|
||||
}
|
||||
if( !viewOk && pTab->pSelect ){
|
||||
sqliteErrorMsg(pParse, "cannot modify %s because it is a view",pTab->zName);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Process a DELETE FROM statement.
|
||||
*/
|
||||
void sqliteDeleteFrom(
|
||||
Parse *pParse, /* The parser context */
|
||||
SrcList *pTabList, /* The table from which we should delete things */
|
||||
Expr *pWhere /* The WHERE clause. May be null */
|
||||
){
|
||||
Vdbe *v; /* The virtual database engine */
|
||||
Table *pTab; /* The table from which records will be deleted */
|
||||
const char *zDb; /* Name of database holding pTab */
|
||||
int end, addr; /* A couple addresses of generated code */
|
||||
int i; /* Loop counter */
|
||||
WhereInfo *pWInfo; /* Information about the WHERE clause */
|
||||
Index *pIdx; /* For looping over indices of the table */
|
||||
int iCur; /* VDBE Cursor number for pTab */
|
||||
sqlite *db; /* Main database structure */
|
||||
int isView; /* True if attempting to delete from a view */
|
||||
AuthContext sContext; /* Authorization context */
|
||||
|
||||
int row_triggers_exist = 0; /* True if any triggers exist */
|
||||
int before_triggers; /* True if there are BEFORE triggers */
|
||||
int after_triggers; /* True if there are AFTER triggers */
|
||||
int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */
|
||||
|
||||
sContext.pParse = 0;
|
||||
if( pParse->nErr || sqlite_malloc_failed ){
|
||||
pTabList = 0;
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
db = pParse->db;
|
||||
assert( pTabList->nSrc==1 );
|
||||
|
||||
/* Locate the table which we want to delete. This table has to be
|
||||
** put in an SrcList structure because some of the subroutines we
|
||||
** will be calling are designed to work with multiple tables and expect
|
||||
** an SrcList* parameter instead of just a Table* parameter.
|
||||
*/
|
||||
pTab = sqliteSrcListLookup(pParse, pTabList);
|
||||
if( pTab==0 ) goto delete_from_cleanup;
|
||||
before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
|
||||
TK_DELETE, TK_BEFORE, TK_ROW, 0);
|
||||
after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
|
||||
TK_DELETE, TK_AFTER, TK_ROW, 0);
|
||||
row_triggers_exist = before_triggers || after_triggers;
|
||||
isView = pTab->pSelect!=0;
|
||||
if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
assert( pTab->iDb<db->nDb );
|
||||
zDb = db->aDb[pTab->iDb].zName;
|
||||
if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
|
||||
/* If pTab is really a view, make sure it has been initialized.
|
||||
*/
|
||||
if( isView && sqliteViewGetColumnNames(pParse, pTab) ){
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
|
||||
/* Allocate a cursor used to store the old.* data for a trigger.
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
oldIdx = pParse->nTab++;
|
||||
}
|
||||
|
||||
/* Resolve the column names in all the expressions.
|
||||
*/
|
||||
assert( pTabList->nSrc==1 );
|
||||
iCur = pTabList->a[0].iCursor = pParse->nTab++;
|
||||
if( pWhere ){
|
||||
if( sqliteExprResolveIds(pParse, pTabList, 0, pWhere) ){
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start the view context
|
||||
*/
|
||||
if( isView ){
|
||||
sqliteAuthContextPush(pParse, &sContext, pTab->zName);
|
||||
}
|
||||
|
||||
/* Begin generating code.
|
||||
*/
|
||||
v = sqliteGetVdbe(pParse);
|
||||
if( v==0 ){
|
||||
goto delete_from_cleanup;
|
||||
}
|
||||
sqliteBeginWriteOperation(pParse, row_triggers_exist, pTab->iDb);
|
||||
|
||||
/* If we are trying to delete from a view, construct that view into
|
||||
** a temporary table.
|
||||
*/
|
||||
if( isView ){
|
||||
Select *pView = sqliteSelectDup(pTab->pSelect);
|
||||
sqliteSelect(pParse, pView, SRT_TempTable, iCur, 0, 0, 0);
|
||||
sqliteSelectDelete(pView);
|
||||
}
|
||||
|
||||
/* Initialize the counter of the number of rows deleted, if
|
||||
** we are counting rows.
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
|
||||
}
|
||||
|
||||
/* Special case: A DELETE without a WHERE clause deletes everything.
|
||||
** It is easier just to erase the whole table. Note, however, that
|
||||
** this means that the row change count will be incorrect.
|
||||
*/
|
||||
if( pWhere==0 && !row_triggers_exist ){
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
/* If counting rows deleted, just count the total number of
|
||||
** entries in the table. */
|
||||
int endOfLoop = sqliteVdbeMakeLabel(v);
|
||||
int addr;
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Rewind, iCur, sqliteVdbeCurrentAddr(v)+2);
|
||||
addr = sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_Next, iCur, addr);
|
||||
sqliteVdbeResolveLabel(v, endOfLoop);
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
}
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Clear, pTab->tnum, pTab->iDb);
|
||||
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
|
||||
sqliteVdbeAddOp(v, OP_Clear, pIdx->tnum, pIdx->iDb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The usual case: There is a WHERE clause so we have to scan through
|
||||
** the table an pick which records to delete.
|
||||
*/
|
||||
else{
|
||||
/* Begin the database scan
|
||||
*/
|
||||
pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1, 0);
|
||||
if( pWInfo==0 ) goto delete_from_cleanup;
|
||||
|
||||
/* Remember the key of every item to be deleted.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
|
||||
}
|
||||
|
||||
/* End the database scan loop.
|
||||
*/
|
||||
sqliteWhereEnd(pWInfo);
|
||||
|
||||
/* Open the pseudo-table used to store OLD if there are triggers.
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
|
||||
}
|
||||
|
||||
/* Delete every item whose key was written to the list during the
|
||||
** database scan. We have to delete items after the scan is complete
|
||||
** because deleting an item can change the scan order.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
|
||||
end = sqliteVdbeMakeLabel(v);
|
||||
|
||||
/* This is the beginning of the delete loop when there are
|
||||
** row triggers.
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
|
||||
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
|
||||
|
||||
sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
|
||||
sqliteVdbeAddOp(v, OP_RowData, iCur, 0);
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
}
|
||||
|
||||
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_BEFORE, pTab, -1,
|
||||
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
|
||||
addr);
|
||||
}
|
||||
|
||||
if( !isView ){
|
||||
/* Open cursors for the table we are deleting from and all its
|
||||
** indices. If there are row triggers, this happens inside the
|
||||
** OP_ListRead loop because the cursor have to all be closed
|
||||
** before the trigger fires. If there are no row triggers, the
|
||||
** cursors are opened only once on the outside the loop.
|
||||
*/
|
||||
pParse->nTab = iCur + 1;
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum);
|
||||
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, pParse->nTab++, pIdx->tnum);
|
||||
}
|
||||
|
||||
/* This is the beginning of the delete loop when there are no
|
||||
** row triggers */
|
||||
if( !row_triggers_exist ){
|
||||
addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
|
||||
}
|
||||
|
||||
/* Delete the row */
|
||||
sqliteGenerateRowDelete(db, v, pTab, iCur, pParse->trigStack==0);
|
||||
}
|
||||
|
||||
/* If there are row triggers, close all cursors then invoke
|
||||
** the AFTER triggers
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
if( !isView ){
|
||||
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
}
|
||||
sqliteCodeRowTrigger(pParse, TK_DELETE, 0, TK_AFTER, pTab, -1,
|
||||
oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
|
||||
addr);
|
||||
}
|
||||
|
||||
/* End of the delete loop */
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, addr);
|
||||
sqliteVdbeResolveLabel(v, end);
|
||||
sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
|
||||
|
||||
/* Close the cursors after the loop if there are no row triggers */
|
||||
if( !row_triggers_exist ){
|
||||
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
pParse->nTab = iCur;
|
||||
}
|
||||
}
|
||||
sqliteEndWriteOperation(pParse);
|
||||
|
||||
/*
|
||||
** Return the number of rows that were deleted.
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, "rows deleted", P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 1, 0);
|
||||
}
|
||||
|
||||
delete_from_cleanup:
|
||||
sqliteAuthContextPop(&sContext);
|
||||
sqliteSrcListDelete(pTabList);
|
||||
sqliteExprDelete(pWhere);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine generates VDBE code that causes a single row of a
|
||||
** single table to be deleted.
|
||||
**
|
||||
** The VDBE must be in a particular state when this routine is called.
|
||||
** These are the requirements:
|
||||
**
|
||||
** 1. A read/write cursor pointing to pTab, the table containing the row
|
||||
** to be deleted, must be opened as cursor number "base".
|
||||
**
|
||||
** 2. Read/write cursors for all indices of pTab must be open as
|
||||
** cursor number base+i for the i-th index.
|
||||
**
|
||||
** 3. The record number of the row to be deleted must be on the top
|
||||
** of the stack.
|
||||
**
|
||||
** This routine pops the top of the stack to remove the record number
|
||||
** and then generates code to remove both the table record and all index
|
||||
** entries that point to that record.
|
||||
*/
|
||||
void sqliteGenerateRowDelete(
|
||||
sqlite *db, /* The database containing the index */
|
||||
Vdbe *v, /* Generate code into this VDBE */
|
||||
Table *pTab, /* Table containing the row to be deleted */
|
||||
int iCur, /* Cursor number for the table */
|
||||
int count /* Increment the row change counter */
|
||||
){
|
||||
int addr;
|
||||
addr = sqliteVdbeAddOp(v, OP_NotExists, iCur, 0);
|
||||
sqliteGenerateRowIndexDelete(db, v, pTab, iCur, 0);
|
||||
sqliteVdbeAddOp(v, OP_Delete, iCur, count);
|
||||
sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine generates VDBE code that causes the deletion of all
|
||||
** index entries associated with a single row of a single table.
|
||||
**
|
||||
** The VDBE must be in a particular state when this routine is called.
|
||||
** These are the requirements:
|
||||
**
|
||||
** 1. A read/write cursor pointing to pTab, the table containing the row
|
||||
** to be deleted, must be opened as cursor number "iCur".
|
||||
**
|
||||
** 2. Read/write cursors for all indices of pTab must be open as
|
||||
** cursor number iCur+i for the i-th index.
|
||||
**
|
||||
** 3. The "iCur" cursor must be pointing to the row that is to be
|
||||
** deleted.
|
||||
*/
|
||||
void sqliteGenerateRowIndexDelete(
|
||||
sqlite *db, /* The database containing the index */
|
||||
Vdbe *v, /* Generate code into this VDBE */
|
||||
Table *pTab, /* Table containing the row to be deleted */
|
||||
int iCur, /* Cursor number for the table */
|
||||
char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
|
||||
){
|
||||
int i;
|
||||
Index *pIdx;
|
||||
|
||||
for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
|
||||
int j;
|
||||
if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
|
||||
sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
|
||||
for(j=0; j<pIdx->nColumn; j++){
|
||||
int idx = pIdx->aiColumn[j];
|
||||
if( idx==pTab->iPKey ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, j, 0);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_Column, iCur, idx);
|
||||
}
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
|
||||
if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
|
||||
sqliteVdbeAddOp(v, OP_IdxDelete, iCur+i, 0);
|
||||
}
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
/*
|
||||
** 2002 April 25
|
||||
**
|
||||
** 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 contains helper routines used to translate binary data into
|
||||
** a null-terminated string (suitable for use in SQLite) and back again.
|
||||
** These are convenience routines for use by people who want to store binary
|
||||
** data in an SQLite database. The code in this file is not used by any other
|
||||
** part of the SQLite library.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
** How This Encoder Works
|
||||
**
|
||||
** The output is allowed to contain any character except 0x27 (') and
|
||||
** 0x00. This is accomplished by using an escape character to encode
|
||||
** 0x27 and 0x00 as a two-byte sequence. The escape character is always
|
||||
** 0x01. An 0x00 is encoded as the two byte sequence 0x01 0x01. The
|
||||
** 0x27 character is encoded as the two byte sequence 0x01 0x03. Finally,
|
||||
** the escape character itself is encoded as the two-character sequence
|
||||
** 0x01 0x02.
|
||||
**
|
||||
** To summarize, the encoder works by using an escape sequences as follows:
|
||||
**
|
||||
** 0x00 -> 0x01 0x01
|
||||
** 0x01 -> 0x01 0x02
|
||||
** 0x27 -> 0x01 0x03
|
||||
**
|
||||
** If that were all the encoder did, it would work, but in certain cases
|
||||
** it could double the size of the encoded string. For example, to
|
||||
** encode a string of 100 0x27 character would require 100 instances of
|
||||
** the 0x01 0x03 escape sequence resulting in a 200-character output.
|
||||
** We would prefer to keep the size of the encoded string smaller than
|
||||
** this.
|
||||
**
|
||||
** To minimize the encoding size, we first add a fixed offset value to each
|
||||
** byte in the sequence. The addition is module 256. (That is to say, if
|
||||
** the sum of the original character value and the offset exceeds 256, then
|
||||
** the higher order bits are truncated.) The offset is chosen to minimize
|
||||
** the number of characters in the string that need to be escaped. For
|
||||
** example, in the case above where the string was composed of 100 0x27
|
||||
** characters, the offset might be 0x01. Each of the 0x27 characters would
|
||||
** then be converted into an 0x28 character which would not need to be
|
||||
** escaped at all and so the 100 character input string would be converted
|
||||
** into just 100 characters of output. Actually 101 characters of output -
|
||||
** we have to record the offset used as the first byte in the sequence so
|
||||
** that the string can be decoded. Since the offset value is stored as
|
||||
** part of the output string and the output string is not allowed to contain
|
||||
** characters 0x00 or 0x27, the offset cannot be 0x00 or 0x27.
|
||||
**
|
||||
** Here, then, are the encoding steps:
|
||||
**
|
||||
** (1) Choose an offset value and make it the first character of
|
||||
** output.
|
||||
**
|
||||
** (2) Copy each input character into the output buffer, one by
|
||||
** one, adding the offset value as you copy.
|
||||
**
|
||||
** (3) If the value of an input character plus offset is 0x00, replace
|
||||
** that one character by the two-character sequence 0x01 0x01.
|
||||
** If the sum is 0x01, replace it with 0x01 0x02. If the sum
|
||||
** is 0x27, replace it with 0x01 0x03.
|
||||
**
|
||||
** (4) Put a 0x00 terminator at the end of the output.
|
||||
**
|
||||
** Decoding is obvious:
|
||||
**
|
||||
** (5) Copy encoded characters except the first into the decode
|
||||
** buffer. Set the first encoded character aside for use as
|
||||
** the offset in step 7 below.
|
||||
**
|
||||
** (6) Convert each 0x01 0x01 sequence into a single character 0x00.
|
||||
** Convert 0x01 0x02 into 0x01. Convert 0x01 0x03 into 0x27.
|
||||
**
|
||||
** (7) Subtract the offset value that was the first character of
|
||||
** the encoded buffer from all characters in the output buffer.
|
||||
**
|
||||
** The only tricky part is step (1) - how to compute an offset value to
|
||||
** minimize the size of the output buffer. This is accomplished to testing
|
||||
** all offset values and picking the one that results in the fewest number
|
||||
** of escapes. To do that, we first scan the entire input and count the
|
||||
** number of occurances of each character value in the input. Suppose
|
||||
** the number of 0x00 characters is N(0), the number of occurances of 0x01
|
||||
** is N(1), and so forth up to the number of occurances of 0xff is N(256).
|
||||
** An offset of 0 is not allowed so we don't have to test it. The number
|
||||
** of escapes required for an offset of 1 is N(1)+N(2)+N(40). The number
|
||||
** of escapes required for an offset of 2 is N(2)+N(3)+N(41). And so forth.
|
||||
** In this way we find the offset that gives the minimum number of escapes,
|
||||
** and thus minimizes the length of the output string.
|
||||
*/
|
||||
|
||||
/*
|
||||
** Encode a binary buffer "in" of size n bytes so that it contains
|
||||
** no instances of characters '\'' or '\000'. The output is
|
||||
** null-terminated and can be used as a string value in an INSERT
|
||||
** or UPDATE statement. Use sqlite_decode_binary() to convert the
|
||||
** string back into its original binary.
|
||||
**
|
||||
** The result is written into a preallocated output buffer "out".
|
||||
** "out" must be able to hold at least (256*n + 1262)/253 bytes.
|
||||
** In other words, the output will be expanded by as much as 3
|
||||
** bytes for every 253 bytes of input plus 2 bytes of fixed overhead.
|
||||
** (This is approximately 2 + 1.019*n or about a 2% size increase.)
|
||||
**
|
||||
** The return value is the number of characters in the encoded
|
||||
** string, excluding the "\000" terminator.
|
||||
*/
|
||||
int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out){
|
||||
int i, j, e, m;
|
||||
int cnt[256];
|
||||
if( n<=0 ){
|
||||
out[0] = 'x';
|
||||
out[1] = 0;
|
||||
return 1;
|
||||
}
|
||||
memset(cnt, 0, sizeof(cnt));
|
||||
for(i=n-1; i>=0; i--){ cnt[in[i]]++; }
|
||||
m = n;
|
||||
for(i=1; i<256; i++){
|
||||
int sum;
|
||||
if( i=='\'' ) continue;
|
||||
sum = cnt[i] + cnt[(i+1)&0xff] + cnt[(i+'\'')&0xff];
|
||||
if( sum<m ){
|
||||
m = sum;
|
||||
e = i;
|
||||
if( m==0 ) break;
|
||||
}
|
||||
}
|
||||
out[0] = e;
|
||||
j = 1;
|
||||
for(i=0; i<n; i++){
|
||||
int c = (in[i] - e)&0xff;
|
||||
if( c==0 ){
|
||||
out[j++] = 1;
|
||||
out[j++] = 1;
|
||||
}else if( c==1 ){
|
||||
out[j++] = 1;
|
||||
out[j++] = 2;
|
||||
}else if( c=='\'' ){
|
||||
out[j++] = 1;
|
||||
out[j++] = 3;
|
||||
}else{
|
||||
out[j++] = c;
|
||||
}
|
||||
}
|
||||
out[j] = 0;
|
||||
return j;
|
||||
}
|
||||
|
||||
/*
|
||||
** Decode the string "in" into binary data and write it into "out".
|
||||
** This routine reverses the encoded created by sqlite_encode_binary().
|
||||
** The output will always be a few bytes less than the input. The number
|
||||
** of bytes of output is returned. If the input is not a well-formed
|
||||
** encoding, -1 is returned.
|
||||
**
|
||||
** The "in" and "out" parameters may point to the same buffer in order
|
||||
** to decode a string in place.
|
||||
*/
|
||||
int sqlite_decode_binary(const unsigned char *in, unsigned char *out){
|
||||
int i, c, e;
|
||||
e = *(in++);
|
||||
i = 0;
|
||||
while( (c = *(in++))!=0 ){
|
||||
if( c==1 ){
|
||||
c = *(in++);
|
||||
if( c==1 ){
|
||||
c = 0;
|
||||
}else if( c==2 ){
|
||||
c = 1;
|
||||
}else if( c==3 ){
|
||||
c = '\'';
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
out[i++] = (c + e)&0xff;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
#ifdef ENCODER_TEST
|
||||
/*
|
||||
** The subroutines above are not tested by the usual test suite. To test
|
||||
** these routines, compile just this one file with a -DENCODER_TEST=1 option
|
||||
** and run the result.
|
||||
*/
|
||||
int main(int argc, char **argv){
|
||||
int i, j, n, m, nOut;
|
||||
unsigned char in[30000];
|
||||
unsigned char out[33000];
|
||||
|
||||
for(i=0; i<sizeof(in); i++){
|
||||
printf("Test %d: ", i+1);
|
||||
n = rand() % (i+1);
|
||||
if( i%100==0 ){
|
||||
int k;
|
||||
for(j=k=0; j<n; j++){
|
||||
/* if( k==0 || k=='\'' ) k++; */
|
||||
in[j] = k;
|
||||
k = (k+1)&0xff;
|
||||
}
|
||||
}else{
|
||||
for(j=0; j<n; j++) in[j] = rand() & 0xff;
|
||||
}
|
||||
nOut = sqlite_encode_binary(in, n, out);
|
||||
if( nOut!=strlen(out) ){
|
||||
printf(" ERROR return value is %d instead of %d\n", nOut, strlen(out));
|
||||
exit(1);
|
||||
}
|
||||
m = (256*n + 1262)/253;
|
||||
printf("size %d->%d (max %d)", n, strlen(out)+1, m);
|
||||
if( strlen(out)+1>m ){
|
||||
printf(" ERROR output too big\n");
|
||||
exit(1);
|
||||
}
|
||||
for(j=0; out[j]; j++){
|
||||
if( out[j]=='\'' ){
|
||||
printf(" ERROR contains (')\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
j = sqlite_decode_binary(out, out);
|
||||
if( j!=n ){
|
||||
printf(" ERROR decode size %d\n", j);
|
||||
exit(1);
|
||||
}
|
||||
if( memcmp(in, out, n)!=0 ){
|
||||
printf(" ERROR decode mismatch\n");
|
||||
exit(1);
|
||||
}
|
||||
printf(" OK\n");
|
||||
}
|
||||
}
|
||||
#endif /* ENCODER_TEST */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,573 +0,0 @@
|
||||
/*
|
||||
** 2002 February 23
|
||||
**
|
||||
** 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 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$
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** Implementation of the non-aggregate min() and max() functions
|
||||
*/
|
||||
static void minFunc(sqlite_func *context, int argc, const char **argv){
|
||||
const char *zBest;
|
||||
int i;
|
||||
|
||||
if( argc==0 ) return;
|
||||
zBest = argv[0];
|
||||
if( zBest==0 ) return;
|
||||
for(i=1; i<argc; i++){
|
||||
if( argv[i]==0 ) return;
|
||||
if( sqliteCompare(argv[i], zBest)<0 ){
|
||||
zBest = argv[i];
|
||||
}
|
||||
}
|
||||
sqlite_set_result_string(context, zBest, -1);
|
||||
}
|
||||
static void maxFunc(sqlite_func *context, int argc, const char **argv){
|
||||
const char *zBest;
|
||||
int i;
|
||||
|
||||
if( argc==0 ) return;
|
||||
zBest = argv[0];
|
||||
if( zBest==0 ) return;
|
||||
for(i=1; i<argc; i++){
|
||||
if( argv[i]==0 ) return;
|
||||
if( sqliteCompare(argv[i], zBest)>0 ){
|
||||
zBest = argv[i];
|
||||
}
|
||||
}
|
||||
sqlite_set_result_string(context, zBest, -1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the length() function
|
||||
*/
|
||||
static void lengthFunc(sqlite_func *context, int argc, const char **argv){
|
||||
const char *z;
|
||||
int len;
|
||||
|
||||
assert( argc==1 );
|
||||
z = argv[0];
|
||||
if( z==0 ) return;
|
||||
#ifdef SQLITE_UTF8
|
||||
for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
|
||||
#else
|
||||
len = strlen(z);
|
||||
#endif
|
||||
sqlite_set_result_int(context, len);
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the abs() function
|
||||
*/
|
||||
static void absFunc(sqlite_func *context, int argc, const char **argv){
|
||||
const char *z;
|
||||
assert( argc==1 );
|
||||
z = argv[0];
|
||||
if( z==0 ) return;
|
||||
if( z[0]=='-' && isdigit(z[1]) ) z++;
|
||||
sqlite_set_result_string(context, z, -1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the substr() function
|
||||
*/
|
||||
static void substrFunc(sqlite_func *context, int argc, const char **argv){
|
||||
const char *z;
|
||||
#ifdef SQLITE_UTF8
|
||||
const char *z2;
|
||||
int i;
|
||||
#endif
|
||||
int p1, p2, len;
|
||||
assert( argc==3 );
|
||||
z = argv[0];
|
||||
if( z==0 ) return;
|
||||
p1 = atoi(argv[1]?argv[1]:0);
|
||||
p2 = atoi(argv[2]?argv[2]:0);
|
||||
#ifdef SQLITE_UTF8
|
||||
for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
|
||||
#else
|
||||
len = strlen(z);
|
||||
#endif
|
||||
if( p1<0 ){
|
||||
p1 += len;
|
||||
if( p1<0 ){
|
||||
p2 += p1;
|
||||
p1 = 0;
|
||||
}
|
||||
}else if( p1>0 ){
|
||||
p1--;
|
||||
}
|
||||
if( p1+p2>len ){
|
||||
p2 = len-p1;
|
||||
}
|
||||
#ifdef SQLITE_UTF8
|
||||
for(i=0; i<p1; i++){
|
||||
assert( z[i] );
|
||||
if( (z[i]&0xc0)==0x80 ) p1++;
|
||||
}
|
||||
while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
|
||||
for(; i<p1+p2; i++){
|
||||
assert( z[i] );
|
||||
if( (z[i]&0xc0)==0x80 ) p2++;
|
||||
}
|
||||
while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
|
||||
#endif
|
||||
if( p2<0 ) p2 = 0;
|
||||
sqlite_set_result_string(context, &z[p1], p2);
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the round() function
|
||||
*/
|
||||
static void roundFunc(sqlite_func *context, int argc, const char **argv){
|
||||
int n;
|
||||
double r;
|
||||
char zBuf[100];
|
||||
assert( argc==1 || argc==2 );
|
||||
if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
|
||||
n = argc==2 ? atoi(argv[1]) : 0;
|
||||
if( n>30 ) n = 30;
|
||||
if( n<0 ) n = 0;
|
||||
r = atof(argv[0]);
|
||||
sprintf(zBuf,"%.*f",n,r);
|
||||
sqlite_set_result_string(context, zBuf, -1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the upper() and lower() SQL functions.
|
||||
*/
|
||||
static void upperFunc(sqlite_func *context, int argc, const char **argv){
|
||||
char *z;
|
||||
int i;
|
||||
if( argc<1 || argv[0]==0 ) return;
|
||||
z = sqlite_set_result_string(context, argv[0], -1);
|
||||
if( z==0 ) return;
|
||||
for(i=0; z[i]; i++){
|
||||
if( islower(z[i]) ) z[i] = toupper(z[i]);
|
||||
}
|
||||
}
|
||||
static void lowerFunc(sqlite_func *context, int argc, const char **argv){
|
||||
char *z;
|
||||
int i;
|
||||
if( argc<1 || argv[0]==0 ) return;
|
||||
z = sqlite_set_result_string(context, argv[0], -1);
|
||||
if( z==0 ) return;
|
||||
for(i=0; z[i]; i++){
|
||||
if( isupper(z[i]) ) z[i] = tolower(z[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
|
||||
** All three do the same thing. They return the first argument
|
||||
** non-NULL argument.
|
||||
*/
|
||||
static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
|
||||
int i;
|
||||
for(i=0; i<argc; i++){
|
||||
if( argv[i] ){
|
||||
sqlite_set_result_string(context, argv[i], -1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of random(). Return a random integer.
|
||||
*/
|
||||
static void randomFunc(sqlite_func *context, int argc, const char **argv){
|
||||
sqlite_set_result_int(context, sqliteRandomInteger());
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the last_insert_rowid() SQL function. The return
|
||||
** value is the same as the sqlite_last_insert_rowid() API function.
|
||||
*/
|
||||
static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
|
||||
sqlite *db = sqlite_user_data(context);
|
||||
sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the like() SQL function. This function implements
|
||||
** the build-in LIKE operator. The first argument to the function is the
|
||||
** string and the second argument is the pattern. So, the SQL statements:
|
||||
**
|
||||
** A LIKE B
|
||||
**
|
||||
** is implemented as like(A,B).
|
||||
*/
|
||||
static void likeFunc(sqlite_func *context, int arg, const char **argv){
|
||||
if( argv[0]==0 || argv[1]==0 ) return;
|
||||
sqlite_set_result_int(context, sqliteLikeCompare(argv[0], argv[1]));
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the glob() SQL function. This function implements
|
||||
** the build-in GLOB operator. The first argument to the function is the
|
||||
** string and the second argument is the pattern. So, the SQL statements:
|
||||
**
|
||||
** A GLOB B
|
||||
**
|
||||
** is implemented as glob(A,B).
|
||||
*/
|
||||
static void globFunc(sqlite_func *context, int arg, const char **argv){
|
||||
if( argv[0]==0 || argv[1]==0 ) return;
|
||||
sqlite_set_result_int(context, sqliteGlobCompare(argv[0], argv[1]));
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the NULLIF(x,y) function. The result is the first
|
||||
** argument if the arguments are different. The result is NULL if the
|
||||
** arguments are equal to each other.
|
||||
*/
|
||||
static void nullifFunc(sqlite_func *context, int argc, const char **argv){
|
||||
if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
|
||||
sqlite_set_result_string(context, argv[0], -1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Implementation of the VERSION(*) function. The result is the version
|
||||
** of the SQLite library that is running.
|
||||
*/
|
||||
static void versionFunc(sqlite_func *context, int argc, const char **argv){
|
||||
sqlite_set_result_string(context, sqlite_version, -1);
|
||||
}
|
||||
|
||||
#ifdef SQLITE_SOUNDEX
|
||||
/*
|
||||
** Compute the soundex encoding of a word.
|
||||
*/
|
||||
static void soundexFunc(sqlite_func *context, int argc, const char **argv){
|
||||
char zResult[8];
|
||||
const char *zIn;
|
||||
int i, j;
|
||||
static const unsigned char iCode[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
|
||||
1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
|
||||
0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
|
||||
1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
|
||||
};
|
||||
assert( argc==1 );
|
||||
zIn = argv[0];
|
||||
for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
|
||||
if( zIn[i] ){
|
||||
zResult[0] = toupper(zIn[i]);
|
||||
for(j=1; j<4 && zIn[i]; i++){
|
||||
int code = iCode[zIn[i]&0x7f];
|
||||
if( code>0 ){
|
||||
zResult[j++] = code + '0';
|
||||
}
|
||||
}
|
||||
while( j<4 ){
|
||||
zResult[j++] = '0';
|
||||
}
|
||||
zResult[j] = 0;
|
||||
sqlite_set_result_string(context, zResult, 4);
|
||||
}else{
|
||||
sqlite_set_result_string(context, zResult, "?000", 4);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SQLITE_TEST
|
||||
/*
|
||||
** This function generates a string of random characters. Used for
|
||||
** generating test data.
|
||||
*/
|
||||
static void randStr(sqlite_func *context, int argc, const char **argv){
|
||||
static const char zSrc[] =
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"0123456789"
|
||||
".-!,:*^+=_|?/<> ";
|
||||
int iMin, iMax, n, r, i;
|
||||
char zBuf[1000];
|
||||
if( argc>=1 ){
|
||||
iMin = atoi(argv[0]);
|
||||
if( iMin<0 ) iMin = 0;
|
||||
if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
|
||||
}else{
|
||||
iMin = 1;
|
||||
}
|
||||
if( argc>=2 ){
|
||||
iMax = atoi(argv[1]);
|
||||
if( iMax<iMin ) iMax = iMin;
|
||||
if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf);
|
||||
}else{
|
||||
iMax = 50;
|
||||
}
|
||||
n = iMin;
|
||||
if( iMax>iMin ){
|
||||
r = sqliteRandomInteger() & 0x7fffffff;
|
||||
n += r%(iMax + 1 - iMin);
|
||||
}
|
||||
r = 0;
|
||||
for(i=0; i<n; i++){
|
||||
r = (r + sqliteRandomByte())% (sizeof(zSrc)-1);
|
||||
zBuf[i] = zSrc[r];
|
||||
}
|
||||
zBuf[n] = 0;
|
||||
sqlite_set_result_string(context, zBuf, n);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
** An instance of the following structure holds the context of a
|
||||
** sum() or avg() aggregate computation.
|
||||
*/
|
||||
typedef struct SumCtx SumCtx;
|
||||
struct SumCtx {
|
||||
double sum; /* Sum of terms */
|
||||
int cnt; /* Number of elements summed */
|
||||
};
|
||||
|
||||
/*
|
||||
** Routines used to compute the sum or average.
|
||||
*/
|
||||
static void sumStep(sqlite_func *context, int argc, const char **argv){
|
||||
SumCtx *p;
|
||||
if( argc<1 ) return;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p && argv[0] ){
|
||||
p->sum += atof(argv[0]);
|
||||
p->cnt++;
|
||||
}
|
||||
}
|
||||
static void sumFinalize(sqlite_func *context){
|
||||
SumCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
sqlite_set_result_double(context, p ? p->sum : 0.0);
|
||||
}
|
||||
static void avgFinalize(sqlite_func *context){
|
||||
SumCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p && p->cnt>0 ){
|
||||
sqlite_set_result_double(context, p->sum/(double)p->cnt);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** An instance of the following structure holds the context of a
|
||||
** variance or standard deviation computation.
|
||||
*/
|
||||
typedef struct StdDevCtx StdDevCtx;
|
||||
struct StdDevCtx {
|
||||
double sum; /* Sum of terms */
|
||||
double sum2; /* Sum of the squares of terms */
|
||||
int cnt; /* Number of terms counted */
|
||||
};
|
||||
|
||||
#if 0 /* Omit because math library is required */
|
||||
/*
|
||||
** Routines used to compute the standard deviation as an aggregate.
|
||||
*/
|
||||
static void stdDevStep(sqlite_func *context, int argc, const char **argv){
|
||||
StdDevCtx *p;
|
||||
double x;
|
||||
if( argc<1 ) return;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p && argv[0] ){
|
||||
x = atof(argv[0]);
|
||||
p->sum += x;
|
||||
p->sum2 += x*x;
|
||||
p->cnt++;
|
||||
}
|
||||
}
|
||||
static void stdDevFinalize(sqlite_func *context){
|
||||
double rN = sqlite_aggregate_count(context);
|
||||
StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p && p->cnt>1 ){
|
||||
double rCnt = cnt;
|
||||
sqlite_set_result_double(context,
|
||||
sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
** The following structure keeps track of state information for the
|
||||
** count() aggregate function.
|
||||
*/
|
||||
typedef struct CountCtx CountCtx;
|
||||
struct CountCtx {
|
||||
int n;
|
||||
};
|
||||
|
||||
/*
|
||||
** Routines to implement the count() aggregate function.
|
||||
*/
|
||||
static void countStep(sqlite_func *context, int argc, const char **argv){
|
||||
CountCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( (argc==0 || argv[0]) && p ){
|
||||
p->n++;
|
||||
}
|
||||
}
|
||||
static void countFinalize(sqlite_func *context){
|
||||
CountCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
sqlite_set_result_int(context, p ? p->n : 0);
|
||||
}
|
||||
|
||||
/*
|
||||
** This function tracks state information for the min() and max()
|
||||
** aggregate functions.
|
||||
*/
|
||||
typedef struct MinMaxCtx MinMaxCtx;
|
||||
struct MinMaxCtx {
|
||||
char *z; /* The best so far */
|
||||
char zBuf[28]; /* Space that can be used for storage */
|
||||
};
|
||||
|
||||
/*
|
||||
** Routines to implement min() and max() aggregate functions.
|
||||
*/
|
||||
static void minStep(sqlite_func *context, int argc, const char **argv){
|
||||
MinMaxCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p==0 || argc<1 || argv[0]==0 ) return;
|
||||
if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){
|
||||
int len;
|
||||
if( p->z && p->z!=p->zBuf ){
|
||||
sqliteFree(p->z);
|
||||
}
|
||||
len = strlen(argv[0]);
|
||||
if( len < sizeof(p->zBuf) ){
|
||||
p->z = p->zBuf;
|
||||
}else{
|
||||
p->z = sqliteMalloc( len+1 );
|
||||
if( p->z==0 ) return;
|
||||
}
|
||||
strcpy(p->z, argv[0]);
|
||||
}
|
||||
}
|
||||
static void maxStep(sqlite_func *context, int argc, const char **argv){
|
||||
MinMaxCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p==0 || argc<1 || argv[0]==0 ) return;
|
||||
if( p->z==0 || sqliteCompare(argv[0],p->z)>0 ){
|
||||
int len;
|
||||
if( p->z && p->z!=p->zBuf ){
|
||||
sqliteFree(p->z);
|
||||
}
|
||||
len = strlen(argv[0]);
|
||||
if( len < sizeof(p->zBuf) ){
|
||||
p->z = p->zBuf;
|
||||
}else{
|
||||
p->z = sqliteMalloc( len+1 );
|
||||
if( p->z==0 ) return;
|
||||
}
|
||||
strcpy(p->z, argv[0]);
|
||||
}
|
||||
}
|
||||
static void minMaxFinalize(sqlite_func *context){
|
||||
MinMaxCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p && p->z ){
|
||||
sqlite_set_result_string(context, p->z, strlen(p->z));
|
||||
}
|
||||
if( p && p->z && p->z!=p->zBuf ){
|
||||
sqliteFree(p->z);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** This function registered all of the above C functions as SQL
|
||||
** functions. This should be the only routine in this file with
|
||||
** external linkage.
|
||||
*/
|
||||
void sqliteRegisterBuiltinFunctions(sqlite *db){
|
||||
static struct {
|
||||
char *zName;
|
||||
int nArg;
|
||||
int dataType;
|
||||
void (*xFunc)(sqlite_func*,int,const char**);
|
||||
} aFuncs[] = {
|
||||
{ "min", -1, SQLITE_ARGS, minFunc },
|
||||
{ "min", 0, 0, 0 },
|
||||
{ "max", -1, SQLITE_ARGS, maxFunc },
|
||||
{ "max", 0, 0, 0 },
|
||||
{ "length", 1, SQLITE_NUMERIC, lengthFunc },
|
||||
{ "substr", 3, SQLITE_TEXT, substrFunc },
|
||||
{ "abs", 1, SQLITE_NUMERIC, absFunc },
|
||||
{ "round", 1, SQLITE_NUMERIC, roundFunc },
|
||||
{ "round", 2, SQLITE_NUMERIC, roundFunc },
|
||||
{ "upper", 1, SQLITE_TEXT, upperFunc },
|
||||
{ "lower", 1, SQLITE_TEXT, lowerFunc },
|
||||
{ "coalesce", -1, SQLITE_ARGS, ifnullFunc },
|
||||
{ "coalesce", 0, 0, 0 },
|
||||
{ "coalesce", 1, 0, 0 },
|
||||
{ "ifnull", 2, SQLITE_ARGS, ifnullFunc },
|
||||
{ "random", -1, SQLITE_NUMERIC, randomFunc },
|
||||
{ "like", 2, SQLITE_NUMERIC, likeFunc },
|
||||
{ "glob", 2, SQLITE_NUMERIC, globFunc },
|
||||
{ "nullif", 2, SQLITE_ARGS, nullifFunc },
|
||||
{ "sqlite_version",0,SQLITE_TEXT, versionFunc},
|
||||
#ifdef SQLITE_SOUNDEX
|
||||
{ "soundex", 1, SQLITE_TEXT, soundexFunc},
|
||||
#endif
|
||||
#ifdef SQLITE_TEST
|
||||
{ "randstr", 2, SQLITE_TEXT, randStr },
|
||||
#endif
|
||||
};
|
||||
static struct {
|
||||
char *zName;
|
||||
int nArg;
|
||||
int dataType;
|
||||
void (*xStep)(sqlite_func*,int,const char**);
|
||||
void (*xFinalize)(sqlite_func*);
|
||||
} aAggs[] = {
|
||||
{ "min", 1, 0, minStep, minMaxFinalize },
|
||||
{ "max", 1, 0, maxStep, minMaxFinalize },
|
||||
{ "sum", 1, SQLITE_NUMERIC, sumStep, sumFinalize },
|
||||
{ "avg", 1, SQLITE_NUMERIC, sumStep, avgFinalize },
|
||||
{ "count", 0, SQLITE_NUMERIC, countStep, countFinalize },
|
||||
{ "count", 1, SQLITE_NUMERIC, countStep, countFinalize },
|
||||
#if 0
|
||||
{ "stddev", 1, SQLITE_NUMERIC, stdDevStep, stdDevFinalize },
|
||||
#endif
|
||||
};
|
||||
int i;
|
||||
|
||||
for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
|
||||
sqlite_create_function(db, aFuncs[i].zName,
|
||||
aFuncs[i].nArg, aFuncs[i].xFunc, 0);
|
||||
if( aFuncs[i].xFunc ){
|
||||
sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
|
||||
}
|
||||
}
|
||||
sqlite_create_function(db, "last_insert_rowid", 0,
|
||||
last_insert_rowid, db);
|
||||
sqlite_function_type(db, "last_insert_rowid", SQLITE_NUMERIC);
|
||||
for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
|
||||
sqlite_create_aggregate(db, aAggs[i].zName,
|
||||
aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
|
||||
sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
|
||||
}
|
||||
}
|
||||
@@ -1,354 +0,0 @@
|
||||
/*
|
||||
** 2001 September 22
|
||||
**
|
||||
** 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 is the implementation of generic hash-tables
|
||||
** used in SQLite.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include <assert.h>
|
||||
|
||||
/* Turn bulk memory into a hash table object by initializing the
|
||||
** fields of the Hash structure.
|
||||
**
|
||||
** "new" is a pointer to the hash table that is to be initialized.
|
||||
** keyClass is one of the constants SQLITE_HASH_INT, SQLITE_HASH_POINTER,
|
||||
** SQLITE_HASH_BINARY, or SQLITE_HASH_STRING. The value of keyClass
|
||||
** determines what kind of key the hash table will use. "copyKey" is
|
||||
** true if the hash table should make its own private copy of keys and
|
||||
** false if it should just use the supplied pointer. CopyKey only makes
|
||||
** sense for SQLITE_HASH_STRING and SQLITE_HASH_BINARY and is ignored
|
||||
** for other key classes.
|
||||
*/
|
||||
void sqliteHashInit(Hash *new, int keyClass, int copyKey){
|
||||
assert( new!=0 );
|
||||
assert( keyClass>=SQLITE_HASH_INT && keyClass<=SQLITE_HASH_BINARY );
|
||||
new->keyClass = keyClass;
|
||||
new->copyKey = copyKey &&
|
||||
(keyClass==SQLITE_HASH_STRING || keyClass==SQLITE_HASH_BINARY);
|
||||
new->first = 0;
|
||||
new->count = 0;
|
||||
new->htsize = 0;
|
||||
new->ht = 0;
|
||||
}
|
||||
|
||||
/* Remove all entries from a hash table. Reclaim all memory.
|
||||
** Call this routine to delete a hash table or to reset a hash table
|
||||
** to the empty state.
|
||||
*/
|
||||
void sqliteHashClear(Hash *pH){
|
||||
HashElem *elem; /* For looping over all elements of the table */
|
||||
|
||||
assert( pH!=0 );
|
||||
elem = pH->first;
|
||||
pH->first = 0;
|
||||
if( pH->ht ) sqliteFree(pH->ht);
|
||||
pH->ht = 0;
|
||||
pH->htsize = 0;
|
||||
while( elem ){
|
||||
HashElem *next_elem = elem->next;
|
||||
if( pH->copyKey && elem->pKey ){
|
||||
sqliteFree(elem->pKey);
|
||||
}
|
||||
sqliteFree(elem);
|
||||
elem = next_elem;
|
||||
}
|
||||
pH->count = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Hash and comparison functions when the mode is SQLITE_HASH_INT
|
||||
*/
|
||||
static int intHash(const void *pKey, int nKey){
|
||||
return nKey ^ (nKey<<8) ^ (nKey>>8);
|
||||
}
|
||||
static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
|
||||
return n2 - n1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Hash and comparison functions when the mode is SQLITE_HASH_POINTER
|
||||
*/
|
||||
static int ptrHash(const void *pKey, int nKey){
|
||||
uptr x = Addr(pKey);
|
||||
return x ^ (x<<8) ^ (x>>8);
|
||||
}
|
||||
static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
|
||||
if( pKey1==pKey2 ) return 0;
|
||||
if( pKey1<pKey2 ) return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Hash and comparison functions when the mode is SQLITE_HASH_STRING
|
||||
*/
|
||||
static int strHash(const void *pKey, int nKey){
|
||||
return sqliteHashNoCase((const char*)pKey, nKey);
|
||||
}
|
||||
static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
|
||||
if( n1!=n2 ) return n2-n1;
|
||||
return sqliteStrNICmp((const char*)pKey1,(const char*)pKey2,n1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Hash and comparison functions when the mode is SQLITE_HASH_BINARY
|
||||
*/
|
||||
static int binHash(const void *pKey, int nKey){
|
||||
int h = 0;
|
||||
const char *z = (const char *)pKey;
|
||||
while( nKey-- > 0 ){
|
||||
h = (h<<3) ^ h ^ *(z++);
|
||||
}
|
||||
return h & 0x7fffffff;
|
||||
}
|
||||
static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
|
||||
if( n1!=n2 ) return n2-n1;
|
||||
return memcmp(pKey1,pKey2,n1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Return a pointer to the appropriate hash function given the key class.
|
||||
**
|
||||
** The C syntax in this function definition may be unfamilar to some
|
||||
** programmers, so we provide the following additional explanation:
|
||||
**
|
||||
** The name of the function is "hashFunction". The function takes a
|
||||
** single parameter "keyClass". The return value of hashFunction()
|
||||
** is a pointer to another function. Specifically, the return value
|
||||
** of hashFunction() is a pointer to a function that takes two parameters
|
||||
** with types "const void*" and "int" and returns an "int".
|
||||
*/
|
||||
static int (*hashFunction(int keyClass))(const void*,int){
|
||||
switch( keyClass ){
|
||||
case SQLITE_HASH_INT: return &intHash;
|
||||
case SQLITE_HASH_POINTER: return &ptrHash;
|
||||
case SQLITE_HASH_STRING: return &strHash;
|
||||
case SQLITE_HASH_BINARY: return &binHash;;
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return a pointer to the appropriate hash function given the key class.
|
||||
**
|
||||
** For help in interpreted the obscure C code in the function definition,
|
||||
** see the header comment on the previous function.
|
||||
*/
|
||||
static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
|
||||
switch( keyClass ){
|
||||
case SQLITE_HASH_INT: return &intCompare;
|
||||
case SQLITE_HASH_POINTER: return &ptrCompare;
|
||||
case SQLITE_HASH_STRING: return &strCompare;
|
||||
case SQLITE_HASH_BINARY: return &binCompare;
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Resize the hash table so that it cantains "new_size" buckets.
|
||||
** "new_size" must be a power of 2. The hash table might fail
|
||||
** to resize if sqliteMalloc() fails.
|
||||
*/
|
||||
static void rehash(Hash *pH, int new_size){
|
||||
struct _ht *new_ht; /* The new hash table */
|
||||
HashElem *elem, *next_elem; /* For looping over existing elements */
|
||||
HashElem *x; /* Element being copied to new hash table */
|
||||
int (*xHash)(const void*,int); /* The hash function */
|
||||
|
||||
assert( (new_size & (new_size-1))==0 );
|
||||
new_ht = (struct _ht *)sqliteMalloc( new_size*sizeof(struct _ht) );
|
||||
if( new_ht==0 ) return;
|
||||
if( pH->ht ) sqliteFree(pH->ht);
|
||||
pH->ht = new_ht;
|
||||
pH->htsize = new_size;
|
||||
xHash = hashFunction(pH->keyClass);
|
||||
for(elem=pH->first, pH->first=0; elem; elem = next_elem){
|
||||
int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
|
||||
next_elem = elem->next;
|
||||
x = new_ht[h].chain;
|
||||
if( x ){
|
||||
elem->next = x;
|
||||
elem->prev = x->prev;
|
||||
if( x->prev ) x->prev->next = elem;
|
||||
else pH->first = elem;
|
||||
x->prev = elem;
|
||||
}else{
|
||||
elem->next = pH->first;
|
||||
if( pH->first ) pH->first->prev = elem;
|
||||
elem->prev = 0;
|
||||
pH->first = elem;
|
||||
}
|
||||
new_ht[h].chain = elem;
|
||||
new_ht[h].count++;
|
||||
}
|
||||
}
|
||||
|
||||
/* This function (for internal use only) locates an element in an
|
||||
** hash table that matches the given key. The hash for this key has
|
||||
** already been computed and is passed as the 4th parameter.
|
||||
*/
|
||||
static HashElem *findElementGivenHash(
|
||||
const Hash *pH, /* The pH to be searched */
|
||||
const void *pKey, /* The key we are searching for */
|
||||
int nKey,
|
||||
int h /* The hash for this key. */
|
||||
){
|
||||
HashElem *elem; /* Used to loop thru the element list */
|
||||
int count; /* Number of elements left to test */
|
||||
int (*xCompare)(const void*,int,const void*,int); /* comparison function */
|
||||
|
||||
if( pH->ht ){
|
||||
elem = pH->ht[h].chain;
|
||||
count = pH->ht[h].count;
|
||||
xCompare = compareFunction(pH->keyClass);
|
||||
while( count-- && elem ){
|
||||
if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
|
||||
return elem;
|
||||
}
|
||||
elem = elem->next;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Remove a single entry from the hash table given a pointer to that
|
||||
** element and a hash on the element's key.
|
||||
*/
|
||||
static void removeElementGivenHash(
|
||||
Hash *pH, /* The pH containing "elem" */
|
||||
HashElem* elem, /* The element to be removed from the pH */
|
||||
int h /* Hash value for the element */
|
||||
){
|
||||
if( elem->prev ){
|
||||
elem->prev->next = elem->next;
|
||||
}else{
|
||||
pH->first = elem->next;
|
||||
}
|
||||
if( elem->next ){
|
||||
elem->next->prev = elem->prev;
|
||||
}
|
||||
if( pH->ht[h].chain==elem ){
|
||||
pH->ht[h].chain = elem->next;
|
||||
}
|
||||
pH->ht[h].count--;
|
||||
if( pH->ht[h].count<=0 ){
|
||||
pH->ht[h].chain = 0;
|
||||
}
|
||||
if( pH->copyKey && elem->pKey ){
|
||||
sqliteFree(elem->pKey);
|
||||
}
|
||||
sqliteFree( elem );
|
||||
pH->count--;
|
||||
}
|
||||
|
||||
/* Attempt to locate an element of the hash table pH with a key
|
||||
** that matches pKey,nKey. Return the data for this element if it is
|
||||
** found, or NULL if there is no match.
|
||||
*/
|
||||
void *sqliteHashFind(const Hash *pH, const void *pKey, int nKey){
|
||||
int h; /* A hash on key */
|
||||
HashElem *elem; /* The element that matches key */
|
||||
int (*xHash)(const void*,int); /* The hash function */
|
||||
|
||||
if( pH==0 || pH->ht==0 ) return 0;
|
||||
xHash = hashFunction(pH->keyClass);
|
||||
assert( xHash!=0 );
|
||||
h = (*xHash)(pKey,nKey);
|
||||
assert( (pH->htsize & (pH->htsize-1))==0 );
|
||||
elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
|
||||
return elem ? elem->data : 0;
|
||||
}
|
||||
|
||||
/* Insert an element into the hash table pH. The key is pKey,nKey
|
||||
** and the data is "data".
|
||||
**
|
||||
** If no element exists with a matching key, then a new
|
||||
** element is created. A copy of the key is made if the copyKey
|
||||
** flag is set. NULL is returned.
|
||||
**
|
||||
** If another element already exists with the same key, then the
|
||||
** new data replaces the old data and the old data is returned.
|
||||
** The key is not copied in this instance. If a malloc fails, then
|
||||
** the new data is returned and the hash table is unchanged.
|
||||
**
|
||||
** If the "data" parameter to this function is NULL, then the
|
||||
** element corresponding to "key" is removed from the hash table.
|
||||
*/
|
||||
void *sqliteHashInsert(Hash *pH, const void *pKey, int nKey, void *data){
|
||||
int hraw; /* Raw hash value of the key */
|
||||
int h; /* the hash of the key modulo hash table size */
|
||||
HashElem *elem; /* Used to loop thru the element list */
|
||||
HashElem *new_elem; /* New element added to the pH */
|
||||
int (*xHash)(const void*,int); /* The hash function */
|
||||
|
||||
assert( pH!=0 );
|
||||
xHash = hashFunction(pH->keyClass);
|
||||
assert( xHash!=0 );
|
||||
hraw = (*xHash)(pKey, nKey);
|
||||
assert( (pH->htsize & (pH->htsize-1))==0 );
|
||||
h = hraw & (pH->htsize-1);
|
||||
elem = findElementGivenHash(pH,pKey,nKey,h);
|
||||
if( elem ){
|
||||
void *old_data = elem->data;
|
||||
if( data==0 ){
|
||||
removeElementGivenHash(pH,elem,h);
|
||||
}else{
|
||||
elem->data = data;
|
||||
}
|
||||
return old_data;
|
||||
}
|
||||
if( data==0 ) return 0;
|
||||
new_elem = (HashElem*)sqliteMalloc( sizeof(HashElem) );
|
||||
if( new_elem==0 ) return data;
|
||||
if( pH->copyKey && pKey!=0 ){
|
||||
new_elem->pKey = sqliteMallocRaw( nKey );
|
||||
if( new_elem->pKey==0 ){
|
||||
sqliteFree(new_elem);
|
||||
return data;
|
||||
}
|
||||
memcpy((void*)new_elem->pKey, pKey, nKey);
|
||||
}else{
|
||||
new_elem->pKey = (void*)pKey;
|
||||
}
|
||||
new_elem->nKey = nKey;
|
||||
pH->count++;
|
||||
if( pH->htsize==0 ) rehash(pH,8);
|
||||
if( pH->htsize==0 ){
|
||||
pH->count = 0;
|
||||
sqliteFree(new_elem);
|
||||
return data;
|
||||
}
|
||||
if( pH->count > pH->htsize ){
|
||||
rehash(pH,pH->htsize*2);
|
||||
}
|
||||
assert( (pH->htsize & (pH->htsize-1))==0 );
|
||||
h = hraw & (pH->htsize-1);
|
||||
elem = pH->ht[h].chain;
|
||||
if( elem ){
|
||||
new_elem->next = elem;
|
||||
new_elem->prev = elem->prev;
|
||||
if( elem->prev ){ elem->prev->next = new_elem; }
|
||||
else { pH->first = new_elem; }
|
||||
elem->prev = new_elem;
|
||||
}else{
|
||||
new_elem->next = pH->first;
|
||||
new_elem->prev = 0;
|
||||
if( pH->first ){ pH->first->prev = new_elem; }
|
||||
pH->first = new_elem;
|
||||
}
|
||||
pH->ht[h].count++;
|
||||
pH->ht[h].chain = new_elem;
|
||||
new_elem->data = data;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
** 2001 September 22
|
||||
**
|
||||
** 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 is the header file for the generic hash-table implemenation
|
||||
** used in SQLite.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#ifndef _SQLITE_HASH_H_
|
||||
#define _SQLITE_HASH_H_
|
||||
|
||||
/* Forward declarations of structures. */
|
||||
typedef struct Hash Hash;
|
||||
typedef struct HashElem HashElem;
|
||||
|
||||
/* A complete hash table is an instance of the following structure.
|
||||
** The internals of this structure are intended to be opaque -- client
|
||||
** code should not attempt to access or modify the fields of this structure
|
||||
** directly. Change this structure only by using the routines below.
|
||||
** However, many of the "procedures" and "functions" for modifying and
|
||||
** accessing this structure are really macros, so we can't really make
|
||||
** this structure opaque.
|
||||
*/
|
||||
struct Hash {
|
||||
char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
|
||||
char copyKey; /* True if copy of key made on insert */
|
||||
int count; /* Number of entries in this table */
|
||||
HashElem *first; /* The first element of the array */
|
||||
int htsize; /* Number of buckets in the hash table */
|
||||
struct _ht { /* the hash table */
|
||||
int count; /* Number of entries with this hash */
|
||||
HashElem *chain; /* Pointer to first entry with this hash */
|
||||
} *ht;
|
||||
};
|
||||
|
||||
/* Each element in the hash table is an instance of the following
|
||||
** structure. All elements are stored on a single doubly-linked list.
|
||||
**
|
||||
** Again, this structure is intended to be opaque, but it can't really
|
||||
** be opaque because it is used by macros.
|
||||
*/
|
||||
struct HashElem {
|
||||
HashElem *next, *prev; /* Next and previous elements in the table */
|
||||
void *data; /* Data associated with this element */
|
||||
void *pKey; int nKey; /* Key associated with this element */
|
||||
};
|
||||
|
||||
/*
|
||||
** There are 4 different modes of operation for a hash table:
|
||||
**
|
||||
** SQLITE_HASH_INT nKey is used as the key and pKey is ignored.
|
||||
**
|
||||
** SQLITE_HASH_POINTER pKey is used as the key and nKey is ignored.
|
||||
**
|
||||
** SQLITE_HASH_STRING pKey points to a string that is nKey bytes long
|
||||
** (including the null-terminator, if any). Case
|
||||
** is ignored in comparisons.
|
||||
**
|
||||
** SQLITE_HASH_BINARY pKey points to binary data nKey bytes long.
|
||||
** memcmp() is used to compare keys.
|
||||
**
|
||||
** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY
|
||||
** if the copyKey parameter to HashInit is 1.
|
||||
*/
|
||||
#define SQLITE_HASH_INT 1
|
||||
#define SQLITE_HASH_POINTER 2
|
||||
#define SQLITE_HASH_STRING 3
|
||||
#define SQLITE_HASH_BINARY 4
|
||||
|
||||
/*
|
||||
** Access routines. To delete, insert a NULL pointer.
|
||||
*/
|
||||
void sqliteHashInit(Hash*, int keytype, int copyKey);
|
||||
void *sqliteHashInsert(Hash*, const void *pKey, int nKey, void *pData);
|
||||
void *sqliteHashFind(const Hash*, const void *pKey, int nKey);
|
||||
void sqliteHashClear(Hash*);
|
||||
|
||||
/*
|
||||
** Macros for looping over all elements of a hash table. The idiom is
|
||||
** like this:
|
||||
**
|
||||
** Hash h;
|
||||
** HashElem *p;
|
||||
** ...
|
||||
** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
|
||||
** SomeStructure *pData = sqliteHashData(p);
|
||||
** // do something with pData
|
||||
** }
|
||||
*/
|
||||
#define sqliteHashFirst(H) ((H)->first)
|
||||
#define sqliteHashNext(E) ((E)->next)
|
||||
#define sqliteHashData(E) ((E)->data)
|
||||
#define sqliteHashKey(E) ((E)->pKey)
|
||||
#define sqliteHashKeysize(E) ((E)->nKey)
|
||||
|
||||
/*
|
||||
** Number of entries in a hash table
|
||||
*/
|
||||
#define sqliteHashCount(H) ((H)->count)
|
||||
|
||||
#endif /* _SQLITE_HASH_H_ */
|
||||
@@ -1,891 +0,0 @@
|
||||
/*
|
||||
** 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 contains C code routines that are called by the parser
|
||||
** to handle INSERT statements in SQLite.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** This routine is call to handle SQL of the following forms:
|
||||
**
|
||||
** insert into TABLE (IDLIST) values(EXPRLIST)
|
||||
** insert into TABLE (IDLIST) select
|
||||
**
|
||||
** The IDLIST following the table name is always optional. If omitted,
|
||||
** then a list of all columns for the table is substituted. The IDLIST
|
||||
** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
|
||||
**
|
||||
** The pList parameter holds EXPRLIST in the first form of the INSERT
|
||||
** statement above, and pSelect is NULL. For the second form, pList is
|
||||
** NULL and pSelect is a pointer to the select statement used to generate
|
||||
** data for the insert.
|
||||
**
|
||||
** The code generated follows one of three templates. For a simple
|
||||
** select with data coming from a VALUES clause, the code executes
|
||||
** once straight down through. The template looks like this:
|
||||
**
|
||||
** open write cursor to <table> and its indices
|
||||
** puts VALUES clause expressions onto the stack
|
||||
** write the resulting record into <table>
|
||||
** cleanup
|
||||
**
|
||||
** If the statement is of the form
|
||||
**
|
||||
** INSERT INTO <table> SELECT ...
|
||||
**
|
||||
** And the SELECT clause does not read from <table> at any time, then
|
||||
** the generated code follows this template:
|
||||
**
|
||||
** goto B
|
||||
** A: setup for the SELECT
|
||||
** loop over the tables in the SELECT
|
||||
** gosub C
|
||||
** end loop
|
||||
** cleanup after the SELECT
|
||||
** goto D
|
||||
** B: open write cursor to <table> and its indices
|
||||
** goto A
|
||||
** C: insert the select result into <table>
|
||||
** return
|
||||
** D: cleanup
|
||||
**
|
||||
** The third template is used if the insert statement takes its
|
||||
** values from a SELECT but the data is being inserted into a table
|
||||
** that is also read as part of the SELECT. In the third form,
|
||||
** we have to use a intermediate table to store the results of
|
||||
** the select. The template is like this:
|
||||
**
|
||||
** goto B
|
||||
** A: setup for the SELECT
|
||||
** loop over the tables in the SELECT
|
||||
** gosub C
|
||||
** end loop
|
||||
** cleanup after the SELECT
|
||||
** goto D
|
||||
** C: insert the select result into the intermediate table
|
||||
** return
|
||||
** B: open a cursor to an intermediate table
|
||||
** goto A
|
||||
** D: open write cursor to <table> and its indices
|
||||
** loop over the intermediate table
|
||||
** transfer values form intermediate table into <table>
|
||||
** end the loop
|
||||
** cleanup
|
||||
*/
|
||||
void sqliteInsert(
|
||||
Parse *pParse, /* Parser context */
|
||||
SrcList *pTabList, /* Name of table into which we are inserting */
|
||||
ExprList *pList, /* List of values to be inserted */
|
||||
Select *pSelect, /* A SELECT statement to use as the data source */
|
||||
IdList *pColumn, /* Column names corresponding to IDLIST. */
|
||||
int onError /* How to handle constraint errors */
|
||||
){
|
||||
Table *pTab; /* The table to insert into */
|
||||
char *zTab; /* Name of the table into which we are inserting */
|
||||
const char *zDb; /* Name of the database holding this table */
|
||||
int i, j, idx; /* Loop counters */
|
||||
Vdbe *v; /* Generate code into this virtual machine */
|
||||
Index *pIdx; /* For looping over indices of the table */
|
||||
int nColumn; /* Number of columns in the data */
|
||||
int base; /* VDBE Cursor number for pTab */
|
||||
int iCont, iBreak; /* Beginning and end of the loop over srcTab */
|
||||
sqlite *db; /* The main database structure */
|
||||
int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
|
||||
int endOfLoop; /* Label for the end of the insertion loop */
|
||||
int useTempTable; /* Store SELECT results in intermediate table */
|
||||
int srcTab; /* Data comes from this temporary cursor if >=0 */
|
||||
int iSelectLoop; /* Address of code that implements the SELECT */
|
||||
int iCleanup; /* Address of the cleanup code */
|
||||
int iInsertBlock; /* Address of the subroutine used to insert data */
|
||||
int iCntMem; /* Memory cell used for the row counter */
|
||||
int isView; /* True if attempting to insert into a view */
|
||||
|
||||
int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
|
||||
int before_triggers; /* True if there are BEFORE triggers */
|
||||
int after_triggers; /* True if there are AFTER triggers */
|
||||
int newIdx = -1; /* Cursor for the NEW table */
|
||||
|
||||
if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
|
||||
db = pParse->db;
|
||||
|
||||
/* Locate the table into which we will be inserting new information.
|
||||
*/
|
||||
assert( pTabList->nSrc==1 );
|
||||
zTab = pTabList->a[0].zName;
|
||||
if( zTab==0 ) goto insert_cleanup;
|
||||
pTab = sqliteSrcListLookup(pParse, pTabList);
|
||||
if( pTab==0 ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
assert( pTab->iDb<db->nDb );
|
||||
zDb = db->aDb[pTab->iDb].zName;
|
||||
if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
|
||||
/* Ensure that:
|
||||
* (a) the table is not read-only,
|
||||
* (b) that if it is a view then ON INSERT triggers exist
|
||||
*/
|
||||
before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
|
||||
TK_BEFORE, TK_ROW, 0);
|
||||
after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
|
||||
TK_AFTER, TK_ROW, 0);
|
||||
row_triggers_exist = before_triggers || after_triggers;
|
||||
isView = pTab->pSelect!=0;
|
||||
if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
if( pTab==0 ) goto insert_cleanup;
|
||||
|
||||
/* If pTab is really a view, make sure it has been initialized.
|
||||
*/
|
||||
if( isView && sqliteViewGetColumnNames(pParse, pTab) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
|
||||
/* Allocate a VDBE
|
||||
*/
|
||||
v = sqliteGetVdbe(pParse);
|
||||
if( v==0 ) goto insert_cleanup;
|
||||
sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb);
|
||||
|
||||
/* if there are row triggers, allocate a temp table for new.* references. */
|
||||
if( row_triggers_exist ){
|
||||
newIdx = pParse->nTab++;
|
||||
}
|
||||
|
||||
/* Figure out how many columns of data are supplied. If the data
|
||||
** is coming from a SELECT statement, then this step also generates
|
||||
** all the code to implement the SELECT statement and invoke a subroutine
|
||||
** to process each row of the result. (Template 2.) If the SELECT
|
||||
** statement uses the the table that is being inserted into, then the
|
||||
** subroutine is also coded here. That subroutine stores the SELECT
|
||||
** results in a temporary table. (Template 3.)
|
||||
*/
|
||||
if( pSelect ){
|
||||
/* Data is coming from a SELECT. Generate code to implement that SELECT
|
||||
*/
|
||||
int rc, iInitCode;
|
||||
iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
|
||||
iSelectLoop = sqliteVdbeCurrentAddr(v);
|
||||
iInsertBlock = sqliteVdbeMakeLabel(v);
|
||||
rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
|
||||
if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
|
||||
iCleanup = sqliteVdbeMakeLabel(v);
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup);
|
||||
assert( pSelect->pEList );
|
||||
nColumn = pSelect->pEList->nExpr;
|
||||
|
||||
/* Set useTempTable to TRUE if the result of the SELECT statement
|
||||
** should be written into a temporary table. Set to FALSE if each
|
||||
** row of the SELECT can be written directly into the result table.
|
||||
**
|
||||
** A temp table must be used if the table being updated is also one
|
||||
** of the tables being read by the SELECT statement. Also use a
|
||||
** temp table in the case of row triggers.
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
useTempTable = 1;
|
||||
}else{
|
||||
int addr = sqliteVdbeFindOp(v, OP_OpenRead, pTab->tnum);
|
||||
useTempTable = 0;
|
||||
if( addr>0 ){
|
||||
VdbeOp *pOp = sqliteVdbeGetOp(v, addr-2);
|
||||
if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
|
||||
useTempTable = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( useTempTable ){
|
||||
/* Generate the subroutine that SELECT calls to process each row of
|
||||
** the result. Store the result in a temporary table
|
||||
*/
|
||||
srcTab = pParse->nTab++;
|
||||
sqliteVdbeResolveLabel(v, iInsertBlock);
|
||||
sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
|
||||
sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0);
|
||||
sqliteVdbeAddOp(v, OP_Pull, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0);
|
||||
sqliteVdbeAddOp(v, OP_Return, 0, 0);
|
||||
|
||||
/* The following code runs first because the GOTO at the very top
|
||||
** of the program jumps to it. Create the temporary table, then jump
|
||||
** back up and execute the SELECT code above.
|
||||
*/
|
||||
sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
|
||||
sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
|
||||
sqliteVdbeResolveLabel(v, iCleanup);
|
||||
}else{
|
||||
sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
|
||||
}
|
||||
}else{
|
||||
/* This is the case if the data for the INSERT is coming from a VALUES
|
||||
** clause
|
||||
*/
|
||||
SrcList dummy;
|
||||
assert( pList!=0 );
|
||||
srcTab = -1;
|
||||
useTempTable = 0;
|
||||
assert( pList );
|
||||
nColumn = pList->nExpr;
|
||||
dummy.nSrc = 0;
|
||||
for(i=0; i<nColumn; i++){
|
||||
if( sqliteExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Make sure the number of columns in the source data matches the number
|
||||
** of columns to be inserted into the table.
|
||||
*/
|
||||
if( pColumn==0 && nColumn!=pTab->nCol ){
|
||||
sqliteErrorMsg(pParse,
|
||||
"table %S has %d columns but %d values were supplied",
|
||||
pTabList, 0, pTab->nCol, nColumn);
|
||||
goto insert_cleanup;
|
||||
}
|
||||
if( pColumn!=0 && nColumn!=pColumn->nId ){
|
||||
sqliteErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
|
||||
goto insert_cleanup;
|
||||
}
|
||||
|
||||
/* If the INSERT statement included an IDLIST term, then make sure
|
||||
** all elements of the IDLIST really are columns of the table and
|
||||
** remember the column indices.
|
||||
**
|
||||
** If the table has an INTEGER PRIMARY KEY column and that column
|
||||
** is named in the IDLIST, then record in the keyColumn variable
|
||||
** the index into IDLIST of the primary key column. keyColumn is
|
||||
** the index of the primary key as it appears in IDLIST, not as
|
||||
** is appears in the original table. (The index of the primary
|
||||
** key in the original table is pTab->iPKey.)
|
||||
*/
|
||||
if( pColumn ){
|
||||
for(i=0; i<pColumn->nId; i++){
|
||||
pColumn->a[i].idx = -1;
|
||||
}
|
||||
for(i=0; i<pColumn->nId; i++){
|
||||
for(j=0; j<pTab->nCol; j++){
|
||||
if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
|
||||
pColumn->a[i].idx = j;
|
||||
if( j==pTab->iPKey ){
|
||||
keyColumn = i;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( j>=pTab->nCol ){
|
||||
if( sqliteIsRowid(pColumn->a[i].zName) ){
|
||||
keyColumn = i;
|
||||
}else{
|
||||
sqliteErrorMsg(pParse, "table %S has no column named %s",
|
||||
pTabList, 0, pColumn->a[i].zName);
|
||||
pParse->nErr++;
|
||||
goto insert_cleanup;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If there is no IDLIST term but the table has an integer primary
|
||||
** key, the set the keyColumn variable to the primary key column index
|
||||
** in the original table definition.
|
||||
*/
|
||||
if( pColumn==0 ){
|
||||
keyColumn = pTab->iPKey;
|
||||
}
|
||||
|
||||
/* Open the temp table for FOR EACH ROW triggers
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
|
||||
}
|
||||
|
||||
/* Initialize the count of rows to be inserted
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
iCntMem = pParse->nMem++;
|
||||
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1);
|
||||
}
|
||||
|
||||
/* Open tables and indices if there are no row triggers */
|
||||
if( !row_triggers_exist ){
|
||||
base = pParse->nTab;
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
|
||||
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
|
||||
}
|
||||
pParse->nTab += idx;
|
||||
}
|
||||
|
||||
/* If the data source is a temporary table, then we have to create
|
||||
** a loop because there might be multiple rows of data. If the data
|
||||
** source is a subroutine call from the SELECT statement, then we need
|
||||
** to launch the SELECT statement processing.
|
||||
*/
|
||||
if( useTempTable ){
|
||||
iBreak = sqliteVdbeMakeLabel(v);
|
||||
sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
|
||||
iCont = sqliteVdbeCurrentAddr(v);
|
||||
}else if( pSelect ){
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
|
||||
sqliteVdbeResolveLabel(v, iInsertBlock);
|
||||
}
|
||||
|
||||
/* Run the BEFORE and INSTEAD OF triggers, if there are any
|
||||
*/
|
||||
endOfLoop = sqliteVdbeMakeLabel(v);
|
||||
if( before_triggers ){
|
||||
|
||||
/* build the NEW.* reference row. Note that if there is an INTEGER
|
||||
** PRIMARY KEY into which a NULL is being inserted, that NULL will be
|
||||
** translated into a unique ID for the row. But on a BEFORE trigger,
|
||||
** we do not know what the unique ID will be (because the insert has
|
||||
** not happened yet) so we substitute a rowid of -1
|
||||
*/
|
||||
if( keyColumn<0 ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, -1, 0);
|
||||
}else if( useTempTable ){
|
||||
sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
|
||||
}else if( pSelect ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
|
||||
}else{
|
||||
sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
|
||||
sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
|
||||
sqliteVdbeAddOp(v, OP_Pop, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_Integer, -1, 0);
|
||||
sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
|
||||
}
|
||||
|
||||
/* Create the new column data
|
||||
*/
|
||||
for(i=0; i<pTab->nCol; i++){
|
||||
if( pColumn==0 ){
|
||||
j = i;
|
||||
}else{
|
||||
for(j=0; j<pColumn->nId; j++){
|
||||
if( pColumn->a[j].idx==i ) break;
|
||||
}
|
||||
}
|
||||
if( pColumn && j>=pColumn->nId ){
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
|
||||
}else if( useTempTable ){
|
||||
sqliteVdbeAddOp(v, OP_Column, srcTab, j);
|
||||
}else if( pSelect ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
|
||||
}else{
|
||||
sqliteExprCode(pParse, pList->a[j].pExpr);
|
||||
}
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
|
||||
|
||||
/* Fire BEFORE or INSTEAD OF triggers */
|
||||
if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab,
|
||||
newIdx, -1, onError, endOfLoop) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
/* If any triggers exists, the opening of tables and indices is deferred
|
||||
** until now.
|
||||
*/
|
||||
if( row_triggers_exist && !isView ){
|
||||
base = pParse->nTab;
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
|
||||
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
|
||||
}
|
||||
pParse->nTab += idx;
|
||||
}
|
||||
|
||||
/* Push the record number for the new entry onto the stack. The
|
||||
** record number is a randomly generate integer created by NewRecno
|
||||
** except when the table has an INTEGER PRIMARY KEY column, in which
|
||||
** case the record number is the same as that column.
|
||||
*/
|
||||
if( !isView ){
|
||||
if( keyColumn>=0 ){
|
||||
if( useTempTable ){
|
||||
sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
|
||||
}else if( pSelect ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
|
||||
}else{
|
||||
sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
|
||||
}
|
||||
/* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
|
||||
** to generate a unique primary key value.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
|
||||
sqliteVdbeAddOp(v, OP_Pop, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
|
||||
sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
|
||||
}
|
||||
|
||||
/* Push onto the stack, data for all columns of the new entry, beginning
|
||||
** with the first column.
|
||||
*/
|
||||
for(i=0; i<pTab->nCol; i++){
|
||||
if( i==pTab->iPKey ){
|
||||
/* The value of the INTEGER PRIMARY KEY column is always a NULL.
|
||||
** Whenever this column is read, the record number will be substituted
|
||||
** in its place. So will fill this column with a NULL to avoid
|
||||
** taking up data space with information that will never be used. */
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
continue;
|
||||
}
|
||||
if( pColumn==0 ){
|
||||
j = i;
|
||||
}else{
|
||||
for(j=0; j<pColumn->nId; j++){
|
||||
if( pColumn->a[j].idx==i ) break;
|
||||
}
|
||||
}
|
||||
if( pColumn && j>=pColumn->nId ){
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
|
||||
}else if( useTempTable ){
|
||||
sqliteVdbeAddOp(v, OP_Column, srcTab, j);
|
||||
}else if( pSelect ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
|
||||
}else{
|
||||
sqliteExprCode(pParse, pList->a[j].pExpr);
|
||||
}
|
||||
}
|
||||
|
||||
/* Generate code to check constraints and generate index keys and
|
||||
** do the insertion.
|
||||
*/
|
||||
sqliteGenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
|
||||
0, onError, endOfLoop);
|
||||
sqliteCompleteInsertion(pParse, pTab, base, 0,0,0,
|
||||
after_triggers ? newIdx : -1);
|
||||
}
|
||||
|
||||
/* Update the count of rows that are inserted
|
||||
*/
|
||||
if( (db->flags & SQLITE_CountRows)!=0 ){
|
||||
sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0);
|
||||
}
|
||||
|
||||
if( row_triggers_exist ){
|
||||
/* Close all tables opened */
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Close, base, 0);
|
||||
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
|
||||
sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Code AFTER triggers */
|
||||
if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
|
||||
onError, endOfLoop) ){
|
||||
goto insert_cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
/* The bottom of the loop, if the data source is a SELECT statement
|
||||
*/
|
||||
sqliteVdbeResolveLabel(v, endOfLoop);
|
||||
if( useTempTable ){
|
||||
sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
|
||||
sqliteVdbeResolveLabel(v, iBreak);
|
||||
sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
|
||||
}else if( pSelect ){
|
||||
sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
|
||||
sqliteVdbeAddOp(v, OP_Return, 0, 0);
|
||||
sqliteVdbeResolveLabel(v, iCleanup);
|
||||
}
|
||||
|
||||
if( !row_triggers_exist ){
|
||||
/* Close all tables opened */
|
||||
sqliteVdbeAddOp(v, OP_Close, base, 0);
|
||||
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
|
||||
sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
|
||||
}
|
||||
}
|
||||
|
||||
sqliteEndWriteOperation(pParse);
|
||||
|
||||
/*
|
||||
** Return the number of rows inserted.
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows ){
|
||||
sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 1, 0);
|
||||
}
|
||||
|
||||
insert_cleanup:
|
||||
sqliteSrcListDelete(pTabList);
|
||||
if( pList ) sqliteExprListDelete(pList);
|
||||
if( pSelect ) sqliteSelectDelete(pSelect);
|
||||
sqliteIdListDelete(pColumn);
|
||||
}
|
||||
|
||||
/*
|
||||
** Generate code to do a constraint check prior to an INSERT or an UPDATE.
|
||||
**
|
||||
** When this routine is called, the stack contains (from bottom to top)
|
||||
** the following values:
|
||||
**
|
||||
** 1. The recno of the row to be updated before the update. This
|
||||
** value is omitted unless we are doing an UPDATE that involves a
|
||||
** change to the record number.
|
||||
**
|
||||
** 2. The recno of the row after the update.
|
||||
**
|
||||
** 3. The data in the first column of the entry after the update.
|
||||
**
|
||||
** i. Data from middle columns...
|
||||
**
|
||||
** N. The data in the last column of the entry after the update.
|
||||
**
|
||||
** The old recno shown as entry (1) above is omitted unless both isUpdate
|
||||
** and recnoChng are 1. isUpdate is true for UPDATEs and false for
|
||||
** INSERTs and recnoChng is true if the record number is being changed.
|
||||
**
|
||||
** The code generated by this routine pushes additional entries onto
|
||||
** the stack which are the keys for new index entries for the new record.
|
||||
** The order of index keys is the same as the order of the indices on
|
||||
** the pTable->pIndex list. A key is only created for index i if
|
||||
** aIdxUsed!=0 and aIdxUsed[i]!=0.
|
||||
**
|
||||
** This routine also generates code to check constraints. NOT NULL,
|
||||
** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
|
||||
** then the appropriate action is performed. There are five possible
|
||||
** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
|
||||
**
|
||||
** Constraint type Action What Happens
|
||||
** --------------- ---------- ----------------------------------------
|
||||
** any ROLLBACK The current transaction is rolled back and
|
||||
** sqlite_exec() returns immediately with a
|
||||
** return code of SQLITE_CONSTRAINT.
|
||||
**
|
||||
** any ABORT Back out changes from the current command
|
||||
** only (do not do a complete rollback) then
|
||||
** cause sqlite_exec() to return immediately
|
||||
** with SQLITE_CONSTRAINT.
|
||||
**
|
||||
** any FAIL Sqlite_exec() returns immediately with a
|
||||
** return code of SQLITE_CONSTRAINT. The
|
||||
** transaction is not rolled back and any
|
||||
** prior changes are retained.
|
||||
**
|
||||
** any IGNORE The record number and data is popped from
|
||||
** the stack and there is an immediate jump
|
||||
** to label ignoreDest.
|
||||
**
|
||||
** NOT NULL REPLACE The NULL value is replace by the default
|
||||
** value for that column. If the default value
|
||||
** is NULL, the action is the same as ABORT.
|
||||
**
|
||||
** UNIQUE REPLACE The other row that conflicts with the row
|
||||
** being inserted is removed.
|
||||
**
|
||||
** CHECK REPLACE Illegal. The results in an exception.
|
||||
**
|
||||
** Which action to take is determined by the overrideError parameter.
|
||||
** Or if overrideError==OE_Default, then the pParse->onError parameter
|
||||
** is used. Or if pParse->onError==OE_Default then the onError value
|
||||
** for the constraint is used.
|
||||
**
|
||||
** The calling routine must open a read/write cursor for pTab with
|
||||
** cursor number "base". All indices of pTab must also have open
|
||||
** read/write cursors with cursor number base+i for the i-th cursor.
|
||||
** Except, if there is no possibility of a REPLACE action then
|
||||
** cursors do not need to be open for indices where aIdxUsed[i]==0.
|
||||
**
|
||||
** If the isUpdate flag is true, it means that the "base" cursor is
|
||||
** initially pointing to an entry that is being updated. The isUpdate
|
||||
** flag causes extra code to be generated so that the "base" cursor
|
||||
** is still pointing at the same entry after the routine returns.
|
||||
** Without the isUpdate flag, the "base" cursor might be moved.
|
||||
*/
|
||||
void sqliteGenerateConstraintChecks(
|
||||
Parse *pParse, /* The parser context */
|
||||
Table *pTab, /* the table into which we are inserting */
|
||||
int base, /* Index of a read/write cursor pointing at pTab */
|
||||
char *aIdxUsed, /* Which indices are used. NULL means all are used */
|
||||
int recnoChng, /* True if the record number will change */
|
||||
int isUpdate, /* True for UPDATE, False for INSERT */
|
||||
int overrideError, /* Override onError to this if not OE_Default */
|
||||
int ignoreDest /* Jump to this label on an OE_Ignore resolution */
|
||||
){
|
||||
int i;
|
||||
Vdbe *v;
|
||||
int nCol;
|
||||
int onError;
|
||||
int addr;
|
||||
int extra;
|
||||
int iCur;
|
||||
Index *pIdx;
|
||||
int seenReplace = 0;
|
||||
int jumpInst1, jumpInst2;
|
||||
int contAddr;
|
||||
int hasTwoRecnos = (isUpdate && recnoChng);
|
||||
|
||||
v = sqliteGetVdbe(pParse);
|
||||
assert( v!=0 );
|
||||
assert( pTab->pSelect==0 ); /* This table is not a VIEW */
|
||||
nCol = pTab->nCol;
|
||||
|
||||
/* Test all NOT NULL constraints.
|
||||
*/
|
||||
for(i=0; i<nCol; i++){
|
||||
if( i==pTab->iPKey ){
|
||||
continue;
|
||||
}
|
||||
onError = pTab->aCol[i].notNull;
|
||||
if( onError==OE_None ) continue;
|
||||
if( overrideError!=OE_Default ){
|
||||
onError = overrideError;
|
||||
}else if( pParse->db->onError!=OE_Default ){
|
||||
onError = pParse->db->onError;
|
||||
}else if( onError==OE_Default ){
|
||||
onError = OE_Abort;
|
||||
}
|
||||
if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
|
||||
onError = OE_Abort;
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
|
||||
addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
|
||||
switch( onError ){
|
||||
case OE_Rollback:
|
||||
case OE_Abort:
|
||||
case OE_Fail: {
|
||||
char *zMsg = 0;
|
||||
sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
|
||||
sqliteSetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
|
||||
" may not be NULL", 0);
|
||||
sqliteVdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
|
||||
break;
|
||||
}
|
||||
case OE_Ignore: {
|
||||
sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
|
||||
break;
|
||||
}
|
||||
case OE_Replace: {
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
|
||||
break;
|
||||
}
|
||||
default: assert(0);
|
||||
}
|
||||
sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
|
||||
}
|
||||
|
||||
/* Test all CHECK constraints
|
||||
*/
|
||||
/**** TBD ****/
|
||||
|
||||
/* If we have an INTEGER PRIMARY KEY, make sure the primary key
|
||||
** of the new record does not previously exist. Except, if this
|
||||
** is an UPDATE and the primary key is not changing, that is OK.
|
||||
*/
|
||||
if( recnoChng ){
|
||||
onError = pTab->keyConf;
|
||||
if( overrideError!=OE_Default ){
|
||||
onError = overrideError;
|
||||
}else if( pParse->db->onError!=OE_Default ){
|
||||
onError = pParse->db->onError;
|
||||
}else if( onError==OE_Default ){
|
||||
onError = OE_Abort;
|
||||
}
|
||||
|
||||
if( isUpdate ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
|
||||
jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
|
||||
jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
|
||||
switch( onError ){
|
||||
default: {
|
||||
onError = OE_Abort;
|
||||
/* Fall thru into the next case */
|
||||
}
|
||||
case OE_Rollback:
|
||||
case OE_Abort:
|
||||
case OE_Fail: {
|
||||
sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
|
||||
sqliteVdbeChangeP3(v, -1, "PRIMARY KEY must be unique", P3_STATIC);
|
||||
break;
|
||||
}
|
||||
case OE_Replace: {
|
||||
sqliteGenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
|
||||
if( isUpdate ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1);
|
||||
sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
|
||||
}
|
||||
seenReplace = 1;
|
||||
break;
|
||||
}
|
||||
case OE_Ignore: {
|
||||
assert( seenReplace==0 );
|
||||
sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
|
||||
break;
|
||||
}
|
||||
}
|
||||
contAddr = sqliteVdbeCurrentAddr(v);
|
||||
sqliteVdbeChangeP2(v, jumpInst2, contAddr);
|
||||
if( isUpdate ){
|
||||
sqliteVdbeChangeP2(v, jumpInst1, contAddr);
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
|
||||
sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Test all UNIQUE constraints by creating entries for each UNIQUE
|
||||
** index and making sure that duplicate entries do not already exist.
|
||||
** Add the new records to the indices as we go.
|
||||
*/
|
||||
extra = -1;
|
||||
for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
|
||||
if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
|
||||
extra++;
|
||||
|
||||
/* Create a key for accessing the index entry */
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
|
||||
for(i=0; i<pIdx->nColumn; i++){
|
||||
int idx = pIdx->aiColumn[i];
|
||||
if( idx==pTab->iPKey ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
|
||||
}
|
||||
}
|
||||
jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
|
||||
if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
|
||||
|
||||
/* Find out what action to take in case there is an indexing conflict */
|
||||
onError = pIdx->onError;
|
||||
if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
|
||||
if( overrideError!=OE_Default ){
|
||||
onError = overrideError;
|
||||
}else if( pParse->db->onError!=OE_Default ){
|
||||
onError = pParse->db->onError;
|
||||
}else if( onError==OE_Default ){
|
||||
onError = OE_Abort;
|
||||
}
|
||||
if( seenReplace ){
|
||||
if( onError==OE_Ignore ) onError = OE_Replace;
|
||||
else if( onError==OE_Fail ) onError = OE_Abort;
|
||||
}
|
||||
|
||||
|
||||
/* Check to see if the new index entry will be unique */
|
||||
sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
|
||||
jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
|
||||
|
||||
/* Generate code that executes if the new index entry is not unique */
|
||||
switch( onError ){
|
||||
case OE_Rollback:
|
||||
case OE_Abort:
|
||||
case OE_Fail: {
|
||||
sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
|
||||
sqliteVdbeChangeP3(v, -1, "uniqueness constraint failed", P3_STATIC);
|
||||
break;
|
||||
}
|
||||
case OE_Ignore: {
|
||||
assert( seenReplace==0 );
|
||||
sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
|
||||
break;
|
||||
}
|
||||
case OE_Replace: {
|
||||
sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0);
|
||||
if( isUpdate ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
|
||||
sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
|
||||
}
|
||||
seenReplace = 1;
|
||||
break;
|
||||
}
|
||||
default: assert(0);
|
||||
}
|
||||
contAddr = sqliteVdbeCurrentAddr(v);
|
||||
#if NULL_DISTINCT_FOR_UNIQUE
|
||||
sqliteVdbeChangeP2(v, jumpInst1, contAddr);
|
||||
#endif
|
||||
sqliteVdbeChangeP2(v, jumpInst2, contAddr);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine generates code to finish the INSERT or UPDATE operation
|
||||
** that was started by a prior call to sqliteGenerateConstraintChecks.
|
||||
** The stack must contain keys for all active indices followed by data
|
||||
** and the recno for the new entry. This routine creates the new
|
||||
** entries in all indices and in the main table.
|
||||
**
|
||||
** The arguments to this routine should be the same as the first six
|
||||
** arguments to sqliteGenerateConstraintChecks.
|
||||
*/
|
||||
void sqliteCompleteInsertion(
|
||||
Parse *pParse, /* The parser context */
|
||||
Table *pTab, /* the table into which we are inserting */
|
||||
int base, /* Index of a read/write cursor pointing at pTab */
|
||||
char *aIdxUsed, /* Which indices are used. NULL means all are used */
|
||||
int recnoChng, /* True if the record number will change */
|
||||
int isUpdate, /* True for UPDATE, False for INSERT */
|
||||
int newIdx /* Index of NEW table for triggers. -1 if none */
|
||||
){
|
||||
int i;
|
||||
Vdbe *v;
|
||||
int nIdx;
|
||||
Index *pIdx;
|
||||
|
||||
v = sqliteGetVdbe(pParse);
|
||||
assert( v!=0 );
|
||||
assert( pTab->pSelect==0 ); /* This table is not a VIEW */
|
||||
for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
|
||||
for(i=nIdx-1; i>=0; i--){
|
||||
if( aIdxUsed && aIdxUsed[i]==0 ) continue;
|
||||
sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
|
||||
if( newIdx>=0 ){
|
||||
sqliteVdbeAddOp(v, OP_Dup, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_Dup, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
|
||||
if( isUpdate && recnoChng ){
|
||||
sqliteVdbeAddOp(v, OP_Pop, 1, 0);
|
||||
}
|
||||
}
|
||||
@@ -1,991 +0,0 @@
|
||||
/*
|
||||
** 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.
|
||||
**
|
||||
*************************************************************************
|
||||
** Main file for the SQLite library. The routines in this file
|
||||
** implement the programmer interface to the library. Routines in
|
||||
** other files are for internal use by SQLite and should not be
|
||||
** accessed by users of the library.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
#include <ctype.h>
|
||||
|
||||
/*
|
||||
** A pointer to this structure is used to communicate information
|
||||
** from sqliteInit into the sqliteInitCallback.
|
||||
*/
|
||||
typedef struct {
|
||||
sqlite *db; /* The database being initialized */
|
||||
char **pzErrMsg; /* Error message stored here */
|
||||
} InitData;
|
||||
|
||||
/*
|
||||
** Fill the InitData structure with an error message that indicates
|
||||
** that the database is corrupt.
|
||||
*/
|
||||
static void corruptSchema(InitData *pData){
|
||||
sqliteSetString(pData->pzErrMsg, "malformed database schema", 0);
|
||||
}
|
||||
|
||||
/*
|
||||
** This is the callback routine for the code that initializes the
|
||||
** database. See sqliteInit() below for additional information.
|
||||
**
|
||||
** Each callback contains the following information:
|
||||
**
|
||||
** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
|
||||
** argv[1] = table or index name or meta statement type.
|
||||
** argv[2] = root page number for table or index. NULL for meta.
|
||||
** argv[3] = SQL text for a CREATE TABLE or CREATE INDEX statement.
|
||||
** argv[4] = "1" for temporary files, "0" for main database, "2" or more
|
||||
** for auxiliary database files.
|
||||
**
|
||||
*/
|
||||
static
|
||||
int sqliteInitCallback(void *pInit, int argc, char **argv, char **azColName){
|
||||
InitData *pData = (InitData*)pInit;
|
||||
Parse sParse;
|
||||
int nErr = 0;
|
||||
|
||||
assert( argc==5 );
|
||||
if( argv[0]==0 ){
|
||||
corruptSchema(pData);
|
||||
return 1;
|
||||
}
|
||||
switch( argv[0][0] ){
|
||||
case 'v':
|
||||
case 'i':
|
||||
case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
|
||||
if( argv[2]==0 || argv[4]==0 ){
|
||||
corruptSchema(pData);
|
||||
return 1;
|
||||
}
|
||||
if( argv[3] && argv[3][0] ){
|
||||
/* Call the parser to process a CREATE TABLE, INDEX or VIEW.
|
||||
** But because sParse.initFlag is set to 1, no VDBE code is generated
|
||||
** or executed. All the parser does is build the internal data
|
||||
** structures that describe the table, index, or view.
|
||||
*/
|
||||
memset(&sParse, 0, sizeof(sParse));
|
||||
sParse.db = pData->db;
|
||||
sParse.initFlag = 1;
|
||||
sParse.iDb = atoi(argv[4]);
|
||||
sParse.newTnum = atoi(argv[2]);
|
||||
sParse.useCallback = 1;
|
||||
sqliteRunParser(&sParse, argv[3], pData->pzErrMsg);
|
||||
}else{
|
||||
/* If the SQL column is blank it means this is an index that
|
||||
** was created to be the PRIMARY KEY or to fulfill a UNIQUE
|
||||
** constraint for a CREATE TABLE. The index should have already
|
||||
** been created when we processed the CREATE TABLE. All we have
|
||||
** to do here is record the root page number for that index.
|
||||
*/
|
||||
int iDb;
|
||||
Index *pIndex;
|
||||
|
||||
iDb = atoi(argv[4]);
|
||||
assert( iDb>=0 && iDb<pData->db->nDb );
|
||||
pIndex = sqliteFindIndex(pData->db, argv[1], pData->db->aDb[iDb].zName);
|
||||
if( pIndex==0 || pIndex->tnum!=0 ){
|
||||
/* This can occur if there exists an index on a TEMP table which
|
||||
** has the same name as another index on a permanent index. Since
|
||||
** the permanent table is hidden by the TEMP table, we can also
|
||||
** safely ignore the index on the permanent table.
|
||||
*/
|
||||
/* Do Nothing */;
|
||||
}else{
|
||||
pIndex->tnum = atoi(argv[2]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
/* This can not happen! */
|
||||
nErr = 1;
|
||||
assert( nErr==0 );
|
||||
}
|
||||
}
|
||||
return nErr;
|
||||
}
|
||||
|
||||
/*
|
||||
** This is a callback procedure used to reconstruct a table. The
|
||||
** name of the table to be reconstructed is passed in as argv[0].
|
||||
**
|
||||
** This routine is used to automatically upgrade a database from
|
||||
** format version 1 or 2 to version 3. The correct operation of
|
||||
** this routine relys on the fact that no indices are used when
|
||||
** copying a table out to a temporary file.
|
||||
*/
|
||||
static
|
||||
int upgrade_3_callback(void *pInit, int argc, char **argv, char **NotUsed){
|
||||
InitData *pData = (InitData*)pInit;
|
||||
int rc;
|
||||
Table *pTab;
|
||||
Trigger *pTrig;
|
||||
char *zErr = 0;
|
||||
|
||||
pTab = sqliteFindTable(pData->db, argv[0], 0);
|
||||
assert( pTab!=0 );
|
||||
assert( sqliteStrICmp(pTab->zName, argv[0])==0 );
|
||||
if( pTab ){
|
||||
pTrig = pTab->pTrigger;
|
||||
pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */
|
||||
}
|
||||
rc = sqlite_exec_printf(pData->db,
|
||||
"CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; "
|
||||
"DELETE FROM '%q'; "
|
||||
"INSERT INTO '%q' SELECT * FROM sqlite_x; "
|
||||
"DROP TABLE sqlite_x;",
|
||||
0, 0, &zErr, argv[0], argv[0], argv[0]);
|
||||
if( zErr ){
|
||||
sqliteSetString(pData->pzErrMsg, zErr, 0);
|
||||
sqlite_freemem(zErr);
|
||||
}
|
||||
|
||||
/* If an error occurred in the SQL above, then the transaction will
|
||||
** rollback which will delete the internal symbol tables. This will
|
||||
** cause the structure that pTab points to be deleted. In case that
|
||||
** happened, we need to refetch pTab.
|
||||
*/
|
||||
pTab = sqliteFindTable(pData->db, argv[0], 0);
|
||||
if( pTab ){
|
||||
assert( sqliteStrICmp(pTab->zName, argv[0])==0 );
|
||||
pTab->pTrigger = pTrig; /* Re-enable triggers */
|
||||
}
|
||||
return rc!=SQLITE_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Attempt to read the database schema and initialize internal
|
||||
** data structures for a single database file. The index of the
|
||||
** database file is given by iDb. iDb==0 is used for the main
|
||||
** database. iDb==1 should never be used. iDb>=2 is used for
|
||||
** auxiliary databases. Return one of the SQLITE_ error codes to
|
||||
** indicate success or failure.
|
||||
*/
|
||||
static int sqliteInitOne(sqlite *db, int iDb, char **pzErrMsg){
|
||||
int rc;
|
||||
BtCursor *curMain;
|
||||
int size;
|
||||
Table *pTab;
|
||||
char *azArg[6];
|
||||
char zDbNum[30];
|
||||
int meta[SQLITE_N_BTREE_META];
|
||||
Parse sParse;
|
||||
InitData initData;
|
||||
|
||||
/*
|
||||
** The master database table has a structure like this
|
||||
*/
|
||||
static char master_schema[] =
|
||||
"CREATE TABLE sqlite_master(\n"
|
||||
" type text,\n"
|
||||
" name text,\n"
|
||||
" tbl_name text,\n"
|
||||
" rootpage integer,\n"
|
||||
" sql text\n"
|
||||
")"
|
||||
;
|
||||
static char temp_master_schema[] =
|
||||
"CREATE TEMP TABLE sqlite_temp_master(\n"
|
||||
" type text,\n"
|
||||
" name text,\n"
|
||||
" tbl_name text,\n"
|
||||
" rootpage integer,\n"
|
||||
" sql text\n"
|
||||
")"
|
||||
;
|
||||
|
||||
/* The following SQL will read the schema from the master tables.
|
||||
** The first version works with SQLite file formats 2 or greater.
|
||||
** The second version is for format 1 files.
|
||||
**
|
||||
** Beginning with file format 2, the rowid for new table entries
|
||||
** (including entries in sqlite_master) is an increasing integer.
|
||||
** So for file format 2 and later, we can play back sqlite_master
|
||||
** and all the CREATE statements will appear in the right order.
|
||||
** But with file format 1, table entries were random and so we
|
||||
** have to make sure the CREATE TABLEs occur before their corresponding
|
||||
** CREATE INDEXs. (We don't have to deal with CREATE VIEW or
|
||||
** CREATE TRIGGER in file format 1 because those constructs did
|
||||
** not exist then.)
|
||||
*/
|
||||
static char init_script[] =
|
||||
"SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
|
||||
"UNION ALL "
|
||||
"SELECT type, name, rootpage, sql, 0 FROM sqlite_master";
|
||||
static char older_init_script[] =
|
||||
"SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
|
||||
"UNION ALL "
|
||||
"SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
|
||||
"WHERE type='table' "
|
||||
"UNION ALL "
|
||||
"SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
|
||||
"WHERE type='index'";
|
||||
|
||||
|
||||
assert( iDb>=0 && iDb!=1 && iDb<db->nDb );
|
||||
|
||||
/* Construct the schema tables: sqlite_master and sqlite_temp_master
|
||||
*/
|
||||
azArg[0] = "table";
|
||||
azArg[1] = MASTER_NAME;
|
||||
azArg[2] = "2";
|
||||
azArg[3] = master_schema;
|
||||
sprintf(zDbNum, "%d", iDb);
|
||||
azArg[4] = zDbNum;
|
||||
azArg[5] = 0;
|
||||
initData.db = db;
|
||||
initData.pzErrMsg = pzErrMsg;
|
||||
sqliteInitCallback(&initData, 5, azArg, 0);
|
||||
pTab = sqliteFindTable(db, MASTER_NAME, "main");
|
||||
if( pTab ){
|
||||
pTab->readOnly = 1;
|
||||
}
|
||||
if( iDb==0 ){
|
||||
azArg[1] = TEMP_MASTER_NAME;
|
||||
azArg[3] = temp_master_schema;
|
||||
azArg[4] = "1";
|
||||
sqliteInitCallback(&initData, 5, azArg, 0);
|
||||
pTab = sqliteFindTable(db, TEMP_MASTER_NAME, "temp");
|
||||
if( pTab ){
|
||||
pTab->readOnly = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Create a cursor to hold the database open
|
||||
*/
|
||||
if( db->aDb[iDb].pBt==0 ) return SQLITE_OK;
|
||||
rc = sqliteBtreeCursor(db->aDb[iDb].pBt, 2, 0, &curMain);
|
||||
if( rc ){
|
||||
sqliteSetString(pzErrMsg, sqlite_error_string(rc), 0);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Get the database meta information
|
||||
*/
|
||||
rc = sqliteBtreeGetMeta(db->aDb[iDb].pBt, meta);
|
||||
if( rc ){
|
||||
sqliteSetString(pzErrMsg, sqlite_error_string(rc), 0);
|
||||
sqliteBtreeCloseCursor(curMain);
|
||||
return rc;
|
||||
}
|
||||
db->aDb[iDb].schema_cookie = meta[1];
|
||||
if( iDb==0 ){
|
||||
db->next_cookie = meta[1];
|
||||
db->file_format = meta[2];
|
||||
size = meta[3];
|
||||
if( size==0 ){ size = MAX_PAGES; }
|
||||
db->cache_size = size;
|
||||
db->safety_level = meta[4];
|
||||
if( db->safety_level==0 ) db->safety_level = 2;
|
||||
|
||||
/*
|
||||
** file_format==1 Version 2.1.0.
|
||||
** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY.
|
||||
** file_format==3 Version 2.6.0. Fix empty-string index bug.
|
||||
** file_format==4 Version 2.7.0. Add support for separate numeric and
|
||||
** text datatypes.
|
||||
*/
|
||||
if( db->file_format==0 ){
|
||||
/* This happens if the database was initially empty */
|
||||
db->file_format = 4;
|
||||
}else if( db->file_format>4 ){
|
||||
sqliteBtreeCloseCursor(curMain);
|
||||
sqliteSetString(pzErrMsg, "unsupported file format", 0);
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
}else if( db->file_format!=meta[2] || db->file_format<4 ){
|
||||
assert( db->file_format>=4 );
|
||||
if( meta[2]==0 ){
|
||||
sqliteSetString(pzErrMsg, "cannot attach empty database: ",
|
||||
db->aDb[iDb].zName, 0);
|
||||
}else{
|
||||
sqliteSetString(pzErrMsg, "incompatible file format in auxiliary "
|
||||
"database: ", db->aDb[iDb].zName, 0);
|
||||
}
|
||||
sqliteBtreeClose(db->aDb[iDb].pBt);
|
||||
db->aDb[iDb].pBt = 0;
|
||||
return SQLITE_FORMAT;
|
||||
}
|
||||
sqliteBtreeSetCacheSize(db->aDb[iDb].pBt, db->cache_size);
|
||||
sqliteBtreeSetSafetyLevel(db->aDb[iDb].pBt, meta[4]==0 ? 2 : meta[4]);
|
||||
|
||||
/* Read the schema information out of the schema tables
|
||||
*/
|
||||
memset(&sParse, 0, sizeof(sParse));
|
||||
sParse.db = db;
|
||||
sParse.xCallback = sqliteInitCallback;
|
||||
sParse.pArg = (void*)&initData;
|
||||
sParse.initFlag = 1;
|
||||
sParse.useCallback = 1;
|
||||
if( iDb==0 ){
|
||||
sqliteRunParser(&sParse,
|
||||
db->file_format>=2 ? init_script : older_init_script,
|
||||
pzErrMsg);
|
||||
}else{
|
||||
char *zSql = 0;
|
||||
sqliteSetString(&zSql,
|
||||
"SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"",
|
||||
db->aDb[iDb].zName, "\".sqlite_master", 0);
|
||||
sqliteRunParser(&sParse, zSql, pzErrMsg);
|
||||
sqliteFree(zSql);
|
||||
}
|
||||
sqliteBtreeCloseCursor(curMain);
|
||||
if( sqlite_malloc_failed ){
|
||||
sqliteSetString(pzErrMsg, "out of memory", 0);
|
||||
sParse.rc = SQLITE_NOMEM;
|
||||
sqliteResetInternalSchema(db, 0);
|
||||
}
|
||||
if( sParse.rc==SQLITE_OK ){
|
||||
DbSetProperty(db, iDb, DB_SchemaLoaded);
|
||||
if( iDb==0 ){
|
||||
DbSetProperty(db, 1, DB_SchemaLoaded);
|
||||
}
|
||||
}else{
|
||||
sqliteResetInternalSchema(db, iDb);
|
||||
}
|
||||
return sParse.rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Initialize all database files - the main database file, the file
|
||||
** used to store temporary tables, and any additional database files
|
||||
** created using ATTACH statements. Return a success code. If an
|
||||
** error occurs, write an error message into *pzErrMsg.
|
||||
**
|
||||
** After the database is initialized, the SQLITE_Initialized
|
||||
** bit is set in the flags field of the sqlite structure. An
|
||||
** attempt is made to initialize the database as soon as it
|
||||
** is opened. If that fails (perhaps because another process
|
||||
** has the sqlite_master table locked) than another attempt
|
||||
** is made the first time the database is accessed.
|
||||
*/
|
||||
int sqliteInit(sqlite *db, char **pzErrMsg){
|
||||
int i, rc;
|
||||
|
||||
assert( (db->flags & SQLITE_Initialized)==0 );
|
||||
rc = SQLITE_OK;
|
||||
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
|
||||
if( DbHasProperty(db, i, DB_SchemaLoaded) ) continue;
|
||||
assert( i!=1 ); /* Should have been initialized together with 0 */
|
||||
rc = sqliteInitOne(db, i, pzErrMsg);
|
||||
}
|
||||
if( rc==SQLITE_OK ){
|
||||
db->flags |= SQLITE_Initialized;
|
||||
sqliteCommitInternalChanges(db);
|
||||
}else{
|
||||
db->flags &= ~SQLITE_Initialized;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** The version of the library
|
||||
*/
|
||||
const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
|
||||
const char sqlite_version[] = SQLITE_VERSION;
|
||||
|
||||
/*
|
||||
** Does the library expect data to be encoded as UTF-8 or iso8859? The
|
||||
** following global constant always lets us know.
|
||||
*/
|
||||
#ifdef SQLITE_UTF8
|
||||
const char sqlite_encoding[] = "UTF-8";
|
||||
#else
|
||||
const char sqlite_encoding[] = "iso8859";
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Open a new SQLite database. Construct an "sqlite" structure to define
|
||||
** the state of this database and return a pointer to that structure.
|
||||
**
|
||||
** An attempt is made to initialize the in-memory data structures that
|
||||
** hold the database schema. But if this fails (because the schema file
|
||||
** is locked) then that step is deferred until the first call to
|
||||
** sqlite_exec().
|
||||
*/
|
||||
sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
|
||||
sqlite *db;
|
||||
int rc, i;
|
||||
|
||||
/* Allocate the sqlite data structure */
|
||||
db = sqliteMalloc( sizeof(sqlite) );
|
||||
if( pzErrMsg ) *pzErrMsg = 0;
|
||||
if( db==0 ) goto no_mem_on_open;
|
||||
db->onError = OE_Default;
|
||||
db->priorNewRowid = 0;
|
||||
db->magic = SQLITE_MAGIC_BUSY;
|
||||
db->nDb = 2;
|
||||
db->aDb = db->aDbStatic;
|
||||
sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
|
||||
for(i=0; i<db->nDb; i++){
|
||||
sqliteHashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
|
||||
sqliteHashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
|
||||
sqliteHashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
|
||||
sqliteHashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
|
||||
}
|
||||
|
||||
/* Open the backend database driver */
|
||||
if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){
|
||||
db->temp_store = 2;
|
||||
}
|
||||
rc = sqliteBtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
|
||||
if( rc!=SQLITE_OK ){
|
||||
switch( rc ){
|
||||
default: {
|
||||
sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
|
||||
}
|
||||
}
|
||||
sqliteFree(db);
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
return 0;
|
||||
}
|
||||
db->aDb[0].zName = "main";
|
||||
db->aDb[1].zName = "temp";
|
||||
|
||||
/* Attempt to read the schema */
|
||||
sqliteRegisterBuiltinFunctions(db);
|
||||
rc = sqliteInit(db, pzErrMsg);
|
||||
db->magic = SQLITE_MAGIC_OPEN;
|
||||
if( sqlite_malloc_failed ){
|
||||
sqlite_close(db);
|
||||
goto no_mem_on_open;
|
||||
}else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
|
||||
sqlite_close(db);
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
return 0;
|
||||
}else if( pzErrMsg ){
|
||||
sqliteFree(*pzErrMsg);
|
||||
*pzErrMsg = 0;
|
||||
}
|
||||
|
||||
/* If the database is in formats 1 or 2, then upgrade it to
|
||||
** version 3. This will reconstruct all indices. If the
|
||||
** upgrade fails for any reason (ex: out of disk space, database
|
||||
** is read only, interrupt received, etc.) then refuse to open.
|
||||
*/
|
||||
if( rc==SQLITE_OK && db->file_format<3 ){
|
||||
char *zErr = 0;
|
||||
InitData initData;
|
||||
int meta[SQLITE_N_BTREE_META];
|
||||
|
||||
initData.db = db;
|
||||
initData.pzErrMsg = &zErr;
|
||||
db->file_format = 3;
|
||||
rc = sqlite_exec(db,
|
||||
"BEGIN; SELECT name FROM sqlite_master WHERE type='table';",
|
||||
upgrade_3_callback,
|
||||
&initData,
|
||||
&zErr);
|
||||
if( rc==SQLITE_OK ){
|
||||
sqliteBtreeGetMeta(db->aDb[0].pBt, meta);
|
||||
meta[2] = 4;
|
||||
sqliteBtreeUpdateMeta(db->aDb[0].pBt, meta);
|
||||
sqlite_exec(db, "COMMIT", 0, 0, 0);
|
||||
}
|
||||
if( rc!=SQLITE_OK ){
|
||||
sqliteSetString(pzErrMsg,
|
||||
"unable to upgrade database to the version 2.6 format",
|
||||
zErr ? ": " : 0, zErr, 0);
|
||||
sqlite_freemem(zErr);
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
sqlite_close(db);
|
||||
return 0;
|
||||
}
|
||||
sqlite_freemem(zErr);
|
||||
}
|
||||
|
||||
/* Return a pointer to the newly opened database structure */
|
||||
return db;
|
||||
|
||||
no_mem_on_open:
|
||||
sqliteSetString(pzErrMsg, "out of memory", 0);
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return the ROWID of the most recent insert
|
||||
*/
|
||||
int sqlite_last_insert_rowid(sqlite *db){
|
||||
return db->lastRowid;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return the number of changes in the most recent call to sqlite_exec().
|
||||
*/
|
||||
int sqlite_changes(sqlite *db){
|
||||
return db->nChange;
|
||||
}
|
||||
|
||||
/*
|
||||
** Close an existing SQLite database
|
||||
*/
|
||||
void sqlite_close(sqlite *db){
|
||||
HashElem *i;
|
||||
int j;
|
||||
db->want_to_close = 1;
|
||||
if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){
|
||||
/* printf("DID NOT CLOSE\n"); fflush(stdout); */
|
||||
return;
|
||||
}
|
||||
db->magic = SQLITE_MAGIC_CLOSED;
|
||||
for(j=0; j<db->nDb; j++){
|
||||
if( db->aDb[j].pBt ){
|
||||
sqliteBtreeClose(db->aDb[j].pBt);
|
||||
db->aDb[j].pBt = 0;
|
||||
}
|
||||
if( j>=2 ){
|
||||
sqliteFree(db->aDb[j].zName);
|
||||
db->aDb[j].zName = 0;
|
||||
}
|
||||
}
|
||||
sqliteResetInternalSchema(db, 0);
|
||||
assert( db->nDb<=2 );
|
||||
assert( db->aDb==db->aDbStatic );
|
||||
for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
|
||||
FuncDef *pFunc, *pNext;
|
||||
for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
|
||||
pNext = pFunc->pNext;
|
||||
sqliteFree(pFunc);
|
||||
}
|
||||
}
|
||||
sqliteHashClear(&db->aFunc);
|
||||
sqliteFree(db);
|
||||
}
|
||||
|
||||
/*
|
||||
** Rollback all database files.
|
||||
*/
|
||||
void sqliteRollbackAll(sqlite *db){
|
||||
int i;
|
||||
for(i=0; i<db->nDb; i++){
|
||||
if( db->aDb[i].pBt ){
|
||||
sqliteBtreeRollback(db->aDb[i].pBt);
|
||||
db->aDb[i].inTrans = 0;
|
||||
}
|
||||
}
|
||||
sqliteRollbackInternalChanges(db);
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine does the work of either sqlite_exec() or sqlite_compile().
|
||||
** It works like sqlite_exec() if pVm==NULL and it works like sqlite_compile()
|
||||
** otherwise.
|
||||
*/
|
||||
static int sqliteMain(
|
||||
sqlite *db, /* The database on which the SQL executes */
|
||||
const char *zSql, /* The SQL to be executed */
|
||||
sqlite_callback xCallback, /* Invoke this callback routine */
|
||||
void *pArg, /* First argument to xCallback() */
|
||||
const char **pzTail, /* OUT: Next statement after the first */
|
||||
sqlite_vm **ppVm, /* OUT: The virtual machine */
|
||||
char **pzErrMsg /* OUT: Write error messages here */
|
||||
){
|
||||
Parse sParse;
|
||||
|
||||
if( pzErrMsg ) *pzErrMsg = 0;
|
||||
if( sqliteSafetyOn(db) ) goto exec_misuse;
|
||||
if( (db->flags & SQLITE_Initialized)==0 ){
|
||||
int rc, cnt = 1;
|
||||
while( (rc = sqliteInit(db, pzErrMsg))==SQLITE_BUSY
|
||||
&& db->xBusyCallback && db->xBusyCallback(db->pBusyArg, "", cnt++)!=0 ){}
|
||||
if( rc!=SQLITE_OK ){
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
sqliteSafetyOff(db);
|
||||
return rc;
|
||||
}
|
||||
if( pzErrMsg ){
|
||||
sqliteFree(*pzErrMsg);
|
||||
*pzErrMsg = 0;
|
||||
}
|
||||
}
|
||||
if( db->file_format<3 ){
|
||||
sqliteSafetyOff(db);
|
||||
sqliteSetString(pzErrMsg, "obsolete database file format", 0);
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
if( db->pVdbe==0 ){ db->nChange = 0; }
|
||||
memset(&sParse, 0, sizeof(sParse));
|
||||
sParse.db = db;
|
||||
sParse.xCallback = xCallback;
|
||||
sParse.pArg = pArg;
|
||||
sParse.useCallback = ppVm==0;
|
||||
if( db->xTrace ) db->xTrace(db->pTraceArg, zSql);
|
||||
sqliteRunParser(&sParse, zSql, pzErrMsg);
|
||||
if( sqlite_malloc_failed ){
|
||||
sqliteSetString(pzErrMsg, "out of memory", 0);
|
||||
sParse.rc = SQLITE_NOMEM;
|
||||
sqliteRollbackAll(db);
|
||||
sqliteResetInternalSchema(db, 0);
|
||||
db->flags &= ~SQLITE_InTrans;
|
||||
}
|
||||
if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
|
||||
if( sParse.rc!=SQLITE_OK && pzErrMsg && *pzErrMsg==0 ){
|
||||
sqliteSetString(pzErrMsg, sqlite_error_string(sParse.rc), 0);
|
||||
}
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
if( sParse.rc==SQLITE_SCHEMA ){
|
||||
sqliteResetInternalSchema(db, 0);
|
||||
}
|
||||
if( sParse.useCallback==0 ){
|
||||
assert( ppVm );
|
||||
*ppVm = (sqlite_vm*)sParse.pVdbe;
|
||||
*pzTail = sParse.zTail;
|
||||
}
|
||||
if( sqliteSafetyOff(db) ) goto exec_misuse;
|
||||
return sParse.rc;
|
||||
|
||||
exec_misuse:
|
||||
if( pzErrMsg ){
|
||||
*pzErrMsg = 0;
|
||||
sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
}
|
||||
return SQLITE_MISUSE;
|
||||
}
|
||||
|
||||
/*
|
||||
** Execute SQL code. Return one of the SQLITE_ success/failure
|
||||
** codes. Also write an error message into memory obtained from
|
||||
** malloc() and make *pzErrMsg point to that message.
|
||||
**
|
||||
** If the SQL is a query, then for each row in the query result
|
||||
** the xCallback() function is called. pArg becomes the first
|
||||
** argument to xCallback(). If xCallback=NULL then no callback
|
||||
** is invoked, even for queries.
|
||||
*/
|
||||
int sqlite_exec(
|
||||
sqlite *db, /* The database on which the SQL executes */
|
||||
const char *zSql, /* The SQL to be executed */
|
||||
sqlite_callback xCallback, /* Invoke this callback routine */
|
||||
void *pArg, /* First argument to xCallback() */
|
||||
char **pzErrMsg /* Write error messages here */
|
||||
){
|
||||
return sqliteMain(db, zSql, xCallback, pArg, 0, 0, pzErrMsg);
|
||||
}
|
||||
|
||||
/*
|
||||
** Compile a single statement of SQL into a virtual machine. Return one
|
||||
** of the SQLITE_ success/failure codes. Also write an error message into
|
||||
** memory obtained from malloc() and make *pzErrMsg point to that message.
|
||||
*/
|
||||
int sqlite_compile(
|
||||
sqlite *db, /* The database on which the SQL executes */
|
||||
const char *zSql, /* The SQL to be executed */
|
||||
const char **pzTail, /* OUT: Next statement after the first */
|
||||
sqlite_vm **ppVm, /* OUT: The virtual machine */
|
||||
char **pzErrMsg /* OUT: Write error messages here */
|
||||
){
|
||||
return sqliteMain(db, zSql, 0, 0, pzTail, ppVm, pzErrMsg);
|
||||
}
|
||||
|
||||
/*
|
||||
** The following routine destroys a virtual machine that is created by
|
||||
** the sqlite_compile() routine.
|
||||
**
|
||||
** The integer returned is an SQLITE_ success/failure code that describes
|
||||
** the result of executing the virtual machine. An error message is
|
||||
** written into memory obtained from malloc and *pzErrMsg is made to
|
||||
** point to that error if pzErrMsg is not NULL. The calling routine
|
||||
** should use sqlite_freemem() to delete the message when it has finished
|
||||
** with it.
|
||||
*/
|
||||
int sqlite_finalize(
|
||||
sqlite_vm *pVm, /* The virtual machine to be destroyed */
|
||||
char **pzErrMsg /* OUT: Write error messages here */
|
||||
){
|
||||
int rc = sqliteVdbeFinalize((Vdbe*)pVm, pzErrMsg);
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return a static string that describes the kind of error specified in the
|
||||
** argument.
|
||||
*/
|
||||
const char *sqlite_error_string(int rc){
|
||||
const char *z;
|
||||
switch( rc ){
|
||||
case SQLITE_OK: z = "not an error"; break;
|
||||
case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
|
||||
case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
|
||||
case SQLITE_PERM: z = "access permission denied"; break;
|
||||
case SQLITE_ABORT: z = "callback requested query abort"; break;
|
||||
case SQLITE_BUSY: z = "database is locked"; break;
|
||||
case SQLITE_LOCKED: z = "database table is locked"; break;
|
||||
case SQLITE_NOMEM: z = "out of memory"; break;
|
||||
case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
|
||||
case SQLITE_INTERRUPT: z = "interrupted"; break;
|
||||
case SQLITE_IOERR: z = "disk I/O error"; break;
|
||||
case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
|
||||
case SQLITE_NOTFOUND: z = "table or record not found"; break;
|
||||
case SQLITE_FULL: z = "database is full"; break;
|
||||
case SQLITE_CANTOPEN: z = "unable to open database file"; break;
|
||||
case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
|
||||
case SQLITE_EMPTY: z = "table contains no data"; break;
|
||||
case SQLITE_SCHEMA: z = "database schema has changed"; break;
|
||||
case SQLITE_TOOBIG: z = "too much data for one table row"; break;
|
||||
case SQLITE_CONSTRAINT: z = "constraint failed"; break;
|
||||
case SQLITE_MISMATCH: z = "datatype mismatch"; break;
|
||||
case SQLITE_MISUSE: z = "library routine called out of sequence";break;
|
||||
case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
|
||||
case SQLITE_AUTH: z = "authorization denied"; break;
|
||||
default: z = "unknown error"; break;
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine implements a busy callback that sleeps and tries
|
||||
** again until a timeout value is reached. The timeout value is
|
||||
** an integer number of milliseconds passed in as the first
|
||||
** argument.
|
||||
*/
|
||||
static int sqliteDefaultBusyCallback(
|
||||
void *Timeout, /* Maximum amount of time to wait */
|
||||
const char *NotUsed, /* The name of the table that is busy */
|
||||
int count /* Number of times table has been busy */
|
||||
){
|
||||
#if SQLITE_MIN_SLEEP_MS==1
|
||||
int delay = 10;
|
||||
int prior_delay = 0;
|
||||
int timeout = (int)Timeout;
|
||||
int i;
|
||||
|
||||
for(i=1; i<count; i++){
|
||||
prior_delay += delay;
|
||||
delay = delay*2;
|
||||
if( delay>=1000 ){
|
||||
delay = 1000;
|
||||
prior_delay += 1000*(count - i - 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( prior_delay + delay > timeout ){
|
||||
delay = timeout - prior_delay;
|
||||
if( delay<=0 ) return 0;
|
||||
}
|
||||
sqliteOsSleep(delay);
|
||||
return 1;
|
||||
#else
|
||||
int timeout = (int)Timeout;
|
||||
if( (count+1)*1000 > timeout ){
|
||||
return 0;
|
||||
}
|
||||
sqliteOsSleep(1000);
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine sets the busy callback for an Sqlite database to the
|
||||
** given callback function with the given argument.
|
||||
*/
|
||||
void sqlite_busy_handler(
|
||||
sqlite *db,
|
||||
int (*xBusy)(void*,const char*,int),
|
||||
void *pArg
|
||||
){
|
||||
db->xBusyCallback = xBusy;
|
||||
db->pBusyArg = pArg;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine installs a default busy handler that waits for the
|
||||
** specified number of milliseconds before returning 0.
|
||||
*/
|
||||
void sqlite_busy_timeout(sqlite *db, int ms){
|
||||
if( ms>0 ){
|
||||
sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
|
||||
}else{
|
||||
sqlite_busy_handler(db, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Cause any pending operation to stop at its earliest opportunity.
|
||||
*/
|
||||
void sqlite_interrupt(sqlite *db){
|
||||
db->flags |= SQLITE_Interrupt;
|
||||
}
|
||||
|
||||
/*
|
||||
** Windows systems should call this routine to free memory that
|
||||
** is returned in the in the errmsg parameter of sqlite_open() when
|
||||
** SQLite is a DLL. For some reason, it does not work to call free()
|
||||
** directly.
|
||||
**
|
||||
** Note that we need to call free() not sqliteFree() here, since every
|
||||
** string that is exported from SQLite should have already passed through
|
||||
** sqliteStrRealloc().
|
||||
*/
|
||||
void sqlite_freemem(void *p){ free(p); }
|
||||
|
||||
/*
|
||||
** Windows systems need functions to call to return the sqlite_version
|
||||
** and sqlite_encoding strings since they are unable to access constants
|
||||
** within DLLs.
|
||||
*/
|
||||
const char *sqlite_libversion(void){ return sqlite_version; }
|
||||
const char *sqlite_libencoding(void){ return sqlite_encoding; }
|
||||
|
||||
/*
|
||||
** Create new user-defined functions. The sqlite_create_function()
|
||||
** routine creates a regular function and sqlite_create_aggregate()
|
||||
** creates an aggregate function.
|
||||
**
|
||||
** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
|
||||
** disables the function. Calling sqlite_create_function() with the
|
||||
** same name and number of arguments as a prior call to
|
||||
** sqlite_create_aggregate() disables the prior call to
|
||||
** sqlite_create_aggregate(), and vice versa.
|
||||
**
|
||||
** If nArg is -1 it means that this function will accept any number
|
||||
** of arguments, including 0.
|
||||
*/
|
||||
int sqlite_create_function(
|
||||
sqlite *db, /* Add the function to this database connection */
|
||||
const char *zName, /* Name of the function to add */
|
||||
int nArg, /* Number of arguments */
|
||||
void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
|
||||
void *pUserData /* User data */
|
||||
){
|
||||
FuncDef *p;
|
||||
int nName;
|
||||
if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
|
||||
nName = strlen(zName);
|
||||
if( nName>255 ) return 1;
|
||||
p = sqliteFindFunction(db, zName, nName, nArg, 1);
|
||||
if( p==0 ) return 1;
|
||||
p->xFunc = xFunc;
|
||||
p->xStep = 0;
|
||||
p->xFinalize = 0;
|
||||
p->pUserData = pUserData;
|
||||
return 0;
|
||||
}
|
||||
int sqlite_create_aggregate(
|
||||
sqlite *db, /* Add the function to this database connection */
|
||||
const char *zName, /* Name of the function to add */
|
||||
int nArg, /* Number of arguments */
|
||||
void (*xStep)(sqlite_func*,int,const char**), /* The step function */
|
||||
void (*xFinalize)(sqlite_func*), /* The finalizer */
|
||||
void *pUserData /* User data */
|
||||
){
|
||||
FuncDef *p;
|
||||
int nName;
|
||||
if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
|
||||
nName = strlen(zName);
|
||||
if( nName>255 ) return 1;
|
||||
p = sqliteFindFunction(db, zName, nName, nArg, 1);
|
||||
if( p==0 ) return 1;
|
||||
p->xFunc = 0;
|
||||
p->xStep = xStep;
|
||||
p->xFinalize = xFinalize;
|
||||
p->pUserData = pUserData;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Change the datatype for all functions with a given name. See the
|
||||
** header comment for the prototype of this function in sqlite.h for
|
||||
** additional information.
|
||||
*/
|
||||
int sqlite_function_type(sqlite *db, const char *zName, int dataType){
|
||||
FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName));
|
||||
while( p ){
|
||||
p->dataType = dataType;
|
||||
p = p->pNext;
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Register a trace function. The pArg from the previously registered trace
|
||||
** is returned.
|
||||
**
|
||||
** A NULL trace function means that no tracing is executes. A non-NULL
|
||||
** trace is a pointer to a function that is invoked at the start of each
|
||||
** sqlite_exec().
|
||||
*/
|
||||
void *sqlite_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){
|
||||
void *pOld = db->pTraceArg;
|
||||
db->xTrace = xTrace;
|
||||
db->pTraceArg = pArg;
|
||||
return pOld;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine is called to create a connection to a database BTree
|
||||
** driver. If zFilename is the name of a file, then that file is
|
||||
** opened and used. If zFilename is the magic name ":memory:" then
|
||||
** the database is stored in memory (and is thus forgotten as soon as
|
||||
** the connection is closed.) If zFilename is NULL then the database
|
||||
** is for temporary use only and is deleted as soon as the connection
|
||||
** is closed.
|
||||
**
|
||||
** A temporary database can be either a disk file (that is automatically
|
||||
** deleted when the file is closed) or a set of red-black trees held in memory,
|
||||
** depending on the values of the TEMP_STORE compile-time macro and the
|
||||
** db->temp_store variable, according to the following chart:
|
||||
**
|
||||
** TEMP_STORE db->temp_store Location of temporary database
|
||||
** ---------- -------------- ------------------------------
|
||||
** 0 any file
|
||||
** 1 1 file
|
||||
** 1 2 memory
|
||||
** 1 0 file
|
||||
** 2 1 file
|
||||
** 2 2 memory
|
||||
** 2 0 memory
|
||||
** 3 any memory
|
||||
*/
|
||||
int sqliteBtreeFactory(
|
||||
const sqlite *db, /* Main database when opening aux otherwise 0 */
|
||||
const char *zFilename, /* Name of the file containing the BTree database */
|
||||
int omitJournal, /* if TRUE then do not journal this file */
|
||||
int nCache, /* How many pages in the page cache */
|
||||
Btree **ppBtree){ /* Pointer to new Btree object written here */
|
||||
|
||||
assert( ppBtree != 0);
|
||||
|
||||
#ifndef SQLITE_OMIT_INMEMORYDB
|
||||
if( zFilename==0 ){
|
||||
if (TEMP_STORE == 0) {
|
||||
/* Always use file based temporary DB */
|
||||
return sqliteBtreeOpen(0, omitJournal, nCache, ppBtree);
|
||||
} else if (TEMP_STORE == 1 || TEMP_STORE == 2) {
|
||||
/* Switch depending on compile-time and/or runtime settings. */
|
||||
int location = db->temp_store==0 ? TEMP_STORE : db->temp_store;
|
||||
|
||||
if (location == 1) {
|
||||
return sqliteBtreeOpen(zFilename, omitJournal, nCache, ppBtree);
|
||||
} else {
|
||||
return sqliteRbtreeOpen(0, 0, 0, ppBtree);
|
||||
}
|
||||
} else {
|
||||
/* Always use in-core DB */
|
||||
return sqliteRbtreeOpen(0, 0, 0, ppBtree);
|
||||
}
|
||||
}else if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){
|
||||
return sqliteRbtreeOpen(0, 0, 0, ppBtree);
|
||||
}else
|
||||
#endif
|
||||
{
|
||||
return sqliteBtreeOpen(zFilename, omitJournal, nCache, ppBtree);
|
||||
}
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
/* Automatically generated file. Do not edit */
|
||||
char *sqliteOpcodeNames[] = { "???",
|
||||
"Goto",
|
||||
"Gosub",
|
||||
"Return",
|
||||
"Halt",
|
||||
"Integer",
|
||||
"String",
|
||||
"Pop",
|
||||
"Dup",
|
||||
"Pull",
|
||||
"Push",
|
||||
"ColumnName",
|
||||
"Callback",
|
||||
"NullCallback",
|
||||
"Concat",
|
||||
"Add",
|
||||
"Subtract",
|
||||
"Multiply",
|
||||
"Divide",
|
||||
"Remainder",
|
||||
"Function",
|
||||
"BitAnd",
|
||||
"BitOr",
|
||||
"ShiftLeft",
|
||||
"ShiftRight",
|
||||
"AddImm",
|
||||
"MustBeInt",
|
||||
"Eq",
|
||||
"Ne",
|
||||
"Lt",
|
||||
"Le",
|
||||
"Gt",
|
||||
"Ge",
|
||||
"StrEq",
|
||||
"StrNe",
|
||||
"StrLt",
|
||||
"StrLe",
|
||||
"StrGt",
|
||||
"StrGe",
|
||||
"And",
|
||||
"Or",
|
||||
"Negative",
|
||||
"AbsValue",
|
||||
"Not",
|
||||
"BitNot",
|
||||
"Noop",
|
||||
"If",
|
||||
"IfNot",
|
||||
"IsNull",
|
||||
"NotNull",
|
||||
"MakeRecord",
|
||||
"MakeIdxKey",
|
||||
"MakeKey",
|
||||
"IncrKey",
|
||||
"Checkpoint",
|
||||
"Transaction",
|
||||
"Commit",
|
||||
"Rollback",
|
||||
"ReadCookie",
|
||||
"SetCookie",
|
||||
"VerifyCookie",
|
||||
"OpenRead",
|
||||
"OpenWrite",
|
||||
"OpenTemp",
|
||||
"OpenPseudo",
|
||||
"Close",
|
||||
"MoveLt",
|
||||
"MoveTo",
|
||||
"Distinct",
|
||||
"NotFound",
|
||||
"Found",
|
||||
"IsUnique",
|
||||
"NotExists",
|
||||
"NewRecno",
|
||||
"PutIntKey",
|
||||
"PutStrKey",
|
||||
"Delete",
|
||||
"KeyAsData",
|
||||
"RowData",
|
||||
"Column",
|
||||
"Recno",
|
||||
"FullKey",
|
||||
"NullRow",
|
||||
"Last",
|
||||
"Rewind",
|
||||
"Prev",
|
||||
"Next",
|
||||
"IdxPut",
|
||||
"IdxDelete",
|
||||
"IdxRecno",
|
||||
"IdxLT",
|
||||
"IdxGT",
|
||||
"IdxGE",
|
||||
"Destroy",
|
||||
"Clear",
|
||||
"CreateIndex",
|
||||
"CreateTable",
|
||||
"IntegrityCk",
|
||||
"ListWrite",
|
||||
"ListRewind",
|
||||
"ListRead",
|
||||
"ListReset",
|
||||
"ListPush",
|
||||
"ListPop",
|
||||
"SortPut",
|
||||
"SortMakeRec",
|
||||
"SortMakeKey",
|
||||
"Sort",
|
||||
"SortNext",
|
||||
"SortCallback",
|
||||
"SortReset",
|
||||
"FileOpen",
|
||||
"FileRead",
|
||||
"FileColumn",
|
||||
"MemStore",
|
||||
"MemLoad",
|
||||
"MemIncr",
|
||||
"AggReset",
|
||||
"AggInit",
|
||||
"AggFunc",
|
||||
"AggFocus",
|
||||
"AggSet",
|
||||
"AggGet",
|
||||
"AggNext",
|
||||
"SetInsert",
|
||||
"SetFound",
|
||||
"SetNotFound",
|
||||
"SetFirst",
|
||||
"SetNext",
|
||||
};
|
||||
@@ -1,129 +0,0 @@
|
||||
/* Automatically generated file. Do not edit */
|
||||
#define OP_Goto 1
|
||||
#define OP_Gosub 2
|
||||
#define OP_Return 3
|
||||
#define OP_Halt 4
|
||||
#define OP_Integer 5
|
||||
#define OP_String 6
|
||||
#define OP_Pop 7
|
||||
#define OP_Dup 8
|
||||
#define OP_Pull 9
|
||||
#define OP_Push 10
|
||||
#define OP_ColumnName 11
|
||||
#define OP_Callback 12
|
||||
#define OP_NullCallback 13
|
||||
#define OP_Concat 14
|
||||
#define OP_Add 15
|
||||
#define OP_Subtract 16
|
||||
#define OP_Multiply 17
|
||||
#define OP_Divide 18
|
||||
#define OP_Remainder 19
|
||||
#define OP_Function 20
|
||||
#define OP_BitAnd 21
|
||||
#define OP_BitOr 22
|
||||
#define OP_ShiftLeft 23
|
||||
#define OP_ShiftRight 24
|
||||
#define OP_AddImm 25
|
||||
#define OP_MustBeInt 26
|
||||
#define OP_Eq 27
|
||||
#define OP_Ne 28
|
||||
#define OP_Lt 29
|
||||
#define OP_Le 30
|
||||
#define OP_Gt 31
|
||||
#define OP_Ge 32
|
||||
#define OP_StrEq 33
|
||||
#define OP_StrNe 34
|
||||
#define OP_StrLt 35
|
||||
#define OP_StrLe 36
|
||||
#define OP_StrGt 37
|
||||
#define OP_StrGe 38
|
||||
#define OP_And 39
|
||||
#define OP_Or 40
|
||||
#define OP_Negative 41
|
||||
#define OP_AbsValue 42
|
||||
#define OP_Not 43
|
||||
#define OP_BitNot 44
|
||||
#define OP_Noop 45
|
||||
#define OP_If 46
|
||||
#define OP_IfNot 47
|
||||
#define OP_IsNull 48
|
||||
#define OP_NotNull 49
|
||||
#define OP_MakeRecord 50
|
||||
#define OP_MakeIdxKey 51
|
||||
#define OP_MakeKey 52
|
||||
#define OP_IncrKey 53
|
||||
#define OP_Checkpoint 54
|
||||
#define OP_Transaction 55
|
||||
#define OP_Commit 56
|
||||
#define OP_Rollback 57
|
||||
#define OP_ReadCookie 58
|
||||
#define OP_SetCookie 59
|
||||
#define OP_VerifyCookie 60
|
||||
#define OP_OpenRead 61
|
||||
#define OP_OpenWrite 62
|
||||
#define OP_OpenTemp 63
|
||||
#define OP_OpenPseudo 64
|
||||
#define OP_Close 65
|
||||
#define OP_MoveLt 66
|
||||
#define OP_MoveTo 67
|
||||
#define OP_Distinct 68
|
||||
#define OP_NotFound 69
|
||||
#define OP_Found 70
|
||||
#define OP_IsUnique 71
|
||||
#define OP_NotExists 72
|
||||
#define OP_NewRecno 73
|
||||
#define OP_PutIntKey 74
|
||||
#define OP_PutStrKey 75
|
||||
#define OP_Delete 76
|
||||
#define OP_KeyAsData 77
|
||||
#define OP_RowData 78
|
||||
#define OP_Column 79
|
||||
#define OP_Recno 80
|
||||
#define OP_FullKey 81
|
||||
#define OP_NullRow 82
|
||||
#define OP_Last 83
|
||||
#define OP_Rewind 84
|
||||
#define OP_Prev 85
|
||||
#define OP_Next 86
|
||||
#define OP_IdxPut 87
|
||||
#define OP_IdxDelete 88
|
||||
#define OP_IdxRecno 89
|
||||
#define OP_IdxLT 90
|
||||
#define OP_IdxGT 91
|
||||
#define OP_IdxGE 92
|
||||
#define OP_Destroy 93
|
||||
#define OP_Clear 94
|
||||
#define OP_CreateIndex 95
|
||||
#define OP_CreateTable 96
|
||||
#define OP_IntegrityCk 97
|
||||
#define OP_ListWrite 98
|
||||
#define OP_ListRewind 99
|
||||
#define OP_ListRead 100
|
||||
#define OP_ListReset 101
|
||||
#define OP_ListPush 102
|
||||
#define OP_ListPop 103
|
||||
#define OP_SortPut 104
|
||||
#define OP_SortMakeRec 105
|
||||
#define OP_SortMakeKey 106
|
||||
#define OP_Sort 107
|
||||
#define OP_SortNext 108
|
||||
#define OP_SortCallback 109
|
||||
#define OP_SortReset 110
|
||||
#define OP_FileOpen 111
|
||||
#define OP_FileRead 112
|
||||
#define OP_FileColumn 113
|
||||
#define OP_MemStore 114
|
||||
#define OP_MemLoad 115
|
||||
#define OP_MemIncr 116
|
||||
#define OP_AggReset 117
|
||||
#define OP_AggInit 118
|
||||
#define OP_AggFunc 119
|
||||
#define OP_AggFocus 120
|
||||
#define OP_AggSet 121
|
||||
#define OP_AggGet 122
|
||||
#define OP_AggNext 123
|
||||
#define OP_SetInsert 124
|
||||
#define OP_SetFound 125
|
||||
#define OP_SetNotFound 126
|
||||
#define OP_SetFirst 127
|
||||
#define OP_SetNext 128
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
** 2001 September 16
|
||||
**
|
||||
** 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 header file (together with is companion C source-code file
|
||||
** "os.c") attempt to abstract the underlying operating system so that
|
||||
** the SQLite library will work on both POSIX and windows systems.
|
||||
*/
|
||||
#ifndef _SQLITE_OS_H_
|
||||
#define _SQLITE_OS_H_
|
||||
|
||||
/*
|
||||
** Helpful hint: To get this to compile on HP/UX, add -D_INCLUDE_POSIX_SOURCE
|
||||
** to the compiler command line.
|
||||
*/
|
||||
|
||||
/*
|
||||
** These #defines should enable >2GB file support on Posix if the
|
||||
** underlying operating system supports it. If the OS lacks
|
||||
** large file support, or if the OS is windows, these should be no-ops.
|
||||
**
|
||||
** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
|
||||
** on the compiler command line. This is necessary if you are compiling
|
||||
** on a recent machine (ex: RedHat 7.2) but you want your code to work
|
||||
** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
|
||||
** without this option, LFS is enable. But LFS does not exist in the kernel
|
||||
** in RedHat 6.0, so the code won't work. Hence, for maximum binary
|
||||
** portability you should omit LFS.
|
||||
**
|
||||
** Similar is true for MacOS. LFS is only supported on MacOS 9 and later.
|
||||
*/
|
||||
#ifndef SQLITE_DISABLE_LFS
|
||||
# define _LARGE_FILE 1
|
||||
# define _FILE_OFFSET_BITS 64
|
||||
# define _LARGEFILE_SOURCE 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Temporary files are named starting with this prefix followed by 16 random
|
||||
** alphanumeric characters, and no file extension. They are stored in the
|
||||
** OS's standard temporary file directory, and are deleted prior to exit.
|
||||
** If sqlite is being embedded in another program, you may wish to change the
|
||||
** prefix to reflect your program's name, so that if your program exits
|
||||
** prematurely, old temporary files can be easily identified. This can be done
|
||||
** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
|
||||
*/
|
||||
#ifndef TEMP_FILE_PREFIX
|
||||
# define TEMP_FILE_PREFIX "sqlite_"
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Figure out if we are dealing with Unix, Windows or MacOS.
|
||||
**
|
||||
** N.B. MacOS means Mac Classic (or Carbon). Treat Darwin (OS X) as Unix.
|
||||
** The MacOS build is designed to use CodeWarrior (tested with v8)
|
||||
*/
|
||||
#ifndef OS_UNIX
|
||||
# ifndef OS_WIN
|
||||
# ifndef OS_MAC
|
||||
# if defined(__MACOS__)
|
||||
# define OS_MAC 1
|
||||
# define OS_WIN 0
|
||||
# define OS_UNIX 0
|
||||
# elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
|
||||
# define OS_MAC 0
|
||||
# define OS_WIN 1
|
||||
# define OS_UNIX 0
|
||||
# else
|
||||
# define OS_MAC 0
|
||||
# define OS_WIN 0
|
||||
# define OS_UNIX 1
|
||||
# endif
|
||||
# else
|
||||
# define OS_WIN 0
|
||||
# define OS_UNIX 0
|
||||
# endif
|
||||
# else
|
||||
# define OS_MAC 0
|
||||
# define OS_UNIX 0
|
||||
# endif
|
||||
#else
|
||||
# define OS_MAC 0
|
||||
# define OS_WIN 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
** A handle for an open file is stored in an OsFile object.
|
||||
*/
|
||||
#if OS_UNIX
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
# include <unistd.h>
|
||||
typedef struct OsFile OsFile;
|
||||
struct OsFile {
|
||||
struct lockInfo *pLock; /* Information about locks on this inode */
|
||||
int fd; /* The file descriptor */
|
||||
int locked; /* True if this user holds the lock */
|
||||
};
|
||||
# define SQLITE_TEMPNAME_SIZE 200
|
||||
# if defined(HAVE_USLEEP) && HAVE_USLEEP
|
||||
# define SQLITE_MIN_SLEEP_MS 1
|
||||
# else
|
||||
# define SQLITE_MIN_SLEEP_MS 1000
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if OS_WIN
|
||||
#include <windows.h>
|
||||
#include <winbase.h>
|
||||
typedef struct OsFile OsFile;
|
||||
struct OsFile {
|
||||
HANDLE h; /* Handle for accessing the file */
|
||||
int locked; /* 0: unlocked, <0: write lock, >0: read lock */
|
||||
};
|
||||
# if defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
typedef __int64 off_t;
|
||||
# else
|
||||
typedef long long off_t;
|
||||
# endif
|
||||
# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
|
||||
# define SQLITE_MIN_SLEEP_MS 1
|
||||
#endif
|
||||
|
||||
#if OS_MAC
|
||||
# include <unistd.h>
|
||||
# include <Files.h>
|
||||
typedef struct OsFile OsFile;
|
||||
struct OsFile {
|
||||
SInt16 refNum; /* Data fork/file reference number */
|
||||
SInt16 refNumRF; /* Resource fork reference number (for locking) */
|
||||
int locked; /* 0: unlocked, <0: write lock, >0: read lock */
|
||||
int delOnClose; /* True if file is to be deleted on close */
|
||||
char *pathToDel; /* Name of file to delete on close */
|
||||
};
|
||||
# ifdef _LARGE_FILE
|
||||
typedef SInt64 off_t;
|
||||
# else
|
||||
typedef SInt32 off_t;
|
||||
# endif
|
||||
# define SQLITE_TEMPNAME_SIZE _MAX_PATH
|
||||
# define SQLITE_MIN_SLEEP_MS 17
|
||||
#endif
|
||||
|
||||
int sqliteOsDelete(const char*);
|
||||
int sqliteOsFileExists(const char*);
|
||||
int sqliteOsFileRename(const char*, const char*);
|
||||
int sqliteOsOpenReadWrite(const char*, OsFile*, int*);
|
||||
int sqliteOsOpenExclusive(const char*, OsFile*, int);
|
||||
int sqliteOsOpenReadOnly(const char*, OsFile*);
|
||||
int sqliteOsTempFileName(char*);
|
||||
int sqliteOsClose(OsFile*);
|
||||
int sqliteOsRead(OsFile*, void*, int amt);
|
||||
int sqliteOsWrite(OsFile*, const void*, int amt);
|
||||
int sqliteOsSeek(OsFile*, off_t offset);
|
||||
int sqliteOsSync(OsFile*);
|
||||
int sqliteOsTruncate(OsFile*, off_t size);
|
||||
int sqliteOsFileSize(OsFile*, off_t *pSize);
|
||||
int sqliteOsReadLock(OsFile*);
|
||||
int sqliteOsWriteLock(OsFile*);
|
||||
int sqliteOsUnlock(OsFile*);
|
||||
int sqliteOsRandomSeed(char*);
|
||||
int sqliteOsSleep(int ms);
|
||||
void sqliteOsEnterMutex(void);
|
||||
void sqliteOsLeaveMutex(void);
|
||||
char *sqliteOsFullPathname(const char*);
|
||||
|
||||
|
||||
|
||||
#endif /* _SQLITE_OS_H_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
** 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 header file defines the interface that the sqlite page cache
|
||||
** subsystem. The page cache subsystem reads and writes a file a page
|
||||
** at a time and provides a journal for rollback.
|
||||
**
|
||||
** @(#) $Id$
|
||||
*/
|
||||
|
||||
/*
|
||||
** The size of one page
|
||||
**
|
||||
** You can change this value to another (reasonable) power of two
|
||||
** such as 512, 2048, 4096, or 8192 and things will still work. But
|
||||
** experiments show that a page size of 1024 gives the best speed.
|
||||
** (The speed differences are minimal.)
|
||||
*/
|
||||
#define SQLITE_PAGE_SIZE 1024
|
||||
|
||||
/*
|
||||
** Maximum number of pages in one database. (This is a limitation of
|
||||
** imposed by 4GB files size limits.)
|
||||
*/
|
||||
#define SQLITE_MAX_PAGE 1073741823
|
||||
|
||||
/*
|
||||
** The type used to represent a page number. The first page in a file
|
||||
** is called page 1. 0 is used to represent "not a page".
|
||||
*/
|
||||
typedef unsigned int Pgno;
|
||||
|
||||
/*
|
||||
** Each open file is managed by a separate instance of the "Pager" structure.
|
||||
*/
|
||||
typedef struct Pager Pager;
|
||||
|
||||
/*
|
||||
** See source code comments for a detailed description of the following
|
||||
** routines:
|
||||
*/
|
||||
int sqlitepager_open(Pager **ppPager, const char *zFilename,
|
||||
int nPage, int nExtra, int useJournal);
|
||||
void sqlitepager_set_destructor(Pager*, void(*)(void*));
|
||||
void sqlitepager_set_cachesize(Pager*, int);
|
||||
int sqlitepager_close(Pager *pPager);
|
||||
int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage);
|
||||
void *sqlitepager_lookup(Pager *pPager, Pgno pgno);
|
||||
int sqlitepager_ref(void*);
|
||||
int sqlitepager_unref(void*);
|
||||
Pgno sqlitepager_pagenumber(void*);
|
||||
int sqlitepager_write(void*);
|
||||
int sqlitepager_iswriteable(void*);
|
||||
int sqlitepager_overwrite(Pager *pPager, Pgno pgno, void*);
|
||||
int sqlitepager_pagecount(Pager*);
|
||||
int sqlitepager_truncate(Pager*,Pgno);
|
||||
int sqlitepager_begin(void*);
|
||||
int sqlitepager_commit(Pager*);
|
||||
int sqlitepager_rollback(Pager*);
|
||||
int sqlitepager_isreadonly(Pager*);
|
||||
int sqlitepager_ckpt_begin(Pager*);
|
||||
int sqlitepager_ckpt_commit(Pager*);
|
||||
int sqlitepager_ckpt_rollback(Pager*);
|
||||
void sqlitepager_dont_rollback(void*);
|
||||
void sqlitepager_dont_write(Pager*, Pgno);
|
||||
int *sqlitepager_stats(Pager*);
|
||||
void sqlitepager_set_safety_level(Pager*,int);
|
||||
const char *sqlitepager_filename(Pager*);
|
||||
int sqlitepager_rename(Pager*, const char *zNewName);
|
||||
|
||||
#ifdef SQLITE_TEST
|
||||
void sqlitepager_refdump(Pager*);
|
||||
int pager_refinfo_enable;
|
||||
int journal_format;
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,130 +0,0 @@
|
||||
#define TK_ABORT 1
|
||||
#define TK_AFTER 2
|
||||
#define TK_AGG_FUNCTION 3
|
||||
#define TK_ALL 4
|
||||
#define TK_AND 5
|
||||
#define TK_AS 6
|
||||
#define TK_ASC 7
|
||||
#define TK_ATTACH 8
|
||||
#define TK_BEFORE 9
|
||||
#define TK_BEGIN 10
|
||||
#define TK_BETWEEN 11
|
||||
#define TK_BITAND 12
|
||||
#define TK_BITNOT 13
|
||||
#define TK_BITOR 14
|
||||
#define TK_BY 15
|
||||
#define TK_CASCADE 16
|
||||
#define TK_CASE 17
|
||||
#define TK_CHECK 18
|
||||
#define TK_CLUSTER 19
|
||||
#define TK_COLLATE 20
|
||||
#define TK_COLUMN 21
|
||||
#define TK_COMMA 22
|
||||
#define TK_COMMENT 23
|
||||
#define TK_COMMIT 24
|
||||
#define TK_CONCAT 25
|
||||
#define TK_CONFLICT 26
|
||||
#define TK_CONSTRAINT 27
|
||||
#define TK_COPY 28
|
||||
#define TK_CREATE 29
|
||||
#define TK_DATABASE 30
|
||||
#define TK_DEFAULT 31
|
||||
#define TK_DEFERRABLE 32
|
||||
#define TK_DEFERRED 33
|
||||
#define TK_DELETE 34
|
||||
#define TK_DELIMITERS 35
|
||||
#define TK_DESC 36
|
||||
#define TK_DETACH 37
|
||||
#define TK_DISTINCT 38
|
||||
#define TK_DOT 39
|
||||
#define TK_DROP 40
|
||||
#define TK_EACH 41
|
||||
#define TK_ELSE 42
|
||||
#define TK_END 43
|
||||
#define TK_END_OF_FILE 44
|
||||
#define TK_EQ 45
|
||||
#define TK_EXCEPT 46
|
||||
#define TK_EXPLAIN 47
|
||||
#define TK_FAIL 48
|
||||
#define TK_FLOAT 49
|
||||
#define TK_FOR 50
|
||||
#define TK_FOREIGN 51
|
||||
#define TK_FROM 52
|
||||
#define TK_FUNCTION 53
|
||||
#define TK_GE 54
|
||||
#define TK_GLOB 55
|
||||
#define TK_GROUP 56
|
||||
#define TK_GT 57
|
||||
#define TK_HAVING 58
|
||||
#define TK_ID 59
|
||||
#define TK_IGNORE 60
|
||||
#define TK_ILLEGAL 61
|
||||
#define TK_IMMEDIATE 62
|
||||
#define TK_IN 63
|
||||
#define TK_INDEX 64
|
||||
#define TK_INITIALLY 65
|
||||
#define TK_INSERT 66
|
||||
#define TK_INSTEAD 67
|
||||
#define TK_INTEGER 68
|
||||
#define TK_INTERSECT 69
|
||||
#define TK_INTO 70
|
||||
#define TK_IS 71
|
||||
#define TK_ISNULL 72
|
||||
#define TK_JOIN 73
|
||||
#define TK_JOIN_KW 74
|
||||
#define TK_KEY 75
|
||||
#define TK_LE 76
|
||||
#define TK_LIKE 77
|
||||
#define TK_LIMIT 78
|
||||
#define TK_LP 79
|
||||
#define TK_LSHIFT 80
|
||||
#define TK_LT 81
|
||||
#define TK_MATCH 82
|
||||
#define TK_MINUS 83
|
||||
#define TK_NE 84
|
||||
#define TK_NOT 85
|
||||
#define TK_NOTNULL 86
|
||||
#define TK_NULL 87
|
||||
#define TK_OF 88
|
||||
#define TK_OFFSET 89
|
||||
#define TK_ON 90
|
||||
#define TK_OR 91
|
||||
#define TK_ORACLE_OUTER_JOIN 92
|
||||
#define TK_ORDER 93
|
||||
#define TK_PLUS 94
|
||||
#define TK_PRAGMA 95
|
||||
#define TK_PRIMARY 96
|
||||
#define TK_RAISE 97
|
||||
#define TK_REFERENCES 98
|
||||
#define TK_REM 99
|
||||
#define TK_REPLACE 100
|
||||
#define TK_RESTRICT 101
|
||||
#define TK_ROLLBACK 102
|
||||
#define TK_ROW 103
|
||||
#define TK_RP 104
|
||||
#define TK_RSHIFT 105
|
||||
#define TK_SELECT 106
|
||||
#define TK_SEMI 107
|
||||
#define TK_SET 108
|
||||
#define TK_SLASH 109
|
||||
#define TK_SPACE 110
|
||||
#define TK_STAR 111
|
||||
#define TK_STATEMENT 112
|
||||
#define TK_STRING 113
|
||||
#define TK_TABLE 114
|
||||
#define TK_TEMP 115
|
||||
#define TK_THEN 116
|
||||
#define TK_TRANSACTION 117
|
||||
#define TK_TRIGGER 118
|
||||
#define TK_UMINUS 119
|
||||
#define TK_UNCLOSED_STRING 120
|
||||
#define TK_UNION 121
|
||||
#define TK_UNIQUE 122
|
||||
#define TK_UPDATE 123
|
||||
#define TK_UPLUS 124
|
||||
#define TK_USING 125
|
||||
#define TK_VACUUM 126
|
||||
#define TK_VALUES 127
|
||||
#define TK_VIEW 128
|
||||
#define TK_WHEN 129
|
||||
#define TK_WHERE 130
|
||||
@@ -1,865 +0,0 @@
|
||||
/*
|
||||
** 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 contains SQLite's grammar for SQL. Process this file
|
||||
** using the lemon parser generator to generate C code that runs
|
||||
** the parser. Lemon will also generate a header file containing
|
||||
** numeric codes for all of the tokens.
|
||||
**
|
||||
** @(#) $Id$
|
||||
*/
|
||||
%token_prefix TK_
|
||||
%token_type {Token}
|
||||
%default_type {Token}
|
||||
%extra_argument {Parse *pParse}
|
||||
%syntax_error {
|
||||
if( pParse->zErrMsg==0 ){
|
||||
if( TOKEN.z[0] ){
|
||||
sqliteSetNString(&pParse->zErrMsg,
|
||||
"near \"", -1, TOKEN.z, TOKEN.n, "\": syntax error", -1, 0);
|
||||
}else{
|
||||
sqliteSetString(&pParse->zErrMsg, "incomplete SQL statement", 0);
|
||||
}
|
||||
}
|
||||
pParse->nErr++;
|
||||
}
|
||||
%name sqliteParser
|
||||
%include {
|
||||
#include "sqliteInt.h"
|
||||
#include "parse.h"
|
||||
|
||||
/*
|
||||
** An instance of this structure holds information about the
|
||||
** LIMIT clause of a SELECT statement.
|
||||
*/
|
||||
struct LimitVal {
|
||||
int limit; /* The LIMIT value. -1 if there is no limit */
|
||||
int offset; /* The OFFSET. 0 if there is none */
|
||||
};
|
||||
|
||||
/*
|
||||
** An instance of the following structure describes the event of a
|
||||
** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
|
||||
** TK_DELETE, or TK_INSTEAD. If the event is of the form
|
||||
**
|
||||
** UPDATE ON (a,b,c)
|
||||
**
|
||||
** Then the "b" IdList records the list "a,b,c".
|
||||
*/
|
||||
struct TrigEvent { int a; IdList * b; };
|
||||
|
||||
} // end %include
|
||||
|
||||
// These are extra tokens used by the lexer but never seen by the
|
||||
// parser. We put them in a rule so that the parser generator will
|
||||
// add them to the parse.h output file.
|
||||
//
|
||||
%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
|
||||
COLUMN AGG_FUNCTION.
|
||||
|
||||
// Input is zero or more commands.
|
||||
input ::= cmdlist.
|
||||
|
||||
// A list of commands is zero or more commands
|
||||
//
|
||||
cmdlist ::= ecmd.
|
||||
cmdlist ::= cmdlist ecmd.
|
||||
ecmd ::= explain cmdx SEMI.
|
||||
ecmd ::= SEMI.
|
||||
cmdx ::= cmd. { sqliteExec(pParse); }
|
||||
explain ::= EXPLAIN. { sqliteBeginParse(pParse, 1); }
|
||||
explain ::= . { sqliteBeginParse(pParse, 0); }
|
||||
|
||||
///////////////////// Begin and end transactions. ////////////////////////////
|
||||
//
|
||||
|
||||
cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}
|
||||
trans_opt ::= .
|
||||
trans_opt ::= TRANSACTION.
|
||||
trans_opt ::= TRANSACTION nm.
|
||||
cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
|
||||
cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
|
||||
cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
|
||||
|
||||
///////////////////// The CREATE TABLE statement ////////////////////////////
|
||||
//
|
||||
cmd ::= create_table create_table_args.
|
||||
create_table ::= CREATE(X) temp(T) TABLE nm(Y). {
|
||||
sqliteStartTable(pParse,&X,&Y,T,0);
|
||||
}
|
||||
%type temp {int}
|
||||
temp(A) ::= TEMP. {A = 1;}
|
||||
temp(A) ::= . {A = 0;}
|
||||
create_table_args ::= LP columnlist conslist_opt RP(X). {
|
||||
sqliteEndTable(pParse,&X,0);
|
||||
}
|
||||
create_table_args ::= AS select(S). {
|
||||
sqliteEndTable(pParse,0,S);
|
||||
sqliteSelectDelete(S);
|
||||
}
|
||||
columnlist ::= columnlist COMMA column.
|
||||
columnlist ::= column.
|
||||
|
||||
// About the only information used for a column is the name of the
|
||||
// column. The type is always just "text". But the code will accept
|
||||
// an elaborate typename. Perhaps someday we'll do something with it.
|
||||
//
|
||||
column ::= columnid type carglist.
|
||||
columnid ::= nm(X). {sqliteAddColumn(pParse,&X);}
|
||||
|
||||
// An IDENTIFIER can be a generic identifier, or one of several
|
||||
// keywords. Any non-standard keyword can also be an identifier.
|
||||
//
|
||||
%type id {Token}
|
||||
id(A) ::= ID(X). {A = X;}
|
||||
|
||||
// The following directive causes tokens ABORT, AFTER, ASC, etc. to
|
||||
// fallback to ID if they will not parse as their original value.
|
||||
// This obviates the need for the "id" nonterminal.
|
||||
//
|
||||
%fallback ID
|
||||
ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT
|
||||
COPY DATABASE DEFERRED DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR
|
||||
IGNORE IMMEDIATE INITIALLY INSTEAD MATCH KEY
|
||||
OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
|
||||
TEMP TRIGGER VACUUM VIEW.
|
||||
|
||||
// And "ids" is an identifer-or-string.
|
||||
//
|
||||
%type ids {Token}
|
||||
ids(A) ::= ID(X). {A = X;}
|
||||
ids(A) ::= STRING(X). {A = X;}
|
||||
|
||||
// The name of a column or table can be any of the following:
|
||||
//
|
||||
%type nm {Token}
|
||||
nm(A) ::= ID(X). {A = X;}
|
||||
nm(A) ::= STRING(X). {A = X;}
|
||||
nm(A) ::= JOIN_KW(X). {A = X;}
|
||||
|
||||
type ::= .
|
||||
type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
|
||||
type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
|
||||
type ::= typename(X) LP signed COMMA signed RP(Y).
|
||||
{sqliteAddColumnType(pParse,&X,&Y);}
|
||||
%type typename {Token}
|
||||
typename(A) ::= ids(X). {A = X;}
|
||||
typename(A) ::= typename(X) ids. {A = X;}
|
||||
signed ::= INTEGER.
|
||||
signed ::= PLUS INTEGER.
|
||||
signed ::= MINUS INTEGER.
|
||||
carglist ::= carglist carg.
|
||||
carglist ::= .
|
||||
carg ::= CONSTRAINT nm ccons.
|
||||
carg ::= ccons.
|
||||
carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
|
||||
carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
|
||||
carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
|
||||
carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
|
||||
carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
|
||||
carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
|
||||
carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
|
||||
carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
|
||||
carg ::= DEFAULT NULL.
|
||||
|
||||
// In addition to the type name, we also care about the primary key and
|
||||
// UNIQUE constraints.
|
||||
//
|
||||
ccons ::= NULL onconf.
|
||||
ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
|
||||
ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
|
||||
ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0,0);}
|
||||
ccons ::= CHECK LP expr RP onconf.
|
||||
ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
|
||||
{sqliteCreateForeignKey(pParse,0,&T,TA,R);}
|
||||
ccons ::= defer_subclause(D). {sqliteDeferForeignKey(pParse,D);}
|
||||
ccons ::= COLLATE id(C). {
|
||||
sqliteAddCollateType(pParse, sqliteCollateType(C.z, C.n));
|
||||
}
|
||||
|
||||
// The next group of rules parses the arguments to a REFERENCES clause
|
||||
// that determine if the referential integrity checking is deferred or
|
||||
// or immediate and which determine what action to take if a ref-integ
|
||||
// check fails.
|
||||
//
|
||||
%type refargs {int}
|
||||
refargs(A) ::= . { A = OE_Restrict * 0x010101; }
|
||||
refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
|
||||
%type refarg {struct {int value; int mask;}}
|
||||
refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
|
||||
refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
|
||||
refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
|
||||
refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }
|
||||
%type refact {int}
|
||||
refact(A) ::= SET NULL. { A = OE_SetNull; }
|
||||
refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }
|
||||
refact(A) ::= CASCADE. { A = OE_Cascade; }
|
||||
refact(A) ::= RESTRICT. { A = OE_Restrict; }
|
||||
%type defer_subclause {int}
|
||||
defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}
|
||||
defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
|
||||
%type init_deferred_pred_opt {int}
|
||||
init_deferred_pred_opt(A) ::= . {A = 0;}
|
||||
init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
|
||||
init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
|
||||
|
||||
// For the time being, the only constraint we care about is the primary
|
||||
// key and UNIQUE. Both create indices.
|
||||
//
|
||||
conslist_opt ::= .
|
||||
conslist_opt ::= COMMA conslist.
|
||||
conslist ::= conslist COMMA tcons.
|
||||
conslist ::= conslist tcons.
|
||||
conslist ::= tcons.
|
||||
tcons ::= CONSTRAINT nm.
|
||||
tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
|
||||
{sqliteAddPrimaryKey(pParse,X,R);}
|
||||
tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
|
||||
{sqliteCreateIndex(pParse,0,0,X,R,0,0,0);}
|
||||
tcons ::= CHECK expr onconf.
|
||||
tcons ::= FOREIGN KEY LP idxlist(FA) RP
|
||||
REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
|
||||
sqliteCreateForeignKey(pParse, FA, &T, TA, R);
|
||||
sqliteDeferForeignKey(pParse, D);
|
||||
}
|
||||
%type defer_subclause_opt {int}
|
||||
defer_subclause_opt(A) ::= . {A = 0;}
|
||||
defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
|
||||
|
||||
// The following is a non-standard extension that allows us to declare the
|
||||
// default behavior when there is a constraint conflict.
|
||||
//
|
||||
%type onconf {int}
|
||||
%type orconf {int}
|
||||
%type resolvetype {int}
|
||||
onconf(A) ::= . { A = OE_Default; }
|
||||
onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
|
||||
orconf(A) ::= . { A = OE_Default; }
|
||||
orconf(A) ::= OR resolvetype(X). { A = X; }
|
||||
resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
|
||||
resolvetype(A) ::= ABORT. { A = OE_Abort; }
|
||||
resolvetype(A) ::= FAIL. { A = OE_Fail; }
|
||||
resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
|
||||
resolvetype(A) ::= REPLACE. { A = OE_Replace; }
|
||||
|
||||
////////////////////////// The DROP TABLE /////////////////////////////////////
|
||||
//
|
||||
cmd ::= DROP TABLE nm(X). {sqliteDropTable(pParse,&X,0);}
|
||||
|
||||
///////////////////// The CREATE VIEW statement /////////////////////////////
|
||||
//
|
||||
cmd ::= CREATE(X) temp(T) VIEW nm(Y) AS select(S). {
|
||||
sqliteCreateView(pParse, &X, &Y, S, T);
|
||||
}
|
||||
cmd ::= DROP VIEW nm(X). {
|
||||
sqliteDropTable(pParse, &X, 1);
|
||||
}
|
||||
|
||||
//////////////////////// The SELECT statement /////////////////////////////////
|
||||
//
|
||||
cmd ::= select(X). {
|
||||
sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
|
||||
sqliteSelectDelete(X);
|
||||
}
|
||||
|
||||
%type select {Select*}
|
||||
%destructor select {sqliteSelectDelete($$);}
|
||||
%type oneselect {Select*}
|
||||
%destructor oneselect {sqliteSelectDelete($$);}
|
||||
|
||||
select(A) ::= oneselect(X). {A = X;}
|
||||
select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
|
||||
if( Z ){
|
||||
Z->op = Y;
|
||||
Z->pPrior = X;
|
||||
}
|
||||
A = Z;
|
||||
}
|
||||
%type multiselect_op {int}
|
||||
multiselect_op(A) ::= UNION. {A = TK_UNION;}
|
||||
multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
|
||||
multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
|
||||
multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
|
||||
oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
|
||||
groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
|
||||
A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.limit,L.offset);
|
||||
}
|
||||
|
||||
// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
|
||||
// present and false (0) if it is not.
|
||||
//
|
||||
%type distinct {int}
|
||||
distinct(A) ::= DISTINCT. {A = 1;}
|
||||
distinct(A) ::= ALL. {A = 0;}
|
||||
distinct(A) ::= . {A = 0;}
|
||||
|
||||
// selcollist is a list of expressions that are to become the return
|
||||
// values of the SELECT statement. The "*" in statements like
|
||||
// "SELECT * FROM ..." is encoded as a special expression with an
|
||||
// opcode of TK_ALL.
|
||||
//
|
||||
%type selcollist {ExprList*}
|
||||
%destructor selcollist {sqliteExprListDelete($$);}
|
||||
%type sclp {ExprList*}
|
||||
%destructor sclp {sqliteExprListDelete($$);}
|
||||
sclp(A) ::= selcollist(X) COMMA. {A = X;}
|
||||
sclp(A) ::= . {A = 0;}
|
||||
selcollist(A) ::= sclp(P) expr(X) as(Y). {
|
||||
A = sqliteExprListAppend(P,X,Y.n?&Y:0);
|
||||
}
|
||||
selcollist(A) ::= sclp(P) STAR. {
|
||||
A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
|
||||
}
|
||||
selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
|
||||
Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
|
||||
Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
|
||||
A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
|
||||
}
|
||||
|
||||
// An option "AS <id>" phrase that can follow one of the expressions that
|
||||
// define the result set, or one of the tables in the FROM clause.
|
||||
//
|
||||
%type as {Token}
|
||||
as(X) ::= AS nm(Y). { X = Y; }
|
||||
as(X) ::= ids(Y). { X = Y; }
|
||||
as(X) ::= . { X.n = 0; }
|
||||
|
||||
|
||||
%type seltablist {SrcList*}
|
||||
%destructor seltablist {sqliteSrcListDelete($$);}
|
||||
%type stl_prefix {SrcList*}
|
||||
%destructor stl_prefix {sqliteSrcListDelete($$);}
|
||||
%type from {SrcList*}
|
||||
%destructor from {sqliteSrcListDelete($$);}
|
||||
|
||||
// A complete FROM clause.
|
||||
//
|
||||
from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
|
||||
from(A) ::= FROM seltablist(X). {A = X;}
|
||||
|
||||
// "seltablist" is a "Select Table List" - the content of the FROM clause
|
||||
// in a SELECT statement. "stl_prefix" is a prefix of this list.
|
||||
//
|
||||
stl_prefix(A) ::= seltablist(X) joinop(Y). {
|
||||
A = X;
|
||||
if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
|
||||
}
|
||||
stl_prefix(A) ::= . {A = 0;}
|
||||
seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
|
||||
A = sqliteSrcListAppend(X,&Y,&D);
|
||||
if( Z.n ) sqliteSrcListAddAlias(A,&Z);
|
||||
if( N ){
|
||||
if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
|
||||
else { sqliteExprDelete(N); }
|
||||
}
|
||||
if( U ){
|
||||
if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
|
||||
else { sqliteIdListDelete(U); }
|
||||
}
|
||||
}
|
||||
seltablist(A) ::= stl_prefix(X) LP select(S) RP as(Z) on_opt(N) using_opt(U). {
|
||||
A = sqliteSrcListAppend(X,0,0);
|
||||
A->a[A->nSrc-1].pSelect = S;
|
||||
if( Z.n ) sqliteSrcListAddAlias(A,&Z);
|
||||
if( N ){
|
||||
if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
|
||||
else { sqliteExprDelete(N); }
|
||||
}
|
||||
if( U ){
|
||||
if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
|
||||
else { sqliteIdListDelete(U); }
|
||||
}
|
||||
}
|
||||
|
||||
%type dbnm {Token}
|
||||
dbnm(A) ::= . {A.z=0; A.n=0;}
|
||||
dbnm(A) ::= DOT nm(X). {A = X;}
|
||||
|
||||
%type joinop {int}
|
||||
%type joinop2 {int}
|
||||
joinop(X) ::= COMMA. { X = JT_INNER; }
|
||||
joinop(X) ::= JOIN. { X = JT_INNER; }
|
||||
joinop(X) ::= JOIN_KW(A) JOIN. { X = sqliteJoinType(pParse,&A,0,0); }
|
||||
joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqliteJoinType(pParse,&A,&B,0); }
|
||||
joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
|
||||
{ X = sqliteJoinType(pParse,&A,&B,&C); }
|
||||
|
||||
%type on_opt {Expr*}
|
||||
%destructor on_opt {sqliteExprDelete($$);}
|
||||
on_opt(N) ::= ON expr(E). {N = E;}
|
||||
on_opt(N) ::= . {N = 0;}
|
||||
|
||||
%type using_opt {IdList*}
|
||||
%destructor using_opt {sqliteIdListDelete($$);}
|
||||
using_opt(U) ::= USING LP idxlist(L) RP. {U = L;}
|
||||
using_opt(U) ::= . {U = 0;}
|
||||
|
||||
|
||||
%type orderby_opt {ExprList*}
|
||||
%destructor orderby_opt {sqliteExprListDelete($$);}
|
||||
%type sortlist {ExprList*}
|
||||
%destructor sortlist {sqliteExprListDelete($$);}
|
||||
%type sortitem {Expr*}
|
||||
%destructor sortitem {sqliteExprDelete($$);}
|
||||
|
||||
orderby_opt(A) ::= . {A = 0;}
|
||||
orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
|
||||
sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
|
||||
A = sqliteExprListAppend(X,Y,0);
|
||||
if( A ) A->a[A->nExpr-1].sortOrder = C+Z;
|
||||
}
|
||||
sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
|
||||
A = sqliteExprListAppend(0,Y,0);
|
||||
if( A ) A->a[0].sortOrder = C+Z;
|
||||
}
|
||||
sortitem(A) ::= expr(X). {A = X;}
|
||||
|
||||
%type sortorder {int}
|
||||
%type collate {int}
|
||||
|
||||
sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
|
||||
sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
|
||||
sortorder(A) ::= . {A = SQLITE_SO_ASC;}
|
||||
collate(C) ::= . {C = SQLITE_SO_UNK;}
|
||||
collate(C) ::= COLLATE id(X). {C = sqliteCollateType(X.z, X.n);}
|
||||
|
||||
%type groupby_opt {ExprList*}
|
||||
%destructor groupby_opt {sqliteExprListDelete($$);}
|
||||
groupby_opt(A) ::= . {A = 0;}
|
||||
groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
|
||||
|
||||
%type having_opt {Expr*}
|
||||
%destructor having_opt {sqliteExprDelete($$);}
|
||||
having_opt(A) ::= . {A = 0;}
|
||||
having_opt(A) ::= HAVING expr(X). {A = X;}
|
||||
|
||||
%type limit_opt {struct LimitVal}
|
||||
limit_opt(A) ::= . {A.limit = -1; A.offset = 0;}
|
||||
limit_opt(A) ::= LIMIT INTEGER(X). {A.limit = atoi(X.z); A.offset = 0;}
|
||||
limit_opt(A) ::= LIMIT INTEGER(X) OFFSET INTEGER(Y).
|
||||
{A.limit = atoi(X.z); A.offset = atoi(Y.z);}
|
||||
limit_opt(A) ::= LIMIT INTEGER(X) COMMA INTEGER(Y).
|
||||
{A.limit = atoi(Y.z); A.offset = atoi(X.z);}
|
||||
|
||||
/////////////////////////// The DELETE statement /////////////////////////////
|
||||
//
|
||||
cmd ::= DELETE FROM nm(X) dbnm(D) where_opt(Y). {
|
||||
sqliteDeleteFrom(pParse, sqliteSrcListAppend(0,&X,&D), Y);
|
||||
}
|
||||
|
||||
%type where_opt {Expr*}
|
||||
%destructor where_opt {sqliteExprDelete($$);}
|
||||
|
||||
where_opt(A) ::= . {A = 0;}
|
||||
where_opt(A) ::= WHERE expr(X). {A = X;}
|
||||
|
||||
%type setlist {ExprList*}
|
||||
%destructor setlist {sqliteExprListDelete($$);}
|
||||
|
||||
////////////////////////// The UPDATE command ////////////////////////////////
|
||||
//
|
||||
cmd ::= UPDATE orconf(R) nm(X) dbnm(D) SET setlist(Y) where_opt(Z).
|
||||
{sqliteUpdate(pParse,sqliteSrcListAppend(0,&X,&D),Y,Z,R);}
|
||||
|
||||
setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
|
||||
{A = sqliteExprListAppend(Z,Y,&X);}
|
||||
setlist(A) ::= nm(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
|
||||
|
||||
////////////////////////// The INSERT command /////////////////////////////////
|
||||
//
|
||||
cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F)
|
||||
VALUES LP itemlist(Y) RP.
|
||||
{sqliteInsert(pParse, sqliteSrcListAppend(0,&X,&D), Y, 0, F, R);}
|
||||
cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F) select(S).
|
||||
{sqliteInsert(pParse, sqliteSrcListAppend(0,&X,&D), 0, S, F, R);}
|
||||
|
||||
%type insert_cmd {int}
|
||||
insert_cmd(A) ::= INSERT orconf(R). {A = R;}
|
||||
insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
|
||||
|
||||
|
||||
%type itemlist {ExprList*}
|
||||
%destructor itemlist {sqliteExprListDelete($$);}
|
||||
|
||||
itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
|
||||
itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
|
||||
|
||||
%type inscollist_opt {IdList*}
|
||||
%destructor inscollist_opt {sqliteIdListDelete($$);}
|
||||
%type inscollist {IdList*}
|
||||
%destructor inscollist {sqliteIdListDelete($$);}
|
||||
|
||||
inscollist_opt(A) ::= . {A = 0;}
|
||||
inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
|
||||
inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqliteIdListAppend(X,&Y);}
|
||||
inscollist(A) ::= nm(Y). {A = sqliteIdListAppend(0,&Y);}
|
||||
|
||||
/////////////////////////// Expression Processing /////////////////////////////
|
||||
//
|
||||
%left OR.
|
||||
%left AND.
|
||||
%right NOT.
|
||||
%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
|
||||
%left GT GE LT LE.
|
||||
%left BITAND BITOR LSHIFT RSHIFT.
|
||||
%left PLUS MINUS.
|
||||
%left STAR SLASH REM.
|
||||
%left CONCAT.
|
||||
%right UMINUS UPLUS BITNOT.
|
||||
%right ORACLE_OUTER_JOIN.
|
||||
|
||||
%type expr {Expr*}
|
||||
%destructor expr {sqliteExprDelete($$);}
|
||||
|
||||
expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E); }
|
||||
expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
|
||||
expr(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
|
||||
expr(A) ::= JOIN_KW(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
|
||||
expr(A) ::= nm(X) DOT nm(Y). {
|
||||
Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
|
||||
Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
|
||||
A = sqliteExpr(TK_DOT, temp1, temp2, 0);
|
||||
}
|
||||
expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
|
||||
Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
|
||||
Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
|
||||
Expr *temp3 = sqliteExpr(TK_ID, 0, 0, &Z);
|
||||
Expr *temp4 = sqliteExpr(TK_DOT, temp2, temp3, 0);
|
||||
A = sqliteExpr(TK_DOT, temp1, temp4, 0);
|
||||
}
|
||||
expr(A) ::= expr(B) ORACLE_OUTER_JOIN.
|
||||
{A = B; ExprSetProperty(A,EP_Oracle8Join);}
|
||||
expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
|
||||
expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
|
||||
expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
|
||||
expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
|
||||
A = sqliteExprFunction(Y, &X);
|
||||
sqliteExprSpan(A,&X,&E);
|
||||
}
|
||||
expr(A) ::= ID(X) LP STAR RP(E). {
|
||||
A = sqliteExprFunction(0, &X);
|
||||
sqliteExprSpan(A,&X,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
|
||||
expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
|
||||
expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
|
||||
expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
|
||||
expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
|
||||
expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
|
||||
expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
|
||||
expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
|
||||
expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
|
||||
expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
|
||||
expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
|
||||
expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
|
||||
expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
|
||||
ExprList *pList = sqliteExprListAppend(0, Y, 0);
|
||||
pList = sqliteExprListAppend(pList, X, 0);
|
||||
A = sqliteExprFunction(pList, 0);
|
||||
if( A ) A->op = OP;
|
||||
sqliteExprSpan(A, &X->span, &Y->span);
|
||||
}
|
||||
expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
|
||||
ExprList *pList = sqliteExprListAppend(0, Y, 0);
|
||||
pList = sqliteExprListAppend(pList, X, 0);
|
||||
A = sqliteExprFunction(pList, 0);
|
||||
if( A ) A->op = OP;
|
||||
A = sqliteExpr(TK_NOT, A, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&Y->span);
|
||||
}
|
||||
%type likeop {int}
|
||||
likeop(A) ::= LIKE. {A = TK_LIKE;}
|
||||
likeop(A) ::= GLOB. {A = TK_GLOB;}
|
||||
expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
|
||||
expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
|
||||
expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
|
||||
expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
|
||||
expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
|
||||
expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
|
||||
expr(A) ::= expr(X) ISNULL(E). {
|
||||
A = sqliteExpr(TK_ISNULL, X, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) IS NULL(E). {
|
||||
A = sqliteExpr(TK_ISNULL, X, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) NOTNULL(E). {
|
||||
A = sqliteExpr(TK_NOTNULL, X, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) NOT NULL(E). {
|
||||
A = sqliteExpr(TK_NOTNULL, X, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) IS NOT NULL(E). {
|
||||
A = sqliteExpr(TK_NOTNULL, X, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= NOT(B) expr(X). {
|
||||
A = sqliteExpr(TK_NOT, X, 0, 0);
|
||||
sqliteExprSpan(A,&B,&X->span);
|
||||
}
|
||||
expr(A) ::= BITNOT(B) expr(X). {
|
||||
A = sqliteExpr(TK_BITNOT, X, 0, 0);
|
||||
sqliteExprSpan(A,&B,&X->span);
|
||||
}
|
||||
expr(A) ::= MINUS(B) expr(X). [UMINUS] {
|
||||
A = sqliteExpr(TK_UMINUS, X, 0, 0);
|
||||
sqliteExprSpan(A,&B,&X->span);
|
||||
}
|
||||
expr(A) ::= PLUS(B) expr(X). [UPLUS] {
|
||||
A = sqliteExpr(TK_UPLUS, X, 0, 0);
|
||||
sqliteExprSpan(A,&B,&X->span);
|
||||
}
|
||||
expr(A) ::= LP(B) select(X) RP(E). {
|
||||
A = sqliteExpr(TK_SELECT, 0, 0, 0);
|
||||
if( A ) A->pSelect = X;
|
||||
sqliteExprSpan(A,&B,&E);
|
||||
}
|
||||
expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
|
||||
ExprList *pList = sqliteExprListAppend(0, X, 0);
|
||||
pList = sqliteExprListAppend(pList, Y, 0);
|
||||
A = sqliteExpr(TK_BETWEEN, W, 0, 0);
|
||||
if( A ) A->pList = pList;
|
||||
sqliteExprSpan(A,&W->span,&Y->span);
|
||||
}
|
||||
expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
|
||||
ExprList *pList = sqliteExprListAppend(0, X, 0);
|
||||
pList = sqliteExprListAppend(pList, Y, 0);
|
||||
A = sqliteExpr(TK_BETWEEN, W, 0, 0);
|
||||
if( A ) A->pList = pList;
|
||||
A = sqliteExpr(TK_NOT, A, 0, 0);
|
||||
sqliteExprSpan(A,&W->span,&Y->span);
|
||||
}
|
||||
expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
|
||||
A = sqliteExpr(TK_IN, X, 0, 0);
|
||||
if( A ) A->pList = Y;
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) IN LP select(Y) RP(E). {
|
||||
A = sqliteExpr(TK_IN, X, 0, 0);
|
||||
if( A ) A->pSelect = Y;
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
|
||||
A = sqliteExpr(TK_IN, X, 0, 0);
|
||||
if( A ) A->pList = Y;
|
||||
A = sqliteExpr(TK_NOT, A, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
|
||||
A = sqliteExpr(TK_IN, X, 0, 0);
|
||||
if( A ) A->pSelect = Y;
|
||||
A = sqliteExpr(TK_NOT, A, 0, 0);
|
||||
sqliteExprSpan(A,&X->span,&E);
|
||||
}
|
||||
|
||||
/* CASE expressions */
|
||||
expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
|
||||
A = sqliteExpr(TK_CASE, X, Z, 0);
|
||||
if( A ) A->pList = Y;
|
||||
sqliteExprSpan(A, &C, &E);
|
||||
}
|
||||
%type case_exprlist {ExprList*}
|
||||
%destructor case_exprlist {sqliteExprListDelete($$);}
|
||||
case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
|
||||
A = sqliteExprListAppend(X, Y, 0);
|
||||
A = sqliteExprListAppend(A, Z, 0);
|
||||
}
|
||||
case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
|
||||
A = sqliteExprListAppend(0, Y, 0);
|
||||
A = sqliteExprListAppend(A, Z, 0);
|
||||
}
|
||||
%type case_else {Expr*}
|
||||
case_else(A) ::= ELSE expr(X). {A = X;}
|
||||
case_else(A) ::= . {A = 0;}
|
||||
%type case_operand {Expr*}
|
||||
case_operand(A) ::= expr(X). {A = X;}
|
||||
case_operand(A) ::= . {A = 0;}
|
||||
|
||||
%type exprlist {ExprList*}
|
||||
%destructor exprlist {sqliteExprListDelete($$);}
|
||||
%type expritem {Expr*}
|
||||
%destructor expritem {sqliteExprDelete($$);}
|
||||
|
||||
exprlist(A) ::= exprlist(X) COMMA expritem(Y).
|
||||
{A = sqliteExprListAppend(X,Y,0);}
|
||||
exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
|
||||
expritem(A) ::= expr(X). {A = X;}
|
||||
expritem(A) ::= . {A = 0;}
|
||||
|
||||
///////////////////////////// The CREATE INDEX command ///////////////////////
|
||||
//
|
||||
cmd ::= CREATE(S) temp(T) uniqueflag(U) INDEX nm(X)
|
||||
ON nm(Y) dbnm(D) LP idxlist(Z) RP(E) onconf(R). {
|
||||
SrcList *pSrc = sqliteSrcListAppend(0, &Y, &D);
|
||||
if( U!=OE_None ) U = R;
|
||||
if( U==OE_Default) U = OE_Abort;
|
||||
sqliteCreateIndex(pParse, &X, pSrc, Z, U, T, &S, &E);
|
||||
}
|
||||
|
||||
%type uniqueflag {int}
|
||||
uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
|
||||
uniqueflag(A) ::= . { A = OE_None; }
|
||||
|
||||
%type idxlist {IdList*}
|
||||
%destructor idxlist {sqliteIdListDelete($$);}
|
||||
%type idxlist_opt {IdList*}
|
||||
%destructor idxlist_opt {sqliteIdListDelete($$);}
|
||||
%type idxitem {Token}
|
||||
|
||||
idxlist_opt(A) ::= . {A = 0;}
|
||||
idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
|
||||
idxlist(A) ::= idxlist(X) COMMA idxitem(Y). {A = sqliteIdListAppend(X,&Y);}
|
||||
idxlist(A) ::= idxitem(Y). {A = sqliteIdListAppend(0,&Y);}
|
||||
idxitem(A) ::= nm(X) sortorder. {A = X;}
|
||||
|
||||
///////////////////////////// The DROP INDEX command /////////////////////////
|
||||
//
|
||||
|
||||
cmd ::= DROP INDEX nm(X) dbnm(Y). {
|
||||
sqliteDropIndex(pParse, sqliteSrcListAppend(0,&X,&Y));
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////// The COPY command ///////////////////////////////
|
||||
//
|
||||
cmd ::= COPY orconf(R) nm(X) dbnm(D) FROM nm(Y) USING DELIMITERS STRING(Z).
|
||||
{sqliteCopy(pParse,sqliteSrcListAppend(0,&X,&D),&Y,&Z,R);}
|
||||
cmd ::= COPY orconf(R) nm(X) dbnm(D) FROM nm(Y).
|
||||
{sqliteCopy(pParse,sqliteSrcListAppend(0,&X,&D),&Y,0,R);}
|
||||
|
||||
///////////////////////////// The VACUUM command /////////////////////////////
|
||||
//
|
||||
cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
|
||||
cmd ::= VACUUM nm(X). {sqliteVacuum(pParse,&X);}
|
||||
|
||||
///////////////////////////// The PRAGMA command /////////////////////////////
|
||||
//
|
||||
cmd ::= PRAGMA ids(X) EQ nm(Y). {sqlitePragma(pParse,&X,&Y,0);}
|
||||
cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
|
||||
cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
|
||||
cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
|
||||
cmd ::= PRAGMA ids(X) LP nm(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
|
||||
cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
|
||||
plus_num(A) ::= plus_opt number(X). {A = X;}
|
||||
minus_num(A) ::= MINUS number(X). {A = X;}
|
||||
number(A) ::= INTEGER(X). {A = X;}
|
||||
number(A) ::= FLOAT(X). {A = X;}
|
||||
plus_opt ::= PLUS.
|
||||
plus_opt ::= .
|
||||
|
||||
//////////////////////////// The CREATE TRIGGER command /////////////////////
|
||||
|
||||
cmd ::= CREATE(A) trigger_decl BEGIN trigger_cmd_list(S) END(Z). {
|
||||
Token all;
|
||||
all.z = A.z;
|
||||
all.n = (Z.z - A.z) + Z.n;
|
||||
sqliteFinishTrigger(pParse, S, &all);
|
||||
}
|
||||
|
||||
trigger_decl ::= temp(T) TRIGGER nm(B) trigger_time(C) trigger_event(D)
|
||||
ON nm(E) dbnm(DB) foreach_clause(F) when_clause(G). {
|
||||
SrcList *pTab = sqliteSrcListAppend(0, &E, &DB);
|
||||
sqliteBeginTrigger(pParse, &B, C, D.a, D.b, pTab, F, G, T);
|
||||
}
|
||||
|
||||
%type trigger_time {int}
|
||||
trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
|
||||
trigger_time(A) ::= AFTER. { A = TK_AFTER; }
|
||||
trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
|
||||
trigger_time(A) ::= . { A = TK_BEFORE; }
|
||||
|
||||
%type trigger_event {struct TrigEvent}
|
||||
%destructor trigger_event {sqliteIdListDelete($$.b);}
|
||||
trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; }
|
||||
trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; }
|
||||
trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;}
|
||||
trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; }
|
||||
|
||||
%type foreach_clause {int}
|
||||
foreach_clause(A) ::= . { A = TK_ROW; }
|
||||
foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
|
||||
foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
|
||||
|
||||
%type when_clause {Expr *}
|
||||
when_clause(A) ::= . { A = 0; }
|
||||
when_clause(A) ::= WHEN expr(X). { A = X; }
|
||||
|
||||
%type trigger_cmd_list {TriggerStep *}
|
||||
%destructor trigger_cmd_list {sqliteDeleteTriggerStep($$);}
|
||||
trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
|
||||
X->pNext = Y;
|
||||
A = X;
|
||||
}
|
||||
trigger_cmd_list(A) ::= . { A = 0; }
|
||||
|
||||
%type trigger_cmd {TriggerStep *}
|
||||
%destructor trigger_cmd {sqliteDeleteTriggerStep($$);}
|
||||
// UPDATE
|
||||
trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
|
||||
{ A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
|
||||
|
||||
// INSERT
|
||||
trigger_cmd(A) ::= INSERT orconf(R) INTO nm(X) inscollist_opt(F)
|
||||
VALUES LP itemlist(Y) RP.
|
||||
{A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
|
||||
|
||||
trigger_cmd(A) ::= INSERT orconf(R) INTO nm(X) inscollist_opt(F) select(S).
|
||||
{A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
|
||||
|
||||
// DELETE
|
||||
trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
|
||||
{A = sqliteTriggerDeleteStep(&X, Y);}
|
||||
|
||||
// SELECT
|
||||
trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); }
|
||||
|
||||
// The special RAISE expression that may occur in trigger programs
|
||||
expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
|
||||
A = sqliteExpr(TK_RAISE, 0, 0, 0);
|
||||
A->iColumn = OE_Ignore;
|
||||
sqliteExprSpan(A, &X, &Y);
|
||||
}
|
||||
expr(A) ::= RAISE(X) LP ROLLBACK COMMA nm(Z) RP(Y). {
|
||||
A = sqliteExpr(TK_RAISE, 0, 0, &Z);
|
||||
A->iColumn = OE_Rollback;
|
||||
sqliteExprSpan(A, &X, &Y);
|
||||
}
|
||||
expr(A) ::= RAISE(X) LP ABORT COMMA nm(Z) RP(Y). {
|
||||
A = sqliteExpr(TK_RAISE, 0, 0, &Z);
|
||||
A->iColumn = OE_Abort;
|
||||
sqliteExprSpan(A, &X, &Y);
|
||||
}
|
||||
expr(A) ::= RAISE(X) LP FAIL COMMA nm(Z) RP(Y). {
|
||||
A = sqliteExpr(TK_RAISE, 0, 0, &Z);
|
||||
A->iColumn = OE_Fail;
|
||||
sqliteExprSpan(A, &X, &Y);
|
||||
}
|
||||
|
||||
//////////////////////// DROP TRIGGER statement //////////////////////////////
|
||||
cmd ::= DROP TRIGGER nm(X) dbnm(D). {
|
||||
sqliteDropTrigger(pParse,sqliteSrcListAppend(0,&X,&D));
|
||||
}
|
||||
|
||||
//////////////////////// ATTACH DATABASE file AS name /////////////////////////
|
||||
cmd ::= ATTACH database_kw_opt ids(F) AS nm(D). {
|
||||
sqliteAttach(pParse, &F, &D);
|
||||
}
|
||||
|
||||
database_kw_opt ::= DATABASE.
|
||||
database_kw_opt ::= .
|
||||
|
||||
//////////////////////// DETACH DATABASE name /////////////////////////////////
|
||||
cmd ::= DETACH database_kw_opt nm(D). {
|
||||
sqliteDetach(pParse, &D);
|
||||
}
|
||||
@@ -1,539 +0,0 @@
|
||||
/*
|
||||
** 2003 April 6
|
||||
**
|
||||
** 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 contains code used to implement the PRAGMA command.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include <ctype.h>
|
||||
|
||||
/*
|
||||
** Interpret the given string as a boolean value.
|
||||
*/
|
||||
static int getBoolean(char *z){
|
||||
static char *azTrue[] = { "yes", "on", "true" };
|
||||
int i;
|
||||
if( z[0]==0 ) return 0;
|
||||
if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
|
||||
return atoi(z);
|
||||
}
|
||||
for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
|
||||
if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Interpret the given string as a safety level. Return 0 for OFF,
|
||||
** 1 for ON or NORMAL and 2 for FULL.
|
||||
**
|
||||
** Note that the values returned are one less that the values that
|
||||
** should be passed into sqliteBtreeSetSafetyLevel(). The is done
|
||||
** to support legacy SQL code. The safety level used to be boolean
|
||||
** and older scripts may have used numbers 0 for OFF and 1 for ON.
|
||||
*/
|
||||
static int getSafetyLevel(char *z){
|
||||
static const struct {
|
||||
const char *zWord;
|
||||
int val;
|
||||
} aKey[] = {
|
||||
{ "no", 0 },
|
||||
{ "off", 0 },
|
||||
{ "false", 0 },
|
||||
{ "yes", 1 },
|
||||
{ "on", 1 },
|
||||
{ "true", 1 },
|
||||
{ "full", 2 },
|
||||
};
|
||||
int i;
|
||||
if( z[0]==0 ) return 1;
|
||||
if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
|
||||
return atoi(z);
|
||||
}
|
||||
for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
|
||||
if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Interpret the given string as a temp db location. Return 1 for file
|
||||
** backed temporary databases, 2 for the Red-Black tree in memory database
|
||||
** and 0 to use the compile-time default.
|
||||
*/
|
||||
static int getTempStore(char *z){
|
||||
if( z[0]>='0' || z[0]<='2' ){
|
||||
return z[0] - '0';
|
||||
}else if( sqliteStrICmp(z, "file")==0 ){
|
||||
return 1;
|
||||
}else if( sqliteStrICmp(z, "memory")==0 ){
|
||||
return 2;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Process a pragma statement.
|
||||
**
|
||||
** Pragmas are of this form:
|
||||
**
|
||||
** PRAGMA id = value
|
||||
**
|
||||
** The identifier might also be a string. The value is a string, and
|
||||
** identifier, or a number. If minusFlag is true, then the value is
|
||||
** a number that was preceded by a minus sign.
|
||||
*/
|
||||
void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
|
||||
char *zLeft = 0;
|
||||
char *zRight = 0;
|
||||
sqlite *db = pParse->db;
|
||||
Vdbe *v = sqliteGetVdbe(pParse);
|
||||
if( v==0 ) return;
|
||||
|
||||
zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
|
||||
sqliteDequote(zLeft);
|
||||
if( minusFlag ){
|
||||
zRight = 0;
|
||||
sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
|
||||
}else{
|
||||
zRight = sqliteStrNDup(pRight->z, pRight->n);
|
||||
sqliteDequote(zRight);
|
||||
}
|
||||
if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, 0) ){
|
||||
sqliteFree(zLeft);
|
||||
sqliteFree(zRight);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
** PRAGMA default_cache_size
|
||||
** PRAGMA default_cache_size=N
|
||||
**
|
||||
** The first form reports the current persistent setting for the
|
||||
** page cache size. The value returned is the maximum number of
|
||||
** pages in the page cache. The second form sets both the current
|
||||
** page cache size value and the persistent page cache size value
|
||||
** stored in the database file.
|
||||
**
|
||||
** The default cache size is stored in meta-value 2 of page 1 of the
|
||||
** database file. The cache size is actually the absolute value of
|
||||
** this memory location. The sign of meta-value 2 determines the
|
||||
** synchronous setting. A negative value means synchronous is off
|
||||
** and a positive value means synchronous is on.
|
||||
*/
|
||||
if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
|
||||
static VdbeOp getCacheSize[] = {
|
||||
{ OP_ReadCookie, 0, 2, 0},
|
||||
{ OP_AbsValue, 0, 0, 0},
|
||||
{ OP_Dup, 0, 0, 0},
|
||||
{ OP_Integer, 0, 0, 0},
|
||||
{ OP_Ne, 0, 6, 0},
|
||||
{ OP_Integer, MAX_PAGES,0, 0},
|
||||
{ OP_ColumnName, 0, 0, "cache_size"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
};
|
||||
if( pRight->z==pLeft->z ){
|
||||
sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
|
||||
}else{
|
||||
int addr;
|
||||
int size = atoi(zRight);
|
||||
if( size<0 ) size = -size;
|
||||
sqliteBeginWriteOperation(pParse, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_Integer, size, 0);
|
||||
sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
|
||||
addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
|
||||
sqliteVdbeAddOp(v, OP_Negative, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
|
||||
sqliteEndWriteOperation(pParse);
|
||||
db->cache_size = db->cache_size<0 ? -size : size;
|
||||
sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
|
||||
}
|
||||
}else
|
||||
|
||||
/*
|
||||
** PRAGMA cache_size
|
||||
** PRAGMA cache_size=N
|
||||
**
|
||||
** The first form reports the current local setting for the
|
||||
** page cache size. The local setting can be different from
|
||||
** the persistent cache size value that is stored in the database
|
||||
** file itself. The value returned is the maximum number of
|
||||
** pages in the page cache. The second form sets the local
|
||||
** page cache size value. It does not change the persistent
|
||||
** cache size stored on the disk so the cache size will revert
|
||||
** to its default value when the database is closed and reopened.
|
||||
** N should be a positive integer.
|
||||
*/
|
||||
if( sqliteStrICmp(zLeft,"cache_size")==0 ){
|
||||
static VdbeOp getCacheSize[] = {
|
||||
{ OP_ColumnName, 0, 0, "cache_size"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
};
|
||||
if( pRight->z==pLeft->z ){
|
||||
int size = db->cache_size;;
|
||||
if( size<0 ) size = -size;
|
||||
sqliteVdbeAddOp(v, OP_Integer, size, 0);
|
||||
sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
|
||||
}else{
|
||||
int size = atoi(zRight);
|
||||
if( size<0 ) size = -size;
|
||||
if( db->cache_size<0 ) size = -size;
|
||||
db->cache_size = size;
|
||||
sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
|
||||
}
|
||||
}else
|
||||
|
||||
/*
|
||||
** PRAGMA default_synchronous
|
||||
** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
|
||||
**
|
||||
** The first form returns the persistent value of the "synchronous" setting
|
||||
** that is stored in the database. This is the synchronous setting that
|
||||
** is used whenever the database is opened unless overridden by a separate
|
||||
** "synchronous" pragma. The second form changes the persistent and the
|
||||
** local synchronous setting to the value given.
|
||||
**
|
||||
** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
|
||||
** to make sure data is committed to disk. Write operations are very fast,
|
||||
** but a power failure can leave the database in an inconsistent state.
|
||||
** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
|
||||
** make sure data is being written to disk. The risk of corruption due to
|
||||
** a power loss in this mode is negligible but non-zero. If synchronous
|
||||
** is FULL, extra fsync()s occur to reduce the risk of corruption to near
|
||||
** zero, but with a write performance penalty. The default mode is NORMAL.
|
||||
*/
|
||||
if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
|
||||
static VdbeOp getSync[] = {
|
||||
{ OP_ColumnName, 0, 0, "synchronous"},
|
||||
{ OP_ReadCookie, 0, 3, 0},
|
||||
{ OP_Dup, 0, 0, 0},
|
||||
{ OP_If, 0, 0, 0}, /* 3 */
|
||||
{ OP_ReadCookie, 0, 2, 0},
|
||||
{ OP_Integer, 0, 0, 0},
|
||||
{ OP_Lt, 0, 5, 0},
|
||||
{ OP_AddImm, 1, 0, 0},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
{ OP_Halt, 0, 0, 0},
|
||||
{ OP_AddImm, -1, 0, 0}, /* 10 */
|
||||
{ OP_Callback, 1, 0, 0}
|
||||
};
|
||||
if( pRight->z==pLeft->z ){
|
||||
int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
|
||||
sqliteVdbeChangeP2(v, addr+3, addr+10);
|
||||
}else{
|
||||
int addr;
|
||||
int size = db->cache_size;
|
||||
if( size<0 ) size = -size;
|
||||
sqliteBeginWriteOperation(pParse, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
|
||||
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
|
||||
addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
|
||||
sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
|
||||
sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
|
||||
db->safety_level = getSafetyLevel(zRight)+1;
|
||||
if( db->safety_level==1 ){
|
||||
sqliteVdbeAddOp(v, OP_Negative, 0, 0);
|
||||
size = -size;
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
|
||||
sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
|
||||
sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
|
||||
sqliteEndWriteOperation(pParse);
|
||||
db->cache_size = size;
|
||||
sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
|
||||
sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
|
||||
}
|
||||
}else
|
||||
|
||||
/*
|
||||
** PRAGMA synchronous
|
||||
** PRAGMA synchronous=OFF|ON|NORMAL|FULL
|
||||
**
|
||||
** Return or set the local value of the synchronous flag. Changing
|
||||
** the local value does not make changes to the disk file and the
|
||||
** default value will be restored the next time the database is
|
||||
** opened.
|
||||
*/
|
||||
if( sqliteStrICmp(zLeft,"synchronous")==0 ){
|
||||
static VdbeOp getSync[] = {
|
||||
{ OP_ColumnName, 0, 0, "synchronous"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
};
|
||||
if( pRight->z==pLeft->z ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
|
||||
sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
|
||||
}else{
|
||||
int size = db->cache_size;
|
||||
if( size<0 ) size = -size;
|
||||
db->safety_level = getSafetyLevel(zRight)+1;
|
||||
if( db->safety_level==1 ) size = -size;
|
||||
db->cache_size = size;
|
||||
sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
|
||||
sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
|
||||
if( getBoolean(zRight) ){
|
||||
always_code_trigger_setup = 1;
|
||||
}else{
|
||||
always_code_trigger_setup = 0;
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
|
||||
if( getBoolean(zRight) ){
|
||||
db->flags |= SQLITE_VdbeTrace;
|
||||
}else{
|
||||
db->flags &= ~SQLITE_VdbeTrace;
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
|
||||
if( getBoolean(zRight) ){
|
||||
db->flags |= SQLITE_FullColNames;
|
||||
}else{
|
||||
db->flags &= ~SQLITE_FullColNames;
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
|
||||
if( getBoolean(zRight) ){
|
||||
db->flags |= SQLITE_ReportTypes;
|
||||
}else{
|
||||
db->flags &= ~SQLITE_ReportTypes;
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "count_changes")==0 ){
|
||||
if( getBoolean(zRight) ){
|
||||
db->flags |= SQLITE_CountRows;
|
||||
}else{
|
||||
db->flags &= ~SQLITE_CountRows;
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
|
||||
if( getBoolean(zRight) ){
|
||||
db->flags |= SQLITE_NullCallback;
|
||||
}else{
|
||||
db->flags &= ~SQLITE_NullCallback;
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "table_info")==0 ){
|
||||
Table *pTab;
|
||||
pTab = sqliteFindTable(db, zRight, 0);
|
||||
if( pTab ){
|
||||
static VdbeOp tableInfoPreface[] = {
|
||||
{ OP_ColumnName, 0, 0, "cid"},
|
||||
{ OP_ColumnName, 1, 0, "name"},
|
||||
{ OP_ColumnName, 2, 0, "type"},
|
||||
{ OP_ColumnName, 3, 0, "notnull"},
|
||||
{ OP_ColumnName, 4, 0, "dflt_value"},
|
||||
};
|
||||
int i;
|
||||
sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
|
||||
sqliteViewGetColumnNames(pParse, pTab);
|
||||
for(i=0; i<pTab->nCol; i++){
|
||||
sqliteVdbeAddOp(v, OP_Integer, i, 0);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1,
|
||||
pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 5, 0);
|
||||
}
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "index_info")==0 ){
|
||||
Index *pIdx;
|
||||
Table *pTab;
|
||||
pIdx = sqliteFindIndex(db, zRight, 0);
|
||||
if( pIdx ){
|
||||
static VdbeOp tableInfoPreface[] = {
|
||||
{ OP_ColumnName, 0, 0, "seqno"},
|
||||
{ OP_ColumnName, 1, 0, "cid"},
|
||||
{ OP_ColumnName, 2, 0, "name"},
|
||||
};
|
||||
int i;
|
||||
pTab = pIdx->pTable;
|
||||
sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
|
||||
for(i=0; i<pIdx->nColumn; i++){
|
||||
int cnum = pIdx->aiColumn[i];
|
||||
sqliteVdbeAddOp(v, OP_Integer, i, 0);
|
||||
sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
assert( pTab->nCol>cnum );
|
||||
sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 3, 0);
|
||||
}
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "index_list")==0 ){
|
||||
Index *pIdx;
|
||||
Table *pTab;
|
||||
pTab = sqliteFindTable(db, zRight, 0);
|
||||
if( pTab ){
|
||||
v = sqliteGetVdbe(pParse);
|
||||
pIdx = pTab->pIndex;
|
||||
}
|
||||
if( pTab && pIdx ){
|
||||
int i = 0;
|
||||
static VdbeOp indexListPreface[] = {
|
||||
{ OP_ColumnName, 0, 0, "seq"},
|
||||
{ OP_ColumnName, 1, 0, "name"},
|
||||
{ OP_ColumnName, 2, 0, "unique"},
|
||||
};
|
||||
|
||||
sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
|
||||
while(pIdx){
|
||||
sqliteVdbeAddOp(v, OP_Integer, i, 0);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 3, 0);
|
||||
++i;
|
||||
pIdx = pIdx->pNext;
|
||||
}
|
||||
}
|
||||
}else
|
||||
|
||||
if( sqliteStrICmp(zLeft, "database_list")==0 ){
|
||||
int i;
|
||||
static VdbeOp indexListPreface[] = {
|
||||
{ OP_ColumnName, 0, 0, "seq"},
|
||||
{ OP_ColumnName, 1, 0, "name"},
|
||||
{ OP_ColumnName, 2, 0, "file"},
|
||||
};
|
||||
|
||||
sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
|
||||
for(i=0; i<db->nDb; i++){
|
||||
if( db->aDb[i].pBt==0 ) continue;
|
||||
assert( db->aDb[i].zName!=0 );
|
||||
sqliteVdbeAddOp(v, OP_Integer, i, 0);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, db->aDb[i].zName, P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, sqliteBtreeGetFilename(db->aDb[i].pBt),
|
||||
P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 3, 0);
|
||||
}
|
||||
}else
|
||||
/*
|
||||
** PRAGMA temp_store
|
||||
** PRAGMA temp_store = "default"|"memory"|"file"
|
||||
**
|
||||
** Return or set the local value of the temp_store flag. Changing
|
||||
** the local value does not make changes to the disk file and the default
|
||||
** value will be restored the next time the database is opened.
|
||||
**
|
||||
** Note that it is possible for the library compile-time options to
|
||||
** override this setting
|
||||
*/
|
||||
if( sqliteStrICmp(zLeft, "temp_store")==0 ){
|
||||
static VdbeOp getTmpDbLoc[] = {
|
||||
{ OP_ColumnName, 0, 0, "temp_store"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
};
|
||||
if( pRight->z==pLeft->z ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, db->temp_store, 0);
|
||||
sqliteVdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
|
||||
}else{
|
||||
if (&db->aDb[1].pBt != 0) {
|
||||
sqliteErrorMsg(pParse, "The temporary database already exists - "
|
||||
"its location cannot now be changed");
|
||||
} else {
|
||||
db->temp_store = getTempStore(zRight);
|
||||
}
|
||||
}
|
||||
}else
|
||||
|
||||
/*
|
||||
** PRAGMA default_temp_store
|
||||
** PRAGMA default_temp_store = "default"|"memory"|"file"
|
||||
**
|
||||
** Return or set the value of the persistent temp_store flag (as
|
||||
** well as the value currently in force).
|
||||
**
|
||||
** Note that it is possible for the library compile-time options to
|
||||
** override this setting
|
||||
*/
|
||||
if( sqliteStrICmp(zLeft, "default_temp_store")==0 ){
|
||||
static VdbeOp getTmpDbLoc[] = {
|
||||
{ OP_ColumnName, 0, 0, "temp_store"},
|
||||
{ OP_ReadCookie, 0, 5, 0},
|
||||
{ OP_Callback, 1, 0, 0}};
|
||||
if( pRight->z==pLeft->z ){
|
||||
sqliteVdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
|
||||
}else{
|
||||
if (&db->aDb[1].pBt != 0) {
|
||||
sqliteErrorMsg(pParse, "The temporary database already exists - "
|
||||
"its location cannot now be changed");
|
||||
} else {
|
||||
sqliteBeginWriteOperation(pParse, 0, 0);
|
||||
db->temp_store = getTempStore(zRight);
|
||||
sqliteVdbeAddOp(v, OP_Integer, db->temp_store, 0);
|
||||
sqliteVdbeAddOp(v, OP_SetCookie, 0, 5);
|
||||
sqliteEndWriteOperation(pParse);
|
||||
}
|
||||
}
|
||||
}else
|
||||
|
||||
#ifndef NDEBUG
|
||||
if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
|
||||
extern void sqliteParserTrace(FILE*, char *);
|
||||
if( getBoolean(zRight) ){
|
||||
sqliteParserTrace(stdout, "parser: ");
|
||||
}else{
|
||||
sqliteParserTrace(0, 0);
|
||||
}
|
||||
}else
|
||||
#endif
|
||||
|
||||
if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
|
||||
static VdbeOp checkDb[] = {
|
||||
{ OP_SetInsert, 0, 0, "2"},
|
||||
{ OP_Integer, 0, 0, 0},
|
||||
{ OP_OpenRead, 0, 2, 0},
|
||||
{ OP_Rewind, 0, 7, 0},
|
||||
{ OP_Column, 0, 3, 0}, /* 4 */
|
||||
{ OP_SetInsert, 0, 0, 0},
|
||||
{ OP_Next, 0, 4, 0},
|
||||
{ OP_IntegrityCk, 0, 0, 0}, /* 7 */
|
||||
{ OP_ColumnName, 0, 0, "integrity_check"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
{ OP_SetInsert, 1, 0, "2"},
|
||||
{ OP_Integer, 1, 0, 0},
|
||||
{ OP_OpenRead, 1, 2, 0},
|
||||
{ OP_Rewind, 1, 17, 0},
|
||||
{ OP_Column, 1, 3, 0}, /* 14 */
|
||||
{ OP_SetInsert, 1, 0, 0},
|
||||
{ OP_Next, 1, 14, 0},
|
||||
{ OP_IntegrityCk, 1, 1, 0}, /* 17 */
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
};
|
||||
sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
|
||||
}else
|
||||
|
||||
{}
|
||||
sqliteFree(zLeft);
|
||||
sqliteFree(zRight);
|
||||
}
|
||||
@@ -1,823 +0,0 @@
|
||||
/*
|
||||
** The "printf" code that follows dates from the 1980's. It is in
|
||||
** the public domain. The original comments are included here for
|
||||
** completeness. They are slightly out-of-date.
|
||||
**
|
||||
** The following modules is an enhanced replacement for the "printf" subroutines
|
||||
** found in the standard C library. The following enhancements are
|
||||
** supported:
|
||||
**
|
||||
** + Additional functions. The standard set of "printf" functions
|
||||
** includes printf, fprintf, sprintf, vprintf, vfprintf, and
|
||||
** vsprintf. This module adds the following:
|
||||
**
|
||||
** * snprintf -- Works like sprintf, but has an extra argument
|
||||
** which is the size of the buffer written to.
|
||||
**
|
||||
** * mprintf -- Similar to sprintf. Writes output to memory
|
||||
** obtained from malloc.
|
||||
**
|
||||
** * xprintf -- Calls a function to dispose of output.
|
||||
**
|
||||
** * nprintf -- No output, but returns the number of characters
|
||||
** that would have been output by printf.
|
||||
**
|
||||
** * A v- version (ex: vsnprintf) of every function is also
|
||||
** supplied.
|
||||
**
|
||||
** + A few extensions to the formatting notation are supported:
|
||||
**
|
||||
** * The "=" flag (similar to "-") causes the output to be
|
||||
** be centered in the appropriately sized field.
|
||||
**
|
||||
** * The %b field outputs an integer in binary notation.
|
||||
**
|
||||
** * The %c field now accepts a precision. The character output
|
||||
** is repeated by the number of times the precision specifies.
|
||||
**
|
||||
** * The %' field works like %c, but takes as its character the
|
||||
** next character of the format string, instead of the next
|
||||
** argument. For example, printf("%.78'-") prints 78 minus
|
||||
** signs, the same as printf("%.78c",'-').
|
||||
**
|
||||
** + When compiled using GCC on a SPARC, this version of printf is
|
||||
** faster than the library printf for SUN OS 4.1.
|
||||
**
|
||||
** + All functions are fully reentrant.
|
||||
**
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** Undefine COMPATIBILITY to make some slight changes in the way things
|
||||
** work. I think the changes are an improvement, but they are not
|
||||
** backwards compatible.
|
||||
*/
|
||||
/* #define COMPATIBILITY / * Compatible with SUN OS 4.1 */
|
||||
|
||||
/*
|
||||
** Conversion types fall into various categories as defined by the
|
||||
** following enumeration.
|
||||
*/
|
||||
enum et_type { /* The type of the format field */
|
||||
etRADIX, /* Integer types. %d, %x, %o, and so forth */
|
||||
etFLOAT, /* Floating point. %f */
|
||||
etEXP, /* Exponentional notation. %e and %E */
|
||||
etGENERIC, /* Floating or exponential, depending on exponent. %g */
|
||||
etSIZE, /* Return number of characters processed so far. %n */
|
||||
etSTRING, /* Strings. %s */
|
||||
etPERCENT, /* Percent symbol. %% */
|
||||
etCHARX, /* Characters. %c */
|
||||
etERROR, /* Used to indicate no such conversion type */
|
||||
/* The rest are extensions, not normally found in printf() */
|
||||
etCHARLIT, /* Literal characters. %' */
|
||||
etSQLESCAPE, /* Strings with '\'' doubled. %q */
|
||||
etSQLESCAPE2, /* Strings with '\'' doubled and enclosed in '',
|
||||
NULL pointers replaced by SQL NULL. %Q */
|
||||
etORDINAL /* 1st, 2nd, 3rd and so forth */
|
||||
};
|
||||
|
||||
/*
|
||||
** Each builtin conversion character (ex: the 'd' in "%d") is described
|
||||
** by an instance of the following structure
|
||||
*/
|
||||
typedef struct et_info { /* Information about each format field */
|
||||
int fmttype; /* The format field code letter */
|
||||
int base; /* The base for radix conversion */
|
||||
char *charset; /* The character set for conversion */
|
||||
int flag_signed; /* Is the quantity signed? */
|
||||
char *prefix; /* Prefix on non-zero values in alt format */
|
||||
enum et_type type; /* Conversion paradigm */
|
||||
} et_info;
|
||||
|
||||
/*
|
||||
** The following table is searched linearly, so it is good to put the
|
||||
** most frequently used conversion types first.
|
||||
*/
|
||||
static et_info fmtinfo[] = {
|
||||
{ 'd', 10, "0123456789", 1, 0, etRADIX, },
|
||||
{ 's', 0, 0, 0, 0, etSTRING, },
|
||||
{ 'q', 0, 0, 0, 0, etSQLESCAPE, },
|
||||
{ 'Q', 0, 0, 0, 0, etSQLESCAPE2, },
|
||||
{ 'c', 0, 0, 0, 0, etCHARX, },
|
||||
{ 'o', 8, "01234567", 0, "0", etRADIX, },
|
||||
{ 'u', 10, "0123456789", 0, 0, etRADIX, },
|
||||
{ 'x', 16, "0123456789abcdef", 0, "x0", etRADIX, },
|
||||
{ 'X', 16, "0123456789ABCDEF", 0, "X0", etRADIX, },
|
||||
{ 'r', 10, "0123456789", 0, 0, etORDINAL, },
|
||||
{ 'f', 0, 0, 1, 0, etFLOAT, },
|
||||
{ 'e', 0, "e", 1, 0, etEXP, },
|
||||
{ 'E', 0, "E", 1, 0, etEXP, },
|
||||
{ 'g', 0, "e", 1, 0, etGENERIC, },
|
||||
{ 'G', 0, "E", 1, 0, etGENERIC, },
|
||||
{ 'i', 10, "0123456789", 1, 0, etRADIX, },
|
||||
{ 'n', 0, 0, 0, 0, etSIZE, },
|
||||
{ '%', 0, 0, 0, 0, etPERCENT, },
|
||||
{ 'b', 2, "01", 0, "b0", etRADIX, }, /* Binary */
|
||||
{ 'p', 10, "0123456789", 0, 0, etRADIX, }, /* Pointers */
|
||||
{ '\'', 0, 0, 0, 0, etCHARLIT, }, /* Literal char */
|
||||
};
|
||||
#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
|
||||
|
||||
/*
|
||||
** If NOFLOATINGPOINT is defined, then none of the floating point
|
||||
** conversions will work.
|
||||
*/
|
||||
#ifndef etNOFLOATINGPOINT
|
||||
/*
|
||||
** "*val" is a double such that 0.1 <= *val < 10.0
|
||||
** Return the ascii code for the leading digit of *val, then
|
||||
** multiply "*val" by 10.0 to renormalize.
|
||||
**
|
||||
** Example:
|
||||
** input: *val = 3.14159
|
||||
** output: *val = 1.4159 function return = '3'
|
||||
**
|
||||
** The counter *cnt is incremented each time. After counter exceeds
|
||||
** 16 (the number of significant digits in a 64-bit float) '0' is
|
||||
** always returned.
|
||||
*/
|
||||
static int et_getdigit(double *val, int *cnt){
|
||||
int digit;
|
||||
double d;
|
||||
if( (*cnt)++ >= 16 ) return '0';
|
||||
digit = (int)*val;
|
||||
d = digit;
|
||||
digit += '0';
|
||||
*val = (*val - d)*10.0;
|
||||
return digit;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define etBUFSIZE 1000 /* Size of the output buffer */
|
||||
|
||||
/*
|
||||
** The root program. All variations call this core.
|
||||
**
|
||||
** INPUTS:
|
||||
** func This is a pointer to a function taking three arguments
|
||||
** 1. A pointer to anything. Same as the "arg" parameter.
|
||||
** 2. A pointer to the list of characters to be output
|
||||
** (Note, this list is NOT null terminated.)
|
||||
** 3. An integer number of characters to be output.
|
||||
** (Note: This number might be zero.)
|
||||
**
|
||||
** arg This is the pointer to anything which will be passed as the
|
||||
** first argument to "func". Use it for whatever you like.
|
||||
**
|
||||
** fmt This is the format string, as in the usual print.
|
||||
**
|
||||
** ap This is a pointer to a list of arguments. Same as in
|
||||
** vfprint.
|
||||
**
|
||||
** OUTPUTS:
|
||||
** The return value is the total number of characters sent to
|
||||
** the function "func". Returns -1 on a error.
|
||||
**
|
||||
** Note that the order in which automatic variables are declared below
|
||||
** seems to make a big difference in determining how fast this beast
|
||||
** will run.
|
||||
*/
|
||||
static int vxprintf(
|
||||
void (*func)(void*,char*,int),
|
||||
void *arg,
|
||||
const char *format,
|
||||
va_list ap
|
||||
){
|
||||
register const char *fmt; /* The format string. */
|
||||
register int c; /* Next character in the format string */
|
||||
register char *bufpt; /* Pointer to the conversion buffer */
|
||||
register int precision; /* Precision of the current field */
|
||||
register int length; /* Length of the field */
|
||||
register int idx; /* A general purpose loop counter */
|
||||
int count; /* Total number of characters output */
|
||||
int width; /* Width of the current field */
|
||||
int flag_leftjustify; /* True if "-" flag is present */
|
||||
int flag_plussign; /* True if "+" flag is present */
|
||||
int flag_blanksign; /* True if " " flag is present */
|
||||
int flag_alternateform; /* True if "#" flag is present */
|
||||
int flag_zeropad; /* True if field width constant starts with zero */
|
||||
int flag_long; /* True if "l" flag is present */
|
||||
int flag_center; /* True if "=" flag is present */
|
||||
unsigned long longvalue; /* Value for integer types */
|
||||
double realvalue; /* Value for real types */
|
||||
et_info *infop; /* Pointer to the appropriate info structure */
|
||||
char buf[etBUFSIZE]; /* Conversion buffer */
|
||||
char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
|
||||
int errorflag = 0; /* True if an error is encountered */
|
||||
enum et_type xtype; /* Conversion paradigm */
|
||||
char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
|
||||
static char spaces[] = " "
|
||||
" ";
|
||||
#define etSPACESIZE (sizeof(spaces)-1)
|
||||
#ifndef etNOFLOATINGPOINT
|
||||
int exp; /* exponent of real numbers */
|
||||
double rounder; /* Used for rounding floating point values */
|
||||
int flag_dp; /* True if decimal point should be shown */
|
||||
int flag_rtz; /* True if trailing zeros should be removed */
|
||||
int flag_exp; /* True to force display of the exponent */
|
||||
int nsd; /* Number of significant digits returned */
|
||||
#endif
|
||||
|
||||
fmt = format; /* Put in a register for speed */
|
||||
count = length = 0;
|
||||
bufpt = 0;
|
||||
for(; (c=(*fmt))!=0; ++fmt){
|
||||
if( c!='%' ){
|
||||
register int amt;
|
||||
bufpt = (char *)fmt;
|
||||
amt = 1;
|
||||
while( (c=(*++fmt))!='%' && c!=0 ) amt++;
|
||||
(*func)(arg,bufpt,amt);
|
||||
count += amt;
|
||||
if( c==0 ) break;
|
||||
}
|
||||
if( (c=(*++fmt))==0 ){
|
||||
errorflag = 1;
|
||||
(*func)(arg,"%",1);
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
/* Find out what flags are present */
|
||||
flag_leftjustify = flag_plussign = flag_blanksign =
|
||||
flag_alternateform = flag_zeropad = flag_center = 0;
|
||||
do{
|
||||
switch( c ){
|
||||
case '-': flag_leftjustify = 1; c = 0; break;
|
||||
case '+': flag_plussign = 1; c = 0; break;
|
||||
case ' ': flag_blanksign = 1; c = 0; break;
|
||||
case '#': flag_alternateform = 1; c = 0; break;
|
||||
case '0': flag_zeropad = 1; c = 0; break;
|
||||
case '=': flag_center = 1; c = 0; break;
|
||||
default: break;
|
||||
}
|
||||
}while( c==0 && (c=(*++fmt))!=0 );
|
||||
if( flag_center ) flag_leftjustify = 0;
|
||||
/* Get the field width */
|
||||
width = 0;
|
||||
if( c=='*' ){
|
||||
width = va_arg(ap,int);
|
||||
if( width<0 ){
|
||||
flag_leftjustify = 1;
|
||||
width = -width;
|
||||
}
|
||||
c = *++fmt;
|
||||
}else{
|
||||
while( c>='0' && c<='9' ){
|
||||
width = width*10 + c - '0';
|
||||
c = *++fmt;
|
||||
}
|
||||
}
|
||||
if( width > etBUFSIZE-10 ){
|
||||
width = etBUFSIZE-10;
|
||||
}
|
||||
/* Get the precision */
|
||||
if( c=='.' ){
|
||||
precision = 0;
|
||||
c = *++fmt;
|
||||
if( c=='*' ){
|
||||
precision = va_arg(ap,int);
|
||||
#ifndef etCOMPATIBILITY
|
||||
/* This is sensible, but SUN OS 4.1 doesn't do it. */
|
||||
if( precision<0 ) precision = -precision;
|
||||
#endif
|
||||
c = *++fmt;
|
||||
}else{
|
||||
while( c>='0' && c<='9' ){
|
||||
precision = precision*10 + c - '0';
|
||||
c = *++fmt;
|
||||
}
|
||||
}
|
||||
/* Limit the precision to prevent overflowing buf[] during conversion */
|
||||
if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;
|
||||
}else{
|
||||
precision = -1;
|
||||
}
|
||||
/* Get the conversion type modifier */
|
||||
if( c=='l' ){
|
||||
flag_long = 1;
|
||||
c = *++fmt;
|
||||
}else{
|
||||
flag_long = 0;
|
||||
}
|
||||
/* Fetch the info entry for the field */
|
||||
infop = 0;
|
||||
for(idx=0; idx<etNINFO; idx++){
|
||||
if( c==fmtinfo[idx].fmttype ){
|
||||
infop = &fmtinfo[idx];
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* No info entry found. It must be an error. */
|
||||
if( infop==0 ){
|
||||
xtype = etERROR;
|
||||
}else{
|
||||
xtype = infop->type;
|
||||
}
|
||||
zExtra = 0;
|
||||
|
||||
/*
|
||||
** At this point, variables are initialized as follows:
|
||||
**
|
||||
** flag_alternateform TRUE if a '#' is present.
|
||||
** flag_plussign TRUE if a '+' is present.
|
||||
** flag_leftjustify TRUE if a '-' is present or if the
|
||||
** field width was negative.
|
||||
** flag_zeropad TRUE if the width began with 0.
|
||||
** flag_long TRUE if the letter 'l' (ell) prefixed
|
||||
** the conversion character.
|
||||
** flag_blanksign TRUE if a ' ' is present.
|
||||
** width The specified field width. This is
|
||||
** always non-negative. Zero is the default.
|
||||
** precision The specified precision. The default
|
||||
** is -1.
|
||||
** xtype The class of the conversion.
|
||||
** infop Pointer to the appropriate info struct.
|
||||
*/
|
||||
switch( xtype ){
|
||||
case etORDINAL:
|
||||
case etRADIX:
|
||||
if( flag_long ) longvalue = va_arg(ap,long);
|
||||
else longvalue = va_arg(ap,int);
|
||||
#ifdef etCOMPATIBILITY
|
||||
/* For the format %#x, the value zero is printed "0" not "0x0".
|
||||
** I think this is stupid. */
|
||||
if( longvalue==0 ) flag_alternateform = 0;
|
||||
#else
|
||||
/* More sensible: turn off the prefix for octal (to prevent "00"),
|
||||
** but leave the prefix for hex. */
|
||||
if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;
|
||||
#endif
|
||||
if( infop->flag_signed ){
|
||||
if( *(long*)&longvalue<0 ){
|
||||
longvalue = -*(long*)&longvalue;
|
||||
prefix = '-';
|
||||
}else if( flag_plussign ) prefix = '+';
|
||||
else if( flag_blanksign ) prefix = ' ';
|
||||
else prefix = 0;
|
||||
}else prefix = 0;
|
||||
if( flag_zeropad && precision<width-(prefix!=0) ){
|
||||
precision = width-(prefix!=0);
|
||||
}
|
||||
bufpt = &buf[etBUFSIZE];
|
||||
if( xtype==etORDINAL ){
|
||||
long a,b;
|
||||
a = longvalue%10;
|
||||
b = longvalue%100;
|
||||
bufpt -= 2;
|
||||
if( a==0 || a>3 || (b>10 && b<14) ){
|
||||
bufpt[0] = 't';
|
||||
bufpt[1] = 'h';
|
||||
}else if( a==1 ){
|
||||
bufpt[0] = 's';
|
||||
bufpt[1] = 't';
|
||||
}else if( a==2 ){
|
||||
bufpt[0] = 'n';
|
||||
bufpt[1] = 'd';
|
||||
}else if( a==3 ){
|
||||
bufpt[0] = 'r';
|
||||
bufpt[1] = 'd';
|
||||
}
|
||||
}
|
||||
{
|
||||
register char *cset; /* Use registers for speed */
|
||||
register int base;
|
||||
cset = infop->charset;
|
||||
base = infop->base;
|
||||
do{ /* Convert to ascii */
|
||||
*(--bufpt) = cset[longvalue%base];
|
||||
longvalue = longvalue/base;
|
||||
}while( longvalue>0 );
|
||||
}
|
||||
length = (long)&buf[etBUFSIZE]-(long)bufpt;
|
||||
for(idx=precision-length; idx>0; idx--){
|
||||
*(--bufpt) = '0'; /* Zero pad */
|
||||
}
|
||||
if( prefix ) *(--bufpt) = prefix; /* Add sign */
|
||||
if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
|
||||
char *pre, x;
|
||||
pre = infop->prefix;
|
||||
if( *bufpt!=pre[0] ){
|
||||
for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
|
||||
}
|
||||
}
|
||||
length = (long)&buf[etBUFSIZE]-(long)bufpt;
|
||||
break;
|
||||
case etFLOAT:
|
||||
case etEXP:
|
||||
case etGENERIC:
|
||||
realvalue = va_arg(ap,double);
|
||||
#ifndef etNOFLOATINGPOINT
|
||||
if( precision<0 ) precision = 6; /* Set default precision */
|
||||
if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;
|
||||
if( realvalue<0.0 ){
|
||||
realvalue = -realvalue;
|
||||
prefix = '-';
|
||||
}else{
|
||||
if( flag_plussign ) prefix = '+';
|
||||
else if( flag_blanksign ) prefix = ' ';
|
||||
else prefix = 0;
|
||||
}
|
||||
if( infop->type==etGENERIC && precision>0 ) precision--;
|
||||
rounder = 0.0;
|
||||
#ifdef COMPATIBILITY
|
||||
/* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
|
||||
for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
|
||||
#else
|
||||
/* It makes more sense to use 0.5 */
|
||||
for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);
|
||||
#endif
|
||||
if( infop->type==etFLOAT ) realvalue += rounder;
|
||||
/* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
|
||||
exp = 0;
|
||||
if( realvalue>0.0 ){
|
||||
int k = 0;
|
||||
while( realvalue>=1e8 && k++<100 ){ realvalue *= 1e-8; exp+=8; }
|
||||
while( realvalue>=10.0 && k++<100 ){ realvalue *= 0.1; exp++; }
|
||||
while( realvalue<1e-8 && k++<100 ){ realvalue *= 1e8; exp-=8; }
|
||||
while( realvalue<1.0 && k++<100 ){ realvalue *= 10.0; exp--; }
|
||||
if( k>=100 ){
|
||||
bufpt = "NaN";
|
||||
length = 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
bufpt = buf;
|
||||
/*
|
||||
** If the field type is etGENERIC, then convert to either etEXP
|
||||
** or etFLOAT, as appropriate.
|
||||
*/
|
||||
flag_exp = xtype==etEXP;
|
||||
if( xtype!=etFLOAT ){
|
||||
realvalue += rounder;
|
||||
if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
|
||||
}
|
||||
if( xtype==etGENERIC ){
|
||||
flag_rtz = !flag_alternateform;
|
||||
if( exp<-4 || exp>precision ){
|
||||
xtype = etEXP;
|
||||
}else{
|
||||
precision = precision - exp;
|
||||
xtype = etFLOAT;
|
||||
}
|
||||
}else{
|
||||
flag_rtz = 0;
|
||||
}
|
||||
/*
|
||||
** The "exp+precision" test causes output to be of type etEXP if
|
||||
** the precision is too large to fit in buf[].
|
||||
*/
|
||||
nsd = 0;
|
||||
if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){
|
||||
flag_dp = (precision>0 || flag_alternateform);
|
||||
if( prefix ) *(bufpt++) = prefix; /* Sign */
|
||||
if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */
|
||||
else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd);
|
||||
if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */
|
||||
for(exp++; exp<0 && precision>0; precision--, exp++){
|
||||
*(bufpt++) = '0';
|
||||
}
|
||||
while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
|
||||
*(bufpt--) = 0; /* Null terminate */
|
||||
if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */
|
||||
while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
|
||||
if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
|
||||
}
|
||||
bufpt++; /* point to next free slot */
|
||||
}else{ /* etEXP or etGENERIC */
|
||||
flag_dp = (precision>0 || flag_alternateform);
|
||||
if( prefix ) *(bufpt++) = prefix; /* Sign */
|
||||
*(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */
|
||||
if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */
|
||||
while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd);
|
||||
bufpt--; /* point to last digit */
|
||||
if( flag_rtz && flag_dp ){ /* Remove tail zeros */
|
||||
while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0;
|
||||
if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0;
|
||||
}
|
||||
bufpt++; /* point to next free slot */
|
||||
if( exp || flag_exp ){
|
||||
*(bufpt++) = infop->charset[0];
|
||||
if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */
|
||||
else { *(bufpt++) = '+'; }
|
||||
if( exp>=100 ){
|
||||
*(bufpt++) = (exp/100)+'0'; /* 100's digit */
|
||||
exp %= 100;
|
||||
}
|
||||
*(bufpt++) = exp/10+'0'; /* 10's digit */
|
||||
*(bufpt++) = exp%10+'0'; /* 1's digit */
|
||||
}
|
||||
}
|
||||
/* The converted number is in buf[] and zero terminated. Output it.
|
||||
** Note that the number is in the usual order, not reversed as with
|
||||
** integer conversions. */
|
||||
length = (long)bufpt-(long)buf;
|
||||
bufpt = buf;
|
||||
|
||||
/* Special case: Add leading zeros if the flag_zeropad flag is
|
||||
** set and we are not left justified */
|
||||
if( flag_zeropad && !flag_leftjustify && length < width){
|
||||
int i;
|
||||
int nPad = width - length;
|
||||
for(i=width; i>=nPad; i--){
|
||||
bufpt[i] = bufpt[i-nPad];
|
||||
}
|
||||
i = prefix!=0;
|
||||
while( nPad-- ) bufpt[i++] = '0';
|
||||
length = width;
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case etSIZE:
|
||||
*(va_arg(ap,int*)) = count;
|
||||
length = width = 0;
|
||||
break;
|
||||
case etPERCENT:
|
||||
buf[0] = '%';
|
||||
bufpt = buf;
|
||||
length = 1;
|
||||
break;
|
||||
case etCHARLIT:
|
||||
case etCHARX:
|
||||
c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
|
||||
if( precision>=0 ){
|
||||
for(idx=1; idx<precision; idx++) buf[idx] = c;
|
||||
length = precision;
|
||||
}else{
|
||||
length =1;
|
||||
}
|
||||
bufpt = buf;
|
||||
break;
|
||||
case etSTRING:
|
||||
bufpt = va_arg(ap,char*);
|
||||
if( bufpt==0 ) bufpt = "(null)";
|
||||
length = strlen(bufpt);
|
||||
if( precision>=0 && precision<length ) length = precision;
|
||||
break;
|
||||
case etSQLESCAPE:
|
||||
case etSQLESCAPE2:
|
||||
{
|
||||
int i, j, n, c, isnull;
|
||||
char *arg = va_arg(ap,char*);
|
||||
isnull = arg==0;
|
||||
if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
|
||||
for(i=n=0; (c=arg[i])!=0; i++){
|
||||
if( c=='\'' ) n++;
|
||||
}
|
||||
n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0);
|
||||
if( n>etBUFSIZE ){
|
||||
bufpt = zExtra = sqliteMalloc( n );
|
||||
if( bufpt==0 ) return -1;
|
||||
}else{
|
||||
bufpt = buf;
|
||||
}
|
||||
j = 0;
|
||||
if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
|
||||
for(i=0; (c=arg[i])!=0; i++){
|
||||
bufpt[j++] = c;
|
||||
if( c=='\'' ) bufpt[j++] = c;
|
||||
}
|
||||
if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\'';
|
||||
bufpt[j] = 0;
|
||||
length = j;
|
||||
if( precision>=0 && precision<length ) length = precision;
|
||||
}
|
||||
break;
|
||||
case etERROR:
|
||||
buf[0] = '%';
|
||||
buf[1] = c;
|
||||
errorflag = 0;
|
||||
idx = 1+(c!=0);
|
||||
(*func)(arg,"%",idx);
|
||||
count += idx;
|
||||
if( c==0 ) fmt--;
|
||||
break;
|
||||
}/* End switch over the format type */
|
||||
/*
|
||||
** The text of the conversion is pointed to by "bufpt" and is
|
||||
** "length" characters long. The field width is "width". Do
|
||||
** the output.
|
||||
*/
|
||||
if( !flag_leftjustify ){
|
||||
register int nspace;
|
||||
nspace = width-length;
|
||||
if( nspace>0 ){
|
||||
if( flag_center ){
|
||||
nspace = nspace/2;
|
||||
width -= nspace;
|
||||
flag_leftjustify = 1;
|
||||
}
|
||||
count += nspace;
|
||||
while( nspace>=etSPACESIZE ){
|
||||
(*func)(arg,spaces,etSPACESIZE);
|
||||
nspace -= etSPACESIZE;
|
||||
}
|
||||
if( nspace>0 ) (*func)(arg,spaces,nspace);
|
||||
}
|
||||
}
|
||||
if( length>0 ){
|
||||
(*func)(arg,bufpt,length);
|
||||
count += length;
|
||||
}
|
||||
if( flag_leftjustify ){
|
||||
register int nspace;
|
||||
nspace = width-length;
|
||||
if( nspace>0 ){
|
||||
count += nspace;
|
||||
while( nspace>=etSPACESIZE ){
|
||||
(*func)(arg,spaces,etSPACESIZE);
|
||||
nspace -= etSPACESIZE;
|
||||
}
|
||||
if( nspace>0 ) (*func)(arg,spaces,nspace);
|
||||
}
|
||||
}
|
||||
if( zExtra ){
|
||||
sqliteFree(zExtra);
|
||||
}
|
||||
}/* End for loop over the format string */
|
||||
return errorflag ? -1 : count;
|
||||
} /* End of function */
|
||||
|
||||
|
||||
/* This structure is used to store state information about the
|
||||
** write to memory that is currently in progress.
|
||||
*/
|
||||
struct sgMprintf {
|
||||
char *zBase; /* A base allocation */
|
||||
char *zText; /* The string collected so far */
|
||||
int nChar; /* Length of the string so far */
|
||||
int nAlloc; /* Amount of space allocated in zText */
|
||||
};
|
||||
|
||||
/*
|
||||
** This function implements the callback from vxprintf.
|
||||
**
|
||||
** This routine add nNewChar characters of text in zNewText to
|
||||
** the sgMprintf structure pointed to by "arg".
|
||||
*/
|
||||
static void mout(void *arg, char *zNewText, int nNewChar){
|
||||
struct sgMprintf *pM = (struct sgMprintf*)arg;
|
||||
if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
|
||||
pM->nAlloc = pM->nChar + nNewChar*2 + 1;
|
||||
if( pM->zText==pM->zBase ){
|
||||
pM->zText = sqliteMalloc(pM->nAlloc);
|
||||
if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar);
|
||||
}else{
|
||||
char *z = sqliteRealloc(pM->zText, pM->nAlloc);
|
||||
if( z==0 ){
|
||||
sqliteFree(pM->zText);
|
||||
pM->nChar = 0;
|
||||
pM->nAlloc = 0;
|
||||
}
|
||||
pM->zText = z;
|
||||
}
|
||||
}
|
||||
if( pM->zText ){
|
||||
memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
|
||||
pM->nChar += nNewChar;
|
||||
pM->zText[pM->nChar] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** sqlite_mprintf() works like printf(), but allocations memory to hold the
|
||||
** resulting string and returns a pointer to the allocated memory. Use
|
||||
** sqliteFree() to release the memory allocated.
|
||||
*/
|
||||
char *sqliteMPrintf(const char *zFormat, ...){
|
||||
va_list ap;
|
||||
struct sgMprintf sMprintf;
|
||||
char zBuf[200];
|
||||
|
||||
sMprintf.nChar = 0;
|
||||
sMprintf.nAlloc = sizeof(zBuf);
|
||||
sMprintf.zText = zBuf;
|
||||
sMprintf.zBase = zBuf;
|
||||
va_start(ap,zFormat);
|
||||
vxprintf(mout,&sMprintf,zFormat,ap);
|
||||
va_end(ap);
|
||||
sMprintf.zText[sMprintf.nChar] = 0;
|
||||
return sqliteRealloc(sMprintf.zText, sMprintf.nChar+1);
|
||||
}
|
||||
|
||||
/*
|
||||
** sqlite_mprintf() works like printf(), but allocations memory to hold the
|
||||
** resulting string and returns a pointer to the allocated memory. Use
|
||||
** sqliteFree() to release the memory allocated.
|
||||
*/
|
||||
char *sqlite_mprintf(const char *zFormat, ...){
|
||||
va_list ap;
|
||||
struct sgMprintf sMprintf;
|
||||
char *zNew;
|
||||
char zBuf[200];
|
||||
|
||||
sMprintf.nChar = 0;
|
||||
sMprintf.nAlloc = sizeof(zBuf);
|
||||
sMprintf.zText = zBuf;
|
||||
sMprintf.zBase = zBuf;
|
||||
va_start(ap,zFormat);
|
||||
vxprintf(mout,&sMprintf,zFormat,ap);
|
||||
va_end(ap);
|
||||
sMprintf.zText[sMprintf.nChar] = 0;
|
||||
zNew = malloc( sMprintf.nChar+1 );
|
||||
if( zNew ) strcpy(zNew,sMprintf.zText);
|
||||
if( sMprintf.zText!=sMprintf.zBase ){
|
||||
sqliteFree(sMprintf.zText);
|
||||
}
|
||||
return zNew;
|
||||
}
|
||||
|
||||
/* This is the varargs version of sqlite_mprintf.
|
||||
*/
|
||||
char *sqlite_vmprintf(const char *zFormat, va_list ap){
|
||||
struct sgMprintf sMprintf;
|
||||
char *zNew;
|
||||
char zBuf[200];
|
||||
sMprintf.nChar = 0;
|
||||
sMprintf.zText = zBuf;
|
||||
sMprintf.nAlloc = sizeof(zBuf);
|
||||
sMprintf.zBase = zBuf;
|
||||
vxprintf(mout,&sMprintf,zFormat,ap);
|
||||
sMprintf.zText[sMprintf.nChar] = 0;
|
||||
zNew = malloc( sMprintf.nChar+1 );
|
||||
if( zNew ) strcpy(zNew,sMprintf.zText);
|
||||
if( sMprintf.zText!=sMprintf.zBase ){
|
||||
sqliteFree(sMprintf.zText);
|
||||
}
|
||||
return zNew;
|
||||
}
|
||||
|
||||
/*
|
||||
** The following four routines implement the varargs versions of the
|
||||
** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h
|
||||
** header files for a more detailed description of how these interfaces
|
||||
** work.
|
||||
**
|
||||
** These routines are all just simple wrappers.
|
||||
*/
|
||||
int sqlite_exec_printf(
|
||||
sqlite *db, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
sqlite_callback xCallback, /* Callback function */
|
||||
void *pArg, /* 1st argument to callback function */
|
||||
char **errmsg, /* Error msg written here */
|
||||
... /* Arguments to the format string. */
|
||||
){
|
||||
va_list ap;
|
||||
int rc;
|
||||
|
||||
va_start(ap, errmsg);
|
||||
rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
int sqlite_exec_vprintf(
|
||||
sqlite *db, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
sqlite_callback xCallback, /* Callback function */
|
||||
void *pArg, /* 1st argument to callback function */
|
||||
char **errmsg, /* Error msg written here */
|
||||
va_list ap /* Arguments to the format string. */
|
||||
){
|
||||
char *zSql;
|
||||
int rc;
|
||||
|
||||
zSql = sqlite_vmprintf(sqlFormat, ap);
|
||||
rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg);
|
||||
free(zSql);
|
||||
return rc;
|
||||
}
|
||||
int sqlite_get_table_printf(
|
||||
sqlite *db, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncol, /* Number of result columns written here */
|
||||
char **errmsg, /* Error msg written here */
|
||||
... /* Arguments to the format string */
|
||||
){
|
||||
va_list ap;
|
||||
int rc;
|
||||
|
||||
va_start(ap, errmsg);
|
||||
rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
int sqlite_get_table_vprintf(
|
||||
sqlite *db, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg, /* Error msg written here */
|
||||
va_list ap /* Arguments to the format string */
|
||||
){
|
||||
char *zSql;
|
||||
int rc;
|
||||
|
||||
zSql = sqlite_vmprintf(sqlFormat, ap);
|
||||
rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg);
|
||||
free(zSql);
|
||||
return rc;
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
/*
|
||||
** 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 contains code to implement a pseudo-random number
|
||||
** generator (PRNG) for SQLite.
|
||||
**
|
||||
** Random numbers are used by some of the database backends in order
|
||||
** to generate random integer keys for tables or random filenames.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
|
||||
|
||||
/*
|
||||
** Get a single 8-bit random value from the RC4 PRNG. The Mutex
|
||||
** must be held while executing this routine.
|
||||
**
|
||||
** Why not just use a library random generator like lrand48() for this?
|
||||
** Because the OP_NewRecno opcode in the VDBE depends on having a very
|
||||
** good source of random numbers. The lrand48() library function may
|
||||
** well be good enough. But maybe not. Or maybe lrand48() has some
|
||||
** subtle problems on some systems that could cause problems. It is hard
|
||||
** to know. To minimize the risk of problems due to bad lrand48()
|
||||
** implementations, SQLite uses this random number generator based
|
||||
** on RC4, which we know works very well.
|
||||
*/
|
||||
static int randomByte(){
|
||||
int t;
|
||||
|
||||
/* All threads share a single random number generator.
|
||||
** This structure is the current state of the generator.
|
||||
*/
|
||||
static struct {
|
||||
int isInit; /* True if initialized */
|
||||
int i, j; /* State variables */
|
||||
int s[256]; /* State variables */
|
||||
} prng;
|
||||
|
||||
/* Initialize the state of the random number generator once,
|
||||
** the first time this routine is called. The seed value does
|
||||
** not need to contain a lot of randomness since we are not
|
||||
** trying to do secure encryption or anything like that...
|
||||
**
|
||||
** Nothing in this file or anywhere else in SQLite does any kind of
|
||||
** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
|
||||
** number generator) not as an encryption device.
|
||||
*/
|
||||
if( !prng.isInit ){
|
||||
int i;
|
||||
char k[256];
|
||||
prng.j = 0;
|
||||
prng.i = 0;
|
||||
sqliteOsRandomSeed(k);
|
||||
for(i=0; i<256; i++){
|
||||
prng.s[i] = i;
|
||||
}
|
||||
for(i=0; i<256; i++){
|
||||
int t;
|
||||
prng.j = (prng.j + prng.s[i] + k[i]) & 0xff;
|
||||
t = prng.s[prng.j];
|
||||
prng.s[prng.j] = prng.s[i];
|
||||
prng.s[i] = t;
|
||||
}
|
||||
prng.isInit = 1;
|
||||
}
|
||||
|
||||
/* Generate and return single random byte
|
||||
*/
|
||||
prng.i = (prng.i + 1) & 0xff;
|
||||
prng.j = (prng.j + prng.s[prng.i]) & 0xff;
|
||||
t = prng.s[prng.i];
|
||||
prng.s[prng.i] = prng.s[prng.j];
|
||||
prng.s[prng.j] = t;
|
||||
t = prng.s[prng.i] + prng.s[prng.j];
|
||||
return prng.s[t & 0xff];
|
||||
}
|
||||
|
||||
/*
|
||||
** Return an random 8-bit integer.
|
||||
*/
|
||||
int sqliteRandomByte(){
|
||||
int r;
|
||||
sqliteOsEnterMutex();
|
||||
r = randomByte();
|
||||
sqliteOsLeaveMutex();
|
||||
return r;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return a random 32-bit integer. The integer is generated by making
|
||||
** 4 calls to sqliteRandomByte().
|
||||
*/
|
||||
int sqliteRandomInteger(){
|
||||
int r;
|
||||
int i;
|
||||
sqliteOsEnterMutex();
|
||||
r = randomByte();
|
||||
for(i=1; i<4; i++){
|
||||
r = (r<<8) + randomByte();
|
||||
}
|
||||
sqliteOsLeaveMutex();
|
||||
return r;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,691 +0,0 @@
|
||||
/*
|
||||
** 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 header file defines the interface that the SQLite library
|
||||
** presents to client programs.
|
||||
**
|
||||
** @(#) $Id$
|
||||
*/
|
||||
#ifndef _SQLITE_H_
|
||||
#define _SQLITE_H_
|
||||
#include <stdarg.h> /* Needed for the definition of va_list */
|
||||
|
||||
/*
|
||||
** Make sure we can call this stuff from C++.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
** The version of the SQLite library.
|
||||
*/
|
||||
#define SQLITE_VERSION "--VERS--"
|
||||
|
||||
/*
|
||||
** The version string is also compiled into the library so that a program
|
||||
** can check to make sure that the lib*.a file and the *.h file are from
|
||||
** the same version.
|
||||
*/
|
||||
extern const char sqlite_version[];
|
||||
|
||||
/*
|
||||
** The SQLITE_UTF8 macro is defined if the library expects to see
|
||||
** UTF-8 encoded data. The SQLITE_ISO8859 macro is defined if the
|
||||
** iso8859 encoded should be used.
|
||||
*/
|
||||
#define SQLITE_--ENCODING-- 1
|
||||
|
||||
/*
|
||||
** The following constant holds one of two strings, "UTF-8" or "iso8859",
|
||||
** depending on which character encoding the SQLite library expects to
|
||||
** see. The character encoding makes a difference for the LIKE and GLOB
|
||||
** operators and for the LENGTH() and SUBSTR() functions.
|
||||
*/
|
||||
extern const char sqlite_encoding[];
|
||||
|
||||
/*
|
||||
** Each open sqlite database is represented by an instance of the
|
||||
** following opaque structure.
|
||||
*/
|
||||
typedef struct sqlite sqlite;
|
||||
|
||||
/*
|
||||
** A function to open a new sqlite database.
|
||||
**
|
||||
** If the database does not exist and mode indicates write
|
||||
** permission, then a new database is created. If the database
|
||||
** does not exist and mode does not indicate write permission,
|
||||
** then the open fails, an error message generated (if errmsg!=0)
|
||||
** and the function returns 0.
|
||||
**
|
||||
** If mode does not indicates user write permission, then the
|
||||
** database is opened read-only.
|
||||
**
|
||||
** The Truth: As currently implemented, all databases are opened
|
||||
** for writing all the time. Maybe someday we will provide the
|
||||
** ability to open a database readonly. The mode parameters is
|
||||
** provided in anticipation of that enhancement.
|
||||
*/
|
||||
sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
|
||||
|
||||
/*
|
||||
** A function to close the database.
|
||||
**
|
||||
** Call this function with a pointer to a structure that was previously
|
||||
** returned from sqlite_open() and the corresponding database will by closed.
|
||||
*/
|
||||
void sqlite_close(sqlite *);
|
||||
|
||||
/*
|
||||
** The type for a callback function.
|
||||
*/
|
||||
typedef int (*sqlite_callback)(void*,int,char**, char**);
|
||||
|
||||
/*
|
||||
** A function to executes one or more statements of SQL.
|
||||
**
|
||||
** If one or more of the SQL statements are queries, then
|
||||
** the callback function specified by the 3rd parameter is
|
||||
** invoked once for each row of the query result. This callback
|
||||
** should normally return 0. If the callback returns a non-zero
|
||||
** value then the query is aborted, all subsequent SQL statements
|
||||
** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
|
||||
**
|
||||
** The 4th parameter is an arbitrary pointer that is passed
|
||||
** to the callback function as its first parameter.
|
||||
**
|
||||
** The 2nd parameter to the callback function is the number of
|
||||
** columns in the query result. The 3rd parameter to the callback
|
||||
** is an array of strings holding the values for each column.
|
||||
** The 4th parameter to the callback is an array of strings holding
|
||||
** the names of each column.
|
||||
**
|
||||
** The callback function may be NULL, even for queries. A NULL
|
||||
** callback is not an error. It just means that no callback
|
||||
** will be invoked.
|
||||
**
|
||||
** If an error occurs while parsing or evaluating the SQL (but
|
||||
** not while executing the callback) then an appropriate error
|
||||
** message is written into memory obtained from malloc() and
|
||||
** *errmsg is made to point to that message. The calling function
|
||||
** is responsible for freeing the memory that holds the error
|
||||
** message. Use sqlite_freemem() for this. If errmsg==NULL,
|
||||
** then no error message is ever written.
|
||||
**
|
||||
** The return value is is SQLITE_OK if there are no errors and
|
||||
** some other return code if there is an error. The particular
|
||||
** return value depends on the type of error.
|
||||
**
|
||||
** If the query could not be executed because a database file is
|
||||
** locked or busy, then this function returns SQLITE_BUSY. (This
|
||||
** behavior can be modified somewhat using the sqlite_busy_handler()
|
||||
** and sqlite_busy_timeout() functions below.)
|
||||
*/
|
||||
int sqlite_exec(
|
||||
sqlite*, /* An open database */
|
||||
const char *sql, /* SQL to be executed */
|
||||
sqlite_callback, /* Callback function */
|
||||
void *, /* 1st argument to callback function */
|
||||
char **errmsg /* Error msg written here */
|
||||
);
|
||||
|
||||
/*
|
||||
** Return values for sqlite_exec() and sqlite_step()
|
||||
*/
|
||||
#define SQLITE_OK 0 /* Successful result */
|
||||
#define SQLITE_ERROR 1 /* SQL error or missing database */
|
||||
#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
|
||||
#define SQLITE_PERM 3 /* Access permission denied */
|
||||
#define SQLITE_ABORT 4 /* Callback routine requested an abort */
|
||||
#define SQLITE_BUSY 5 /* The database file is locked */
|
||||
#define SQLITE_LOCKED 6 /* A table in the database is locked */
|
||||
#define SQLITE_NOMEM 7 /* A malloc() failed */
|
||||
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
|
||||
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
|
||||
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
|
||||
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
|
||||
#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
|
||||
#define SQLITE_FULL 13 /* Insertion failed because database is full */
|
||||
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
|
||||
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
|
||||
#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
|
||||
#define SQLITE_SCHEMA 17 /* The database schema changed */
|
||||
#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
|
||||
#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
|
||||
#define SQLITE_MISMATCH 20 /* Data type mismatch */
|
||||
#define SQLITE_MISUSE 21 /* Library used incorrectly */
|
||||
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
|
||||
#define SQLITE_AUTH 23 /* Authorization denied */
|
||||
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
|
||||
#define SQLITE_ROW 100 /* sqlite_step() has another row ready */
|
||||
#define SQLITE_DONE 101 /* sqlite_step() has finished executing */
|
||||
|
||||
/*
|
||||
** Each entry in an SQLite table has a unique integer key. (The key is
|
||||
** the value of the INTEGER PRIMARY KEY column if there is such a column,
|
||||
** otherwise the key is generated at random. The unique key is always
|
||||
** available as the ROWID, OID, or _ROWID_ column.) The following routine
|
||||
** returns the integer key of the most recent insert in the database.
|
||||
**
|
||||
** This function is similar to the mysql_insert_id() function from MySQL.
|
||||
*/
|
||||
int sqlite_last_insert_rowid(sqlite*);
|
||||
|
||||
/*
|
||||
** This function returns the number of database rows that were changed
|
||||
** (or inserted or deleted) by the most recent called sqlite_exec().
|
||||
**
|
||||
** All changes are counted, even if they were later undone by a
|
||||
** ROLLBACK or ABORT. Except, changes associated with creating and
|
||||
** dropping tables are not counted.
|
||||
**
|
||||
** If a callback invokes sqlite_exec() recursively, then the changes
|
||||
** in the inner, recursive call are counted together with the changes
|
||||
** in the outer call.
|
||||
**
|
||||
** SQLite implements the command "DELETE FROM table" without a WHERE clause
|
||||
** by dropping and recreating the table. (This is much faster than going
|
||||
** through and deleting individual elements form the table.) Because of
|
||||
** this optimization, the change count for "DELETE FROM table" will be
|
||||
** zero regardless of the number of elements that were originally in the
|
||||
** table. To get an accurate count of the number of rows deleted, use
|
||||
** "DELETE FROM table WHERE 1" instead.
|
||||
*/
|
||||
int sqlite_changes(sqlite*);
|
||||
|
||||
/* If the parameter to this routine is one of the return value constants
|
||||
** defined above, then this routine returns a constant text string which
|
||||
** descripts (in English) the meaning of the return value.
|
||||
*/
|
||||
const char *sqlite_error_string(int);
|
||||
#define sqliteErrStr sqlite_error_string /* Legacy. Do not use in new code. */
|
||||
|
||||
/* This function causes any pending database operation to abort and
|
||||
** return at its earliest opportunity. This routine is typically
|
||||
** called in response to a user action such as pressing "Cancel"
|
||||
** or Ctrl-C where the user wants a long query operation to halt
|
||||
** immediately.
|
||||
*/
|
||||
void sqlite_interrupt(sqlite*);
|
||||
|
||||
|
||||
/* This function returns true if the given input string comprises
|
||||
** one or more complete SQL statements.
|
||||
**
|
||||
** The algorithm is simple. If the last token other than spaces
|
||||
** and comments is a semicolon, then return true. otherwise return
|
||||
** false.
|
||||
*/
|
||||
int sqlite_complete(const char *sql);
|
||||
|
||||
/*
|
||||
** This routine identifies a callback function that is invoked
|
||||
** whenever an attempt is made to open a database table that is
|
||||
** currently locked by another process or thread. If the busy callback
|
||||
** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
|
||||
** it finds a locked table. If the busy callback is not NULL, then
|
||||
** sqlite_exec() invokes the callback with three arguments. The
|
||||
** second argument is the name of the locked table and the third
|
||||
** argument is the number of times the table has been busy. If the
|
||||
** busy callback returns 0, then sqlite_exec() immediately returns
|
||||
** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
|
||||
** tries to open the table again and the cycle repeats.
|
||||
**
|
||||
** The default busy callback is NULL.
|
||||
**
|
||||
** Sqlite is re-entrant, so the busy handler may start a new query.
|
||||
** (It is not clear why anyone would every want to do this, but it
|
||||
** is allowed, in theory.) But the busy handler may not close the
|
||||
** database. Closing the database from a busy handler will delete
|
||||
** data structures out from under the executing query and will
|
||||
** probably result in a coredump.
|
||||
*/
|
||||
void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
|
||||
|
||||
/*
|
||||
** This routine sets a busy handler that sleeps for a while when a
|
||||
** table is locked. The handler will sleep multiple times until
|
||||
** at least "ms" milleseconds of sleeping have been done. After
|
||||
** "ms" milleseconds of sleeping, the handler returns 0 which
|
||||
** causes sqlite_exec() to return SQLITE_BUSY.
|
||||
**
|
||||
** Calling this routine with an argument less than or equal to zero
|
||||
** turns off all busy handlers.
|
||||
*/
|
||||
void sqlite_busy_timeout(sqlite*, int ms);
|
||||
|
||||
/*
|
||||
** This next routine is really just a wrapper around sqlite_exec().
|
||||
** Instead of invoking a user-supplied callback for each row of the
|
||||
** result, this routine remembers each row of the result in memory
|
||||
** obtained from malloc(), then returns all of the result after the
|
||||
** query has finished.
|
||||
**
|
||||
** As an example, suppose the query result where this table:
|
||||
**
|
||||
** Name | Age
|
||||
** -----------------------
|
||||
** Alice | 43
|
||||
** Bob | 28
|
||||
** Cindy | 21
|
||||
**
|
||||
** If the 3rd argument were &azResult then after the function returns
|
||||
** azResult will contain the following data:
|
||||
**
|
||||
** azResult[0] = "Name";
|
||||
** azResult[1] = "Age";
|
||||
** azResult[2] = "Alice";
|
||||
** azResult[3] = "43";
|
||||
** azResult[4] = "Bob";
|
||||
** azResult[5] = "28";
|
||||
** azResult[6] = "Cindy";
|
||||
** azResult[7] = "21";
|
||||
**
|
||||
** Notice that there is an extra row of data containing the column
|
||||
** headers. But the *nrow return value is still 3. *ncolumn is
|
||||
** set to 2. In general, the number of values inserted into azResult
|
||||
** will be ((*nrow) + 1)*(*ncolumn).
|
||||
**
|
||||
** After the calling function has finished using the result, it should
|
||||
** pass the result data pointer to sqlite_free_table() in order to
|
||||
** release the memory that was malloc-ed. Because of the way the
|
||||
** malloc() happens, the calling function must not try to call
|
||||
** malloc() directly. Only sqlite_free_table() is able to release
|
||||
** the memory properly and safely.
|
||||
**
|
||||
** The return value of this routine is the same as from sqlite_exec().
|
||||
*/
|
||||
int sqlite_get_table(
|
||||
sqlite*, /* An open database */
|
||||
const char *sql, /* SQL to be executed */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg /* Error msg written here */
|
||||
);
|
||||
|
||||
/*
|
||||
** Call this routine to free the memory that sqlite_get_table() allocated.
|
||||
*/
|
||||
void sqlite_free_table(char **result);
|
||||
|
||||
/*
|
||||
** The following routines are wrappers around sqlite_exec() and
|
||||
** sqlite_get_table(). The only difference between the routines that
|
||||
** follow and the originals is that the second argument to the
|
||||
** routines that follow is really a printf()-style format
|
||||
** string describing the SQL to be executed. Arguments to the format
|
||||
** string appear at the end of the argument list.
|
||||
**
|
||||
** All of the usual printf formatting options apply. In addition, there
|
||||
** is a "%q" option. %q works like %s in that it substitutes a null-terminated
|
||||
** string from the argument list. But %q also doubles every '\'' character.
|
||||
** %q is designed for use inside a string literal. By doubling each '\''
|
||||
** character it escapes that character and allows it to be inserted into
|
||||
** the string.
|
||||
**
|
||||
** For example, so some string variable contains text as follows:
|
||||
**
|
||||
** char *zText = "It's a happy day!";
|
||||
**
|
||||
** We can use this text in an SQL statement as follows:
|
||||
**
|
||||
** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
|
||||
** callback1, 0, 0, zText);
|
||||
**
|
||||
** Because the %q format string is used, the '\'' character in zText
|
||||
** is escaped and the SQL generated is as follows:
|
||||
**
|
||||
** INSERT INTO table1 VALUES('It''s a happy day!')
|
||||
**
|
||||
** This is correct. Had we used %s instead of %q, the generated SQL
|
||||
** would have looked like this:
|
||||
**
|
||||
** INSERT INTO table1 VALUES('It's a happy day!');
|
||||
**
|
||||
** This second example is an SQL syntax error. As a general rule you
|
||||
** should always use %q instead of %s when inserting text into a string
|
||||
** literal.
|
||||
*/
|
||||
int sqlite_exec_printf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
sqlite_callback, /* Callback function */
|
||||
void *, /* 1st argument to callback function */
|
||||
char **errmsg, /* Error msg written here */
|
||||
... /* Arguments to the format string. */
|
||||
);
|
||||
int sqlite_exec_vprintf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
sqlite_callback, /* Callback function */
|
||||
void *, /* 1st argument to callback function */
|
||||
char **errmsg, /* Error msg written here */
|
||||
va_list ap /* Arguments to the format string. */
|
||||
);
|
||||
int sqlite_get_table_printf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg, /* Error msg written here */
|
||||
... /* Arguments to the format string */
|
||||
);
|
||||
int sqlite_get_table_vprintf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg, /* Error msg written here */
|
||||
va_list ap /* Arguments to the format string */
|
||||
);
|
||||
char *sqlite_mprintf(const char*,...);
|
||||
char *sqlite_vmprintf(const char*, va_list);
|
||||
|
||||
/*
|
||||
** Windows systems should call this routine to free memory that
|
||||
** is returned in the in the errmsg parameter of sqlite_open() when
|
||||
** SQLite is a DLL. For some reason, it does not work to call free()
|
||||
** directly.
|
||||
*/
|
||||
void sqlite_freemem(void *p);
|
||||
|
||||
/*
|
||||
** Windows systems need functions to call to return the sqlite_version
|
||||
** and sqlite_encoding strings.
|
||||
*/
|
||||
const char *sqlite_libversion(void);
|
||||
const char *sqlite_libencoding(void);
|
||||
|
||||
/*
|
||||
** A pointer to the following structure is used to communicate with
|
||||
** the implementations of user-defined functions.
|
||||
*/
|
||||
typedef struct sqlite_func sqlite_func;
|
||||
|
||||
/*
|
||||
** Use the following routines to create new user-defined functions. See
|
||||
** the documentation for details.
|
||||
*/
|
||||
int sqlite_create_function(
|
||||
sqlite*, /* Database where the new function is registered */
|
||||
const char *zName, /* Name of the new function */
|
||||
int nArg, /* Number of arguments. -1 means any number */
|
||||
void (*xFunc)(sqlite_func*,int,const char**), /* C code to implement */
|
||||
void *pUserData /* Available via the sqlite_user_data() call */
|
||||
);
|
||||
int sqlite_create_aggregate(
|
||||
sqlite*, /* Database where the new function is registered */
|
||||
const char *zName, /* Name of the function */
|
||||
int nArg, /* Number of arguments */
|
||||
void (*xStep)(sqlite_func*,int,const char**), /* Called for each row */
|
||||
void (*xFinalize)(sqlite_func*), /* Called once to get final result */
|
||||
void *pUserData /* Available via the sqlite_user_data() call */
|
||||
);
|
||||
|
||||
/*
|
||||
** Use the following routine to define the datatype returned by a
|
||||
** user-defined function. The second argument can be one of the
|
||||
** constants SQLITE_NUMERIC, SQLITE_TEXT, or SQLITE_ARGS or it
|
||||
** can be an integer greater than or equal to zero. The datatype
|
||||
** will be numeric or text (the only two types supported) if the
|
||||
** argument is SQLITE_NUMERIC or SQLITE_TEXT. If the argument is
|
||||
** SQLITE_ARGS, then the datatype is numeric if any argument to the
|
||||
** function is numeric and is text otherwise. If the second argument
|
||||
** is an integer, then the datatype of the result is the same as the
|
||||
** parameter to the function that corresponds to that integer.
|
||||
*/
|
||||
int sqlite_function_type(
|
||||
sqlite *db, /* The database there the function is registered */
|
||||
const char *zName, /* Name of the function */
|
||||
int datatype /* The datatype for this function */
|
||||
);
|
||||
#define SQLITE_NUMERIC (-1)
|
||||
#define SQLITE_TEXT (-2)
|
||||
#define SQLITE_ARGS (-3)
|
||||
|
||||
/*
|
||||
** The user function implementations call one of the following four routines
|
||||
** in order to return their results. The first parameter to each of these
|
||||
** routines is a copy of the first argument to xFunc() or xFinialize().
|
||||
** The second parameter to these routines is the result to be returned.
|
||||
** A NULL can be passed as the second parameter to sqlite_set_result_string()
|
||||
** in order to return a NULL result.
|
||||
**
|
||||
** The 3rd argument to _string and _error is the number of characters to
|
||||
** take from the string. If this argument is negative, then all characters
|
||||
** up to and including the first '\000' are used.
|
||||
**
|
||||
** The sqlite_set_result_string() function allocates a buffer to hold the
|
||||
** result and returns a pointer to this buffer. The calling routine
|
||||
** (that is, the implmentation of a user function) can alter the content
|
||||
** of this buffer if desired.
|
||||
*/
|
||||
char *sqlite_set_result_string(sqlite_func*,const char*,int);
|
||||
void sqlite_set_result_int(sqlite_func*,int);
|
||||
void sqlite_set_result_double(sqlite_func*,double);
|
||||
void sqlite_set_result_error(sqlite_func*,const char*,int);
|
||||
|
||||
/*
|
||||
** The pUserData parameter to the sqlite_create_function() and
|
||||
** sqlite_create_aggregate() routines used to register user functions
|
||||
** is available to the implementation of the function using this
|
||||
** call.
|
||||
*/
|
||||
void *sqlite_user_data(sqlite_func*);
|
||||
|
||||
/*
|
||||
** Aggregate functions use the following routine to allocate
|
||||
** a structure for storing their state. The first time this routine
|
||||
** is called for a particular aggregate, a new structure of size nBytes
|
||||
** is allocated, zeroed, and returned. On subsequent calls (for the
|
||||
** same aggregate instance) the same buffer is returned. The implementation
|
||||
** of the aggregate can use the returned buffer to accumulate data.
|
||||
**
|
||||
** The buffer allocated is freed automatically be SQLite.
|
||||
*/
|
||||
void *sqlite_aggregate_context(sqlite_func*, int nBytes);
|
||||
|
||||
/*
|
||||
** The next routine returns the number of calls to xStep for a particular
|
||||
** aggregate function instance. The current call to xStep counts so this
|
||||
** routine always returns at least 1.
|
||||
*/
|
||||
int sqlite_aggregate_count(sqlite_func*);
|
||||
|
||||
/*
|
||||
** This routine registers a callback with the SQLite library. The
|
||||
** callback is invoked (at compile-time, not at run-time) for each
|
||||
** attempt to access a column of a table in the database. The callback
|
||||
** returns SQLITE_OK if access is allowed, SQLITE_DENY if the entire
|
||||
** SQL statement should be aborted with an error and SQLITE_IGNORE
|
||||
** if the column should be treated as a NULL value.
|
||||
*/
|
||||
int sqlite_set_authorizer(
|
||||
sqlite*,
|
||||
int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
|
||||
void *pUserData
|
||||
);
|
||||
|
||||
/*
|
||||
** The second parameter to the access authorization function above will
|
||||
** be one of the values below. These values signify what kind of operation
|
||||
** is to be authorized. The 3rd and 4th parameters to the authorization
|
||||
** function will be parameters or NULL depending on which of the following
|
||||
** codes is used as the second parameter. The 5th parameter is the name
|
||||
** of the database ("main", "temp", etc.) if applicable. The 6th parameter
|
||||
** is the name of the inner-most trigger or view that is responsible for
|
||||
** the access attempt or NULL if this access attempt is directly from
|
||||
** input SQL code.
|
||||
**
|
||||
** Arg-3 Arg-4
|
||||
*/
|
||||
#define SQLITE_COPY 0 /* Table Name File Name */
|
||||
#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
|
||||
#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
|
||||
#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
|
||||
#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
|
||||
#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
|
||||
#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
|
||||
#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
|
||||
#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
|
||||
#define SQLITE_DELETE 9 /* Table Name NULL */
|
||||
#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
|
||||
#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
|
||||
#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
|
||||
#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
|
||||
#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
|
||||
#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
|
||||
#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
|
||||
#define SQLITE_DROP_VIEW 17 /* View Name NULL */
|
||||
#define SQLITE_INSERT 18 /* Table Name NULL */
|
||||
#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
|
||||
#define SQLITE_READ 20 /* Table Name Column Name */
|
||||
#define SQLITE_SELECT 21 /* NULL NULL */
|
||||
#define SQLITE_TRANSACTION 22 /* NULL NULL */
|
||||
#define SQLITE_UPDATE 23 /* Table Name Column Name */
|
||||
#define SQLITE_ATTACH 24 /* Filename NULL */
|
||||
#define SQLITE_DETACH 25 /* Database Name NULL */
|
||||
|
||||
|
||||
/*
|
||||
** The return value of the authorization function should be one of the
|
||||
** following constants:
|
||||
*/
|
||||
/* #define SQLITE_OK 0 // Allow access (This is actually defined above) */
|
||||
#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
|
||||
#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
|
||||
|
||||
/*
|
||||
** Register a function that is called at every invocation of sqlite_exec()
|
||||
** or sqlite_compile(). This function can be used (for example) to generate
|
||||
** a log file of all SQL executed against a database.
|
||||
*/
|
||||
void *sqlite_trace(sqlite*, void(*xTrace)(void*,const char*), void*);
|
||||
|
||||
/*** The Callback-Free API
|
||||
**
|
||||
** The following routines implement a new way to access SQLite that does not
|
||||
** involve the use of callbacks.
|
||||
**
|
||||
** An sqlite_vm is an opaque object that represents a single SQL statement
|
||||
** that is ready to be executed.
|
||||
*/
|
||||
typedef struct sqlite_vm sqlite_vm;
|
||||
|
||||
/*
|
||||
** To execute an SQLite query without the use of callbacks, you first have
|
||||
** to compile the SQL using this routine. The 1st parameter "db" is a pointer
|
||||
** to an sqlite object obtained from sqlite_open(). The 2nd parameter
|
||||
** "zSql" is the text of the SQL to be compiled. The remaining parameters
|
||||
** are all outputs.
|
||||
**
|
||||
** *pzTail is made to point to the first character past the end of the first
|
||||
** SQL statement in zSql. This routine only compiles the first statement
|
||||
** in zSql, so *pzTail is left pointing to what remains uncompiled.
|
||||
**
|
||||
** *ppVm is left pointing to a "virtual machine" that can be used to execute
|
||||
** the compiled statement. Or if there is an error, *ppVm may be set to NULL.
|
||||
** If the input text contained no SQL (if the input is and empty string or
|
||||
** a comment) then *ppVm is set to NULL.
|
||||
**
|
||||
** If any errors are detected during compilation, an error message is written
|
||||
** into space obtained from malloc() and *pzErrMsg is made to point to that
|
||||
** error message. The calling routine is responsible for freeing the text
|
||||
** of this message when it has finished with it. Use sqlite_freemem() to
|
||||
** free the message. pzErrMsg may be NULL in which case no error message
|
||||
** will be generated.
|
||||
**
|
||||
** On success, SQLITE_OK is returned. Otherwise and error code is returned.
|
||||
*/
|
||||
int sqlite_compile(
|
||||
sqlite *db, /* The open database */
|
||||
const char *zSql, /* SQL statement to be compiled */
|
||||
const char **pzTail, /* OUT: uncompiled tail of zSql */
|
||||
sqlite_vm **ppVm, /* OUT: the virtual machine to execute zSql */
|
||||
char **pzErrmsg /* OUT: Error message. */
|
||||
);
|
||||
|
||||
/*
|
||||
** After an SQL statement has been compiled, it is handed to this routine
|
||||
** to be executed. This routine executes the statement as far as it can
|
||||
** go then returns. The return value will be one of SQLITE_DONE,
|
||||
** SQLITE_ERROR, SQLITE_BUSY, SQLITE_ROW, or SQLITE_MISUSE.
|
||||
**
|
||||
** SQLITE_DONE means that the execute of the SQL statement is complete
|
||||
** an no errors have occurred. sqlite_step() should not be called again
|
||||
** for the same virtual machine. *pN is set to the number of columns in
|
||||
** the result set and *pazColName is set to an array of strings that
|
||||
** describe the column names and datatypes. The name of the i-th column
|
||||
** is (*pazColName)[i] and the datatype of the i-th column is
|
||||
** (*pazColName)[i+*pN]. *pazValue is set to NULL.
|
||||
**
|
||||
** SQLITE_ERROR means that the virtual machine encountered a run-time
|
||||
** error. sqlite_step() should not be called again for the same
|
||||
** virtual machine. *pN is set to 0 and *pazColName and *pazValue are set
|
||||
** to NULL. Use sqlite_finalize() to obtain the specific error code
|
||||
** and the error message text for the error.
|
||||
**
|
||||
** SQLITE_BUSY means that an attempt to open the database failed because
|
||||
** another thread or process is holding a lock. The calling routine
|
||||
** can try again to open the database by calling sqlite_step() again.
|
||||
** The return code will only be SQLITE_BUSY if no busy handler is registered
|
||||
** using the sqlite_busy_handler() or sqlite_busy_timeout() routines. If
|
||||
** a busy handler callback has been registered but returns 0, then this
|
||||
** routine will return SQLITE_ERROR and sqltie_finalize() will return
|
||||
** SQLITE_BUSY when it is called.
|
||||
**
|
||||
** SQLITE_ROW means that a single row of the result is now available.
|
||||
** The data is contained in *pazValue. The value of the i-th column is
|
||||
** (*azValue)[i]. *pN and *pazColName are set as described in SQLITE_DONE.
|
||||
** Invoke sqlite_step() again to advance to the next row.
|
||||
**
|
||||
** SQLITE_MISUSE is returned if sqlite_step() is called incorrectly.
|
||||
** For example, if you call sqlite_step() after the virtual machine
|
||||
** has halted (after a prior call to sqlite_step() has returned SQLITE_DONE)
|
||||
** or if you call sqlite_step() with an incorrectly initialized virtual
|
||||
** machine or a virtual machine that has been deleted or that is associated
|
||||
** with an sqlite structure that has been closed.
|
||||
*/
|
||||
int sqlite_step(
|
||||
sqlite_vm *pVm, /* The virtual machine to execute */
|
||||
int *pN, /* OUT: Number of columns in result */
|
||||
const char ***pazValue, /* OUT: Column data */
|
||||
const char ***pazColName /* OUT: Column names and datatypes */
|
||||
);
|
||||
|
||||
/*
|
||||
** This routine is called to delete a virtual machine after it has finished
|
||||
** executing. The return value is the result code. SQLITE_OK is returned
|
||||
** if the statement executed successfully and some other value is returned if
|
||||
** there was any kind of error. If an error occurred and pzErrMsg is not
|
||||
** NULL, then an error message is written into memory obtained from malloc()
|
||||
** and *pzErrMsg is made to point to that error message. The calling routine
|
||||
** should use sqlite_freemem() to delete this message when it has finished
|
||||
** with it.
|
||||
**
|
||||
** This routine can be called at any point during the execution of the
|
||||
** virtual machine. If the virtual machine has not completed execution
|
||||
** when this routine is called, that is like encountering an error or
|
||||
** an interrupt. (See sqlite_interrupt().) Incomplete updates may be
|
||||
** rolled back and transactions cancelled, depending on the circumstances,
|
||||
** and the result code returned will be SQLITE_ABORT.
|
||||
*/
|
||||
int sqlite_finalize(sqlite_vm*, char **pzErrMsg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* End of the 'extern "C"' block */
|
||||
#endif
|
||||
|
||||
#endif /* _SQLITE_H_ */
|
||||
@@ -1,691 +0,0 @@
|
||||
/*
|
||||
** 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 header file defines the interface that the SQLite library
|
||||
** presents to client programs.
|
||||
**
|
||||
** @(#) $Id$
|
||||
*/
|
||||
#ifndef _SQLITE_H_
|
||||
#define _SQLITE_H_
|
||||
#include <stdarg.h> /* Needed for the definition of va_list */
|
||||
|
||||
/*
|
||||
** Make sure we can call this stuff from C++.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
** The version of the SQLite library.
|
||||
*/
|
||||
#define SQLITE_VERSION "2.8.3"
|
||||
|
||||
/*
|
||||
** The version string is also compiled into the library so that a program
|
||||
** can check to make sure that the lib*.a file and the *.h file are from
|
||||
** the same version.
|
||||
*/
|
||||
extern const char sqlite_version[];
|
||||
|
||||
/*
|
||||
** The SQLITE_UTF8 macro is defined if the library expects to see
|
||||
** UTF-8 encoded data. The SQLITE_ISO8859 macro is defined if the
|
||||
** iso8859 encoded should be used.
|
||||
*/
|
||||
#define SQLITE_ISO8859 1
|
||||
|
||||
/*
|
||||
** The following constant holds one of two strings, "UTF-8" or "iso8859",
|
||||
** depending on which character encoding the SQLite library expects to
|
||||
** see. The character encoding makes a difference for the LIKE and GLOB
|
||||
** operators and for the LENGTH() and SUBSTR() functions.
|
||||
*/
|
||||
extern const char sqlite_encoding[];
|
||||
|
||||
/*
|
||||
** Each open sqlite database is represented by an instance of the
|
||||
** following opaque structure.
|
||||
*/
|
||||
typedef struct sqlite sqlite;
|
||||
|
||||
/*
|
||||
** A function to open a new sqlite database.
|
||||
**
|
||||
** If the database does not exist and mode indicates write
|
||||
** permission, then a new database is created. If the database
|
||||
** does not exist and mode does not indicate write permission,
|
||||
** then the open fails, an error message generated (if errmsg!=0)
|
||||
** and the function returns 0.
|
||||
**
|
||||
** If mode does not indicates user write permission, then the
|
||||
** database is opened read-only.
|
||||
**
|
||||
** The Truth: As currently implemented, all databases are opened
|
||||
** for writing all the time. Maybe someday we will provide the
|
||||
** ability to open a database readonly. The mode parameters is
|
||||
** provided in anticipation of that enhancement.
|
||||
*/
|
||||
sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
|
||||
|
||||
/*
|
||||
** A function to close the database.
|
||||
**
|
||||
** Call this function with a pointer to a structure that was previously
|
||||
** returned from sqlite_open() and the corresponding database will by closed.
|
||||
*/
|
||||
void sqlite_close(sqlite *);
|
||||
|
||||
/*
|
||||
** The type for a callback function.
|
||||
*/
|
||||
typedef int (*sqlite_callback)(void*,int,char**, char**);
|
||||
|
||||
/*
|
||||
** A function to executes one or more statements of SQL.
|
||||
**
|
||||
** If one or more of the SQL statements are queries, then
|
||||
** the callback function specified by the 3rd parameter is
|
||||
** invoked once for each row of the query result. This callback
|
||||
** should normally return 0. If the callback returns a non-zero
|
||||
** value then the query is aborted, all subsequent SQL statements
|
||||
** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
|
||||
**
|
||||
** The 4th parameter is an arbitrary pointer that is passed
|
||||
** to the callback function as its first parameter.
|
||||
**
|
||||
** The 2nd parameter to the callback function is the number of
|
||||
** columns in the query result. The 3rd parameter to the callback
|
||||
** is an array of strings holding the values for each column.
|
||||
** The 4th parameter to the callback is an array of strings holding
|
||||
** the names of each column.
|
||||
**
|
||||
** The callback function may be NULL, even for queries. A NULL
|
||||
** callback is not an error. It just means that no callback
|
||||
** will be invoked.
|
||||
**
|
||||
** If an error occurs while parsing or evaluating the SQL (but
|
||||
** not while executing the callback) then an appropriate error
|
||||
** message is written into memory obtained from malloc() and
|
||||
** *errmsg is made to point to that message. The calling function
|
||||
** is responsible for freeing the memory that holds the error
|
||||
** message. Use sqlite_freemem() for this. If errmsg==NULL,
|
||||
** then no error message is ever written.
|
||||
**
|
||||
** The return value is is SQLITE_OK if there are no errors and
|
||||
** some other return code if there is an error. The particular
|
||||
** return value depends on the type of error.
|
||||
**
|
||||
** If the query could not be executed because a database file is
|
||||
** locked or busy, then this function returns SQLITE_BUSY. (This
|
||||
** behavior can be modified somewhat using the sqlite_busy_handler()
|
||||
** and sqlite_busy_timeout() functions below.)
|
||||
*/
|
||||
int sqlite_exec(
|
||||
sqlite*, /* An open database */
|
||||
const char *sql, /* SQL to be executed */
|
||||
sqlite_callback, /* Callback function */
|
||||
void *, /* 1st argument to callback function */
|
||||
char **errmsg /* Error msg written here */
|
||||
);
|
||||
|
||||
/*
|
||||
** Return values for sqlite_exec() and sqlite_step()
|
||||
*/
|
||||
#define SQLITE_OK 0 /* Successful result */
|
||||
#define SQLITE_ERROR 1 /* SQL error or missing database */
|
||||
#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
|
||||
#define SQLITE_PERM 3 /* Access permission denied */
|
||||
#define SQLITE_ABORT 4 /* Callback routine requested an abort */
|
||||
#define SQLITE_BUSY 5 /* The database file is locked */
|
||||
#define SQLITE_LOCKED 6 /* A table in the database is locked */
|
||||
#define SQLITE_NOMEM 7 /* A malloc() failed */
|
||||
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
|
||||
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
|
||||
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
|
||||
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
|
||||
#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
|
||||
#define SQLITE_FULL 13 /* Insertion failed because database is full */
|
||||
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
|
||||
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
|
||||
#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
|
||||
#define SQLITE_SCHEMA 17 /* The database schema changed */
|
||||
#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
|
||||
#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
|
||||
#define SQLITE_MISMATCH 20 /* Data type mismatch */
|
||||
#define SQLITE_MISUSE 21 /* Library used incorrectly */
|
||||
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
|
||||
#define SQLITE_AUTH 23 /* Authorization denied */
|
||||
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
|
||||
#define SQLITE_ROW 100 /* sqlite_step() has another row ready */
|
||||
#define SQLITE_DONE 101 /* sqlite_step() has finished executing */
|
||||
|
||||
/*
|
||||
** Each entry in an SQLite table has a unique integer key. (The key is
|
||||
** the value of the INTEGER PRIMARY KEY column if there is such a column,
|
||||
** otherwise the key is generated at random. The unique key is always
|
||||
** available as the ROWID, OID, or _ROWID_ column.) The following routine
|
||||
** returns the integer key of the most recent insert in the database.
|
||||
**
|
||||
** This function is similar to the mysql_insert_id() function from MySQL.
|
||||
*/
|
||||
int sqlite_last_insert_rowid(sqlite*);
|
||||
|
||||
/*
|
||||
** This function returns the number of database rows that were changed
|
||||
** (or inserted or deleted) by the most recent called sqlite_exec().
|
||||
**
|
||||
** All changes are counted, even if they were later undone by a
|
||||
** ROLLBACK or ABORT. Except, changes associated with creating and
|
||||
** dropping tables are not counted.
|
||||
**
|
||||
** If a callback invokes sqlite_exec() recursively, then the changes
|
||||
** in the inner, recursive call are counted together with the changes
|
||||
** in the outer call.
|
||||
**
|
||||
** SQLite implements the command "DELETE FROM table" without a WHERE clause
|
||||
** by dropping and recreating the table. (This is much faster than going
|
||||
** through and deleting individual elements form the table.) Because of
|
||||
** this optimization, the change count for "DELETE FROM table" will be
|
||||
** zero regardless of the number of elements that were originally in the
|
||||
** table. To get an accurate count of the number of rows deleted, use
|
||||
** "DELETE FROM table WHERE 1" instead.
|
||||
*/
|
||||
int sqlite_changes(sqlite*);
|
||||
|
||||
/* If the parameter to this routine is one of the return value constants
|
||||
** defined above, then this routine returns a constant text string which
|
||||
** descripts (in English) the meaning of the return value.
|
||||
*/
|
||||
const char *sqlite_error_string(int);
|
||||
#define sqliteErrStr sqlite_error_string /* Legacy. Do not use in new code. */
|
||||
|
||||
/* This function causes any pending database operation to abort and
|
||||
** return at its earliest opportunity. This routine is typically
|
||||
** called in response to a user action such as pressing "Cancel"
|
||||
** or Ctrl-C where the user wants a long query operation to halt
|
||||
** immediately.
|
||||
*/
|
||||
void sqlite_interrupt(sqlite*);
|
||||
|
||||
|
||||
/* This function returns true if the given input string comprises
|
||||
** one or more complete SQL statements.
|
||||
**
|
||||
** The algorithm is simple. If the last token other than spaces
|
||||
** and comments is a semicolon, then return true. otherwise return
|
||||
** false.
|
||||
*/
|
||||
int sqlite_complete(const char *sql);
|
||||
|
||||
/*
|
||||
** This routine identifies a callback function that is invoked
|
||||
** whenever an attempt is made to open a database table that is
|
||||
** currently locked by another process or thread. If the busy callback
|
||||
** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
|
||||
** it finds a locked table. If the busy callback is not NULL, then
|
||||
** sqlite_exec() invokes the callback with three arguments. The
|
||||
** second argument is the name of the locked table and the third
|
||||
** argument is the number of times the table has been busy. If the
|
||||
** busy callback returns 0, then sqlite_exec() immediately returns
|
||||
** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
|
||||
** tries to open the table again and the cycle repeats.
|
||||
**
|
||||
** The default busy callback is NULL.
|
||||
**
|
||||
** Sqlite is re-entrant, so the busy handler may start a new query.
|
||||
** (It is not clear why anyone would every want to do this, but it
|
||||
** is allowed, in theory.) But the busy handler may not close the
|
||||
** database. Closing the database from a busy handler will delete
|
||||
** data structures out from under the executing query and will
|
||||
** probably result in a coredump.
|
||||
*/
|
||||
void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
|
||||
|
||||
/*
|
||||
** This routine sets a busy handler that sleeps for a while when a
|
||||
** table is locked. The handler will sleep multiple times until
|
||||
** at least "ms" milleseconds of sleeping have been done. After
|
||||
** "ms" milleseconds of sleeping, the handler returns 0 which
|
||||
** causes sqlite_exec() to return SQLITE_BUSY.
|
||||
**
|
||||
** Calling this routine with an argument less than or equal to zero
|
||||
** turns off all busy handlers.
|
||||
*/
|
||||
void sqlite_busy_timeout(sqlite*, int ms);
|
||||
|
||||
/*
|
||||
** This next routine is really just a wrapper around sqlite_exec().
|
||||
** Instead of invoking a user-supplied callback for each row of the
|
||||
** result, this routine remembers each row of the result in memory
|
||||
** obtained from malloc(), then returns all of the result after the
|
||||
** query has finished.
|
||||
**
|
||||
** As an example, suppose the query result where this table:
|
||||
**
|
||||
** Name | Age
|
||||
** -----------------------
|
||||
** Alice | 43
|
||||
** Bob | 28
|
||||
** Cindy | 21
|
||||
**
|
||||
** If the 3rd argument were &azResult then after the function returns
|
||||
** azResult will contain the following data:
|
||||
**
|
||||
** azResult[0] = "Name";
|
||||
** azResult[1] = "Age";
|
||||
** azResult[2] = "Alice";
|
||||
** azResult[3] = "43";
|
||||
** azResult[4] = "Bob";
|
||||
** azResult[5] = "28";
|
||||
** azResult[6] = "Cindy";
|
||||
** azResult[7] = "21";
|
||||
**
|
||||
** Notice that there is an extra row of data containing the column
|
||||
** headers. But the *nrow return value is still 3. *ncolumn is
|
||||
** set to 2. In general, the number of values inserted into azResult
|
||||
** will be ((*nrow) + 1)*(*ncolumn).
|
||||
**
|
||||
** After the calling function has finished using the result, it should
|
||||
** pass the result data pointer to sqlite_free_table() in order to
|
||||
** release the memory that was malloc-ed. Because of the way the
|
||||
** malloc() happens, the calling function must not try to call
|
||||
** malloc() directly. Only sqlite_free_table() is able to release
|
||||
** the memory properly and safely.
|
||||
**
|
||||
** The return value of this routine is the same as from sqlite_exec().
|
||||
*/
|
||||
int sqlite_get_table(
|
||||
sqlite*, /* An open database */
|
||||
const char *sql, /* SQL to be executed */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg /* Error msg written here */
|
||||
);
|
||||
|
||||
/*
|
||||
** Call this routine to free the memory that sqlite_get_table() allocated.
|
||||
*/
|
||||
void sqlite_free_table(char **result);
|
||||
|
||||
/*
|
||||
** The following routines are wrappers around sqlite_exec() and
|
||||
** sqlite_get_table(). The only difference between the routines that
|
||||
** follow and the originals is that the second argument to the
|
||||
** routines that follow is really a printf()-style format
|
||||
** string describing the SQL to be executed. Arguments to the format
|
||||
** string appear at the end of the argument list.
|
||||
**
|
||||
** All of the usual printf formatting options apply. In addition, there
|
||||
** is a "%q" option. %q works like %s in that it substitutes a null-terminated
|
||||
** string from the argument list. But %q also doubles every '\'' character.
|
||||
** %q is designed for use inside a string literal. By doubling each '\''
|
||||
** character it escapes that character and allows it to be inserted into
|
||||
** the string.
|
||||
**
|
||||
** For example, so some string variable contains text as follows:
|
||||
**
|
||||
** char *zText = "It's a happy day!";
|
||||
**
|
||||
** We can use this text in an SQL statement as follows:
|
||||
**
|
||||
** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
|
||||
** callback1, 0, 0, zText);
|
||||
**
|
||||
** Because the %q format string is used, the '\'' character in zText
|
||||
** is escaped and the SQL generated is as follows:
|
||||
**
|
||||
** INSERT INTO table1 VALUES('It''s a happy day!')
|
||||
**
|
||||
** This is correct. Had we used %s instead of %q, the generated SQL
|
||||
** would have looked like this:
|
||||
**
|
||||
** INSERT INTO table1 VALUES('It's a happy day!');
|
||||
**
|
||||
** This second example is an SQL syntax error. As a general rule you
|
||||
** should always use %q instead of %s when inserting text into a string
|
||||
** literal.
|
||||
*/
|
||||
int sqlite_exec_printf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
sqlite_callback, /* Callback function */
|
||||
void *, /* 1st argument to callback function */
|
||||
char **errmsg, /* Error msg written here */
|
||||
... /* Arguments to the format string. */
|
||||
);
|
||||
int sqlite_exec_vprintf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
sqlite_callback, /* Callback function */
|
||||
void *, /* 1st argument to callback function */
|
||||
char **errmsg, /* Error msg written here */
|
||||
va_list ap /* Arguments to the format string. */
|
||||
);
|
||||
int sqlite_get_table_printf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg, /* Error msg written here */
|
||||
... /* Arguments to the format string */
|
||||
);
|
||||
int sqlite_get_table_vprintf(
|
||||
sqlite*, /* An open database */
|
||||
const char *sqlFormat, /* printf-style format string for the SQL */
|
||||
char ***resultp, /* Result written to a char *[] that this points to */
|
||||
int *nrow, /* Number of result rows written here */
|
||||
int *ncolumn, /* Number of result columns written here */
|
||||
char **errmsg, /* Error msg written here */
|
||||
va_list ap /* Arguments to the format string */
|
||||
);
|
||||
char *sqlite_mprintf(const char*,...);
|
||||
char *sqlite_vmprintf(const char*, va_list);
|
||||
|
||||
/*
|
||||
** Windows systems should call this routine to free memory that
|
||||
** is returned in the in the errmsg parameter of sqlite_open() when
|
||||
** SQLite is a DLL. For some reason, it does not work to call free()
|
||||
** directly.
|
||||
*/
|
||||
void sqlite_freemem(void *p);
|
||||
|
||||
/*
|
||||
** Windows systems need functions to call to return the sqlite_version
|
||||
** and sqlite_encoding strings.
|
||||
*/
|
||||
const char *sqlite_libversion(void);
|
||||
const char *sqlite_libencoding(void);
|
||||
|
||||
/*
|
||||
** A pointer to the following structure is used to communicate with
|
||||
** the implementations of user-defined functions.
|
||||
*/
|
||||
typedef struct sqlite_func sqlite_func;
|
||||
|
||||
/*
|
||||
** Use the following routines to create new user-defined functions. See
|
||||
** the documentation for details.
|
||||
*/
|
||||
int sqlite_create_function(
|
||||
sqlite*, /* Database where the new function is registered */
|
||||
const char *zName, /* Name of the new function */
|
||||
int nArg, /* Number of arguments. -1 means any number */
|
||||
void (*xFunc)(sqlite_func*,int,const char**), /* C code to implement */
|
||||
void *pUserData /* Available via the sqlite_user_data() call */
|
||||
);
|
||||
int sqlite_create_aggregate(
|
||||
sqlite*, /* Database where the new function is registered */
|
||||
const char *zName, /* Name of the function */
|
||||
int nArg, /* Number of arguments */
|
||||
void (*xStep)(sqlite_func*,int,const char**), /* Called for each row */
|
||||
void (*xFinalize)(sqlite_func*), /* Called once to get final result */
|
||||
void *pUserData /* Available via the sqlite_user_data() call */
|
||||
);
|
||||
|
||||
/*
|
||||
** Use the following routine to define the datatype returned by a
|
||||
** user-defined function. The second argument can be one of the
|
||||
** constants SQLITE_NUMERIC, SQLITE_TEXT, or SQLITE_ARGS or it
|
||||
** can be an integer greater than or equal to zero. The datatype
|
||||
** will be numeric or text (the only two types supported) if the
|
||||
** argument is SQLITE_NUMERIC or SQLITE_TEXT. If the argument is
|
||||
** SQLITE_ARGS, then the datatype is numeric if any argument to the
|
||||
** function is numeric and is text otherwise. If the second argument
|
||||
** is an integer, then the datatype of the result is the same as the
|
||||
** parameter to the function that corresponds to that integer.
|
||||
*/
|
||||
int sqlite_function_type(
|
||||
sqlite *db, /* The database there the function is registered */
|
||||
const char *zName, /* Name of the function */
|
||||
int datatype /* The datatype for this function */
|
||||
);
|
||||
#define SQLITE_NUMERIC (-1)
|
||||
#define SQLITE_TEXT (-2)
|
||||
#define SQLITE_ARGS (-3)
|
||||
|
||||
/*
|
||||
** The user function implementations call one of the following four routines
|
||||
** in order to return their results. The first parameter to each of these
|
||||
** routines is a copy of the first argument to xFunc() or xFinialize().
|
||||
** The second parameter to these routines is the result to be returned.
|
||||
** A NULL can be passed as the second parameter to sqlite_set_result_string()
|
||||
** in order to return a NULL result.
|
||||
**
|
||||
** The 3rd argument to _string and _error is the number of characters to
|
||||
** take from the string. If this argument is negative, then all characters
|
||||
** up to and including the first '\000' are used.
|
||||
**
|
||||
** The sqlite_set_result_string() function allocates a buffer to hold the
|
||||
** result and returns a pointer to this buffer. The calling routine
|
||||
** (that is, the implmentation of a user function) can alter the content
|
||||
** of this buffer if desired.
|
||||
*/
|
||||
char *sqlite_set_result_string(sqlite_func*,const char*,int);
|
||||
void sqlite_set_result_int(sqlite_func*,int);
|
||||
void sqlite_set_result_double(sqlite_func*,double);
|
||||
void sqlite_set_result_error(sqlite_func*,const char*,int);
|
||||
|
||||
/*
|
||||
** The pUserData parameter to the sqlite_create_function() and
|
||||
** sqlite_create_aggregate() routines used to register user functions
|
||||
** is available to the implementation of the function using this
|
||||
** call.
|
||||
*/
|
||||
void *sqlite_user_data(sqlite_func*);
|
||||
|
||||
/*
|
||||
** Aggregate functions use the following routine to allocate
|
||||
** a structure for storing their state. The first time this routine
|
||||
** is called for a particular aggregate, a new structure of size nBytes
|
||||
** is allocated, zeroed, and returned. On subsequent calls (for the
|
||||
** same aggregate instance) the same buffer is returned. The implementation
|
||||
** of the aggregate can use the returned buffer to accumulate data.
|
||||
**
|
||||
** The buffer allocated is freed automatically be SQLite.
|
||||
*/
|
||||
void *sqlite_aggregate_context(sqlite_func*, int nBytes);
|
||||
|
||||
/*
|
||||
** The next routine returns the number of calls to xStep for a particular
|
||||
** aggregate function instance. The current call to xStep counts so this
|
||||
** routine always returns at least 1.
|
||||
*/
|
||||
int sqlite_aggregate_count(sqlite_func*);
|
||||
|
||||
/*
|
||||
** This routine registers a callback with the SQLite library. The
|
||||
** callback is invoked (at compile-time, not at run-time) for each
|
||||
** attempt to access a column of a table in the database. The callback
|
||||
** returns SQLITE_OK if access is allowed, SQLITE_DENY if the entire
|
||||
** SQL statement should be aborted with an error and SQLITE_IGNORE
|
||||
** if the column should be treated as a NULL value.
|
||||
*/
|
||||
int sqlite_set_authorizer(
|
||||
sqlite*,
|
||||
int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
|
||||
void *pUserData
|
||||
);
|
||||
|
||||
/*
|
||||
** The second parameter to the access authorization function above will
|
||||
** be one of the values below. These values signify what kind of operation
|
||||
** is to be authorized. The 3rd and 4th parameters to the authorization
|
||||
** function will be parameters or NULL depending on which of the following
|
||||
** codes is used as the second parameter. The 5th parameter is the name
|
||||
** of the database ("main", "temp", etc.) if applicable. The 6th parameter
|
||||
** is the name of the inner-most trigger or view that is responsible for
|
||||
** the access attempt or NULL if this access attempt is directly from
|
||||
** input SQL code.
|
||||
**
|
||||
** Arg-3 Arg-4
|
||||
*/
|
||||
#define SQLITE_COPY 0 /* Table Name File Name */
|
||||
#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
|
||||
#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
|
||||
#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
|
||||
#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
|
||||
#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
|
||||
#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
|
||||
#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
|
||||
#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
|
||||
#define SQLITE_DELETE 9 /* Table Name NULL */
|
||||
#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
|
||||
#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
|
||||
#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
|
||||
#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
|
||||
#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
|
||||
#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
|
||||
#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
|
||||
#define SQLITE_DROP_VIEW 17 /* View Name NULL */
|
||||
#define SQLITE_INSERT 18 /* Table Name NULL */
|
||||
#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
|
||||
#define SQLITE_READ 20 /* Table Name Column Name */
|
||||
#define SQLITE_SELECT 21 /* NULL NULL */
|
||||
#define SQLITE_TRANSACTION 22 /* NULL NULL */
|
||||
#define SQLITE_UPDATE 23 /* Table Name Column Name */
|
||||
#define SQLITE_ATTACH 24 /* Filename NULL */
|
||||
#define SQLITE_DETACH 25 /* Database Name NULL */
|
||||
|
||||
|
||||
/*
|
||||
** The return value of the authorization function should be one of the
|
||||
** following constants:
|
||||
*/
|
||||
/* #define SQLITE_OK 0 // Allow access (This is actually defined above) */
|
||||
#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
|
||||
#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
|
||||
|
||||
/*
|
||||
** Register a function that is called at every invocation of sqlite_exec()
|
||||
** or sqlite_compile(). This function can be used (for example) to generate
|
||||
** a log file of all SQL executed against a database.
|
||||
*/
|
||||
void *sqlite_trace(sqlite*, void(*xTrace)(void*,const char*), void*);
|
||||
|
||||
/*** The Callback-Free API
|
||||
**
|
||||
** The following routines implement a new way to access SQLite that does not
|
||||
** involve the use of callbacks.
|
||||
**
|
||||
** An sqlite_vm is an opaque object that represents a single SQL statement
|
||||
** that is ready to be executed.
|
||||
*/
|
||||
typedef struct sqlite_vm sqlite_vm;
|
||||
|
||||
/*
|
||||
** To execute an SQLite query without the use of callbacks, you first have
|
||||
** to compile the SQL using this routine. The 1st parameter "db" is a pointer
|
||||
** to an sqlite object obtained from sqlite_open(). The 2nd parameter
|
||||
** "zSql" is the text of the SQL to be compiled. The remaining parameters
|
||||
** are all outputs.
|
||||
**
|
||||
** *pzTail is made to point to the first character past the end of the first
|
||||
** SQL statement in zSql. This routine only compiles the first statement
|
||||
** in zSql, so *pzTail is left pointing to what remains uncompiled.
|
||||
**
|
||||
** *ppVm is left pointing to a "virtual machine" that can be used to execute
|
||||
** the compiled statement. Or if there is an error, *ppVm may be set to NULL.
|
||||
** If the input text contained no SQL (if the input is and empty string or
|
||||
** a comment) then *ppVm is set to NULL.
|
||||
**
|
||||
** If any errors are detected during compilation, an error message is written
|
||||
** into space obtained from malloc() and *pzErrMsg is made to point to that
|
||||
** error message. The calling routine is responsible for freeing the text
|
||||
** of this message when it has finished with it. Use sqlite_freemem() to
|
||||
** free the message. pzErrMsg may be NULL in which case no error message
|
||||
** will be generated.
|
||||
**
|
||||
** On success, SQLITE_OK is returned. Otherwise and error code is returned.
|
||||
*/
|
||||
int sqlite_compile(
|
||||
sqlite *db, /* The open database */
|
||||
const char *zSql, /* SQL statement to be compiled */
|
||||
const char **pzTail, /* OUT: uncompiled tail of zSql */
|
||||
sqlite_vm **ppVm, /* OUT: the virtual machine to execute zSql */
|
||||
char **pzErrmsg /* OUT: Error message. */
|
||||
);
|
||||
|
||||
/*
|
||||
** After an SQL statement has been compiled, it is handed to this routine
|
||||
** to be executed. This routine executes the statement as far as it can
|
||||
** go then returns. The return value will be one of SQLITE_DONE,
|
||||
** SQLITE_ERROR, SQLITE_BUSY, SQLITE_ROW, or SQLITE_MISUSE.
|
||||
**
|
||||
** SQLITE_DONE means that the execute of the SQL statement is complete
|
||||
** an no errors have occurred. sqlite_step() should not be called again
|
||||
** for the same virtual machine. *pN is set to the number of columns in
|
||||
** the result set and *pazColName is set to an array of strings that
|
||||
** describe the column names and datatypes. The name of the i-th column
|
||||
** is (*pazColName)[i] and the datatype of the i-th column is
|
||||
** (*pazColName)[i+*pN]. *pazValue is set to NULL.
|
||||
**
|
||||
** SQLITE_ERROR means that the virtual machine encountered a run-time
|
||||
** error. sqlite_step() should not be called again for the same
|
||||
** virtual machine. *pN is set to 0 and *pazColName and *pazValue are set
|
||||
** to NULL. Use sqlite_finalize() to obtain the specific error code
|
||||
** and the error message text for the error.
|
||||
**
|
||||
** SQLITE_BUSY means that an attempt to open the database failed because
|
||||
** another thread or process is holding a lock. The calling routine
|
||||
** can try again to open the database by calling sqlite_step() again.
|
||||
** The return code will only be SQLITE_BUSY if no busy handler is registered
|
||||
** using the sqlite_busy_handler() or sqlite_busy_timeout() routines. If
|
||||
** a busy handler callback has been registered but returns 0, then this
|
||||
** routine will return SQLITE_ERROR and sqltie_finalize() will return
|
||||
** SQLITE_BUSY when it is called.
|
||||
**
|
||||
** SQLITE_ROW means that a single row of the result is now available.
|
||||
** The data is contained in *pazValue. The value of the i-th column is
|
||||
** (*azValue)[i]. *pN and *pazColName are set as described in SQLITE_DONE.
|
||||
** Invoke sqlite_step() again to advance to the next row.
|
||||
**
|
||||
** SQLITE_MISUSE is returned if sqlite_step() is called incorrectly.
|
||||
** For example, if you call sqlite_step() after the virtual machine
|
||||
** has halted (after a prior call to sqlite_step() has returned SQLITE_DONE)
|
||||
** or if you call sqlite_step() with an incorrectly initialized virtual
|
||||
** machine or a virtual machine that has been deleted or that is associated
|
||||
** with an sqlite structure that has been closed.
|
||||
*/
|
||||
int sqlite_step(
|
||||
sqlite_vm *pVm, /* The virtual machine to execute */
|
||||
int *pN, /* OUT: Number of columns in result */
|
||||
const char ***pazValue, /* OUT: Column data */
|
||||
const char ***pazColName /* OUT: Column names and datatypes */
|
||||
);
|
||||
|
||||
/*
|
||||
** This routine is called to delete a virtual machine after it has finished
|
||||
** executing. The return value is the result code. SQLITE_OK is returned
|
||||
** if the statement executed successfully and some other value is returned if
|
||||
** there was any kind of error. If an error occurred and pzErrMsg is not
|
||||
** NULL, then an error message is written into memory obtained from malloc()
|
||||
** and *pzErrMsg is made to point to that error message. The calling routine
|
||||
** should use sqlite_freemem() to delete this message when it has finished
|
||||
** with it.
|
||||
**
|
||||
** This routine can be called at any point during the execution of the
|
||||
** virtual machine. If the virtual machine has not completed execution
|
||||
** when this routine is called, that is like encountering an error or
|
||||
** an interrupt. (See sqlite_interrupt().) Incomplete updates may be
|
||||
** rolled back and transactions cancelled, depending on the circumstances,
|
||||
** and the result code returned will be SQLITE_ABORT.
|
||||
*/
|
||||
int sqlite_finalize(sqlite_vm*, char **pzErrMsg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* End of the 'extern "C"' block */
|
||||
#endif
|
||||
|
||||
#endif /* _SQLITE_H_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,8 +0,0 @@
|
||||
#include "config.w32.h"
|
||||
#if ZTS
|
||||
# define THREADSAFE 1
|
||||
#endif
|
||||
#if !ZEND_DEBUG && !defined(NDEBUG)
|
||||
# define NDEBUG
|
||||
#endif
|
||||
#define SQLITE_PTR_SZ 4
|
||||
@@ -1,202 +0,0 @@
|
||||
/*
|
||||
** 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 contains the sqlite_get_table() and sqlite_free_table()
|
||||
** interface routines. These are just wrappers around the main
|
||||
** interface routine of sqlite_exec().
|
||||
**
|
||||
** These routines are in a separate files so that they will not be linked
|
||||
** if they are not used.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** This structure is used to pass data from sqlite_get_table() through
|
||||
** to the callback function is uses to build the result.
|
||||
*/
|
||||
typedef struct TabResult {
|
||||
char **azResult;
|
||||
char *zErrMsg;
|
||||
int nResult;
|
||||
int nAlloc;
|
||||
int nRow;
|
||||
int nColumn;
|
||||
int nData;
|
||||
int rc;
|
||||
} TabResult;
|
||||
|
||||
/*
|
||||
** This routine is called once for each row in the result table. Its job
|
||||
** is to fill in the TabResult structure appropriately, allocating new
|
||||
** memory as necessary.
|
||||
*/
|
||||
static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
|
||||
TabResult *p = (TabResult*)pArg;
|
||||
int need;
|
||||
int i;
|
||||
char *z;
|
||||
|
||||
/* Make sure there is enough space in p->azResult to hold everything
|
||||
** we need to remember from this invocation of the callback.
|
||||
*/
|
||||
if( p->nRow==0 && argv!=0 ){
|
||||
need = nCol*2;
|
||||
}else{
|
||||
need = nCol;
|
||||
}
|
||||
if( p->nData + need >= p->nAlloc ){
|
||||
char **azNew;
|
||||
p->nAlloc = p->nAlloc*2 + need + 1;
|
||||
azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
|
||||
if( azNew==0 ){
|
||||
p->rc = SQLITE_NOMEM;
|
||||
return 1;
|
||||
}
|
||||
p->azResult = azNew;
|
||||
}
|
||||
|
||||
/* If this is the first row, then generate an extra row containing
|
||||
** the names of all columns.
|
||||
*/
|
||||
if( p->nRow==0 ){
|
||||
p->nColumn = nCol;
|
||||
for(i=0; i<nCol; i++){
|
||||
if( colv[i]==0 ){
|
||||
z = 0;
|
||||
}else{
|
||||
z = malloc( strlen(colv[i])+1 );
|
||||
if( z==0 ){
|
||||
p->rc = SQLITE_NOMEM;
|
||||
return 1;
|
||||
}
|
||||
strcpy(z, colv[i]);
|
||||
}
|
||||
p->azResult[p->nData++] = z;
|
||||
}
|
||||
}else if( p->nColumn!=nCol ){
|
||||
sqliteSetString(&p->zErrMsg,
|
||||
"sqlite_get_table() called with two or more incompatible queries", 0);
|
||||
p->rc = SQLITE_ERROR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Copy over the row data
|
||||
*/
|
||||
if( argv!=0 ){
|
||||
for(i=0; i<nCol; i++){
|
||||
if( argv[i]==0 ){
|
||||
z = 0;
|
||||
}else{
|
||||
z = malloc( strlen(argv[i])+1 );
|
||||
if( z==0 ){
|
||||
p->rc = SQLITE_NOMEM;
|
||||
return 1;
|
||||
}
|
||||
strcpy(z, argv[i]);
|
||||
}
|
||||
p->azResult[p->nData++] = z;
|
||||
}
|
||||
p->nRow++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Query the database. But instead of invoking a callback for each row,
|
||||
** malloc() for space to hold the result and return the entire results
|
||||
** at the conclusion of the call.
|
||||
**
|
||||
** The result that is written to ***pazResult is held in memory obtained
|
||||
** from malloc(). But the caller cannot free this memory directly.
|
||||
** Instead, the entire table should be passed to sqlite_free_table() when
|
||||
** the calling procedure is finished using it.
|
||||
*/
|
||||
int sqlite_get_table(
|
||||
sqlite *db, /* The database on which the SQL executes */
|
||||
const char *zSql, /* The SQL to be executed */
|
||||
char ***pazResult, /* Write the result table here */
|
||||
int *pnRow, /* Write the number of rows in the result here */
|
||||
int *pnColumn, /* Write the number of columns of result here */
|
||||
char **pzErrMsg /* Write error messages here */
|
||||
){
|
||||
int rc;
|
||||
TabResult res;
|
||||
if( pazResult==0 ){ return SQLITE_ERROR; }
|
||||
*pazResult = 0;
|
||||
if( pnColumn ) *pnColumn = 0;
|
||||
if( pnRow ) *pnRow = 0;
|
||||
res.zErrMsg = 0;
|
||||
res.nResult = 0;
|
||||
res.nRow = 0;
|
||||
res.nColumn = 0;
|
||||
res.nData = 1;
|
||||
res.nAlloc = 20;
|
||||
res.rc = SQLITE_OK;
|
||||
res.azResult = malloc( sizeof(char*)*res.nAlloc );
|
||||
if( res.azResult==0 ){
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
res.azResult[0] = 0;
|
||||
rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
|
||||
if( res.azResult ){
|
||||
res.azResult[0] = (char*)res.nData;
|
||||
}
|
||||
if( rc==SQLITE_ABORT ){
|
||||
sqlite_free_table(&res.azResult[1]);
|
||||
if( res.zErrMsg ){
|
||||
if( pzErrMsg ){
|
||||
free(*pzErrMsg);
|
||||
*pzErrMsg = res.zErrMsg;
|
||||
sqliteStrRealloc(pzErrMsg);
|
||||
}else{
|
||||
sqliteFree(res.zErrMsg);
|
||||
}
|
||||
}
|
||||
return res.rc;
|
||||
}
|
||||
sqliteFree(res.zErrMsg);
|
||||
if( rc!=SQLITE_OK ){
|
||||
sqlite_free_table(&res.azResult[1]);
|
||||
return rc;
|
||||
}
|
||||
if( res.nAlloc>res.nData ){
|
||||
char **azNew;
|
||||
azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
|
||||
if( azNew==0 ){
|
||||
sqlite_free_table(&res.azResult[1]);
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
res.nAlloc = res.nData+1;
|
||||
res.azResult = azNew;
|
||||
}
|
||||
*pazResult = &res.azResult[1];
|
||||
if( pnColumn ) *pnColumn = res.nColumn;
|
||||
if( pnRow ) *pnRow = res.nRow;
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine frees the space the sqlite_get_table() malloced.
|
||||
*/
|
||||
void sqlite_free_table(
|
||||
char **azResult /* Result returned from from sqlite_get_table() */
|
||||
){
|
||||
if( azResult ){
|
||||
int i, n;
|
||||
azResult--;
|
||||
if( azResult==0 ) return;
|
||||
n = (int)azResult[0];
|
||||
for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
|
||||
free(azResult);
|
||||
}
|
||||
}
|
||||
@@ -1,684 +0,0 @@
|
||||
/*
|
||||
** 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.
|
||||
**
|
||||
*************************************************************************
|
||||
** An tokenizer for SQL
|
||||
**
|
||||
** This file contains C code that splits an SQL input string up into
|
||||
** individual tokens and sends those tokens one-by-one over to the
|
||||
** parser for analysis.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
** All the keywords of the SQL language are stored as in a hash
|
||||
** table composed of instances of the following structure.
|
||||
*/
|
||||
typedef struct Keyword Keyword;
|
||||
struct Keyword {
|
||||
char *zName; /* The keyword name */
|
||||
int len; /* Number of characters in the keyword */
|
||||
int tokenType; /* The token value for this keyword */
|
||||
Keyword *pNext; /* Next keyword with the same hash */
|
||||
};
|
||||
|
||||
/*
|
||||
** These are the keywords
|
||||
*/
|
||||
static Keyword aKeywordTable[] = {
|
||||
{ "ABORT", 0, TK_ABORT, 0 },
|
||||
{ "AFTER", 0, TK_AFTER, 0 },
|
||||
{ "ALL", 0, TK_ALL, 0 },
|
||||
{ "AND", 0, TK_AND, 0 },
|
||||
{ "AS", 0, TK_AS, 0 },
|
||||
{ "ASC", 0, TK_ASC, 0 },
|
||||
{ "ATTACH", 0, TK_ATTACH, 0 },
|
||||
{ "BEFORE", 0, TK_BEFORE, 0 },
|
||||
{ "BEGIN", 0, TK_BEGIN, 0 },
|
||||
{ "BETWEEN", 0, TK_BETWEEN, 0 },
|
||||
{ "BY", 0, TK_BY, 0 },
|
||||
{ "CASCADE", 0, TK_CASCADE, 0 },
|
||||
{ "CASE", 0, TK_CASE, 0 },
|
||||
{ "CHECK", 0, TK_CHECK, 0 },
|
||||
{ "CLUSTER", 0, TK_CLUSTER, 0 },
|
||||
{ "COLLATE", 0, TK_COLLATE, 0 },
|
||||
{ "COMMIT", 0, TK_COMMIT, 0 },
|
||||
{ "CONFLICT", 0, TK_CONFLICT, 0 },
|
||||
{ "CONSTRAINT", 0, TK_CONSTRAINT, 0 },
|
||||
{ "COPY", 0, TK_COPY, 0 },
|
||||
{ "CREATE", 0, TK_CREATE, 0 },
|
||||
{ "CROSS", 0, TK_JOIN_KW, 0 },
|
||||
{ "DATABASE", 0, TK_DATABASE, 0 },
|
||||
{ "DEFAULT", 0, TK_DEFAULT, 0 },
|
||||
{ "DEFERRED", 0, TK_DEFERRED, 0 },
|
||||
{ "DEFERRABLE", 0, TK_DEFERRABLE, 0 },
|
||||
{ "DELETE", 0, TK_DELETE, 0 },
|
||||
{ "DELIMITERS", 0, TK_DELIMITERS, 0 },
|
||||
{ "DESC", 0, TK_DESC, 0 },
|
||||
{ "DETACH", 0, TK_DETACH, 0 },
|
||||
{ "DISTINCT", 0, TK_DISTINCT, 0 },
|
||||
{ "DROP", 0, TK_DROP, 0 },
|
||||
{ "END", 0, TK_END, 0 },
|
||||
{ "EACH", 0, TK_EACH, 0 },
|
||||
{ "ELSE", 0, TK_ELSE, 0 },
|
||||
{ "EXCEPT", 0, TK_EXCEPT, 0 },
|
||||
{ "EXPLAIN", 0, TK_EXPLAIN, 0 },
|
||||
{ "FAIL", 0, TK_FAIL, 0 },
|
||||
{ "FOR", 0, TK_FOR, 0 },
|
||||
{ "FOREIGN", 0, TK_FOREIGN, 0 },
|
||||
{ "FROM", 0, TK_FROM, 0 },
|
||||
{ "FULL", 0, TK_JOIN_KW, 0 },
|
||||
{ "GLOB", 0, TK_GLOB, 0 },
|
||||
{ "GROUP", 0, TK_GROUP, 0 },
|
||||
{ "HAVING", 0, TK_HAVING, 0 },
|
||||
{ "IGNORE", 0, TK_IGNORE, 0 },
|
||||
{ "IMMEDIATE", 0, TK_IMMEDIATE, 0 },
|
||||
{ "IN", 0, TK_IN, 0 },
|
||||
{ "INDEX", 0, TK_INDEX, 0 },
|
||||
{ "INITIALLY", 0, TK_INITIALLY, 0 },
|
||||
{ "INNER", 0, TK_JOIN_KW, 0 },
|
||||
{ "INSERT", 0, TK_INSERT, 0 },
|
||||
{ "INSTEAD", 0, TK_INSTEAD, 0 },
|
||||
{ "INTERSECT", 0, TK_INTERSECT, 0 },
|
||||
{ "INTO", 0, TK_INTO, 0 },
|
||||
{ "IS", 0, TK_IS, 0 },
|
||||
{ "ISNULL", 0, TK_ISNULL, 0 },
|
||||
{ "JOIN", 0, TK_JOIN, 0 },
|
||||
{ "KEY", 0, TK_KEY, 0 },
|
||||
{ "LEFT", 0, TK_JOIN_KW, 0 },
|
||||
{ "LIKE", 0, TK_LIKE, 0 },
|
||||
{ "LIMIT", 0, TK_LIMIT, 0 },
|
||||
{ "MATCH", 0, TK_MATCH, 0 },
|
||||
{ "NATURAL", 0, TK_JOIN_KW, 0 },
|
||||
{ "NOT", 0, TK_NOT, 0 },
|
||||
{ "NOTNULL", 0, TK_NOTNULL, 0 },
|
||||
{ "NULL", 0, TK_NULL, 0 },
|
||||
{ "OF", 0, TK_OF, 0 },
|
||||
{ "OFFSET", 0, TK_OFFSET, 0 },
|
||||
{ "ON", 0, TK_ON, 0 },
|
||||
{ "OR", 0, TK_OR, 0 },
|
||||
{ "ORDER", 0, TK_ORDER, 0 },
|
||||
{ "OUTER", 0, TK_JOIN_KW, 0 },
|
||||
{ "PRAGMA", 0, TK_PRAGMA, 0 },
|
||||
{ "PRIMARY", 0, TK_PRIMARY, 0 },
|
||||
{ "RAISE", 0, TK_RAISE, 0 },
|
||||
{ "REFERENCES", 0, TK_REFERENCES, 0 },
|
||||
{ "REPLACE", 0, TK_REPLACE, 0 },
|
||||
{ "RESTRICT", 0, TK_RESTRICT, 0 },
|
||||
{ "RIGHT", 0, TK_JOIN_KW, 0 },
|
||||
{ "ROLLBACK", 0, TK_ROLLBACK, 0 },
|
||||
{ "ROW", 0, TK_ROW, 0 },
|
||||
{ "SELECT", 0, TK_SELECT, 0 },
|
||||
{ "SET", 0, TK_SET, 0 },
|
||||
{ "STATEMENT", 0, TK_STATEMENT, 0 },
|
||||
{ "TABLE", 0, TK_TABLE, 0 },
|
||||
{ "TEMP", 0, TK_TEMP, 0 },
|
||||
{ "TEMPORARY", 0, TK_TEMP, 0 },
|
||||
{ "THEN", 0, TK_THEN, 0 },
|
||||
{ "TRANSACTION", 0, TK_TRANSACTION, 0 },
|
||||
{ "TRIGGER", 0, TK_TRIGGER, 0 },
|
||||
{ "UNION", 0, TK_UNION, 0 },
|
||||
{ "UNIQUE", 0, TK_UNIQUE, 0 },
|
||||
{ "UPDATE", 0, TK_UPDATE, 0 },
|
||||
{ "USING", 0, TK_USING, 0 },
|
||||
{ "VACUUM", 0, TK_VACUUM, 0 },
|
||||
{ "VALUES", 0, TK_VALUES, 0 },
|
||||
{ "VIEW", 0, TK_VIEW, 0 },
|
||||
{ "WHEN", 0, TK_WHEN, 0 },
|
||||
{ "WHERE", 0, TK_WHERE, 0 },
|
||||
};
|
||||
|
||||
/*
|
||||
** This is the hash table
|
||||
*/
|
||||
#define KEY_HASH_SIZE 71
|
||||
static Keyword *apHashTable[KEY_HASH_SIZE];
|
||||
|
||||
|
||||
/*
|
||||
** This function looks up an identifier to determine if it is a
|
||||
** keyword. If it is a keyword, the token code of that keyword is
|
||||
** returned. If the input is not a keyword, TK_ID is returned.
|
||||
*/
|
||||
int sqliteKeywordCode(const char *z, int n){
|
||||
int h;
|
||||
Keyword *p;
|
||||
if( aKeywordTable[0].len==0 ){
|
||||
/* Initialize the keyword hash table */
|
||||
sqliteOsEnterMutex();
|
||||
if( aKeywordTable[0].len==0 ){
|
||||
int i;
|
||||
int n;
|
||||
n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
|
||||
for(i=0; i<n; i++){
|
||||
aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
|
||||
h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
|
||||
h %= KEY_HASH_SIZE;
|
||||
aKeywordTable[i].pNext = apHashTable[h];
|
||||
apHashTable[h] = &aKeywordTable[i];
|
||||
}
|
||||
}
|
||||
sqliteOsLeaveMutex();
|
||||
}
|
||||
h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
|
||||
for(p=apHashTable[h]; p; p=p->pNext){
|
||||
if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){
|
||||
return p->tokenType;
|
||||
}
|
||||
}
|
||||
return TK_ID;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If X is a character that can be used in an identifier then
|
||||
** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
|
||||
**
|
||||
** In this implementation, an identifier can be a string of
|
||||
** alphabetic characters, digits, and "_" plus any character
|
||||
** with the high-order bit set. The latter rule means that
|
||||
** any sequence of UTF-8 characters or characters taken from
|
||||
** an extended ISO8859 character set can form an identifier.
|
||||
*/
|
||||
static const char isIdChar[] = {
|
||||
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
|
||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
|
||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Return the length of the token that begins at z[0]. Return
|
||||
** -1 if the token is (or might be) incomplete. Store the token
|
||||
** type in *tokenType before returning.
|
||||
*/
|
||||
static int sqliteGetToken(const unsigned char *z, int *tokenType){
|
||||
int i;
|
||||
switch( *z ){
|
||||
case ' ': case '\t': case '\n': case '\f': case '\r': {
|
||||
for(i=1; isspace(z[i]); i++){}
|
||||
*tokenType = TK_SPACE;
|
||||
return i;
|
||||
}
|
||||
case '-': {
|
||||
if( z[1]==0 ) return -1;
|
||||
if( z[1]=='-' ){
|
||||
for(i=2; z[i] && z[i]!='\n'; i++){}
|
||||
*tokenType = TK_COMMENT;
|
||||
return i;
|
||||
}
|
||||
*tokenType = TK_MINUS;
|
||||
return 1;
|
||||
}
|
||||
case '(': {
|
||||
if( z[1]=='+' && z[2]==')' ){
|
||||
*tokenType = TK_ORACLE_OUTER_JOIN;
|
||||
return 3;
|
||||
}else{
|
||||
*tokenType = TK_LP;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
case ')': {
|
||||
*tokenType = TK_RP;
|
||||
return 1;
|
||||
}
|
||||
case ';': {
|
||||
*tokenType = TK_SEMI;
|
||||
return 1;
|
||||
}
|
||||
case '+': {
|
||||
*tokenType = TK_PLUS;
|
||||
return 1;
|
||||
}
|
||||
case '*': {
|
||||
*tokenType = TK_STAR;
|
||||
return 1;
|
||||
}
|
||||
case '/': {
|
||||
if( z[1]!='*' || z[2]==0 ){
|
||||
*tokenType = TK_SLASH;
|
||||
return 1;
|
||||
}
|
||||
for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
|
||||
if( z[i] ) i++;
|
||||
*tokenType = TK_COMMENT;
|
||||
return i;
|
||||
}
|
||||
case '%': {
|
||||
*tokenType = TK_REM;
|
||||
return 1;
|
||||
}
|
||||
case '=': {
|
||||
*tokenType = TK_EQ;
|
||||
return 1 + (z[1]=='=');
|
||||
}
|
||||
case '<': {
|
||||
if( z[1]=='=' ){
|
||||
*tokenType = TK_LE;
|
||||
return 2;
|
||||
}else if( z[1]=='>' ){
|
||||
*tokenType = TK_NE;
|
||||
return 2;
|
||||
}else if( z[1]=='<' ){
|
||||
*tokenType = TK_LSHIFT;
|
||||
return 2;
|
||||
}else{
|
||||
*tokenType = TK_LT;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
case '>': {
|
||||
if( z[1]=='=' ){
|
||||
*tokenType = TK_GE;
|
||||
return 2;
|
||||
}else if( z[1]=='>' ){
|
||||
*tokenType = TK_RSHIFT;
|
||||
return 2;
|
||||
}else{
|
||||
*tokenType = TK_GT;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
case '!': {
|
||||
if( z[1]!='=' ){
|
||||
*tokenType = TK_ILLEGAL;
|
||||
return 2;
|
||||
}else{
|
||||
*tokenType = TK_NE;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
case '|': {
|
||||
if( z[1]!='|' ){
|
||||
*tokenType = TK_BITOR;
|
||||
return 1;
|
||||
}else{
|
||||
*tokenType = TK_CONCAT;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
case ',': {
|
||||
*tokenType = TK_COMMA;
|
||||
return 1;
|
||||
}
|
||||
case '&': {
|
||||
*tokenType = TK_BITAND;
|
||||
return 1;
|
||||
}
|
||||
case '~': {
|
||||
*tokenType = TK_BITNOT;
|
||||
return 1;
|
||||
}
|
||||
case '\'': case '"': {
|
||||
int delim = z[0];
|
||||
for(i=1; z[i]; i++){
|
||||
if( z[i]==delim ){
|
||||
if( z[i+1]==delim ){
|
||||
i++;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( z[i] ) i++;
|
||||
*tokenType = TK_STRING;
|
||||
return i;
|
||||
}
|
||||
case '.': {
|
||||
*tokenType = TK_DOT;
|
||||
return 1;
|
||||
}
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9': {
|
||||
*tokenType = TK_INTEGER;
|
||||
for(i=1; isdigit(z[i]); i++){}
|
||||
if( z[i]=='.' && isdigit(z[i+1]) ){
|
||||
i += 2;
|
||||
while( isdigit(z[i]) ){ i++; }
|
||||
*tokenType = TK_FLOAT;
|
||||
}
|
||||
if( (z[i]=='e' || z[i]=='E') &&
|
||||
( isdigit(z[i+1])
|
||||
|| ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
|
||||
)
|
||||
){
|
||||
i += 2;
|
||||
while( isdigit(z[i]) ){ i++; }
|
||||
*tokenType = TK_FLOAT;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
case '[': {
|
||||
for(i=1; z[i] && z[i-1]!=']'; i++){}
|
||||
*tokenType = TK_ID;
|
||||
return i;
|
||||
}
|
||||
default: {
|
||||
if( !isIdChar[*z] ){
|
||||
break;
|
||||
}
|
||||
for(i=1; isIdChar[z[i]]; i++){}
|
||||
*tokenType = sqliteKeywordCode((char*)z, i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
*tokenType = TK_ILLEGAL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Run the parser on the given SQL string. The parser structure is
|
||||
** passed in. An SQLITE_ status code is returned. If an error occurs
|
||||
** and pzErrMsg!=NULL then an error message might be written into
|
||||
** memory obtained from malloc() and *pzErrMsg made to point to that
|
||||
** error message. Or maybe not.
|
||||
*/
|
||||
int sqliteRunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
|
||||
int nErr = 0;
|
||||
int i;
|
||||
void *pEngine;
|
||||
int tokenType;
|
||||
int lastTokenParsed = -1;
|
||||
sqlite *db = pParse->db;
|
||||
extern void *sqliteParserAlloc(void*(*)(int));
|
||||
extern void sqliteParserFree(void*, void(*)(void*));
|
||||
extern int sqliteParser(void*, int, Token, Parse*);
|
||||
|
||||
db->flags &= ~SQLITE_Interrupt;
|
||||
pParse->rc = SQLITE_OK;
|
||||
i = 0;
|
||||
pEngine = sqliteParserAlloc((void*(*)(int))malloc);
|
||||
if( pEngine==0 ){
|
||||
sqliteSetString(pzErrMsg, "out of memory", 0);
|
||||
return 1;
|
||||
}
|
||||
pParse->sLastToken.dyn = 0;
|
||||
pParse->zTail = zSql;
|
||||
while( sqlite_malloc_failed==0 && zSql[i]!=0 ){
|
||||
|
||||
assert( i>=0 );
|
||||
pParse->sLastToken.z = &zSql[i];
|
||||
assert( pParse->sLastToken.dyn==0 );
|
||||
pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType);
|
||||
i += pParse->sLastToken.n;
|
||||
switch( tokenType ){
|
||||
case TK_SPACE:
|
||||
case TK_COMMENT: {
|
||||
if( (db->flags & SQLITE_Interrupt)!=0 ){
|
||||
pParse->rc = SQLITE_INTERRUPT;
|
||||
sqliteSetString(pzErrMsg, "interrupt", 0);
|
||||
goto abort_parse;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TK_ILLEGAL: {
|
||||
sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1,
|
||||
pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0);
|
||||
nErr++;
|
||||
goto abort_parse;
|
||||
}
|
||||
case TK_SEMI: {
|
||||
pParse->zTail = &zSql[i];
|
||||
/* Fall thru into the default case */
|
||||
}
|
||||
default: {
|
||||
sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse);
|
||||
lastTokenParsed = tokenType;
|
||||
if( pParse->rc!=SQLITE_OK ){
|
||||
goto abort_parse;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
abort_parse:
|
||||
if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
|
||||
if( lastTokenParsed!=TK_SEMI ){
|
||||
sqliteParser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
|
||||
pParse->zTail = &zSql[i];
|
||||
}
|
||||
sqliteParser(pEngine, 0, pParse->sLastToken, pParse);
|
||||
}
|
||||
sqliteParserFree(pEngine, free);
|
||||
if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
|
||||
sqliteSetString(&pParse->zErrMsg, sqlite_error_string(pParse->rc), 0);
|
||||
}
|
||||
if( pParse->zErrMsg ){
|
||||
if( pzErrMsg && *pzErrMsg==0 ){
|
||||
*pzErrMsg = pParse->zErrMsg;
|
||||
}else{
|
||||
sqliteFree(pParse->zErrMsg);
|
||||
}
|
||||
pParse->zErrMsg = 0;
|
||||
if( !nErr ) nErr++;
|
||||
}
|
||||
if( pParse->pVdbe && (pParse->useCallback || pParse->nErr>0) ){
|
||||
sqliteVdbeDelete(pParse->pVdbe);
|
||||
pParse->pVdbe = 0;
|
||||
}
|
||||
if( pParse->pNewTable ){
|
||||
sqliteDeleteTable(pParse->db, pParse->pNewTable);
|
||||
pParse->pNewTable = 0;
|
||||
}
|
||||
if( pParse->pNewTrigger ){
|
||||
sqliteDeleteTrigger(pParse->pNewTrigger);
|
||||
pParse->pNewTrigger = 0;
|
||||
}
|
||||
if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
|
||||
pParse->rc = SQLITE_ERROR;
|
||||
}
|
||||
return nErr;
|
||||
}
|
||||
|
||||
/*
|
||||
** Token types used by the sqlite_complete() routine. See the header
|
||||
** comments on that procedure for additional information.
|
||||
*/
|
||||
#define tkEXPLAIN 0
|
||||
#define tkCREATE 1
|
||||
#define tkTEMP 2
|
||||
#define tkTRIGGER 3
|
||||
#define tkEND 4
|
||||
#define tkSEMI 5
|
||||
#define tkWS 6
|
||||
#define tkOTHER 7
|
||||
|
||||
/*
|
||||
** Return TRUE if the given SQL string ends in a semicolon.
|
||||
**
|
||||
** Special handling is require for CREATE TRIGGER statements.
|
||||
** Whenever the CREATE TRIGGER keywords are seen, the statement
|
||||
** must end with ";END;".
|
||||
**
|
||||
** This implementation uses a state machine with 7 states:
|
||||
**
|
||||
** (0) START At the beginning or end of an SQL statement. This routine
|
||||
** returns 1 if it ends in the START state and 0 if it ends
|
||||
** in any other state.
|
||||
**
|
||||
** (1) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
|
||||
** a statement.
|
||||
**
|
||||
** (2) CREATE The keyword CREATE has been seen at the beginning of a
|
||||
** statement, possibly preceeded by EXPLAIN and/or followed by
|
||||
** TEMP or TEMPORARY
|
||||
**
|
||||
** (3) NORMAL We are in the middle of statement which ends with a single
|
||||
** semicolon.
|
||||
**
|
||||
** (4) TRIGGER We are in the middle of a trigger definition that must be
|
||||
** ended by a semicolon, the keyword END, and another semicolon.
|
||||
**
|
||||
** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
|
||||
** the end of a trigger definition.
|
||||
**
|
||||
** (6) END We've seen the ";END" of the ";END;" that occurs at the end
|
||||
** of a trigger difinition.
|
||||
**
|
||||
** Transitions between states above are determined by tokens extracted
|
||||
** from the input. The following tokens are significant:
|
||||
**
|
||||
** (0) tkEXPLAIN The "explain" keyword.
|
||||
** (1) tkCREATE The "create" keyword.
|
||||
** (2) tkTEMP The "temp" or "temporary" keyword.
|
||||
** (3) tkTRIGGER The "trigger" keyword.
|
||||
** (4) tkEND The "end" keyword.
|
||||
** (5) tkSEMI A semicolon.
|
||||
** (6) tkWS Whitespace
|
||||
** (7) tkOTHER Any other SQL token.
|
||||
**
|
||||
** Whitespace never causes a state transition and is always ignored.
|
||||
*/
|
||||
int sqlite_complete(const char *zSql){
|
||||
u8 state = 0; /* Current state, using numbers defined in header comment */
|
||||
u8 token; /* Value of the next token */
|
||||
|
||||
/* The following matrix defines the transition from one state to another
|
||||
** according to what token is seen. trans[state][token] returns the
|
||||
** next state.
|
||||
*/
|
||||
static const u8 trans[7][8] = {
|
||||
/* Token: */
|
||||
/* State: ** EXPLAIN CREATE TEMP TRIGGER END SEMI WS OTHER */
|
||||
/* 0 START: */ { 1, 2, 3, 3, 3, 0, 0, 3, },
|
||||
/* 1 EXPLAIN: */ { 3, 2, 3, 3, 3, 0, 1, 3, },
|
||||
/* 2 CREATE: */ { 3, 3, 2, 4, 3, 0, 2, 3, },
|
||||
/* 3 NORMAL: */ { 3, 3, 3, 3, 3, 0, 3, 3, },
|
||||
/* 4 TRIGGER: */ { 4, 4, 4, 4, 4, 5, 4, 4, },
|
||||
/* 5 SEMI: */ { 4, 4, 4, 4, 6, 5, 5, 4, },
|
||||
/* 6 END: */ { 4, 4, 4, 4, 4, 0, 6, 4, },
|
||||
};
|
||||
|
||||
while( *zSql ){
|
||||
switch( *zSql ){
|
||||
case ';': { /* A semicolon */
|
||||
token = tkSEMI;
|
||||
break;
|
||||
}
|
||||
case ' ':
|
||||
case '\r':
|
||||
case '\t':
|
||||
case '\n':
|
||||
case '\f': { /* White space is ignored */
|
||||
token = tkWS;
|
||||
break;
|
||||
}
|
||||
case '/': { /* C-style comments */
|
||||
if( zSql[1]!='*' ){
|
||||
token = tkOTHER;
|
||||
break;
|
||||
}
|
||||
zSql += 2;
|
||||
while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
|
||||
if( zSql[0]==0 ) return 0;
|
||||
zSql++;
|
||||
token = tkWS;
|
||||
break;
|
||||
}
|
||||
case '-': { /* SQL-style comments from "--" to end of line */
|
||||
if( zSql[1]!='-' ){
|
||||
token = tkOTHER;
|
||||
break;
|
||||
}
|
||||
while( *zSql && *zSql!='\n' ){ zSql++; }
|
||||
if( *zSql==0 ) return state==0;
|
||||
token = tkWS;
|
||||
break;
|
||||
}
|
||||
case '[': { /* Microsoft-style identifiers in [...] */
|
||||
zSql++;
|
||||
while( *zSql && *zSql!=']' ){ zSql++; }
|
||||
if( *zSql==0 ) return 0;
|
||||
token = tkOTHER;
|
||||
break;
|
||||
}
|
||||
case '"': /* single- and double-quoted strings */
|
||||
case '\'': {
|
||||
int c = *zSql;
|
||||
zSql++;
|
||||
while( *zSql && *zSql!=c ){ zSql++; }
|
||||
if( *zSql==0 ) return 0;
|
||||
token = tkOTHER;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
if( isIdChar[(u8)*zSql] ){
|
||||
/* Keywords and unquoted identifiers */
|
||||
int nId;
|
||||
for(nId=1; isIdChar[(u8)zSql[nId]]; nId++){}
|
||||
switch( *zSql ){
|
||||
case 'c': case 'C': {
|
||||
if( nId==6 && sqliteStrNICmp(zSql, "create", 6)==0 ){
|
||||
token = tkCREATE;
|
||||
}else{
|
||||
token = tkOTHER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 't': case 'T': {
|
||||
if( nId==7 && sqliteStrNICmp(zSql, "trigger", 7)==0 ){
|
||||
token = tkTRIGGER;
|
||||
}else if( nId==4 && sqliteStrNICmp(zSql, "temp", 4)==0 ){
|
||||
token = tkTEMP;
|
||||
}else if( nId==9 && sqliteStrNICmp(zSql, "temporary", 9)==0 ){
|
||||
token = tkTEMP;
|
||||
}else{
|
||||
token = tkOTHER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'e': case 'E': {
|
||||
if( nId==3 && sqliteStrNICmp(zSql, "end", 3)==0 ){
|
||||
token = tkEND;
|
||||
}else if( nId==7 && sqliteStrNICmp(zSql, "explain", 7)==0 ){
|
||||
token = tkEXPLAIN;
|
||||
}else{
|
||||
token = tkOTHER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
token = tkOTHER;
|
||||
break;
|
||||
}
|
||||
}
|
||||
zSql += nId-1;
|
||||
}else{
|
||||
/* Operators and special symbols */
|
||||
token = tkOTHER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
state = trans[state][token];
|
||||
zSql++;
|
||||
}
|
||||
return state==0;
|
||||
}
|
||||
@@ -1,760 +0,0 @@
|
||||
/*
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
*************************************************************************
|
||||
*
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** Delete a linked list of TriggerStep structures.
|
||||
*/
|
||||
void sqliteDeleteTriggerStep(TriggerStep *pTriggerStep){
|
||||
while( pTriggerStep ){
|
||||
TriggerStep * pTmp = pTriggerStep;
|
||||
pTriggerStep = pTriggerStep->pNext;
|
||||
|
||||
if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z);
|
||||
sqliteExprDelete(pTmp->pWhere);
|
||||
sqliteExprListDelete(pTmp->pExprList);
|
||||
sqliteSelectDelete(pTmp->pSelect);
|
||||
sqliteIdListDelete(pTmp->pIdList);
|
||||
|
||||
sqliteFree(pTmp);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** This is called by the parser when it sees a CREATE TRIGGER statement
|
||||
** up to the point of the BEGIN before the trigger actions. A Trigger
|
||||
** structure is generated based on the information available and stored
|
||||
** in pParse->pNewTrigger. After the trigger actions have been parsed, the
|
||||
** sqliteFinishTrigger() function is called to complete the trigger
|
||||
** construction process.
|
||||
*/
|
||||
void sqliteBeginTrigger(
|
||||
Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
|
||||
Token *pName, /* The name of the trigger */
|
||||
int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
|
||||
int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
|
||||
IdList *pColumns, /* column list if this is an UPDATE OF trigger */
|
||||
SrcList *pTableName,/* The name of the table/view the trigger applies to */
|
||||
int foreach, /* One of TK_ROW or TK_STATEMENT */
|
||||
Expr *pWhen, /* WHEN clause */
|
||||
int isTemp /* True if the TEMPORARY keyword is present */
|
||||
){
|
||||
Trigger *nt;
|
||||
Table *tab;
|
||||
char *zName = 0; /* Name of the trigger */
|
||||
sqlite *db = pParse->db;
|
||||
int iDb; /* When database to store the trigger in */
|
||||
DbFixer sFix;
|
||||
|
||||
/* Check that:
|
||||
** 1. the trigger name does not already exist.
|
||||
** 2. the table (or view) does exist in the same database as the trigger.
|
||||
** 3. that we are not trying to create a trigger on the sqlite_master table
|
||||
** 4. That we are not trying to create an INSTEAD OF trigger on a table.
|
||||
** 5. That we are not trying to create a BEFORE or AFTER trigger on a view.
|
||||
*/
|
||||
if( sqlite_malloc_failed ) goto trigger_cleanup;
|
||||
assert( pTableName->nSrc==1 );
|
||||
if( pParse->initFlag
|
||||
&& sqliteFixInit(&sFix, pParse, pParse->iDb, "trigger", pName)
|
||||
&& sqliteFixSrcList(&sFix, pTableName)
|
||||
){
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
tab = sqliteSrcListLookup(pParse, pTableName);
|
||||
if( !tab ){
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
iDb = isTemp ? 1 : tab->iDb;
|
||||
if( iDb>=2 && !pParse->initFlag ){
|
||||
sqliteErrorMsg(pParse, "triggers may not be added to auxiliary "
|
||||
"database %s", db->aDb[tab->iDb].zName);
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
|
||||
zName = sqliteStrNDup(pName->z, pName->n);
|
||||
if( sqliteHashFind(&(db->aDb[iDb].trigHash), zName,pName->n+1) ){
|
||||
sqliteErrorMsg(pParse, "trigger %T already exists", pName);
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
if( sqliteStrNICmp(tab->zName, "sqlite_", 7)==0 ){
|
||||
sqliteErrorMsg(pParse, "cannot create trigger on system table");
|
||||
pParse->nErr++;
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
if( tab->pSelect && tr_tm != TK_INSTEAD ){
|
||||
sqliteErrorMsg(pParse, "cannot create %s trigger on view: %S",
|
||||
(tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
if( !tab->pSelect && tr_tm == TK_INSTEAD ){
|
||||
sqliteErrorMsg(pParse, "cannot create INSTEAD OF"
|
||||
" trigger on table: %S", pTableName, 0);
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
{
|
||||
int code = SQLITE_CREATE_TRIGGER;
|
||||
const char *zDb = db->aDb[tab->iDb].zName;
|
||||
const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
|
||||
if( tab->iDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
|
||||
if( sqliteAuthCheck(pParse, code, zName, tab->zName, zDbTrig) ){
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(tab->iDb), 0, zDb)){
|
||||
goto trigger_cleanup;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* INSTEAD OF triggers can only appear on views and BEGIN triggers
|
||||
** cannot appear on views. So we might as well translate every
|
||||
** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
|
||||
** elsewhere.
|
||||
*/
|
||||
if (tr_tm == TK_INSTEAD){
|
||||
tr_tm = TK_BEFORE;
|
||||
}
|
||||
|
||||
/* Build the Trigger object */
|
||||
nt = (Trigger*)sqliteMalloc(sizeof(Trigger));
|
||||
if( nt==0 ) goto trigger_cleanup;
|
||||
nt->name = zName;
|
||||
zName = 0;
|
||||
nt->table = sqliteStrDup(pTableName->a[0].zName);
|
||||
if( sqlite_malloc_failed ) goto trigger_cleanup;
|
||||
nt->iDb = iDb;
|
||||
nt->iTabDb = tab->iDb;
|
||||
nt->op = op;
|
||||
nt->tr_tm = tr_tm;
|
||||
nt->pWhen = sqliteExprDup(pWhen);
|
||||
nt->pColumns = sqliteIdListDup(pColumns);
|
||||
nt->foreach = foreach;
|
||||
sqliteTokenCopy(&nt->nameToken,pName);
|
||||
assert( pParse->pNewTrigger==0 );
|
||||
pParse->pNewTrigger = nt;
|
||||
|
||||
trigger_cleanup:
|
||||
sqliteFree(zName);
|
||||
sqliteSrcListDelete(pTableName);
|
||||
sqliteIdListDelete(pColumns);
|
||||
sqliteExprDelete(pWhen);
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine is called after all of the trigger actions have been parsed
|
||||
** in order to complete the process of building the trigger.
|
||||
*/
|
||||
void sqliteFinishTrigger(
|
||||
Parse *pParse, /* Parser context */
|
||||
TriggerStep *pStepList, /* The triggered program */
|
||||
Token *pAll /* Token that describes the complete CREATE TRIGGER */
|
||||
){
|
||||
Trigger *nt = 0; /* The trigger whose construction is finishing up */
|
||||
sqlite *db = pParse->db; /* The database */
|
||||
DbFixer sFix;
|
||||
|
||||
if( pParse->nErr || pParse->pNewTrigger==0 ) goto triggerfinish_cleanup;
|
||||
nt = pParse->pNewTrigger;
|
||||
pParse->pNewTrigger = 0;
|
||||
nt->step_list = pStepList;
|
||||
while( pStepList ){
|
||||
pStepList->pTrig = nt;
|
||||
pStepList = pStepList->pNext;
|
||||
}
|
||||
if( sqliteFixInit(&sFix, pParse, nt->iDb, "trigger", &nt->nameToken)
|
||||
&& sqliteFixTriggerStep(&sFix, nt->step_list) ){
|
||||
goto triggerfinish_cleanup;
|
||||
}
|
||||
|
||||
/* if we are not initializing, and this trigger is not on a TEMP table,
|
||||
** build the sqlite_master entry
|
||||
*/
|
||||
if( !pParse->initFlag ){
|
||||
static VdbeOp insertTrig[] = {
|
||||
{ OP_NewRecno, 0, 0, 0 },
|
||||
{ OP_String, 0, 0, "trigger" },
|
||||
{ OP_String, 0, 0, 0 }, /* 2: trigger name */
|
||||
{ OP_String, 0, 0, 0 }, /* 3: table name */
|
||||
{ OP_Integer, 0, 0, 0 },
|
||||
{ OP_String, 0, 0, 0 }, /* 5: SQL */
|
||||
{ OP_MakeRecord, 5, 0, 0 },
|
||||
{ OP_PutIntKey, 0, 0, 0 },
|
||||
};
|
||||
int addr;
|
||||
Vdbe *v;
|
||||
|
||||
/* Make an entry in the sqlite_master table */
|
||||
v = sqliteGetVdbe(pParse);
|
||||
if( v==0 ) goto triggerfinish_cleanup;
|
||||
sqliteBeginWriteOperation(pParse, 0, 0);
|
||||
sqliteOpenMasterTable(v, nt->iDb);
|
||||
addr = sqliteVdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
|
||||
sqliteVdbeChangeP3(v, addr+2, nt->name, 0);
|
||||
sqliteVdbeChangeP3(v, addr+3, nt->table, 0);
|
||||
sqliteVdbeChangeP3(v, addr+5, pAll->z, pAll->n);
|
||||
if( nt->iDb==0 ){
|
||||
sqliteChangeCookie(db, v);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Close, 0, 0);
|
||||
sqliteEndWriteOperation(pParse);
|
||||
}
|
||||
|
||||
if( !pParse->explain ){
|
||||
Table *pTab;
|
||||
sqliteHashInsert(&db->aDb[nt->iDb].trigHash,
|
||||
nt->name, strlen(nt->name)+1, nt);
|
||||
pTab = sqliteLocateTable(pParse, nt->table, db->aDb[nt->iTabDb].zName);
|
||||
assert( pTab!=0 );
|
||||
nt->pNext = pTab->pTrigger;
|
||||
pTab->pTrigger = nt;
|
||||
nt = 0;
|
||||
}
|
||||
|
||||
triggerfinish_cleanup:
|
||||
sqliteDeleteTrigger(nt);
|
||||
sqliteDeleteTrigger(pParse->pNewTrigger);
|
||||
pParse->pNewTrigger = 0;
|
||||
sqliteDeleteTriggerStep(pStepList);
|
||||
}
|
||||
|
||||
/*
|
||||
** Make a copy of all components of the given trigger step. This has
|
||||
** the effect of copying all Expr.token.z values into memory obtained
|
||||
** from sqliteMalloc(). As initially created, the Expr.token.z values
|
||||
** all point to the input string that was fed to the parser. But that
|
||||
** string is ephemeral - it will go away as soon as the sqlite_exec()
|
||||
** call that started the parser exits. This routine makes a persistent
|
||||
** copy of all the Expr.token.z strings so that the TriggerStep structure
|
||||
** will be valid even after the sqlite_exec() call returns.
|
||||
*/
|
||||
static void sqlitePersistTriggerStep(TriggerStep *p){
|
||||
if( p->target.z ){
|
||||
p->target.z = sqliteStrNDup(p->target.z, p->target.n);
|
||||
p->target.dyn = 1;
|
||||
}
|
||||
if( p->pSelect ){
|
||||
Select *pNew = sqliteSelectDup(p->pSelect);
|
||||
sqliteSelectDelete(p->pSelect);
|
||||
p->pSelect = pNew;
|
||||
}
|
||||
if( p->pWhere ){
|
||||
Expr *pNew = sqliteExprDup(p->pWhere);
|
||||
sqliteExprDelete(p->pWhere);
|
||||
p->pWhere = pNew;
|
||||
}
|
||||
if( p->pExprList ){
|
||||
ExprList *pNew = sqliteExprListDup(p->pExprList);
|
||||
sqliteExprListDelete(p->pExprList);
|
||||
p->pExprList = pNew;
|
||||
}
|
||||
if( p->pIdList ){
|
||||
IdList *pNew = sqliteIdListDup(p->pIdList);
|
||||
sqliteIdListDelete(p->pIdList);
|
||||
p->pIdList = pNew;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Turn a SELECT statement (that the pSelect parameter points to) into
|
||||
** a trigger step. Return a pointer to a TriggerStep structure.
|
||||
**
|
||||
** The parser calls this routine when it finds a SELECT statement in
|
||||
** body of a TRIGGER.
|
||||
*/
|
||||
TriggerStep *sqliteTriggerSelectStep(Select *pSelect){
|
||||
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
||||
if( pTriggerStep==0 ) return 0;
|
||||
|
||||
pTriggerStep->op = TK_SELECT;
|
||||
pTriggerStep->pSelect = pSelect;
|
||||
pTriggerStep->orconf = OE_Default;
|
||||
sqlitePersistTriggerStep(pTriggerStep);
|
||||
|
||||
return pTriggerStep;
|
||||
}
|
||||
|
||||
/*
|
||||
** Build a trigger step out of an INSERT statement. Return a pointer
|
||||
** to the new trigger step.
|
||||
**
|
||||
** The parser calls this routine when it sees an INSERT inside the
|
||||
** body of a trigger.
|
||||
*/
|
||||
TriggerStep *sqliteTriggerInsertStep(
|
||||
Token *pTableName, /* Name of the table into which we insert */
|
||||
IdList *pColumn, /* List of columns in pTableName to insert into */
|
||||
ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
|
||||
Select *pSelect, /* A SELECT statement that supplies values */
|
||||
int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
|
||||
){
|
||||
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
||||
if( pTriggerStep==0 ) return 0;
|
||||
|
||||
assert(pEList == 0 || pSelect == 0);
|
||||
assert(pEList != 0 || pSelect != 0);
|
||||
|
||||
pTriggerStep->op = TK_INSERT;
|
||||
pTriggerStep->pSelect = pSelect;
|
||||
pTriggerStep->target = *pTableName;
|
||||
pTriggerStep->pIdList = pColumn;
|
||||
pTriggerStep->pExprList = pEList;
|
||||
pTriggerStep->orconf = orconf;
|
||||
sqlitePersistTriggerStep(pTriggerStep);
|
||||
|
||||
return pTriggerStep;
|
||||
}
|
||||
|
||||
/*
|
||||
** Construct a trigger step that implements an UPDATE statement and return
|
||||
** a pointer to that trigger step. The parser calls this routine when it
|
||||
** sees an UPDATE statement inside the body of a CREATE TRIGGER.
|
||||
*/
|
||||
TriggerStep *sqliteTriggerUpdateStep(
|
||||
Token *pTableName, /* Name of the table to be updated */
|
||||
ExprList *pEList, /* The SET clause: list of column and new values */
|
||||
Expr *pWhere, /* The WHERE clause */
|
||||
int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
|
||||
){
|
||||
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
||||
if( pTriggerStep==0 ) return 0;
|
||||
|
||||
pTriggerStep->op = TK_UPDATE;
|
||||
pTriggerStep->target = *pTableName;
|
||||
pTriggerStep->pExprList = pEList;
|
||||
pTriggerStep->pWhere = pWhere;
|
||||
pTriggerStep->orconf = orconf;
|
||||
sqlitePersistTriggerStep(pTriggerStep);
|
||||
|
||||
return pTriggerStep;
|
||||
}
|
||||
|
||||
/*
|
||||
** Construct a trigger step that implements a DELETE statement and return
|
||||
** a pointer to that trigger step. The parser calls this routine when it
|
||||
** sees a DELETE statement inside the body of a CREATE TRIGGER.
|
||||
*/
|
||||
TriggerStep *sqliteTriggerDeleteStep(Token *pTableName, Expr *pWhere){
|
||||
TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
|
||||
if( pTriggerStep==0 ) return 0;
|
||||
|
||||
pTriggerStep->op = TK_DELETE;
|
||||
pTriggerStep->target = *pTableName;
|
||||
pTriggerStep->pWhere = pWhere;
|
||||
pTriggerStep->orconf = OE_Default;
|
||||
sqlitePersistTriggerStep(pTriggerStep);
|
||||
|
||||
return pTriggerStep;
|
||||
}
|
||||
|
||||
/*
|
||||
** Recursively delete a Trigger structure
|
||||
*/
|
||||
void sqliteDeleteTrigger(Trigger *pTrigger){
|
||||
if( pTrigger==0 ) return;
|
||||
sqliteDeleteTriggerStep(pTrigger->step_list);
|
||||
sqliteFree(pTrigger->name);
|
||||
sqliteFree(pTrigger->table);
|
||||
sqliteExprDelete(pTrigger->pWhen);
|
||||
sqliteIdListDelete(pTrigger->pColumns);
|
||||
if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z);
|
||||
sqliteFree(pTrigger);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function is called to drop a trigger from the database schema.
|
||||
*
|
||||
* This may be called directly from the parser and therefore identifies
|
||||
* the trigger by name. The sqliteDropTriggerPtr() routine does the
|
||||
* same job as this routine except it take a spointer to the trigger
|
||||
* instead of the trigger name.
|
||||
*
|
||||
* Note that this function does not delete the trigger entirely. Instead it
|
||||
* removes it from the internal schema and places it in the trigDrop hash
|
||||
* table. This is so that the trigger can be restored into the database schema
|
||||
* if the transaction is rolled back.
|
||||
*/
|
||||
void sqliteDropTrigger(Parse *pParse, SrcList *pName){
|
||||
Trigger *pTrigger;
|
||||
int i;
|
||||
const char *zDb;
|
||||
const char *zName;
|
||||
int nName;
|
||||
sqlite *db = pParse->db;
|
||||
|
||||
if( sqlite_malloc_failed ) goto drop_trigger_cleanup;
|
||||
assert( pName->nSrc==1 );
|
||||
zDb = pName->a[0].zDatabase;
|
||||
zName = pName->a[0].zName;
|
||||
nName = strlen(zName);
|
||||
for(i=0; i<db->nDb; i++){
|
||||
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
|
||||
if( zDb && sqliteStrICmp(db->aDb[j].zName, zDb) ) continue;
|
||||
pTrigger = sqliteHashFind(&(db->aDb[j].trigHash), zName, nName+1);
|
||||
if( pTrigger ) break;
|
||||
}
|
||||
if( !pTrigger ){
|
||||
sqliteErrorMsg(pParse, "no such trigger: %S", pName, 0);
|
||||
goto drop_trigger_cleanup;
|
||||
}
|
||||
sqliteDropTriggerPtr(pParse, pTrigger, 0);
|
||||
|
||||
drop_trigger_cleanup:
|
||||
sqliteSrcListDelete(pName);
|
||||
}
|
||||
|
||||
/*
|
||||
** Drop a trigger given a pointer to that trigger. If nested is false,
|
||||
** then also generate code to remove the trigger from the SQLITE_MASTER
|
||||
** table.
|
||||
*/
|
||||
void sqliteDropTriggerPtr(Parse *pParse, Trigger *pTrigger, int nested){
|
||||
Table *pTable;
|
||||
Vdbe *v;
|
||||
sqlite *db = pParse->db;
|
||||
|
||||
assert( pTrigger->iDb<db->nDb );
|
||||
if( pTrigger->iDb>=2 ){
|
||||
sqliteErrorMsg(pParse, "triggers may not be removed from "
|
||||
"auxiliary database %s", db->aDb[pTrigger->iDb].zName);
|
||||
return;
|
||||
}
|
||||
pTable = sqliteFindTable(db, pTrigger->table,db->aDb[pTrigger->iTabDb].zName);
|
||||
assert(pTable);
|
||||
assert( pTable->iDb==pTrigger->iDb || pTrigger->iDb==1 );
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
{
|
||||
int code = SQLITE_DROP_TRIGGER;
|
||||
const char *zDb = db->aDb[pTrigger->iDb].zName;
|
||||
const char *zTab = SCHEMA_TABLE(pTrigger->iDb);
|
||||
if( pTrigger->iDb ) code = SQLITE_DROP_TEMP_TRIGGER;
|
||||
if( sqliteAuthCheck(pParse, code, pTrigger->name, pTable->zName, zDb) ||
|
||||
sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Generate code to destroy the database record of the trigger.
|
||||
*/
|
||||
if( pTable!=0 && !nested && (v = sqliteGetVdbe(pParse))!=0 ){
|
||||
int base;
|
||||
static VdbeOp dropTrigger[] = {
|
||||
{ OP_Rewind, 0, ADDR(8), 0},
|
||||
{ OP_String, 0, 0, 0}, /* 1 */
|
||||
{ OP_MemStore, 1, 1, 0},
|
||||
{ OP_MemLoad, 1, 0, 0}, /* 3 */
|
||||
{ OP_Column, 0, 1, 0},
|
||||
{ OP_Ne, 0, ADDR(7), 0},
|
||||
{ OP_Delete, 0, 0, 0},
|
||||
{ OP_Next, 0, ADDR(3), 0}, /* 7 */
|
||||
};
|
||||
|
||||
sqliteBeginWriteOperation(pParse, 0, 0);
|
||||
sqliteOpenMasterTable(v, pTrigger->iDb);
|
||||
base = sqliteVdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
|
||||
sqliteVdbeChangeP3(v, base+1, pTrigger->name, 0);
|
||||
if( pTrigger->iDb==0 ){
|
||||
sqliteChangeCookie(db, v);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Close, 0, 0);
|
||||
sqliteEndWriteOperation(pParse);
|
||||
}
|
||||
|
||||
/*
|
||||
* If this is not an "explain", then delete the trigger structure.
|
||||
*/
|
||||
if( !pParse->explain ){
|
||||
const char *zName = pTrigger->name;
|
||||
int nName = strlen(zName);
|
||||
if( pTable->pTrigger == pTrigger ){
|
||||
pTable->pTrigger = pTrigger->pNext;
|
||||
}else{
|
||||
Trigger *cc = pTable->pTrigger;
|
||||
while( cc ){
|
||||
if( cc->pNext == pTrigger ){
|
||||
cc->pNext = cc->pNext->pNext;
|
||||
break;
|
||||
}
|
||||
cc = cc->pNext;
|
||||
}
|
||||
assert(cc);
|
||||
}
|
||||
sqliteHashInsert(&(db->aDb[pTrigger->iDb].trigHash), zName, nName+1, 0);
|
||||
sqliteDeleteTrigger(pTrigger);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** pEList is the SET clause of an UPDATE statement. Each entry
|
||||
** in pEList is of the format <id>=<expr>. If any of the entries
|
||||
** in pEList have an <id> which matches an identifier in pIdList,
|
||||
** then return TRUE. If pIdList==NULL, then it is considered a
|
||||
** wildcard that matches anything. Likewise if pEList==NULL then
|
||||
** it matches anything so always return true. Return false only
|
||||
** if there is no match.
|
||||
*/
|
||||
static int checkColumnOverLap(IdList *pIdList, ExprList *pEList){
|
||||
int e;
|
||||
if( !pIdList || !pEList ) return 1;
|
||||
for(e=0; e<pEList->nExpr; e++){
|
||||
if( sqliteIdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A global variable that is TRUE if we should always set up temp tables for
|
||||
* for triggers, even if there are no triggers to code. This is used to test
|
||||
* how much overhead the triggers algorithm is causing.
|
||||
*
|
||||
* This flag can be set or cleared using the "trigger_overhead_test" pragma.
|
||||
* The pragma is not documented since it is not really part of the interface
|
||||
* to SQLite, just the test procedure.
|
||||
*/
|
||||
int always_code_trigger_setup = 0;
|
||||
|
||||
/*
|
||||
* Returns true if a trigger matching op, tr_tm and foreach that is NOT already
|
||||
* on the Parse objects trigger-stack (to prevent recursive trigger firing) is
|
||||
* found in the list specified as pTrigger.
|
||||
*/
|
||||
int sqliteTriggersExist(
|
||||
Parse *pParse, /* Used to check for recursive triggers */
|
||||
Trigger *pTrigger, /* A list of triggers associated with a table */
|
||||
int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
|
||||
int tr_tm, /* one of TK_BEFORE, TK_AFTER */
|
||||
int foreach, /* one of TK_ROW or TK_STATEMENT */
|
||||
ExprList *pChanges /* Columns that change in an UPDATE statement */
|
||||
){
|
||||
Trigger * pTriggerCursor;
|
||||
|
||||
if( always_code_trigger_setup ){
|
||||
return 1;
|
||||
}
|
||||
|
||||
pTriggerCursor = pTrigger;
|
||||
while( pTriggerCursor ){
|
||||
if( pTriggerCursor->op == op &&
|
||||
pTriggerCursor->tr_tm == tr_tm &&
|
||||
pTriggerCursor->foreach == foreach &&
|
||||
checkColumnOverLap(pTriggerCursor->pColumns, pChanges) ){
|
||||
TriggerStack * ss;
|
||||
ss = pParse->trigStack;
|
||||
while( ss && ss->pTrigger != pTrigger ){
|
||||
ss = ss->pNext;
|
||||
}
|
||||
if( !ss )return 1;
|
||||
}
|
||||
pTriggerCursor = pTriggerCursor->pNext;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert the pStep->target token into a SrcList and return a pointer
|
||||
** to that SrcList.
|
||||
**
|
||||
** This routine adds a specific database name, if needed, to the target when
|
||||
** forming the SrcList. This prevents a trigger in one database from
|
||||
** referring to a target in another database. An exception is when the
|
||||
** trigger is in TEMP in which case it can refer to any other database it
|
||||
** wants.
|
||||
*/
|
||||
static SrcList *targetSrcList(
|
||||
Parse *pParse, /* The parsing context */
|
||||
TriggerStep *pStep /* The trigger containing the target token */
|
||||
){
|
||||
Token sDb; /* Dummy database name token */
|
||||
int iDb; /* Index of the database to use */
|
||||
SrcList *pSrc; /* SrcList to be returned */
|
||||
|
||||
iDb = pStep->pTrig->iDb;
|
||||
if( iDb==0 || iDb>=2 ){
|
||||
assert( iDb<pParse->db->nDb );
|
||||
sDb.z = pParse->db->aDb[iDb].zName;
|
||||
sDb.n = strlen(sDb.z);
|
||||
pSrc = sqliteSrcListAppend(0, &sDb, &pStep->target);
|
||||
} else {
|
||||
pSrc = sqliteSrcListAppend(0, &pStep->target, 0);
|
||||
}
|
||||
return pSrc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Generate VDBE code for zero or more statements inside the body of a
|
||||
** trigger.
|
||||
*/
|
||||
static int codeTriggerProgram(
|
||||
Parse *pParse, /* The parser context */
|
||||
TriggerStep *pStepList, /* List of statements inside the trigger body */
|
||||
int orconfin /* Conflict algorithm. (OE_Abort, etc) */
|
||||
){
|
||||
TriggerStep * pTriggerStep = pStepList;
|
||||
int orconf;
|
||||
|
||||
while( pTriggerStep ){
|
||||
int saveNTab = pParse->nTab;
|
||||
|
||||
orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
|
||||
pParse->trigStack->orconf = orconf;
|
||||
switch( pTriggerStep->op ){
|
||||
case TK_SELECT: {
|
||||
Select * ss = sqliteSelectDup(pTriggerStep->pSelect);
|
||||
assert(ss);
|
||||
assert(ss->pSrc);
|
||||
sqliteSelect(pParse, ss, SRT_Discard, 0, 0, 0, 0);
|
||||
sqliteSelectDelete(ss);
|
||||
break;
|
||||
}
|
||||
case TK_UPDATE: {
|
||||
SrcList *pSrc;
|
||||
pSrc = targetSrcList(pParse, pTriggerStep);
|
||||
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
|
||||
sqliteUpdate(pParse, pSrc,
|
||||
sqliteExprListDup(pTriggerStep->pExprList),
|
||||
sqliteExprDup(pTriggerStep->pWhere), orconf);
|
||||
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
|
||||
break;
|
||||
}
|
||||
case TK_INSERT: {
|
||||
SrcList *pSrc;
|
||||
pSrc = targetSrcList(pParse, pTriggerStep);
|
||||
sqliteInsert(pParse, pSrc,
|
||||
sqliteExprListDup(pTriggerStep->pExprList),
|
||||
sqliteSelectDup(pTriggerStep->pSelect),
|
||||
sqliteIdListDup(pTriggerStep->pIdList), orconf);
|
||||
break;
|
||||
}
|
||||
case TK_DELETE: {
|
||||
SrcList *pSrc;
|
||||
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPush, 0, 0);
|
||||
pSrc = targetSrcList(pParse, pTriggerStep);
|
||||
sqliteDeleteFrom(pParse, pSrc, sqliteExprDup(pTriggerStep->pWhere));
|
||||
sqliteVdbeAddOp(pParse->pVdbe, OP_ListPop, 0, 0);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
pParse->nTab = saveNTab;
|
||||
pTriggerStep = pTriggerStep->pNext;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** This is called to code FOR EACH ROW triggers.
|
||||
**
|
||||
** When the code that this function generates is executed, the following
|
||||
** must be true:
|
||||
**
|
||||
** 1. No cursors may be open in the main database. (But newIdx and oldIdx
|
||||
** can be indices of cursors in temporary tables. See below.)
|
||||
**
|
||||
** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
|
||||
** a temporary vdbe cursor (index newIdx) must be open and pointing at
|
||||
** a row containing values to be substituted for new.* expressions in the
|
||||
** trigger program(s).
|
||||
**
|
||||
** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
|
||||
** a temporary vdbe cursor (index oldIdx) must be open and pointing at
|
||||
** a row containing values to be substituted for old.* expressions in the
|
||||
** trigger program(s).
|
||||
**
|
||||
*/
|
||||
int sqliteCodeRowTrigger(
|
||||
Parse *pParse, /* Parse context */
|
||||
int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
|
||||
ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
|
||||
int tr_tm, /* One of TK_BEFORE, TK_AFTER */
|
||||
Table *pTab, /* The table to code triggers from */
|
||||
int newIdx, /* The indice of the "new" row to access */
|
||||
int oldIdx, /* The indice of the "old" row to access */
|
||||
int orconf, /* ON CONFLICT policy */
|
||||
int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
|
||||
){
|
||||
Trigger * pTrigger;
|
||||
TriggerStack * pTriggerStack;
|
||||
|
||||
assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
|
||||
assert(tr_tm == TK_BEFORE || tr_tm == TK_AFTER );
|
||||
|
||||
assert(newIdx != -1 || oldIdx != -1);
|
||||
|
||||
pTrigger = pTab->pTrigger;
|
||||
while( pTrigger ){
|
||||
int fire_this = 0;
|
||||
|
||||
/* determine whether we should code this trigger */
|
||||
if( pTrigger->op == op && pTrigger->tr_tm == tr_tm &&
|
||||
pTrigger->foreach == TK_ROW ){
|
||||
fire_this = 1;
|
||||
pTriggerStack = pParse->trigStack;
|
||||
while( pTriggerStack ){
|
||||
if( pTriggerStack->pTrigger == pTrigger ){
|
||||
fire_this = 0;
|
||||
}
|
||||
pTriggerStack = pTriggerStack->pNext;
|
||||
}
|
||||
if( op == TK_UPDATE && pTrigger->pColumns &&
|
||||
!checkColumnOverLap(pTrigger->pColumns, pChanges) ){
|
||||
fire_this = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if( fire_this && (pTriggerStack = sqliteMalloc(sizeof(TriggerStack)))!=0 ){
|
||||
int endTrigger;
|
||||
SrcList dummyTablist;
|
||||
Expr * whenExpr;
|
||||
AuthContext sContext;
|
||||
|
||||
dummyTablist.nSrc = 0;
|
||||
|
||||
/* Push an entry on to the trigger stack */
|
||||
pTriggerStack->pTrigger = pTrigger;
|
||||
pTriggerStack->newIdx = newIdx;
|
||||
pTriggerStack->oldIdx = oldIdx;
|
||||
pTriggerStack->pTab = pTab;
|
||||
pTriggerStack->pNext = pParse->trigStack;
|
||||
pTriggerStack->ignoreJump = ignoreJump;
|
||||
pParse->trigStack = pTriggerStack;
|
||||
sqliteAuthContextPush(pParse, &sContext, pTrigger->name);
|
||||
|
||||
/* code the WHEN clause */
|
||||
endTrigger = sqliteVdbeMakeLabel(pParse->pVdbe);
|
||||
whenExpr = sqliteExprDup(pTrigger->pWhen);
|
||||
if( sqliteExprResolveIds(pParse, &dummyTablist, 0, whenExpr) ){
|
||||
pParse->trigStack = pParse->trigStack->pNext;
|
||||
sqliteFree(pTriggerStack);
|
||||
sqliteExprDelete(whenExpr);
|
||||
return 1;
|
||||
}
|
||||
sqliteExprIfFalse(pParse, whenExpr, endTrigger, 1);
|
||||
sqliteExprDelete(whenExpr);
|
||||
|
||||
codeTriggerProgram(pParse, pTrigger->step_list, orconf);
|
||||
|
||||
/* Pop the entry off the trigger stack */
|
||||
pParse->trigStack = pParse->trigStack->pNext;
|
||||
sqliteAuthContextPop(&sContext);
|
||||
sqliteFree(pTriggerStack);
|
||||
|
||||
sqliteVdbeResolveLabel(pParse->pVdbe, endTrigger);
|
||||
}
|
||||
pTrigger = pTrigger->pNext;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,452 +0,0 @@
|
||||
/*
|
||||
** 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 contains C code routines that are called by the parser
|
||||
** to handle UPDATE statements.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
/*
|
||||
** Process an UPDATE statement.
|
||||
**
|
||||
** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
|
||||
** \_______/ \________/ \______/ \________________/
|
||||
* onError pTabList pChanges pWhere
|
||||
*/
|
||||
void sqliteUpdate(
|
||||
Parse *pParse, /* The parser context */
|
||||
SrcList *pTabList, /* The table in which we should change things */
|
||||
ExprList *pChanges, /* Things to be changed */
|
||||
Expr *pWhere, /* The WHERE clause. May be null */
|
||||
int onError /* How to handle constraint errors */
|
||||
){
|
||||
int i, j; /* Loop counters */
|
||||
Table *pTab; /* The table to be updated */
|
||||
int addr; /* VDBE instruction address of the start of the loop */
|
||||
WhereInfo *pWInfo; /* Information about the WHERE clause */
|
||||
Vdbe *v; /* The virtual database engine */
|
||||
Index *pIdx; /* For looping over indices */
|
||||
int nIdx; /* Number of indices that need updating */
|
||||
int nIdxTotal; /* Total number of indices */
|
||||
int iCur; /* VDBE Cursor number of pTab */
|
||||
sqlite *db; /* The database structure */
|
||||
Index **apIdx = 0; /* An array of indices that need updating too */
|
||||
char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
|
||||
int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
|
||||
** an expression for the i-th column of the table.
|
||||
** aXRef[i]==-1 if the i-th column is not changed. */
|
||||
int chngRecno; /* True if the record number is being changed */
|
||||
Expr *pRecnoExpr; /* Expression defining the new record number */
|
||||
int openAll; /* True if all indices need to be opened */
|
||||
int isView; /* Trying to update a view */
|
||||
AuthContext sContext; /* The authorization context */
|
||||
|
||||
int before_triggers; /* True if there are any BEFORE triggers */
|
||||
int after_triggers; /* True if there are any AFTER triggers */
|
||||
int row_triggers_exist = 0; /* True if any row triggers exist */
|
||||
|
||||
int newIdx = -1; /* index of trigger "new" temp table */
|
||||
int oldIdx = -1; /* index of trigger "old" temp table */
|
||||
|
||||
sContext.pParse = 0;
|
||||
if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
|
||||
db = pParse->db;
|
||||
assert( pTabList->nSrc==1 );
|
||||
|
||||
/* Locate the table which we want to update.
|
||||
*/
|
||||
pTab = sqliteSrcListLookup(pParse, pTabList);
|
||||
if( pTab==0 ) goto update_cleanup;
|
||||
before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
|
||||
TK_UPDATE, TK_BEFORE, TK_ROW, pChanges);
|
||||
after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
|
||||
TK_UPDATE, TK_AFTER, TK_ROW, pChanges);
|
||||
row_triggers_exist = before_triggers || after_triggers;
|
||||
isView = pTab->pSelect!=0;
|
||||
if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
if( isView ){
|
||||
if( sqliteViewGetColumnNames(pParse, pTab) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
}
|
||||
aXRef = sqliteMalloc( sizeof(int) * pTab->nCol );
|
||||
if( aXRef==0 ) goto update_cleanup;
|
||||
for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
|
||||
|
||||
/* If there are FOR EACH ROW triggers, allocate cursors for the
|
||||
** special OLD and NEW tables
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
newIdx = pParse->nTab++;
|
||||
oldIdx = pParse->nTab++;
|
||||
}
|
||||
|
||||
/* Allocate a cursors for the main database table and for all indices.
|
||||
** The index cursors might not be used, but if they are used they
|
||||
** need to occur right after the database cursor. So go ahead and
|
||||
** allocate enough space, just in case.
|
||||
*/
|
||||
pTabList->a[0].iCursor = iCur = pParse->nTab++;
|
||||
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
|
||||
pParse->nTab++;
|
||||
}
|
||||
|
||||
/* Resolve the column names in all the expressions of the
|
||||
** of the UPDATE statement. Also find the column index
|
||||
** for each column to be updated in the pChanges array. For each
|
||||
** column to be updated, make sure we have authorization to change
|
||||
** that column.
|
||||
*/
|
||||
chngRecno = 0;
|
||||
for(i=0; i<pChanges->nExpr; i++){
|
||||
if( sqliteExprResolveIds(pParse, pTabList, 0, pChanges->a[i].pExpr) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
for(j=0; j<pTab->nCol; j++){
|
||||
if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
|
||||
if( j==pTab->iPKey ){
|
||||
chngRecno = 1;
|
||||
pRecnoExpr = pChanges->a[i].pExpr;
|
||||
}
|
||||
aXRef[j] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( j>=pTab->nCol ){
|
||||
if( sqliteIsRowid(pChanges->a[i].zName) ){
|
||||
chngRecno = 1;
|
||||
pRecnoExpr = pChanges->a[i].pExpr;
|
||||
}else{
|
||||
sqliteErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
|
||||
goto update_cleanup;
|
||||
}
|
||||
}
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
{
|
||||
int rc;
|
||||
rc = sqliteAuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
|
||||
pTab->aCol[j].zName, db->aDb[pTab->iDb].zName);
|
||||
if( rc==SQLITE_DENY ){
|
||||
goto update_cleanup;
|
||||
}else if( rc==SQLITE_IGNORE ){
|
||||
aXRef[j] = -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Allocate memory for the array apIdx[] and fill it with pointers to every
|
||||
** index that needs to be updated. Indices only need updating if their
|
||||
** key includes one of the columns named in pChanges or if the record
|
||||
** number of the original table entry is changing.
|
||||
*/
|
||||
for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
|
||||
if( chngRecno ){
|
||||
i = 0;
|
||||
}else {
|
||||
for(i=0; i<pIdx->nColumn; i++){
|
||||
if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
|
||||
}
|
||||
}
|
||||
if( i<pIdx->nColumn ) nIdx++;
|
||||
}
|
||||
if( nIdxTotal>0 ){
|
||||
apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal );
|
||||
if( apIdx==0 ) goto update_cleanup;
|
||||
aIdxUsed = (char*)&apIdx[nIdx];
|
||||
}
|
||||
for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
|
||||
if( chngRecno ){
|
||||
i = 0;
|
||||
}else{
|
||||
for(i=0; i<pIdx->nColumn; i++){
|
||||
if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
|
||||
}
|
||||
}
|
||||
if( i<pIdx->nColumn ){
|
||||
apIdx[nIdx++] = pIdx;
|
||||
aIdxUsed[j] = 1;
|
||||
}else{
|
||||
aIdxUsed[j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Resolve the column names in all the expressions in the
|
||||
** WHERE clause.
|
||||
*/
|
||||
if( pWhere ){
|
||||
if( sqliteExprResolveIds(pParse, pTabList, 0, pWhere) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start the view context
|
||||
*/
|
||||
if( isView ){
|
||||
sqliteAuthContextPush(pParse, &sContext, pTab->zName);
|
||||
}
|
||||
|
||||
/* Begin generating code.
|
||||
*/
|
||||
v = sqliteGetVdbe(pParse);
|
||||
if( v==0 ) goto update_cleanup;
|
||||
sqliteBeginWriteOperation(pParse, 1, pTab->iDb);
|
||||
|
||||
/* If we are trying to update a view, construct that view into
|
||||
** a temporary table.
|
||||
*/
|
||||
if( isView ){
|
||||
Select *pView;
|
||||
pView = sqliteSelectDup(pTab->pSelect);
|
||||
sqliteSelect(pParse, pView, SRT_TempTable, iCur, 0, 0, 0);
|
||||
sqliteSelectDelete(pView);
|
||||
}
|
||||
|
||||
/* Begin the database scan
|
||||
*/
|
||||
pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1, 0);
|
||||
if( pWInfo==0 ) goto update_cleanup;
|
||||
|
||||
/* Remember the index of every item to be updated.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
|
||||
|
||||
/* End the database scan loop.
|
||||
*/
|
||||
sqliteWhereEnd(pWInfo);
|
||||
|
||||
/* Initialize the count of updated rows
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
|
||||
}
|
||||
|
||||
if( row_triggers_exist ){
|
||||
/* Create pseudo-tables for NEW and OLD
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
|
||||
|
||||
/* The top of the update loop for when there are triggers.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
|
||||
addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
|
||||
|
||||
/* Open a cursor and make it point to the record that is
|
||||
** being updated.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
|
||||
|
||||
/* Generate the OLD table
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
|
||||
sqliteVdbeAddOp(v, OP_RowData, iCur, 0);
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
|
||||
|
||||
/* Generate the NEW table
|
||||
*/
|
||||
if( chngRecno ){
|
||||
sqliteExprCode(pParse, pRecnoExpr);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
|
||||
}
|
||||
for(i=0; i<pTab->nCol; i++){
|
||||
if( i==pTab->iPKey ){
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
continue;
|
||||
}
|
||||
j = aXRef[i];
|
||||
if( j<0 ){
|
||||
sqliteVdbeAddOp(v, OP_Column, iCur, i);
|
||||
}else{
|
||||
sqliteExprCode(pParse, pChanges->a[j].pExpr);
|
||||
}
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
|
||||
sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
|
||||
if( !isView ){
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
}
|
||||
|
||||
/* Fire the BEFORE and INSTEAD OF triggers
|
||||
*/
|
||||
if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
|
||||
newIdx, oldIdx, onError, addr) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
if( !isView ){
|
||||
/*
|
||||
** Open every index that needs updating. Note that if any
|
||||
** index could potentially invoke a REPLACE conflict resolution
|
||||
** action, then we need to open all indices because we might need
|
||||
** to be deleting some records.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum);
|
||||
if( onError==OE_Replace ){
|
||||
openAll = 1;
|
||||
}else{
|
||||
openAll = 0;
|
||||
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
|
||||
if( pIdx->onError==OE_Replace ){
|
||||
openAll = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
|
||||
if( openAll || aIdxUsed[i] ){
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenWrite, iCur+i+1, pIdx->tnum);
|
||||
assert( pParse->nTab>iCur+i+1 );
|
||||
}
|
||||
}
|
||||
|
||||
/* Loop over every record that needs updating. We have to load
|
||||
** the old data for each record to be updated because some columns
|
||||
** might not change and we will need to copy the old value.
|
||||
** Also, the old data is needed to delete the old index entires.
|
||||
** So make the cursor point at the old record.
|
||||
*/
|
||||
if( !row_triggers_exist ){
|
||||
sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
|
||||
addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_NotExists, iCur, addr);
|
||||
|
||||
/* If the record number will change, push the record number as it
|
||||
** will be after the update. (The old record number is currently
|
||||
** on top of the stack.)
|
||||
*/
|
||||
if( chngRecno ){
|
||||
sqliteExprCode(pParse, pRecnoExpr);
|
||||
sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
|
||||
}
|
||||
|
||||
/* Compute new data for this record.
|
||||
*/
|
||||
for(i=0; i<pTab->nCol; i++){
|
||||
if( i==pTab->iPKey ){
|
||||
sqliteVdbeAddOp(v, OP_String, 0, 0);
|
||||
continue;
|
||||
}
|
||||
j = aXRef[i];
|
||||
if( j<0 ){
|
||||
sqliteVdbeAddOp(v, OP_Column, iCur, i);
|
||||
}else{
|
||||
sqliteExprCode(pParse, pChanges->a[j].pExpr);
|
||||
}
|
||||
}
|
||||
|
||||
/* Do constraint checks
|
||||
*/
|
||||
sqliteGenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRecno, 1,
|
||||
onError, addr);
|
||||
|
||||
/* Delete the old indices for the current record.
|
||||
*/
|
||||
sqliteGenerateRowIndexDelete(db, v, pTab, iCur, aIdxUsed);
|
||||
|
||||
/* If changing the record number, delete the old record.
|
||||
*/
|
||||
if( chngRecno ){
|
||||
sqliteVdbeAddOp(v, OP_Delete, iCur, 0);
|
||||
}
|
||||
|
||||
/* Create the new index entries and the new record.
|
||||
*/
|
||||
sqliteCompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRecno, 1, -1);
|
||||
}
|
||||
|
||||
/* Increment the row counter
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows && !pParse->trigStack){
|
||||
sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
|
||||
}
|
||||
|
||||
/* If there are triggers, close all the cursors after each iteration
|
||||
** through the loop. The fire the after triggers.
|
||||
*/
|
||||
if( row_triggers_exist ){
|
||||
if( !isView ){
|
||||
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
|
||||
if( openAll || aIdxUsed[i] )
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur+i+1, 0);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
pParse->nTab = iCur;
|
||||
}
|
||||
if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
|
||||
newIdx, oldIdx, onError, addr) ){
|
||||
goto update_cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
/* Repeat the above with the next record to be updated, until
|
||||
** all record selected by the WHERE clause have been updated.
|
||||
*/
|
||||
sqliteVdbeAddOp(v, OP_Goto, 0, addr);
|
||||
sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
|
||||
sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
|
||||
|
||||
/* Close all tables if there were no FOR EACH ROW triggers */
|
||||
if( !row_triggers_exist ){
|
||||
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
|
||||
if( openAll || aIdxUsed[i] ){
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur+i+1, 0);
|
||||
}
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Close, iCur, 0);
|
||||
pParse->nTab = iCur;
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_Close, newIdx, 0);
|
||||
sqliteVdbeAddOp(v, OP_Close, oldIdx, 0);
|
||||
}
|
||||
|
||||
sqliteEndWriteOperation(pParse);
|
||||
|
||||
/*
|
||||
** Return the number of rows that were changed.
|
||||
*/
|
||||
if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
|
||||
sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
|
||||
sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC);
|
||||
sqliteVdbeAddOp(v, OP_Callback, 1, 0);
|
||||
}
|
||||
|
||||
update_cleanup:
|
||||
sqliteAuthContextPop(&sContext);
|
||||
sqliteFree(apIdx);
|
||||
sqliteFree(aXRef);
|
||||
sqliteSrcListDelete(pTabList);
|
||||
sqliteExprListDelete(pChanges);
|
||||
sqliteExprDelete(pWhere);
|
||||
return;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,316 +0,0 @@
|
||||
/*
|
||||
** 2003 April 6
|
||||
**
|
||||
** 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 contains code used to implement the VACUUM command.
|
||||
**
|
||||
** Most of the code in this file may be omitted by defining the
|
||||
** SQLITE_OMIT_VACUUM macro.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
|
||||
/*
|
||||
** A structure for holding a dynamic string - a string that can grow
|
||||
** without bound.
|
||||
*/
|
||||
typedef struct dynStr dynStr;
|
||||
struct dynStr {
|
||||
char *z; /* Text of the string in space obtained from sqliteMalloc() */
|
||||
int nAlloc; /* Amount of space allocated to z[] */
|
||||
int nUsed; /* Next unused slot in z[] */
|
||||
};
|
||||
|
||||
/*
|
||||
** A structure that holds the vacuum context
|
||||
*/
|
||||
typedef struct vacuumStruct vacuumStruct;
|
||||
struct vacuumStruct {
|
||||
sqlite *dbOld; /* Original database */
|
||||
sqlite *dbNew; /* New database */
|
||||
Parse *pParse; /* The parser context */
|
||||
const char *zTable; /* Name of a table being copied */
|
||||
const char *zPragma; /* Pragma to execute with results */
|
||||
dynStr s1, s2; /* Two dynamic strings */
|
||||
};
|
||||
|
||||
#if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
|
||||
/*
|
||||
** Append text to a dynamic string
|
||||
*/
|
||||
static void appendText(dynStr *p, const char *zText, int nText){
|
||||
if( nText<0 ) nText = strlen(zText);
|
||||
if( p->z==0 || p->nUsed + nText + 1 >= p->nAlloc ){
|
||||
char *zNew;
|
||||
p->nAlloc = p->nUsed + nText + 1000;
|
||||
zNew = sqliteRealloc(p->z, p->nAlloc);
|
||||
if( zNew==0 ){
|
||||
sqliteFree(p->z);
|
||||
memset(p, 0, sizeof(*p));
|
||||
return;
|
||||
}
|
||||
p->z = zNew;
|
||||
}
|
||||
memcpy(&p->z[p->nUsed], zText, nText+1);
|
||||
p->nUsed += nText;
|
||||
}
|
||||
|
||||
/*
|
||||
** Append text to a dynamic string, having first put the text in quotes.
|
||||
*/
|
||||
static void appendQuoted(dynStr *p, const char *zText){
|
||||
int i, j;
|
||||
appendText(p, "'", 1);
|
||||
for(i=j=0; zText[i]; i++){
|
||||
if( zText[i]=='\'' ){
|
||||
appendText(p, &zText[j], i-j+1);
|
||||
j = i + 1;
|
||||
appendText(p, "'", 1);
|
||||
}
|
||||
}
|
||||
if( j<i ){
|
||||
appendText(p, &zText[j], i-j);
|
||||
}
|
||||
appendText(p, "'", 1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Execute statements of SQL. If an error occurs, write the error
|
||||
** message into pParse->zErrMsg and return non-zero.
|
||||
*/
|
||||
static int execsql(Parse *pParse, sqlite *db, const char *zSql){
|
||||
int rc;
|
||||
char *zErrMsg = 0;
|
||||
|
||||
/* printf("***** executing *****\n%s\n", zSql); */
|
||||
rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
|
||||
if( rc ){
|
||||
sqliteErrorMsg(pParse, "%s", zErrMsg);
|
||||
sqlite_freemem(zErrMsg);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** This is the second stage callback. Each invocation contains all the
|
||||
** data for a single row of a single table in the original database. This
|
||||
** routine must write that information into the new database.
|
||||
*/
|
||||
static int vacuumCallback2(void *pArg, int argc, char **argv, char **NotUsed){
|
||||
vacuumStruct *p = (vacuumStruct*)pArg;
|
||||
int rc = 0;
|
||||
const char *zSep = "(";
|
||||
int i;
|
||||
|
||||
p->s2.nUsed = 0;
|
||||
appendText(&p->s2, "INSERT INTO ", -1);
|
||||
appendQuoted(&p->s2, p->zTable);
|
||||
appendText(&p->s2, " VALUES", -1);
|
||||
for(i=0; i<argc; i++){
|
||||
appendText(&p->s2, zSep, 1);
|
||||
zSep = ",";
|
||||
if( argv[i]==0 ){
|
||||
appendText(&p->s2, "NULL", 4);
|
||||
}else{
|
||||
appendQuoted(&p->s2, argv[i]);
|
||||
}
|
||||
}
|
||||
appendText(&p->s2,")", 1);
|
||||
rc = execsql(p->pParse, p->dbNew, p->s2.z);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** This is the first stage callback. Each invocation contains three
|
||||
** arguments where are taken from the SQLITE_MASTER table of the original
|
||||
** database: (1) the entry type, (2) the entry name, and (3) the SQL for
|
||||
** the entry. In all cases, execute the SQL of the third argument.
|
||||
** For tables, run a query to select all entries in that table and
|
||||
** transfer them to the second-stage callback.
|
||||
*/
|
||||
static int vacuumCallback1(void *pArg, int argc, char **argv, char **NotUsed){
|
||||
vacuumStruct *p = (vacuumStruct*)pArg;
|
||||
int rc = 0;
|
||||
assert( argc==3 );
|
||||
assert( argv[0]!=0 );
|
||||
assert( argv[1]!=0 );
|
||||
assert( argv[2]!=0 );
|
||||
rc = execsql(p->pParse, p->dbNew, argv[2]);
|
||||
if( rc==SQLITE_OK && strcmp(argv[0],"table")==0 ){
|
||||
char *zErrMsg = 0;
|
||||
p->s1.nUsed = 0;
|
||||
appendText(&p->s1, "SELECT * FROM ", -1);
|
||||
appendQuoted(&p->s1, argv[1]);
|
||||
p->zTable = argv[1];
|
||||
rc = sqlite_exec(p->dbOld, p->s1.z, vacuumCallback2, p, &zErrMsg);
|
||||
if( rc && p->pParse->zErrMsg==0 ){
|
||||
sqliteErrorMsg(p->pParse, "%s", zErrMsg);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** This callback is used to transfer PRAGMA settings from one database
|
||||
** to the other. The value in argv[0] should be passed to a pragma
|
||||
** identified by ((vacuumStruct*)pArg)->zPragma.
|
||||
*/
|
||||
static int vacuumCallback3(void *pArg, int argc, char **argv, char **NotUsed){
|
||||
vacuumStruct *p = (vacuumStruct*)pArg;
|
||||
int rc = 0;
|
||||
char zBuf[200];
|
||||
assert( argc==1 );
|
||||
assert( argv[0]!=0 );
|
||||
assert( strlen(p->zPragma)<100 );
|
||||
assert( strlen(argv[0])<30 );
|
||||
sprintf(zBuf,"PRAGMA %s=%s;", p->zPragma, argv[0]);
|
||||
rc = execsql(p->pParse, p->dbNew, zBuf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Generate a random name of 20 character in length.
|
||||
*/
|
||||
static void randomName(char *zBuf){
|
||||
static const char zChars[] =
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789";
|
||||
int i;
|
||||
for(i=0; i<20; i++){
|
||||
int n = sqliteRandomByte() % (sizeof(zChars)-1);
|
||||
zBuf[i] = zChars[n];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
** The non-standard VACUUM command is used to clean up the database,
|
||||
** collapse free space, etc. It is modelled after the VACUUM command
|
||||
** in PostgreSQL.
|
||||
**
|
||||
** In version 1.0.x of SQLite, the VACUUM command would call
|
||||
** gdbm_reorganize() on all the database tables. But beginning
|
||||
** with 2.0.0, SQLite no longer uses GDBM so this command has
|
||||
** become a no-op.
|
||||
*/
|
||||
void sqliteVacuum(Parse *pParse, Token *pTableName){
|
||||
#if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
|
||||
const char *zFilename; /* full pathname of the database file */
|
||||
int nFilename; /* number of characters in zFilename[] */
|
||||
char *zTemp = 0; /* a temporary file in same directory as zFilename */
|
||||
sqlite *dbNew = 0; /* The new vacuumed database */
|
||||
sqlite *db; /* The original database */
|
||||
int rc = SQLITE_OK; /* Return code from service routines */
|
||||
int i; /* Loop counter */
|
||||
char *zErrMsg = 0; /* Error messages stored here */
|
||||
int safety = 0; /* TRUE if safety is off */
|
||||
vacuumStruct sVac; /* Information passed to callbacks */
|
||||
|
||||
/* These are all of the pragmas that need to be transferred over
|
||||
** to the new database */
|
||||
static const char *zPragma[] = {
|
||||
"default_synchronous",
|
||||
"default_cache_size",
|
||||
/* "default_temp_store", */
|
||||
};
|
||||
|
||||
/* Initial error checks
|
||||
*/
|
||||
if( pParse->explain ){
|
||||
return;
|
||||
}
|
||||
db = pParse->db;
|
||||
if( db->flags & SQLITE_InTrans ){
|
||||
sqliteErrorMsg(pParse, "cannot VACUUM from within a transaction");
|
||||
return;
|
||||
}
|
||||
memset(&sVac, 0, sizeof(sVac));
|
||||
|
||||
/* Get the full pathname of the database file and create two
|
||||
** temporary filenames in the same directory as the original file.
|
||||
*/
|
||||
zFilename = sqliteBtreeGetFilename(db->aDb[0].pBt);
|
||||
if( zFilename==0 ){
|
||||
/* This only happens with the in-memory database. VACUUM is a no-op
|
||||
** there, so just return */
|
||||
return;
|
||||
}
|
||||
nFilename = strlen(zFilename);
|
||||
zTemp = sqliteMalloc( nFilename+100 );
|
||||
if( zTemp==0 ) return;
|
||||
strcpy(zTemp, zFilename);
|
||||
for(i=0; i<10; i++){
|
||||
zTemp[nFilename] = '-';
|
||||
randomName(&zTemp[nFilename+1]);
|
||||
if( !sqliteOsFileExists(zTemp) ) break;
|
||||
}
|
||||
if( i>=10 ){
|
||||
sqliteErrorMsg(pParse, "unable to create a temporary database file "
|
||||
"in the same directory as the original database");
|
||||
goto end_of_vacuum;
|
||||
}
|
||||
|
||||
|
||||
dbNew = sqlite_open(zTemp, 0, &zErrMsg);
|
||||
if( dbNew==0 ){
|
||||
sqliteErrorMsg(pParse, "unable to open a temporary database at %s - %s",
|
||||
zTemp, zErrMsg);
|
||||
goto end_of_vacuum;
|
||||
}
|
||||
if( sqliteSafetyOff(db) ){
|
||||
sqliteErrorMsg(pParse, "library routines called out of sequence");
|
||||
goto end_of_vacuum;
|
||||
}
|
||||
safety = 1;
|
||||
if( execsql(pParse, db, "BEGIN") ) goto end_of_vacuum;
|
||||
if( execsql(pParse, dbNew, "PRAGMA synchronous=off; BEGIN") ){
|
||||
goto end_of_vacuum;
|
||||
}
|
||||
sVac.dbOld = db;
|
||||
sVac.dbNew = dbNew;
|
||||
sVac.pParse = pParse;
|
||||
for(i=0; rc==SQLITE_OK && i<sizeof(zPragma)/sizeof(zPragma[0]); i++){
|
||||
char zBuf[200];
|
||||
assert( strlen(zPragma[i])<100 );
|
||||
sprintf(zBuf, "PRAGMA %s;", zPragma[i]);
|
||||
sVac.zPragma = zPragma[i];
|
||||
rc = sqlite_exec(db, zBuf, vacuumCallback3, &sVac, &zErrMsg);
|
||||
}
|
||||
if( !rc ){
|
||||
rc = sqlite_exec(db, "SELECT type, name, sql FROM sqlite_master "
|
||||
"WHERE sql NOT NULL", vacuumCallback1, &sVac, &zErrMsg);
|
||||
}
|
||||
if( !rc ){
|
||||
rc = sqliteBtreeCopyFile(db->aDb[0].pBt, dbNew->aDb[0].pBt);
|
||||
sqlite_exec(db, "COMMIT", 0, 0, 0);
|
||||
sqlite_exec(db, "ROLLBACK", 0, 0, 0); /* In case the COMMIT failed */
|
||||
sqliteResetInternalSchema(db, 0);
|
||||
}
|
||||
|
||||
end_of_vacuum:
|
||||
if( rc && pParse->zErrMsg==0 && zErrMsg!=0 ){
|
||||
sqliteErrorMsg(pParse, "unable to vacuum database - %s", zErrMsg);
|
||||
}
|
||||
if( safety ) {
|
||||
sqlite_exec(db, "COMMIT", 0, 0, 0);
|
||||
sqlite_exec(db, "ROLLBACK", 0, 0, 0); /* In case the COMMIT failed */
|
||||
sqliteSafetyOn(db);
|
||||
}
|
||||
if( dbNew ) sqlite_close(dbNew);
|
||||
sqliteOsDelete(zTemp);
|
||||
sqliteFree(zTemp);
|
||||
sqliteFree(sVac.s1.z);
|
||||
sqliteFree(sVac.s2.z);
|
||||
if( zErrMsg ) sqlite_freemem(zErrMsg);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
** 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.
|
||||
**
|
||||
*************************************************************************
|
||||
** Header file for the Virtual DataBase Engine (VDBE)
|
||||
**
|
||||
** This header defines the interface to the virtual database engine
|
||||
** or VDBE. The VDBE implements an abstract machine that runs a
|
||||
** simple program to access and modify the underlying database.
|
||||
**
|
||||
** $Id$
|
||||
*/
|
||||
#ifndef _SQLITE_VDBE_H_
|
||||
#define _SQLITE_VDBE_H_
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
** A single VDBE is an opaque structure named "Vdbe". Only routines
|
||||
** in the source file sqliteVdbe.c are allowed to see the insides
|
||||
** of this structure.
|
||||
*/
|
||||
typedef struct Vdbe Vdbe;
|
||||
|
||||
/*
|
||||
** A single instruction of the virtual machine has an opcode
|
||||
** and as many as three operands. The instruction is recorded
|
||||
** as an instance of the following structure:
|
||||
*/
|
||||
struct VdbeOp {
|
||||
int opcode; /* What operation to perform */
|
||||
int p1; /* First operand */
|
||||
int p2; /* Second parameter (often the jump destination) */
|
||||
char *p3; /* Third parameter */
|
||||
int p3type; /* P3_STATIC, P3_DYNAMIC or P3_POINTER */
|
||||
#ifdef VDBE_PROFILE
|
||||
int cnt; /* Number of times this instruction was executed */
|
||||
long long cycles; /* Total time spend executing this instruction */
|
||||
#endif
|
||||
};
|
||||
typedef struct VdbeOp VdbeOp;
|
||||
|
||||
/*
|
||||
** Allowed values of VdbeOp.p3type
|
||||
*/
|
||||
#define P3_NOTUSED 0 /* The P3 parameter is not used */
|
||||
#define P3_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
|
||||
#define P3_STATIC (-2) /* Pointer to a static string */
|
||||
#define P3_POINTER (-3) /* P3 is a pointer to some structure or object */
|
||||
|
||||
/*
|
||||
** The following macro converts a relative address in the p2 field
|
||||
** of a VdbeOp structure into a negative number so that
|
||||
** sqliteVdbeAddOpList() knows that the address is relative. Calling
|
||||
** the macro again restores the address.
|
||||
*/
|
||||
#define ADDR(X) (-1-(X))
|
||||
|
||||
/*
|
||||
** The makefile scans the vdbe.c source file and creates the "opcodes.h"
|
||||
** header file that defines a number for each opcode used by the VDBE.
|
||||
*/
|
||||
#include "opcodes.h"
|
||||
|
||||
/*
|
||||
** Prototypes for the VDBE interface. See comments on the implementation
|
||||
** for a description of what each of these routines does.
|
||||
*/
|
||||
Vdbe *sqliteVdbeCreate(sqlite*);
|
||||
void sqliteVdbeCreateCallback(Vdbe*, int*);
|
||||
int sqliteVdbeAddOp(Vdbe*,int,int,int);
|
||||
int sqliteVdbeAddOpList(Vdbe*, int nOp, VdbeOp const *aOp);
|
||||
void sqliteVdbeChangeP1(Vdbe*, int addr, int P1);
|
||||
void sqliteVdbeChangeP2(Vdbe*, int addr, int P2);
|
||||
void sqliteVdbeChangeP3(Vdbe*, int addr, const char *zP1, int N);
|
||||
void sqliteVdbeDequoteP3(Vdbe*, int addr);
|
||||
int sqliteVdbeFindOp(Vdbe*, int, int);
|
||||
VdbeOp *sqliteVdbeGetOp(Vdbe*, int);
|
||||
int sqliteVdbeMakeLabel(Vdbe*);
|
||||
void sqliteVdbeDelete(Vdbe*);
|
||||
void sqliteVdbeMakeReady(Vdbe*,sqlite_callback,void*,int);
|
||||
int sqliteVdbeExec(Vdbe*);
|
||||
int sqliteVdbeList(Vdbe*);
|
||||
int sqliteVdbeFinalize(Vdbe*,char**);
|
||||
void sqliteVdbeResolveLabel(Vdbe*, int);
|
||||
int sqliteVdbeCurrentAddr(Vdbe*);
|
||||
void sqliteVdbeTrace(Vdbe*,FILE*);
|
||||
void sqliteVdbeCompressSpace(Vdbe*,int);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,37 +0,0 @@
|
||||
EXPORTS
|
||||
sqlite_open
|
||||
sqlite_close
|
||||
sqlite_exec
|
||||
sqlite_last_insert_rowid
|
||||
sqlite_changes
|
||||
sqlite_error_string
|
||||
sqlite_interrupt
|
||||
sqlite_complete
|
||||
sqlite_busy_handler
|
||||
sqlite_busy_timeout
|
||||
sqlite_get_table
|
||||
sqlite_free_table
|
||||
sqlite_exec_printf
|
||||
sqlite_exec_vprintf
|
||||
sqlite_get_table_printf
|
||||
sqlite_get_table_vprintf
|
||||
sqlite_mprintf
|
||||
sqlite_vmprintf
|
||||
sqlite_freemem
|
||||
sqlite_libversion
|
||||
sqlite_libencoding
|
||||
sqlite_create_function
|
||||
sqlite_create_aggregate
|
||||
sqlite_function_type
|
||||
sqlite_set_result_string
|
||||
sqlite_set_result_int
|
||||
sqlite_set_result_double
|
||||
sqlite_set_result_error
|
||||
sqlite_user_data
|
||||
sqlite_aggregate_context
|
||||
sqlite_aggregate_count
|
||||
sqlite_set_authorizer
|
||||
sqlite_trace
|
||||
sqlite_compile
|
||||
sqlite_step
|
||||
sqlite_finalize
|
||||
@@ -1,331 +0,0 @@
|
||||
# Microsoft Developer Studio Project File - Name="sqlite" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=sqlite - Win32 Debug_TS
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "sqlite.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "sqlite.mak" CFG="sqlite - Win32 Debug_TS"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "sqlite - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "sqlite - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "sqlite - Win32 Release_TS"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release_TS"
|
||||
# PROP BASE Intermediate_Dir "Release_TS"
|
||||
# PROP BASE Ignore_Export_Lib 0
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release_TS"
|
||||
# PROP Intermediate_Dir "Release_TS"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SQLITE_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\..\..\php4" /I "..\..\..\php4\main" /I "..\..\..\php4\Zend" /I "..\..\..\php4\TSRM" /I "..\..\..\php4\win32" /I "..\..\..\php_build" /D ZEND_DEBUG=0 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "COMPILE_DL_SQLITE" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_SQLITE=1 /D "PHP_SQLITE_EXPORTS" /FR /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x407 /d "NDEBUG"
|
||||
# ADD RSC /l 0x407 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 php4ts.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS\php_sqlite.dll" /libpath:"..\..\..\php4\Release_TS" /libpath:"..\..\..\php4\Release_TS_Inline" /libpath:"..\..\..\php_build\release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "sqlite - Win32 Debug_TS"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug_TS"
|
||||
# PROP BASE Intermediate_Dir "Debug_TS"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug_TS"
|
||||
# PROP Intermediate_Dir "Debug_TS"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SQLITE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\..\..\php4" /I "..\..\..\php4\main" /I "..\..\..\php4\Zend" /I "..\..\..\php4\TSRM" /I "..\..\..\php4\win32" /I "..\..\..\php_build" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "COMPILE_DL_SQLITE" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_SQLITE=1 /D "PHP_SQLITE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x407 /d "_DEBUG"
|
||||
# ADD RSC /l 0x407 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 php4ts_debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"..\..\Debug_TS\php_sqlite.dll" /pdbtype:sept /libpath:"..\..\..\php4\Debug_TS" /libpath:"..\..\..\php_build\release"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "sqlite - Win32 Release_TS"
|
||||
# Name "sqlite - Win32 Debug_TS"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Group "libsqlite"
|
||||
|
||||
# PROP Default_Filter ""
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\attach.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\auth.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\btree.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\btree.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\btree_rb.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\build.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\config.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\copy.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\delete.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\encode.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\expr.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\func.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\hash.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\hash.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\insert.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\main.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\opcodes.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\opcodes.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\os.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\os.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\pager.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\pager.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\parse.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\parse.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\pragma.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\printf.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\random.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\select.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\sqlite.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\sqlite.w32.h
|
||||
|
||||
!IF "$(CFG)" == "sqlite - Win32 Release_TS"
|
||||
|
||||
# Begin Custom Build
|
||||
InputDir=.\libsqlite\src
|
||||
InputPath=.\libsqlite\src\sqlite.w32.h
|
||||
|
||||
"$(InputDir)\sqlite.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
copy $(InputPath) $(InputDir)\sqlite.h
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "sqlite - Win32 Debug_TS"
|
||||
|
||||
# Begin Custom Build
|
||||
InputDir=.\libsqlite\src
|
||||
InputPath=.\libsqlite\src\sqlite.w32.h
|
||||
|
||||
"$(InputDir)\sqlite.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
copy $(InputPath) $(InputDir)\sqlite.h
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\sqlite_config.w32.h
|
||||
|
||||
!IF "$(CFG)" == "sqlite - Win32 Release_TS"
|
||||
|
||||
# Begin Custom Build
|
||||
InputDir=.\libsqlite\src
|
||||
InputPath=.\libsqlite\src\sqlite_config.w32.h
|
||||
|
||||
"$(InputDir)\config.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
copy $(InputPath) $(InputDir)\config.h
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "sqlite - Win32 Debug_TS"
|
||||
|
||||
# Begin Custom Build
|
||||
InputDir=.\libsqlite\src
|
||||
InputPath=.\libsqlite\src\sqlite_config.w32.h
|
||||
|
||||
"$(InputDir)\config.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
copy $(InputPath) $(InputDir)\config.h
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\sqliteInt.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\table.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\tokenize.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\trigger.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\update.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\util.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\vacuum.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\vdbe.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\vdbe.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\libsqlite\src\where.c
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\php_sqlite.def
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\sqlite.c
|
||||
# ADD CPP /I "libsqlite\src"
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\php_sqlite.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
if (!extension_loaded("sqlite")) {
|
||||
dl("sqlite.so");
|
||||
}
|
||||
|
||||
debug_zval_dump(sqlite_libversion());
|
||||
debug_zval_dump(sqlite_libencoding());
|
||||
|
||||
$s = sqlite_open("weztest.sqlite", 0666, $err);
|
||||
|
||||
debug_zval_dump($err);
|
||||
debug_zval_dump($s);
|
||||
|
||||
$r = sqlite_query("create table foo (a INTEGER PRIMARY KEY, b INTEGER )", $s);
|
||||
debug_zval_dump(sqlite_last_error($s));
|
||||
debug_zval_dump(sqlite_error_string(sqlite_last_error($s)));
|
||||
|
||||
$r = sqlite_query("select *, php('md5', sql) as o from sqlite_master", $s);
|
||||
debug_zval_dump($r);
|
||||
debug_zval_dump(sqlite_num_rows($r));
|
||||
debug_zval_dump(sqlite_num_fields($r));
|
||||
|
||||
for ($j = 0; $j < sqlite_num_fields($r); $j++) {
|
||||
echo "Field $j is " . sqlite_field_name($r, $j) . "\n";
|
||||
}
|
||||
|
||||
while ($row = sqlite_fetch_array($r, SQLITE_ASSOC)) {
|
||||
print_r($row);
|
||||
}
|
||||
|
||||
sqlite_close($s);
|
||||
|
||||
?>
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php #vim:ft=php
|
||||
$dbname = tempnam(dirname(__FILE__), "phpsql");
|
||||
function cleanup() {
|
||||
if ($GLOBALS['db']) {
|
||||
sqlite_close($GLOBALS['db']);
|
||||
}
|
||||
unlink($GLOBALS['dbname']);
|
||||
}
|
||||
register_shutdown_function("cleanup");
|
||||
$db = sqlite_open($dbname);
|
||||
?>
|
||||
@@ -1,17 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: sqlite_open/close
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
require_once('blankdb.inc');
|
||||
echo "$db\n";
|
||||
sqlite_close($db);
|
||||
$db = NULL;
|
||||
echo "Done\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
Resource id #%d
|
||||
Done
|
||||
@@ -1,31 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: Simple insert/select
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
sqlite_query("CREATE TABLE foo(c1 date, c2 time, c3 varchar(64))", $db);
|
||||
sqlite_query("INSERT INTO foo VALUES ('2002-01-02', '12:49:00', NULL)", $db);
|
||||
$r = sqlite_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r));
|
||||
?>
|
||||
--EXPECT--
|
||||
array(6) {
|
||||
[0]=>
|
||||
string(10) "2002-01-02"
|
||||
["c1"]=>
|
||||
string(10) "2002-01-02"
|
||||
[1]=>
|
||||
string(8) "12:49:00"
|
||||
["c2"]=>
|
||||
string(8) "12:49:00"
|
||||
[2]=>
|
||||
NULL
|
||||
["c3"]=>
|
||||
NULL
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: Simple insert/select, different result represenatation
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
sqlite_query("CREATE TABLE foo(c1 date, c2 time, c3 varchar(64))", $db);
|
||||
sqlite_query("INSERT INTO foo VALUES ('2002-01-02', '12:49:00', NULL)", $db);
|
||||
$r = sqlite_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r, SQLITE_BOTH));
|
||||
$r = sqlite_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r, SQLITE_NUM));
|
||||
$r = sqlite_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r, SQLITE_ASSOC));
|
||||
?>
|
||||
--EXPECT--
|
||||
array(6) {
|
||||
[0]=>
|
||||
string(10) "2002-01-02"
|
||||
["c1"]=>
|
||||
string(10) "2002-01-02"
|
||||
[1]=>
|
||||
string(8) "12:49:00"
|
||||
["c2"]=>
|
||||
string(8) "12:49:00"
|
||||
[2]=>
|
||||
NULL
|
||||
["c3"]=>
|
||||
NULL
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(10) "2002-01-02"
|
||||
[1]=>
|
||||
string(8) "12:49:00"
|
||||
[2]=>
|
||||
NULL
|
||||
}
|
||||
array(3) {
|
||||
["c1"]=>
|
||||
string(10) "2002-01-02"
|
||||
["c2"]=>
|
||||
string(8) "12:49:00"
|
||||
["c3"]=>
|
||||
NULL
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: binary encoding
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$strings = array(
|
||||
"hello",
|
||||
"hello\x01o",
|
||||
"\x01hello there",
|
||||
"hello\x00there",
|
||||
""
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a)", $db);
|
||||
|
||||
foreach ($strings as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('" . sqlite_escape_string($str) . "')", $db);
|
||||
}
|
||||
|
||||
$i = 0;
|
||||
$r = sqlite_query("SELECT * from strings", $db);
|
||||
while ($row = sqlite_fetch_array($r, SQLITE_NUM)) {
|
||||
if ($row[0] !== $strings[$i]) {
|
||||
echo "FAIL!\n";
|
||||
var_dump($row[0]);
|
||||
var_dump($strings[$i]);
|
||||
} else {
|
||||
echo "OK!\n";
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
OK!
|
||||
OK!
|
||||
OK!
|
||||
OK!
|
||||
OK!
|
||||
DONE!
|
||||
@@ -1,47 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: aggregate functions
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('" . sqlite_escape_string($str) . "')", $db);
|
||||
}
|
||||
|
||||
function cat_step(&$context, $string)
|
||||
{
|
||||
$context .= $string;
|
||||
}
|
||||
|
||||
function cat_fin(&$context)
|
||||
{
|
||||
return $context;
|
||||
}
|
||||
|
||||
sqlite_create_aggregate($db, "cat", "cat_step", "cat_fin");
|
||||
|
||||
$r = sqlite_query("SELECT cat(a) from strings", $db);
|
||||
while ($row = sqlite_fetch_array($r, SQLITE_NUM)) {
|
||||
var_dump($row);
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(11) "onetwothree"
|
||||
}
|
||||
DONE!
|
||||
@@ -1,52 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: regular functions
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
array("one", "uno"),
|
||||
array("two", "dos"),
|
||||
array("three", "tres"),
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a,b)", $db);
|
||||
|
||||
function implode_args()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$sep = array_shift($args);
|
||||
return implode($sep, $args);
|
||||
}
|
||||
|
||||
foreach ($data as $row) {
|
||||
sqlite_query("INSERT INTO strings VALUES('" . sqlite_escape_string($row[0]) . "','" . sqlite_escape_string($row[1]) . "')", $db);
|
||||
}
|
||||
|
||||
sqlite_create_function($db, "implode", "implode_args");
|
||||
|
||||
$r = sqlite_query("SELECT implode('-', a, b) from strings", $db);
|
||||
while ($row = sqlite_fetch_array($r, SQLITE_NUM)) {
|
||||
var_dump($row);
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(7) "one-uno"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(7) "two-dos"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(10) "three-tres"
|
||||
}
|
||||
DONE!
|
||||
@@ -1,51 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: Simple insert/select (unbuffered)
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
sqlite_query("CREATE TABLE foo(c1 date, c2 time, c3 varchar(64))", $db);
|
||||
sqlite_query("INSERT INTO foo VALUES ('2002-01-02', '12:49:00', NULL)", $db);
|
||||
$r = sqlite_unbuffered_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r, SQLITE_BOTH));
|
||||
$r = sqlite_unbuffered_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r, SQLITE_NUM));
|
||||
$r = sqlite_unbuffered_query("SELECT * from foo", $db);
|
||||
var_dump(sqlite_fetch_array($r, SQLITE_ASSOC));
|
||||
?>
|
||||
--EXPECT--
|
||||
array(6) {
|
||||
[0]=>
|
||||
string(10) "2002-01-02"
|
||||
["c1"]=>
|
||||
string(10) "2002-01-02"
|
||||
[1]=>
|
||||
string(8) "12:49:00"
|
||||
["c2"]=>
|
||||
string(8) "12:49:00"
|
||||
[2]=>
|
||||
NULL
|
||||
["c3"]=>
|
||||
NULL
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(10) "2002-01-02"
|
||||
[1]=>
|
||||
string(8) "12:49:00"
|
||||
[2]=>
|
||||
NULL
|
||||
}
|
||||
array(3) {
|
||||
["c1"]=>
|
||||
string(10) "2002-01-02"
|
||||
["c2"]=>
|
||||
string(8) "12:49:00"
|
||||
["c3"]=>
|
||||
NULL
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch all (buffered)
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('$str')", $db);
|
||||
}
|
||||
|
||||
$r = sqlite_query("SELECT a from strings", $db);
|
||||
while ($row = sqlite_fetch_array($r, SQLITE_NUM)) {
|
||||
var_dump($row);
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
DONE!
|
||||
@@ -1,43 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch all (unbuffered)
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('$str')", $db);
|
||||
}
|
||||
|
||||
$r = sqlite_unbuffered_query("SELECT a from strings", $db);
|
||||
while ($row = sqlite_fetch_array($r, SQLITE_NUM)) {
|
||||
var_dump($row);
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
DONE!
|
||||
@@ -1,78 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch all (iterator)
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('$str')", $db);
|
||||
}
|
||||
|
||||
$r = sqlite_unbuffered_query("SELECT a from strings", $db);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_current($r, SQLITE_NUM));
|
||||
sqlite_next($r);
|
||||
}
|
||||
$r = sqlite_query("SELECT a from strings", $db);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_current($r, SQLITE_NUM));
|
||||
sqlite_next($r);
|
||||
}
|
||||
sqlite_rewind($r);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_current($r, SQLITE_NUM));
|
||||
sqlite_next($r);
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
DONE!
|
||||
@@ -1,27 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: returned associative column names
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
sqlite_query("CREATE TABLE foo (c1 char, c2 char, c3 char)", $db);
|
||||
sqlite_query("CREATE TABLE bar (c1 char, c2 char, c3 char)", $db);
|
||||
sqlite_query("INSERT INTO foo VALUES ('1', '2', '3')", $db);
|
||||
sqlite_query("INSERT INTO bar VALUES ('4', '5', '6')", $db);
|
||||
$r = sqlite_query("SELECT * from foo, bar", $db, SQLITE_ASSOC);
|
||||
var_dump(sqlite_fetch_array($r));
|
||||
?>
|
||||
--EXPECT--
|
||||
array(3) {
|
||||
["c1"]=>
|
||||
string(1) "4"
|
||||
["c2"]=>
|
||||
string(1) "5"
|
||||
["c3"]=>
|
||||
string(1) "6"
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: read field names
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
sqlite_query("CREATE TABLE strings(foo VARCHAR, bar VARCHAR, baz VARCHAR)", $db);
|
||||
|
||||
echo "Buffered\n";
|
||||
$r = sqlite_query("SELECT * from strings", $db);
|
||||
for($i=0; $i<sqlite_num_fields($r); $i++) {
|
||||
var_dump(sqlite_field_name($r, $i));
|
||||
}
|
||||
echo "Unbuffered\n";
|
||||
$r = sqlite_unbuffered_query("SELECT * from strings", $db);
|
||||
for($i=0; $i<sqlite_num_fields($r); $i++) {
|
||||
var_dump(sqlite_field_name($r, $i));
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
Buffered
|
||||
string(3) "foo"
|
||||
string(3) "bar"
|
||||
string(3) "baz"
|
||||
Unbuffered
|
||||
string(3) "foo"
|
||||
string(3) "bar"
|
||||
string(3) "baz"
|
||||
DONE!
|
||||
@@ -1,75 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch column
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
array (0 => 'one', 1 => 'two'),
|
||||
array (0 => 'three', 1 => 'four')
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR, b VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('${str[0]}','${str[1]}')", $db);
|
||||
}
|
||||
|
||||
echo "====BUFFERED====\n";
|
||||
$r = sqlite_query("SELECT a, b from strings", $db);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_current($r, SQLITE_NUM));
|
||||
var_dump(sqlite_column($r, 0));
|
||||
var_dump(sqlite_column($r, 1));
|
||||
var_dump(sqlite_column($r, 'a'));
|
||||
var_dump(sqlite_column($r, 'b'));
|
||||
sqlite_next($r);
|
||||
}
|
||||
echo "====UNBUFFERED====\n";
|
||||
$r = sqlite_unbuffered_query("SELECT a, b from strings", $db);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_column($r, 0));
|
||||
var_dump(sqlite_column($r, 'b'));
|
||||
var_dump(sqlite_column($r, 1));
|
||||
var_dump(sqlite_column($r, 'a'));
|
||||
sqlite_next($r);
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
====BUFFERED====
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(3) "two"
|
||||
}
|
||||
string(3) "one"
|
||||
string(3) "two"
|
||||
string(3) "one"
|
||||
string(3) "two"
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
[1]=>
|
||||
string(4) "four"
|
||||
}
|
||||
string(5) "three"
|
||||
string(4) "four"
|
||||
string(5) "three"
|
||||
string(4) "four"
|
||||
====UNBUFFERED====
|
||||
string(3) "one"
|
||||
string(3) "two"
|
||||
NULL
|
||||
NULL
|
||||
string(5) "three"
|
||||
string(4) "four"
|
||||
NULL
|
||||
NULL
|
||||
DONE!
|
||||
@@ -1,118 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch all (fetch_all)
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('$str')", $db);
|
||||
}
|
||||
|
||||
echo "unbuffered twice\n";
|
||||
$r = sqlite_unbuffered_query("SELECT a from strings", $db, SQLITE_NUM);
|
||||
var_dump(sqlite_fetch_all($r));
|
||||
var_dump(sqlite_fetch_all($r));
|
||||
|
||||
echo "unbuffered with fetch_array\n";
|
||||
$r = sqlite_unbuffered_query("SELECT a from strings", $db, SQLITE_NUM);
|
||||
var_dump(sqlite_fetch_array($r));
|
||||
var_dump(sqlite_fetch_all($r));
|
||||
|
||||
echo "buffered\n";
|
||||
$r = sqlite_query("SELECT a from strings", $db, SQLITE_NUM);
|
||||
var_dump(sqlite_fetch_all($r));
|
||||
var_dump(sqlite_fetch_array($r));
|
||||
var_dump(sqlite_fetch_all($r));
|
||||
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
unbuffered twice
|
||||
array(3) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
}
|
||||
|
||||
Notice: sqlite_fetch_all(): One or more rowsets were already returned in %ssqlite_014.php on line %d
|
||||
array(0) {
|
||||
}
|
||||
unbuffered with fetch_array
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
array(2) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
}
|
||||
buffered
|
||||
array(3) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
}
|
||||
bool(false)
|
||||
array(3) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
}
|
||||
DONE!
|
||||
@@ -1,47 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch all (array_query)
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('$str')", $db);
|
||||
}
|
||||
|
||||
$res = sqlite_array_query("SELECT a from strings", $db, SQLITE_NUM);
|
||||
var_dump($res);
|
||||
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
array(3) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
}
|
||||
}
|
||||
DONE!
|
||||
@@ -1,42 +0,0 @@
|
||||
--TEST--
|
||||
sqlite: fetch string
|
||||
--INI--
|
||||
sqlite.assoc_case=0
|
||||
--SKIPIF--
|
||||
<?php # vim:ft=php
|
||||
if (!extension_loaded("sqlite")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
include "blankdb.inc";
|
||||
|
||||
$data = array(
|
||||
array (0 => 'one', 1 => 'two'),
|
||||
array (0 => 'three', 1 => 'four')
|
||||
);
|
||||
|
||||
sqlite_query("CREATE TABLE strings(a VARCHAR, b VARCHAR)", $db);
|
||||
|
||||
foreach ($data as $str) {
|
||||
sqlite_query("INSERT INTO strings VALUES('${str[0]}','${str[1]}')", $db);
|
||||
}
|
||||
|
||||
echo "====BUFFERED====\n";
|
||||
$r = sqlite_query("SELECT a, b from strings", $db);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_fetch_string($r));
|
||||
}
|
||||
echo "====UNBUFFERED====\n";
|
||||
$r = sqlite_unbuffered_query("SELECT a, b from strings", $db);
|
||||
while (sqlite_has_more($r)) {
|
||||
var_dump(sqlite_fetch_string($r));
|
||||
}
|
||||
echo "DONE!\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
====BUFFERED====
|
||||
string(3) "one"
|
||||
string(5) "three"
|
||||
====UNBUFFERED====
|
||||
string(3) "one"
|
||||
string(5) "three"
|
||||
DONE!
|
||||
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
//
|
||||
// +----------------------------------------------------------------------+
|
||||
// | PHP Version 4 |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | Copyright (c) 1997-2003 The PHP Group |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | This source file is subject to version 3.0 of the PHP license, |
|
||||
// | that is bundled with this package in the file LICENSE, and is |
|
||||
// | available through the world-wide-web at the following url: |
|
||||
// | http://www.php.net/license/3_0.txt. |
|
||||
// | If you did not receive a copy of the PHP license and are unable to |
|
||||
// | obtain it through the world-wide-web, please send a note to |
|
||||
// | license@php.net so we can mail you a copy immediately. |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | Author: Alexander Merz <alexmerz@php.net> |
|
||||
// | |
|
||||
// +----------------------------------------------------------------------+
|
||||
//
|
||||
// $Id$
|
||||
|
||||
require_once "PEAR/Command/Common.php";
|
||||
require_once "PEAR/Command.php";
|
||||
require_once "PEAR/Remote.php";
|
||||
require_once "PEAR.php";
|
||||
|
||||
/**
|
||||
* PEAR commands for providing file mirrors
|
||||
*
|
||||
*/
|
||||
class PEAR_Command_Mirror extends PEAR_Command_Common
|
||||
{
|
||||
// {{{ properties
|
||||
|
||||
var $commands = array(
|
||||
'download-all' => array(
|
||||
'summary' => 'Downloads each avaible Package from master_server',
|
||||
'function' => 'doDownloadAll',
|
||||
'shortcut' => 'da',
|
||||
'options' => array(),
|
||||
'doc' => '
|
||||
Request a list of avaible Packages from the Package-Server
|
||||
(master_server) and downloads them to current working dir'
|
||||
),
|
||||
);
|
||||
|
||||
// }}}
|
||||
|
||||
// {{{ constructor
|
||||
|
||||
/**
|
||||
* PEAR_Command_Mirror constructor.
|
||||
*
|
||||
* @access public
|
||||
* @param object PEAR_Frontend a reference to an frontend
|
||||
* @param object PEAR_Config a reference to the configuration data
|
||||
*/
|
||||
function PEAR_Command_Mirror(&$ui, &$config)
|
||||
{
|
||||
parent::PEAR_Command_Common($ui, $config);
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
// {{{ doDownloadAll()
|
||||
/**
|
||||
* retrieves a list of avaible Packages from master server
|
||||
* and downloads them
|
||||
*
|
||||
* @access public
|
||||
* @param string $command the command
|
||||
* @param array $options the command options before the command
|
||||
* @param array $params the stuff after the command name
|
||||
* @return bool true if succesful
|
||||
* @throw PEAR_Error
|
||||
*/
|
||||
function doDownloadAll($command, $options, $params)
|
||||
{
|
||||
$this->config->set("php_dir", ".");
|
||||
$remote = &new PEAR_Remote($this->config);
|
||||
$remoteInfo = $remote->call("package.listAll");
|
||||
if(PEAR::isError($remoteInfo)) {
|
||||
return $remoteInfo;
|
||||
}
|
||||
$cmd = &PEAR_Command::factory("download", $this->config);
|
||||
if(PEAR::isError($cmd)) {
|
||||
return $cmd;
|
||||
}
|
||||
foreach($remoteInfo as $pkgn=>$pkg) {
|
||||
// error handling not neccesary, because
|
||||
// already done by the download command
|
||||
$cmd->run("download", array(), array($pkgn));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// }}}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
--TEST--
|
||||
Bug #17115 (lambda functions produce segfault with static vars)
|
||||
--FILE--
|
||||
<?php
|
||||
$func = create_function('','
|
||||
static $foo = 0;
|
||||
return $foo++;
|
||||
');
|
||||
var_dump($func());
|
||||
var_dump($func());
|
||||
var_dump($func());
|
||||
?>
|
||||
--EXPECT--
|
||||
int(0)
|
||||
int(1)
|
||||
int(2)
|
||||
Reference in new issue
Block a user