Skip to content

Commit

Permalink
Merge pull request #6 from teliseo/teliseo.array_len
Browse files Browse the repository at this point in the history
Add option to return array length if -1 is passed as index
  • Loading branch information
kazuho committed Jan 7, 2014
2 parents 50f26b4 + f2f5a2a commit c2ff21a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
*.so
main_test
7 changes: 7 additions & 0 deletions Makefile
Expand Up @@ -8,10 +8,17 @@ libdir.i386 = /usr/lib
MACHINE := $(shell uname -i)
libdir = $(libdir.$(MACHINE))

.PHONY: install clean

mysql_json.so: mysql_json.cc

install: mysql_json.so
install -p $^ $(libdir)/mysql/plugin

main_test: main_test.cc mysql_json.cc

clean:
rm -f mysql_json.o mysql_json.so main_test

%.so: %.cc
$(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@
95 changes: 95 additions & 0 deletions main_test.cc
@@ -0,0 +1,95 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <mysql/mysql.h>

#define FUNC_NAME json_get

#define CONCAT2(x,y) x##y
#define CONCAT(x,y) CONCAT2(x,y)
#define FUNC_NAME_SUF(suf) CONCAT(FUNC_NAME, suf)
#define MAX_ARGS 1024
#define STRINGIFY(x) #x
#define STR(x) STRINGIFY(x)

extern "C" {
my_bool FUNC_NAME_SUF(_init)(UDF_INIT* initid, UDF_ARGS* args, char* message);
void FUNC_NAME_SUF(_deinit)(UDF_INIT* initid);
char* FUNC_NAME(UDF_INIT* initid, UDF_ARGS* args, char* result, unsigned long* length, char* is_null, char* error);
}

int main(int argc, char *argv[])
{
UDF_INIT uinit;
UDF_ARGS uargs;
char message[MYSQL_ERRMSG_SIZE], result[1024];
unsigned long length;
char is_null, error;
enum Item_result arg_types[MAX_ARGS];
long long long_args[MAX_ARGS];
unsigned long lengths[MAX_ARGS], attribute_lengths[MAX_ARGS];
char maybe_null[MAX_ARGS];
char attributes[MAX_ARGS][8];
char *attr_ptrs[MAX_ARGS];
const char *ret_res;
char *long_end;
int arg;

uinit.maybe_null = true;
uinit.decimals = 31;
uinit.max_length = 1024;
uinit.ptr = NULL;
uinit.const_item = false;
uinit.extension = NULL;

uargs.arg_count = argc - 1;
uargs.arg_type = arg_types;
uargs.args = argv + 1;
uargs.lengths = lengths;
uargs.maybe_null = maybe_null;
uargs.attributes = attr_ptrs;
uargs.attribute_lengths = attribute_lengths;
uargs.extension = NULL;

for (arg = 0; arg < argc - 1; arg++) {
errno = 0;
if ((isdigit(*argv[arg + 1]) || *argv[arg + 1] == '-' || *argv[arg + 1] == '+') &&
!(long_args[arg] = strtoll(argv[arg + 1], &long_end, 10), errno) && !*long_end) {
arg_types[arg] = INT_RESULT;
lengths[arg] = sizeof(long_args[arg]);
argv[arg + 1] = (char *)&long_args[arg];
} else {
arg_types[arg] = STRING_RESULT;
lengths[arg] = strlen(argv[arg + 1]);
if (!strcmp(argv[arg + 1], "NULL")) {
argv[arg + 1] = NULL;
}
}
maybe_null[arg] = false;
sprintf(attributes[arg], "arg%04d", arg);
attribute_lengths[arg] = strlen(attributes[arg]);
attr_ptrs[arg] = attributes[arg];
}
if (FUNC_NAME_SUF(_init)(&uinit, &uargs, message)) {
fprintf(stderr, STR(FUNC_NAME) " init error: %s\n", message);
return 1;
}

is_null = error = 0;
ret_res = FUNC_NAME(&uinit, &uargs, result, &length, &is_null, &error);
if (error) {
fprintf(stderr, STR(FUNC_NAME) " returned error\n");
FUNC_NAME_SUF(_deinit)(&uinit);
return 2;
}
if (is_null) {
fprintf(stderr, STR(FUNC_NAME) " returned NULL\n");
} else {
fwrite(ret_res, 1, length, stdout);
putchar('\n');
}
FUNC_NAME_SUF(_deinit)(&uinit);
return 0;
}
9 changes: 9 additions & 0 deletions mysql_json.cc
Expand Up @@ -170,6 +170,12 @@ namespace {
return _parse(ctx, in);
}
}
bool parse_array_stop(size_t idx) {
if (wanted_array_index_ == (size_t)-1 && _is_preleaf()) {
_returning_buffered_str() = std::to_string(idx);
}
return true;
}
bool parse_object_start() {
if (_is_leaf()) {
_returning_buffered_str() = "object";
Expand Down Expand Up @@ -217,6 +223,9 @@ namespace {
bool _is_leaf() {
return arg_index_ == g_ctx_->args->arg_count;
}
bool _is_preleaf() {
return arg_index_ == g_ctx_->args->arg_count - 1;
}
void _set_if_leaf(const picojson::value& v) {
if (_is_leaf())
_returning_buffered_str() = v.to_str();
Expand Down
2 changes: 1 addition & 1 deletion picojson
Submodule picojson updated 2 files
+165 −0 README.mkdn
+161 −53 picojson.h

0 comments on commit c2ff21a

Please sign in to comment.