[sql] add startswith/endswith functions

This commit is contained in:
Timothy Stack 2013-07-06 09:16:01 -07:00
parent 3e480b5bad
commit 57deb82f70
4 changed files with 107 additions and 1 deletions

View File

@ -110,12 +110,67 @@ void regexp(sqlite3_context *ctx, int argc, sqlite3_value **argv)
}
}
static
void sql_startswith(sqlite3_context *context,
int argc, sqlite3_value **argv)
{
const char *str_in;
const char *prefix;
if ((sqlite3_value_type(argv[0]) == SQLITE_NULL) ||
(sqlite3_value_type(argv[1]) == SQLITE_NULL)) {
sqlite3_result_null(context);
return;
}
str_in = (const char *)sqlite3_value_text(argv[0]);
prefix = (const char *)sqlite3_value_text(argv[1]);
if (strncmp(str_in, prefix, strlen(prefix)) == 0)
sqlite3_result_int(context, 1);
else
sqlite3_result_int(context, 0);
}
static
void sql_endswith(sqlite3_context *context,
int argc, sqlite3_value **argv)
{
const char *str_in;
const char *suffix;
if ((sqlite3_value_type(argv[0]) == SQLITE_NULL) ||
(sqlite3_value_type(argv[1]) == SQLITE_NULL)) {
sqlite3_result_null(context);
return;
}
str_in = (const char *)sqlite3_value_text(argv[0]);
suffix = (const char *)sqlite3_value_text(argv[1]);
int str_len = strlen(str_in);
int suffix_len = strlen(suffix);
if (str_len < suffix_len) {
sqlite3_result_int(context, 0);
}
else if (strcmp(&str_in[str_len - suffix_len], suffix) == 0) {
sqlite3_result_int(context, 1);
}
else {
sqlite3_result_int(context, 0);
}
}
int string_extension_functions(const struct FuncDef **basic_funcs,
const struct FuncDefAgg **agg_funcs)
{
static const struct FuncDef string_funcs[] = {
{ "regexp", 2, 0, SQLITE_UTF8, 0, regexp },
{ "startswith", 2, 0, SQLITE_UTF8, 0, sql_startswith },
{ "endswith", 2, 0, SQLITE_UTF8, 0, sql_endswith },
{ NULL }
};

View File

@ -226,6 +226,7 @@ TESTS = \
test_pcrepp \
test_sql_coll_func.sh \
test_sql_fs_func.sh \
test_sql_str_func.sh \
test_view_colors.sh \
test_vt52_curses.sh \
test_top_status \

View File

@ -97,7 +97,7 @@ TESTS = test_ansi_scrubber$(EXEEXT) test_auto_fd$(EXEEXT) \
test_grep_proc.sh test_grep_proc2$(EXEEXT) \
test_hist_source$(EXEEXT) test_listview.sh \
test_pcrepp$(EXEEXT) test_sql_coll_func.sh test_sql_fs_func.sh \
test_view_colors.sh test_vt52_curses.sh \
test_sql_str_func.sh test_view_colors.sh test_vt52_curses.sh \
test_top_status$(EXEEXT) test_data_parser.sh \
test_yajlpp$(EXEEXT)
subdir = test
@ -1453,6 +1453,13 @@ test_sql_fs_func.sh.log: test_sql_fs_func.sh
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
test_sql_str_func.sh.log: test_sql_str_func.sh
@p='test_sql_str_func.sh'; \
b='test_sql_str_func.sh'; \
$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
test_view_colors.sh.log: test_view_colors.sh
@p='test_view_colors.sh'; \
b='test_view_colors.sh'; \

43
test/test_sql_str_func.sh Normal file
View File

@ -0,0 +1,43 @@
#! /bin/bash
run_test ./drive_sql "select startswith('.foo', '.')"
check_output "" <<EOF
Row 0:
Column startswith('.foo', '.'): 1
EOF
run_test ./drive_sql "select startswith('foo', '.')"
check_output "" <<EOF
Row 0:
Column startswith('foo', '.'): 0
EOF
run_test ./drive_sql "select endswith('foo', '.')"
check_output "" <<EOF
Row 0:
Column endswith('foo', '.'): 0
EOF
run_test ./drive_sql "select endswith('foo.', '.')"
check_output "" <<EOF
Row 0:
Column endswith('foo.', '.'): 1
EOF
run_test ./drive_sql "select endswith('foo.txt', '.txt')"
check_output "" <<EOF
Row 0:
Column endswith('foo.txt', '.txt'): 1
EOF
run_test ./drive_sql "select endswith('a', '.txt')"
check_output "" <<EOF
Row 0:
Column endswith('a', '.txt'): 0
EOF