├── LICENSE.md ├── Makefile ├── README.md ├── atom-template.xml ├── bind.c ├── compats.c ├── configure ├── extern.h ├── index.css ├── index.xml ├── ksql.3 ├── ksql.c ├── ksql.dot ├── ksql.h ├── ksql_alloc.3 ├── ksql_alloc_child.3 ├── ksql_bind_double.3 ├── ksql_cfg_defaults.3 ├── ksql_close.3 ├── ksql_exec.3 ├── ksql_free.3 ├── ksql_lastid.3 ├── ksql_open.3 ├── ksql_result_double.3 ├── ksql_role.3 ├── ksql_stmt_alloc.3 ├── ksql_stmt_double.3 ├── ksql_stmt_free.3 ├── ksql_stmt_reset.3 ├── ksql_stmt_step.3 ├── ksql_trace.3 ├── ksql_trans_commit.3 ├── ksql_trans_open.3 ├── ksql_untrace.3 ├── log.c ├── mandoc.css ├── manpage.xml ├── notugly.xsl ├── result.c ├── stmt.c ├── test.c ├── test.sql ├── tests.c ├── trace.c ├── trans.c └── versions.xml /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Kristaps Dzonsons 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose 4 | with or without fee is hereby granted, provided that the above copyright notice 5 | and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 8 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 9 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 10 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 11 | OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 12 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 13 | THIS SOFTWARE. 14 | 15 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .3 .3.html .html .dot .svg .3.xml 2 | 3 | include Makefile.configure 4 | 5 | WWWDIR = /var/www/vhosts/kristaps.bsd.lv/htdocs/ksql 6 | VMAJOR != grep 'define KSQL_VMAJOR' ksql.h | cut -f3 7 | VMINOR != grep 'define KSQL_VMINOR' ksql.h | cut -f3 8 | VBUILD != grep 'define KSQL_VBUILD' ksql.h | cut -f3 9 | VERSION := $(VMAJOR).$(VMINOR).$(VBUILD) 10 | BUILT = index.css \ 11 | mandoc.css 12 | HTMLS = ksql.3.html \ 13 | ksql_alloc.3.html \ 14 | ksql_alloc_child.3.html \ 15 | ksql_bind_double.3.html \ 16 | ksql_cfg_defaults.3.html \ 17 | ksql_close.3.html \ 18 | ksql_exec.3.html \ 19 | ksql_free.3.html \ 20 | ksql_lastid.3.html \ 21 | ksql_open.3.html \ 22 | ksql_result_double.3.html \ 23 | ksql_role.3.html \ 24 | ksql_stmt_alloc.3.html \ 25 | ksql_stmt_double.3.html \ 26 | ksql_stmt_free.3.html \ 27 | ksql_stmt_reset.3.html \ 28 | ksql_stmt_step.3.html \ 29 | ksql_trace.3.html \ 30 | ksql_trans_commit.3.html \ 31 | ksql_trans_open.3.html \ 32 | ksql_untrace.3.html 33 | XMLS = ksql.3.xml \ 34 | ksql_alloc.3.xml \ 35 | ksql_alloc_child.3.xml \ 36 | ksql_bind_double.3.xml \ 37 | ksql_cfg_defaults.3.xml \ 38 | ksql_close.3.xml \ 39 | ksql_exec.3.xml \ 40 | ksql_free.3.xml \ 41 | ksql_lastid.3.xml \ 42 | ksql_open.3.xml \ 43 | ksql_result_double.3.xml \ 44 | ksql_role.3.xml \ 45 | ksql_stmt_alloc.3.xml \ 46 | ksql_stmt_double.3.xml \ 47 | ksql_stmt_free.3.xml \ 48 | ksql_stmt_reset.3.xml \ 49 | ksql_stmt_step.3.xml \ 50 | ksql_trace.3.xml \ 51 | ksql_trans_commit.3.xml \ 52 | ksql_trans_open.3.xml \ 53 | ksql_untrace.3.xml 54 | MANS = ksql.3 \ 55 | ksql_alloc.3 \ 56 | ksql_alloc_child.3 \ 57 | ksql_bind_double.3 \ 58 | ksql_cfg_defaults.3 \ 59 | ksql_close.3 \ 60 | ksql_exec.3 \ 61 | ksql_free.3 \ 62 | ksql_lastid.3 \ 63 | ksql_open.3 \ 64 | ksql_result_double.3 \ 65 | ksql_role.3 \ 66 | ksql_stmt_alloc.3 \ 67 | ksql_stmt_double.3 \ 68 | ksql_stmt_free.3 \ 69 | ksql_stmt_reset.3 \ 70 | ksql_stmt_step.3 \ 71 | ksql_trace.3 \ 72 | ksql_trans_commit.3 \ 73 | ksql_trans_open.3 \ 74 | ksql_untrace.3 75 | SRCS = $(MANS) \ 76 | bind.c \ 77 | compats.c \ 78 | extern.h \ 79 | log.c \ 80 | ksql.c \ 81 | ksql.h \ 82 | result.c \ 83 | stmt.c \ 84 | tests.c \ 85 | test.c \ 86 | test.sql \ 87 | trace.c \ 88 | trans.c \ 89 | Makefile 90 | OBJS = bind.o \ 91 | ksql.o \ 92 | log.o \ 93 | result.o \ 94 | stmt.o \ 95 | trace.o \ 96 | trans.o 97 | 98 | # FreeBSD's default .c.o doesn't recognise CPPFLAGS. 99 | # CFLAGS += $(CPPFLAGS) 100 | 101 | # Use this, for now, because we've marked functions as being deprecated 102 | # but still use them internally. 103 | CFLAGS += -Wno-deprecated-declarations 104 | 105 | all: test libksql.a test.db 106 | 107 | test: test.c compats.o libksql.a 108 | $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ test.c compats.o libksql.a -lsqlite3 $(LDFLAGS) 109 | 110 | test.db: test.sql 111 | rm -f $@ 112 | sqlite3 $@ < test.sql 113 | 114 | libksql.a: $(OBJS) compats.o 115 | $(AR) rs $@ $(OBJS) compats.o 116 | 117 | compats.o $(OBJS) test: config.h 118 | 119 | $(OBJS): extern.h ksql.h 120 | 121 | www: $(HTMLS) index.html ksql.svg ksql.tar.gz atom.xml 122 | 123 | installwww: www 124 | mkdir -p $(WWWDIR)/snapshots 125 | $(INSTALL_DATA) atom.xml ksql.svg $(HTMLS) index.html $(BUILT) $(WWWDIR) 126 | $(INSTALL_DATA) ksql.tar.gz $(WWWDIR)/snapshots 127 | $(INSTALL_DATA) ksql.tar.gz $(WWWDIR)/snapshots/ksql-$(VERSION).tar.gz 128 | 129 | ksql.tar.gz: 130 | mkdir -p .dist/ksql-$(VERSION) 131 | install -m 0644 $(SRCS) .dist/ksql-$(VERSION) 132 | install -m 0755 configure .dist/ksql-$(VERSION) 133 | ( cd .dist && tar zvcf ../$@ . ) 134 | rm -rf .dist 135 | 136 | install: libksql.a 137 | mkdir -p $(DESTDIR)$(LIBDIR) 138 | mkdir -p $(DESTDIR)$(INCLUDEDIR) 139 | mkdir -p $(DESTDIR)$(MANDIR)/man3 140 | $(INSTALL_LIB) libksql.a $(DESTDIR)$(LIBDIR) 141 | $(INSTALL_DATA) ksql.h $(DESTDIR)$(INCLUDEDIR) 142 | $(INSTALL_DATA) $(MANS) $(DESTDIR)$(MANDIR)/man3 143 | 144 | clean: 145 | rm -f libksql.a compats.o $(OBJS) test test.db 146 | rm -f $(HTMLS) $(XMLS) index.html atom.xml ksql.tar.gz ksql.svg 147 | 148 | distclean: clean 149 | rm -f Makefile.configure config.h config.log 150 | 151 | .3.3.xml: 152 | ( echo "
" ; \ 153 | mandoc -Ofragment -Thtml $< ; \ 154 | echo "
"; ) >$@ 155 | 156 | $(HTMLS): $(XMLS) 157 | sblg -t manpage.xml -L $(XMLS) 158 | 159 | index.html: $(XMLS) index.xml versions.xml 160 | sblg -s date -t index.xml -o $@ versions.xml $(XMLS) 161 | 162 | atom.xml: versions.xml 163 | sblg -s date -a versions.xml >$@ 164 | 165 | .dot.svg: 166 | dot -Tsvg $< | xsltproc --novalid notugly.xsl - >$@ 167 | 168 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Synopsis 2 | 3 | ksql is a simple wrapper around the SQLite 4 | [C-language interface](https://www.sqlite.org/c3ref/intro.html). 5 | It makes sure your database cleans up properly in the case of 6 | application failure by using the 7 | [atexit(3)](https://man.openbsd.org/atexit) facility. 8 | 9 | It also has the facility to open the database in a protected child, 10 | communicating with the caller via pipes. 11 | This allows for stronger sandboxing of the calling process. 12 | 13 | ## Installation 14 | 15 | Download the latest version's 16 | [source archive](https://kristaps.bsd.lv/ksql/snapshots/ksql.tar.gz) 17 | or download the project from GitHub. 18 | Then run the configuration script with `./configure`. (See the 19 | [configure](https://github.com/kristapsdz/ksql/blob/master/configure) 20 | script for details.) 21 | Finally, compile with `make`, then `sudo make install` (or `doas make 22 | install`, whatever the case may be). 23 | 24 | ## API Reference 25 | 26 | See the [ksql(3) manpage](https://kristaps.bsd.lv/ksql/ksql.3.html) for 27 | complete library documentation. 28 | 29 | ## License 30 | 31 | All sources use the ISC (like OpenBSD) license. 32 | See the [LICENSE.md](LICENSE.md) file for details. 33 | -------------------------------------------------------------------------------- /atom-template.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | ksql version feed 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /bind.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #if HAVE_SYS_QUEUE 20 | # include 21 | #endif 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include "ksql.h" 29 | #include "extern.h" 30 | 31 | enum ksqlc 32 | ksqlsrv_bind(struct ksql *p, enum ksqlop op) 33 | { 34 | enum ksqlc c; 35 | size_t pos; 36 | char *buf = NULL; 37 | struct ksqlstmt *ss; 38 | size_t bufsz; 39 | 40 | if (KSQL_OK != (c = ksql_readptr(p, &ss))) 41 | return(c); 42 | if (KSQL_OK != (c = ksql_readsz(p, &pos))) 43 | return(c); 44 | 45 | if (KSQLOP_BIND_TEXT == op) { 46 | if (KSQL_OK != (c = ksql_readstr(p, &buf))) 47 | return(c); 48 | c = ksql_bind_str(ss, pos, buf); 49 | } else if (KSQLOP_BIND_ZBLOB == op) { 50 | if (KSQL_OK != (c = ksql_readsz(p, &bufsz))) 51 | return(c); 52 | c = ksql_bind_zblob(ss, pos, bufsz); 53 | } else if (KSQLOP_BIND_NULL != op) { 54 | if (KSQL_OK != (c = ksql_readsz(p, &bufsz))) 55 | return(c); 56 | /* FIXME: handle zero-length. */ 57 | if (NULL == (buf = malloc(bufsz))) 58 | return(ksql_err(p, KSQL_MEM, NULL)); 59 | c = ksql_readbuf(p, buf, bufsz, 0); 60 | if (KSQL_OK != c) { 61 | free(buf); 62 | return(c); 63 | } 64 | switch (op) { 65 | case (KSQLOP_BIND_BLOB): 66 | c = ksql_bind_blob(ss, pos, buf, bufsz); 67 | break; 68 | case (KSQLOP_BIND_DOUBLE): 69 | assert(bufsz == sizeof(double)); 70 | c = ksql_bind_double(ss, pos, *(double *)buf); 71 | break; 72 | case (KSQLOP_BIND_INT): 73 | assert(bufsz == sizeof(int64_t)); 74 | c = ksql_bind_int(ss, pos, *(int64_t *)buf); 75 | break; 76 | default: 77 | abort(); 78 | } 79 | } else 80 | c = ksql_bind_null(ss, pos); 81 | 82 | free(buf); 83 | return(ksql_writecode(p, c)); 84 | } 85 | 86 | enum ksqlc 87 | ksql_bind_zblob(struct ksqlstmt *stmt, size_t pos, size_t valsz) 88 | { 89 | int rc; 90 | 91 | if (KSQLSRV_ISPARENT(stmt->sql)) 92 | return(ksql_writebound(stmt, 93 | KSQLOP_BIND_ZBLOB, pos, NULL, valsz)); 94 | if (pos >= stmt->bcols) 95 | return(ksql_verr(stmt->sql, KSQL_BINDCOL, 96 | "parameter index %zu exceeds maximum " 97 | "index %zu", pos, stmt->bcols - 1)); 98 | rc = sqlite3_bind_zeroblob(stmt->stmt, pos + 1, valsz); 99 | if (SQLITE_OK == rc) 100 | return(KSQL_OK); 101 | return(ksql_dberr(stmt->sql)); 102 | } 103 | 104 | enum ksqlc 105 | ksql_bind_blob(struct ksqlstmt *stmt, 106 | size_t pos, const void *val, size_t valsz) 107 | { 108 | int rc; 109 | 110 | if (KSQLSRV_ISPARENT(stmt->sql)) 111 | return(ksql_writebound(stmt, 112 | KSQLOP_BIND_BLOB, pos, val, valsz)); 113 | 114 | if (pos >= stmt->bcols) 115 | return(ksql_verr(stmt->sql, KSQL_BINDCOL, 116 | "parameter index %zu exceeds maximum " 117 | "index %zu", pos, stmt->bcols - 1)); 118 | rc = sqlite3_bind_blob 119 | (stmt->stmt, pos + 1, val, valsz, SQLITE_TRANSIENT); 120 | if (SQLITE_OK == rc) 121 | return(KSQL_OK); 122 | return(ksql_dberr(stmt->sql)); 123 | } 124 | 125 | enum ksqlc 126 | ksql_bind_str(struct ksqlstmt *stmt, size_t pos, const char *val) 127 | { 128 | int rc; 129 | 130 | if (KSQLSRV_ISPARENT(stmt->sql)) 131 | return(ksql_writebound(stmt, 132 | KSQLOP_BIND_TEXT, pos, val, 0)); 133 | 134 | if (pos >= stmt->bcols) 135 | return(ksql_verr(stmt->sql, KSQL_BINDCOL, 136 | "parameter index %zu exceeds maximum " 137 | "index %zu", pos, stmt->bcols - 1)); 138 | rc = sqlite3_bind_text 139 | (stmt->stmt, pos + 1, val, -1, SQLITE_TRANSIENT); 140 | if (SQLITE_OK == rc) 141 | return(KSQL_OK); 142 | return(ksql_dberr(stmt->sql)); 143 | } 144 | 145 | enum ksqlc 146 | ksql_bind_double(struct ksqlstmt *stmt, size_t pos, double val) 147 | { 148 | int rc; 149 | 150 | if (KSQLSRV_ISPARENT(stmt->sql)) 151 | return(ksql_writebound(stmt, 152 | KSQLOP_BIND_DOUBLE, pos, 153 | &val, sizeof(double))); 154 | if (pos >= stmt->bcols) 155 | return(ksql_verr(stmt->sql, KSQL_BINDCOL, 156 | "parameter index %zu exceeds maximum " 157 | "index %zu", pos, stmt->bcols - 1)); 158 | rc = sqlite3_bind_double(stmt->stmt, pos + 1, val); 159 | if (SQLITE_OK == rc) 160 | return(KSQL_OK); 161 | return(ksql_dberr(stmt->sql)); 162 | } 163 | 164 | enum ksqlc 165 | ksql_bind_null(struct ksqlstmt *stmt, size_t pos) 166 | { 167 | 168 | if (KSQLSRV_ISPARENT(stmt->sql)) 169 | return(ksql_writebound(stmt, 170 | KSQLOP_BIND_NULL, pos, NULL, 0)); 171 | if (pos >= stmt->bcols) 172 | return(ksql_verr(stmt->sql, KSQL_BINDCOL, 173 | "parameter index %zu exceeds maximum " 174 | "index %zu", pos, stmt->bcols - 1)); 175 | if (SQLITE_OK == sqlite3_bind_null(stmt->stmt, pos + 1)) 176 | return(KSQL_OK); 177 | return(ksql_dberr(stmt->sql)); 178 | } 179 | 180 | enum ksqlc 181 | ksql_bind_int(struct ksqlstmt *stmt, size_t pos, int64_t val) 182 | { 183 | 184 | if (KSQLSRV_ISPARENT(stmt->sql)) 185 | return(ksql_writebound(stmt, 186 | KSQLOP_BIND_INT, pos, 187 | &val, sizeof(int64_t))); 188 | if (pos >= stmt->bcols) 189 | return(ksql_verr(stmt->sql, KSQL_BINDCOL, 190 | "parameter index %zu exceeds maximum " 191 | "index %zu", pos, stmt->bcols - 1)); 192 | if (SQLITE_OK == sqlite3_bind_int64(stmt->stmt, pos + 1, val)) 193 | return(KSQL_OK); 194 | return(ksql_dberr(stmt->sql)); 195 | } 196 | 197 | -------------------------------------------------------------------------------- /extern.h: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #ifndef EXTERN_H 18 | #define EXTERN_H 19 | 20 | /* 21 | * Operation code used to communicate between client-server. 22 | */ 23 | enum ksqlop { 24 | KSQLOP_BIND_BLOB, /* ksql_bind_blob */ 25 | KSQLOP_BIND_DOUBLE, /* ksql_bind_double */ 26 | KSQLOP_BIND_INT, /* ksql_bind_int */ 27 | KSQLOP_BIND_NULL, /* ksql_bind_null */ 28 | KSQLOP_BIND_TEXT, /* ksql_bind_text */ 29 | KSQLOP_BIND_ZBLOB, /* ksql_bind_zblob */ 30 | KSQLOP_CLOSE, /* ksql_close */ 31 | KSQLOP_COL_BLOB, /* ksql_stmt_blob */ 32 | KSQLOP_COL_BYTES, /* ksql_stmt_bytes */ 33 | KSQLOP_COL_DOUBLE, /* ksql_stmt_double */ 34 | KSQLOP_COL_INT, /* ksql_stmt_int */ 35 | KSQLOP_COL_ISNULL, /* ksql_stmt_isnull */ 36 | KSQLOP_COL_STR, /* ksql_stmt_str */ 37 | KSQLOP_EXEC, /* ksql_exec */ 38 | KSQLOP_LASTID, /* ksql_lastid */ 39 | KSQLOP_OPEN, /* ksql_open */ 40 | KSQLOP_RESULT_BLOB, /* ksql_result_blob */ 41 | KSQLOP_RESULT_BYTES, /* ksql_result_bytes */ 42 | KSQLOP_RESULT_DOUBLE, /* ksql_result_double */ 43 | KSQLOP_RESULT_INT, /* ksql_result_int */ 44 | KSQLOP_RESULT_ISNULL, /* ksql_result_isnull */ 45 | KSQLOP_RESULT_STR, /* ksql_result_str */ 46 | KSQLOP_ROLE, /* ksql_role */ 47 | KSQLOP_STMT_ALLOC, /* ksql_stmt_alloc */ 48 | KSQLOP_STMT_FREE, /* ksql_stmt_free */ 49 | KSQLOP_STMT_RESET, /* ksql_stmt_reset */ 50 | KSQLOP_STMT_STEP, /* ksql_stmt_step */ 51 | KSQLOP_TRACE, /* ksql_trace */ 52 | KSQLOP_TRANS_CLOSE, /* ksql_trans_xxxx */ 53 | KSQLOP_TRANS_OPEN, /* ksql_trans_xxxx */ 54 | KSQLOP_UNTRACE, /* ksql_untrace */ 55 | }; 56 | 57 | TAILQ_HEAD(kcacheq, kcache); 58 | TAILQ_HEAD(ksqlstmtq, ksqlstmt); 59 | 60 | /* 61 | * When running in client-server mode, this holds information about the 62 | * process on the other end of our socket. 63 | * If the "pid" is 0, then we're connected to the parent (i.e., we're 64 | * the child); if it's non-zero, we're the parent. 65 | */ 66 | struct ksqld { 67 | pid_t pid; /* other process of socket */ 68 | int fd; /* -1 on init */ 69 | }; 70 | 71 | /* 72 | * Holds all information about open connections. 73 | * In client-server mode, this holds a "daemon" field used to 74 | * communicate with the other end of the connection. 75 | */ 76 | struct ksql { 77 | struct ksqlcfg cfg; 78 | size_t role; /* current role */ 79 | sqlite3 *db; 80 | char *dbfile; /* fname of db */ 81 | struct ksqlstmtq stmt_used; /* used list */ 82 | struct ksqlstmtq stmt_free; /* free list */ 83 | size_t trans; /* current transactions */ 84 | struct ksqld *daemon; /* if applicable */ 85 | unsigned int flags; 86 | #define KSQLFL_TRANS 0x01 /* trans is open */ 87 | TAILQ_ENTRY(ksql) entries; 88 | }; 89 | 90 | /* 91 | * When obtaining results from the parent-child model, we need to keep 92 | * track of the pointers in blob and text results to maintain SQLite's 93 | * invariant that a pointer will be available til the next type 94 | * conversion, step, reset, or free. 95 | * This is only applicable for the parent. 96 | */ 97 | struct kcache { 98 | void *s; /* pointer to results */ 99 | TAILQ_ENTRY(kcache) entries; 100 | }; 101 | 102 | /* 103 | * Holder for pending SQLite statements. 104 | * If we exit out of state, we'll finalise these statements. 105 | */ 106 | struct ksqlstmt { 107 | sqlite3_stmt *stmt; /* statement */ 108 | size_t id; /* its ID (init'd as SIZE_MAX) */ 109 | size_t bcols; /* valid bind columns */ 110 | size_t rcols; /* valid result set columns */ 111 | struct kcacheq cache; /* pointer cache */ 112 | struct ksql *sql; /* corresponding db */ 113 | void *ptr; /* daemon mode pointer */ 114 | int hasrow; /* has a result row */ 115 | TAILQ_ENTRY(ksqlstmt) entries; 116 | }; 117 | 118 | #define KSQLSRV_ISPARENT(_p) \ 119 | (NULL != (_p)->daemon && (_p)->daemon->pid) 120 | #define KSQLSRV_ISCHILD(_p) \ 121 | (NULL != (_p)->daemon && 0 == (_p)->daemon->pid) 122 | 123 | __BEGIN_DECLS 124 | 125 | enum ksqlc ksql_err(struct ksql *, enum ksqlc, const char *); 126 | void ksql_err_noexit(struct ksql *, enum ksqlc, const char *); 127 | enum ksqlc ksql_dberr(struct ksql *); 128 | void ksql_dberr_noexit(struct ksql *); 129 | enum ksqlc ksql_verr(struct ksql *, enum ksqlc, const char *, ...); 130 | 131 | void ksqlitevmsg(const struct ksql *, enum ksqlc, const char *, ...); 132 | 133 | enum ksqlc ksql_readbuf(struct ksql *, void *, size_t, int); 134 | enum ksqlc ksql_readcode(struct ksql *, enum ksqlc *); 135 | enum ksqlc ksql_readptr(struct ksql *, struct ksqlstmt **); 136 | enum ksqlc ksql_readstr(struct ksql *, char **); 137 | enum ksqlc ksql_readsz(struct ksql *, size_t *); 138 | 139 | enum ksqlc ksql_writebound(struct ksqlstmt *, enum ksqlop, size_t, const void *, size_t); 140 | enum ksqlc ksql_writebuf(struct ksql *, const void *, size_t); 141 | enum ksqlc ksql_writecode(struct ksql *, enum ksqlc); 142 | enum ksqlc ksql_writeop(struct ksql *, enum ksqlop); 143 | enum ksqlc ksql_writeptr(struct ksql *, const struct ksqlstmt *); 144 | enum ksqlc ksql_writestr(struct ksql *, const char *); 145 | enum ksqlc ksql_writesz(struct ksql *, size_t); 146 | 147 | enum ksqlc ksqlsrv_bind(struct ksql *, enum ksqlop); 148 | enum ksqlc ksqlsrv_result_blob(struct ksql *); 149 | enum ksqlc ksqlsrv_result_bytes(struct ksql *); 150 | enum ksqlc ksqlsrv_result_double(struct ksql *); 151 | enum ksqlc ksqlsrv_result_int(struct ksql *); 152 | enum ksqlc ksqlsrv_result_isnull(struct ksql *); 153 | enum ksqlc ksqlsrv_result_str(struct ksql *); 154 | enum ksqlc ksqlsrv_stmt_alloc(struct ksql *); 155 | enum ksqlc ksqlsrv_stmt_bytes(struct ksql *); 156 | enum ksqlc ksqlsrv_stmt_double(struct ksql *); 157 | enum ksqlc ksqlsrv_stmt_int(struct ksql *); 158 | enum ksqlc ksqlsrv_stmt_isnull(struct ksql *); 159 | enum ksqlc ksqlsrv_stmt_blob(struct ksql *); 160 | enum ksqlc ksqlsrv_stmt_str(struct ksql *); 161 | enum ksqlc ksqlsrv_trans_close(struct ksql *); 162 | enum ksqlc ksqlsrv_trans_open(struct ksql *); 163 | 164 | enum ksqlc ksql_exec_private(struct ksql *, const char *); 165 | 166 | __END_DECLS 167 | 168 | #endif 169 | -------------------------------------------------------------------------------- /index.css: -------------------------------------------------------------------------------- 1 | html, body { height: 100%; 2 | margin: 0; } 3 | body { font-family: 'Alegreya sans', sans-serif; 4 | font-weight: 400; 5 | font-size: 14pt; 6 | color: #222; 7 | background-color: #fafafa; } 8 | code, pre { font-size: smaller; } 9 | section { font-size: 16pt; 10 | line-height: 130%; } 11 | #nav { background-color: #1A1A1A; 12 | padding: 1em 25% 0.25em 25%; } 13 | #breadcrumbs { margin: 2em 0; 14 | color: rgba(255, 255, 255, 0.6); } 15 | a { text-decoration: none; 16 | color: blue; } 17 | #nav label { cursor: pointer; } 18 | #nav label:hover, 19 | #nav label, 20 | #nav a { color: #bb6; 21 | text-shadow: thin solid white; } 22 | #nav label:hover, 23 | #nav a:hover { color: #ff2; } 24 | #nav i { font-size: smaller; } 25 | #breadcrumbs a { opacity: 0.8; } 26 | #breadcrumbs a:hover { opacity: 1.0; } 27 | #nav input { display: none; } 28 | #nav ul { list-style-type: none; } 29 | #nav .menu nav > ul { max-height: 0; 30 | overflow: hidden; 31 | transition: max-height 0.5s ease-in-out; } 32 | #nav .menu input:checked + nav > ul { max-height: 100em; } 33 | #nav .menu li:hover > nav > ul { max-height: 100em; } 34 | #nav .menu { display: table; 35 | margin: 0; 36 | padding: 0; } 37 | #nav .menu > li { display: table-cell; 38 | vertical-align: middle; } 39 | #nav .menu > li + li { padding-left: 1em; } 40 | #nav .menu li { position: relative; } 41 | #nav .menu li nav > ul { position: absolute; 42 | left: 0; 43 | padding: 0; 44 | background-color: #1A1A1A; 45 | margin: 0; 46 | top: 100%; } 47 | #nav .menu li nav > ul li:hover { background-color: #4a4a4a; } 48 | #nav .menu li nav > ul li { margin: 0; 49 | padding: 2pt 1.5em; } 50 | #nav .menu li nav > ul li:first-child { margin-top: 8pt; } 51 | #nav .menu li nav > ul li:last-child { margin-bottom: 8pt; } 52 | #nav .menu > li.version { color: #ddd; 53 | padding-right: 0.5em; 54 | border-right: thin solid #aaa; 55 | font-weight: 500; } 56 | #nav .menu > li.version + li { padding-left: 0.75em; } 57 | strong { font-weight: 500; } 58 | article nav[data-sblg-nav] ul { list-style-type: none; 59 | border-top: thin solid rgba(0, 0, 255, 0.2); 60 | color: #555; 61 | padding: 0; 62 | margin-top: 2em; 63 | padding-top: 2em; } 64 | article nav[data-sblg-nav] ul > li { opacity: 0.5; } 65 | article nav[data-sblg-nav] ul > li:first-child 66 | { opacity: 1.0; } 67 | article nav[data-sblg-nav] li + li { margin-top: 1.5em; } 68 | article nav[data-sblg-nav] li:first-child + li 69 | { margin-top: 2.5em; } 70 | article nav[data-sblg-nav] li > div:first-child 71 | { color: #000; } 72 | article nav[data-sblg-nav] div > p:first-child 73 | { margin-top: 0.5em; } 74 | h1 { margin: 0; 75 | font-weight: normal; 76 | color: #000; 77 | font-size: 60pt; } 78 | header { margin: 4rem 0; 79 | font-size: x-large; } 80 | header div { opacity: 0.6; } 81 | article { padding: 0 25%; } 82 | figure { margin: 0; 83 | text-align: center; } 84 | figure img { max-width: 100%; } 85 | a { color: blue; } 86 | footer { padding: 2em; 87 | font-size: 12pt; 88 | opacity: 0.6; 89 | text-align: center; } 90 | 91 | @media only screen and (max-width: 760px), only screen and (max-device-width: 760px) { 92 | h1 { font-size: 32pt; 93 | display: block; 94 | margin: 0.2em auto; } 95 | header { margin-bottom: 1.5em; 96 | margin-top: 1.5em; } 97 | footer { padding: 1em; } 98 | #nav { padding: 1ex 2%; } 99 | article { padding: 0 2%; } 100 | } 101 | -------------------------------------------------------------------------------- /index.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ksql: yet another SQLite wrapper 7 | 8 | 9 | 10 | 11 | 12 | 13 | 42 |
43 |
44 |

ksql

45 |
yet another SQLite wrapper
46 |
47 |
48 |

49 | ksql is a lazy man's C/C++ wrapper for the SQLite API interface. 51 | It is written to make sure sloppy programming won't cause your SQLite database to be inconsistent (transactions and 52 | statements open on exit and certain signals). 53 | Specifically, it exits on any database errors; and upon exit (or signal), cleans up open statements, transactions, and databases. 54 | See ksql(3) and friends for details. 55 |

56 |

57 | It also has some handy security features: a split-process model protecting the database 59 | from your application, full RBAC capabilities on the statement level, and immutable stored 60 | statements to prevent the application from crafting its own SQL. 61 |

62 |
63 | ksql(3) operation 64 |
65 |

66 | The library runs on UNIX systems: OpenBSD, FreeBSD, Linux (musl and 70 | glibc), and Mac OS 71 | X. 72 | It benefits the most from OpenBSD systems using the pledge(2) security mechanism. 74 | It is a BSD.lv project. 75 |

76 |
77 | 86 |
87 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /ksql.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016--2017 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql 22 | .Nd yet another SQLite wrapper 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Fd #define KSQL_VMAJOR x 30 | .Fd #define KSQL_VMINOR y 31 | .Fd #define KSQL_VBUILD z 32 | .Fd #define KSQL_VERSION "x.y.z" 33 | .Fd #define KSQL_VSTAMP xxxyyyzzz 34 | .Sh DESCRIPTION 35 | The 36 | .Nm ksql 37 | library is a 38 | .Dq lazy man's 39 | wrapper of a minimal subset of the SQLite C API. 40 | It makes interfacing with SQLite easy and fast for you, and safe for 41 | your database. 42 | .Pp 43 | The typical usage scenario is as follows: 44 | .Bl -enum 45 | .It 46 | assign configuration defaults with 47 | .Xr ksql_cfg_defaults 3 48 | and pre-set SQL statements in the 49 | .Fa stmts 50 | array; 51 | .It 52 | allocate a handle with 53 | .Xr ksql_alloc_child 3 54 | or, for reduced security, 55 | .Xr ksql_alloc 3 ; 56 | .It 57 | open a database connection on that handle with 58 | .Xr ksql_open 3 ; 59 | .It 60 | create statements with 61 | .Xr ksql_stmt_alloc 3 62 | and use them with 63 | .Xr ksql_bind_double 3 , 64 | .Xr ksql_stmt_double 3 , 65 | and 66 | .Xr ksql_stmt_free 3 ; 67 | or 68 | .It 69 | execute statements with 70 | .Xr ksql_exec 3 ; 71 | .It 72 | close and free with 73 | .Xr ksql_free 3 . 74 | .El 75 | .Pp 76 | There's also support for maintaining transactional consistency with 77 | .Xr ksql_trans_open 3 78 | and 79 | .Xr ksql_trans_commit 3 . 80 | Invoke 81 | .Dq apropos ksql 82 | to see all functions. 83 | .Pp 84 | The current version of the database is defined in 85 | .Dv KSQL_VERSION 86 | as a string of major number, minor number, and build. 87 | These values are available seperately as 88 | .Dv KSQL_VMAJOR , 89 | .Dv KSQL_VMINOR , 90 | and 91 | .Dv KSQL_VBUILD , 92 | respectively. 93 | A numeric concatenation of the version is defined as 94 | .Dv KSQL_VSTAMP , 95 | which may be used to test for version number applicability. 96 | .Ss Database safety 97 | By default (see 98 | .Xr ksql_alloc 3 99 | and 100 | .Xr ksql_alloc_child 3 ) , 101 | the library will invoke 102 | .Xr exit 3 103 | if any of the database functions fail. 104 | .Dq Failure 105 | means that any SQLite function (for example, stepping for result rows or 106 | binding parameters) that fails will trigger exit. 107 | .Pp 108 | It will also register an 109 | .Xr atexit 3 110 | hook that will clean up any open database connections, transactions, and 111 | open statements. 112 | .Pp 113 | Lastly, it will intercept the 114 | .Dv SIGABRT 115 | and 116 | .Dv SIGSEGV 117 | and trigger a controlled exit. 118 | Thus, if your program is signalled while performing (say) transactions, 119 | or any sort of open database, you won't corrupt the data. 120 | .Pp 121 | All of these safety measures can be disabled by providing the 122 | appropriate flags to 123 | .Xr ksql_alloc 3 124 | or 125 | .Xr ksql_alloc_child 3 . 126 | .Ss Compiling and linking 127 | To compile with 128 | .Nm ksql , 129 | use the header file 130 | .In ksql.h . 131 | For linking, use 132 | .Fl l Ns Ar ksql 133 | .Fl l Ns Ar sqlite3 . 134 | .Ss Access protocol 135 | Since SQLite uses a busy-wait approach, 136 | .Nm 137 | will sleep for random intervals between attempts returning 138 | .Dv SQLITE_BUSY 139 | or 140 | .Dv SQLITE_LOCKED . 141 | Within the first 10 attempts, the random interval is within 1/4 second. 142 | Within the 10\(en100 attempts, within 1/10 second. 143 | Greater than 100 attempts, 1/100 second. 144 | This scheme benefits longer waiters. 145 | .Pp 146 | Functions using this algorithm are 147 | .Xr ksql_exec 3 , 148 | .Xr ksql_stmt_step 3 149 | and 150 | .Xr ksql_stmt_cstep 3 , 151 | and 152 | .Xr ksql_stmt_alloc 3 . 153 | .Ss Pledge Promises 154 | The 155 | .Nm ksql 156 | library is built to operate in security-sensitive environments, including 157 | .Xr pledge 2 158 | on 159 | .Ox . 160 | .Pp 161 | If running in single-process mode (i.e., with 162 | .Xr ksql_alloc 3 ) , 163 | the 164 | .Va stdio rpath cpath wpath flock fattr 165 | pledges are required for all functions. 166 | .Pp 167 | If in split-process mode (with 168 | .Xr ksql_alloc_child 3 ) , 169 | then 170 | .Va stdio rpath cpath wpath flock proc fattr 171 | is required for 172 | .Xr ksql_alloc_child 3 , 173 | and only 174 | .Va stdio 175 | otherwise. 176 | .\" .Sh CONTEXT 177 | .\" For section 9 functions only. 178 | .\" .Sh IMPLEMENTATION NOTES 179 | .\" Not used in OpenBSD. 180 | .\" .Sh RETURN VALUES 181 | .\" For sections 2, 3, and 9 function return values only. 182 | .\" .Sh ENVIRONMENT 183 | .\" For sections 1, 6, 7, and 8 only. 184 | .\" .Sh FILES 185 | .\" .Sh EXIT STATUS 186 | .\" For sections 1, 6, and 8 only. 187 | .\" .Sh EXAMPLES 188 | .\" .Sh DIAGNOSTICS 189 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 190 | .\" .Sh ERRORS 191 | .\" For sections 2, 3, 4, and 9 errno settings only. 192 | .Sh SEE ALSO 193 | .Xr ksql_alloc 3 , 194 | .Xr ksql_alloc_child 3 , 195 | .Xr ksql_bind_blob 3 , 196 | .Xr ksql_bind_bytes 3 , 197 | .Xr ksql_bind_double 3 , 198 | .Xr ksql_bind_int 3 , 199 | .Xr ksql_bind_str 3 , 200 | .Xr ksql_bind_zblob 3 , 201 | .Xr ksql_cfg_defaults 3 , 202 | .Xr ksql_close 3 , 203 | .Xr ksql_exec 3 , 204 | .Xr ksql_free 3 , 205 | .Xr ksql_lastid 3 , 206 | .Xr ksql_open 3 , 207 | .Xr ksql_role 3 , 208 | .Xr ksql_stmt_alloc 3 , 209 | .Xr ksql_stmt_blob 3 , 210 | .Xr ksql_stmt_bytes 3 , 211 | .Xr ksql_stmt_cstep 3 , 212 | .Xr ksql_stmt_double 3 , 213 | .Xr ksql_stmt_free 3 , 214 | .Xr ksql_stmt_int 3 , 215 | .Xr ksql_stmt_isnull 3 , 216 | .Xr ksql_stmt_reset 3 , 217 | .Xr ksql_stmt_step 3 , 218 | .Xr ksql_stmt_str 3 , 219 | .Xr ksql_trans_commit 3 , 220 | .Xr ksql_trans_exclopen 3 , 221 | .Xr ksql_trans_functions 3 , 222 | .Xr ksql_trans_open 3 , 223 | .Xr ksql_trans_rollback 3 , 224 | .Xr sqlite3 3 225 | .\" .Xr foobar 1 226 | .\" .Sh STANDARDS 227 | .\" .Sh HISTORY 228 | .\" .Sh AUTHORS 229 | .Sh CAVEATS 230 | This library is not thread-safe and does not plan to be. 231 | .\" .Sh BUGS 232 | .\" .Sh SECURITY CONSIDERATIONS 233 | .\" Not used in OpenBSD. 234 | -------------------------------------------------------------------------------- /ksql.dot: -------------------------------------------------------------------------------- 1 | digraph { 2 | rankdir="TB"; 3 | bgcolor="transparent"; 4 | 5 | subgraph cluster_0 { 6 | stepa[label="ksql_alloc_child"]; 7 | step1a[label="ksql_open"]; 8 | step2a[label="ksql_stmt_alloc"]; 9 | step4a[label="ksql_stmt_step"]; 10 | stepa -> step1a -> step2a -> step4a; 11 | } 12 | 13 | subgraph cluster_1 { 14 | bgcolor="gold"; 15 | stepb[label="child process",shape="plaintext"]; 16 | step1b[label="sqlite3_open"]; 17 | step2b[label="sqlite3_prepare_v2"]; 18 | step4b[label="sqlite3_step"]; 19 | stepb -> step1b[style="invis"]; 20 | step1b -> step2b -> step4b[style="invis"]; 21 | } 22 | 23 | db[label="database", style="filled", fillcolor="gold"]; 24 | 25 | step1b -> db[dir="both"]; 26 | step2b -> db[dir="both"]; 27 | step4b -> db[dir="both"]; 28 | 29 | stepa -> stepb[label="fork(2)"]; 30 | 31 | step1a -> step1b[style="dotted", dir="both"]; 32 | step2a -> step2b[style="dotted", dir="both"]; 33 | step4a -> step4b[style="dotted", dir="both"]; 34 | } 35 | -------------------------------------------------------------------------------- /ksql.h: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #ifndef KSQL_H 18 | #define KSQL_H 19 | 20 | #if !defined(__BEGIN_DECLS) 21 | # ifdef __cplusplus 22 | # define __BEGIN_DECLS extern "C" { 23 | # else 24 | # define __BEGIN_DECLS 25 | # endif 26 | #endif 27 | #if !defined(__END_DECLS) 28 | # ifdef __cplusplus 29 | # define __END_DECLS } 30 | # else 31 | # define __END_DECLS 32 | # endif 33 | #endif 34 | 35 | /* 36 | * Stringification of version major, minor, and build. 37 | * I have on idea if this is necessary. 38 | */ 39 | #define NAME(s) NAME0(s) 40 | #define NAME0(s) #s 41 | #define NAME2(x,y,z) x ## . ## y ## . ## z 42 | #define NAME1(x,y,z) NAME2(x,y,z) 43 | 44 | /* 45 | * Major version. 46 | */ 47 | #define KSQL_VMAJOR 0 48 | 49 | /* 50 | * Minor version. 51 | */ 52 | #define KSQL_VMINOR 3 53 | 54 | /* 55 | * Build version. 56 | */ 57 | #define KSQL_VBUILD 5 58 | 59 | /* 60 | * Version string of major.minor.build (as a literal string). 61 | */ 62 | #define KSQL_VERSION NAME(NAME1(KSQL_VMAJOR,KSQL_VMINOR,KSQL_VBUILD)) 63 | 64 | /* 65 | * Integral stamp of version. 66 | * Guaranteed to be increasing with build, minor, and major. 67 | * (Assumes build and minor never go over 100.) 68 | */ 69 | #define KSQL_VSTAMP \ 70 | ((KSQL_VBUILD+1) + \ 71 | (KSQL_VMINOR+1)*100 + \ 72 | (KSQL_VMAJOR+1)*10000) 73 | 74 | /* 75 | * Error codes returned by all functions. 76 | * In general, checking for zero means success. 77 | */ 78 | 79 | enum ksqlc { 80 | KSQL_OK = 0, /* success */ 81 | KSQL_DONE, /* data done */ 82 | KSQL_ROW, /* row of data */ 83 | KSQL_CONSTRAINT, /* step constraint */ 84 | KSQL_MEM, /* failure to prepare */ 85 | KSQL_NOTOPEN, /* DB not open */ 86 | KSQL_ALREADYOPEN, /* DB already open */ 87 | KSQL_DB, /* errors in DB */ 88 | KSQL_TRANS, /* transaction recursive or not started */ 89 | KSQL_STMT, /* statement still open at close */ 90 | KSQL_EXIT, /* closing database on exit */ 91 | KSQL_SYSTEM, /* system error (fork, socketpair, etc.) */ 92 | KSQL_EOF, /* internal only */ 93 | KSQL_SECURITY, /* security breach */ 94 | KSQL_BINDCOL, /* invalid bind column index */ 95 | KSQL_RESULTCOL, /* invalid result column index */ 96 | KSQL_NULL, /* NULL value when requesting result */ 97 | KSQL_NORESULTS, /* request for results without any rows */ 98 | }; 99 | 100 | typedef void (*ksqldbmsg)(void *, int, int, const char *, const char *); 101 | typedef void (*ksqlmsg)(void *, enum ksqlc, const char *, const char *); 102 | 103 | struct ksqlrole { 104 | const int *roles; /* roles we can access */ 105 | const int *stmts; /* statements we can call */ 106 | unsigned int flags; /* additional role properties */ 107 | #define KSQLROLE_OPEN 0x01 /* open new databases */ 108 | }; 109 | 110 | struct ksqlroles { 111 | struct ksqlrole *roles; 112 | size_t rolesz; 113 | size_t defrole; 114 | }; 115 | 116 | struct ksqlstmts { 117 | const char *const *stmts; 118 | size_t stmtsz; 119 | }; 120 | 121 | struct ksqlcfg { 122 | struct ksqlstmts stmts; 123 | struct ksqlroles roles; 124 | unsigned int flags; 125 | #define KSQL_EXIT_ON_ERR 0x01 126 | #define KSQL_FOREIGN_KEYS 0x02 127 | #define KSQL_SAFE_EXIT 0x04 128 | ksqlmsg err; 129 | ksqldbmsg dberr; 130 | void *arg; 131 | }; 132 | 133 | struct ksql; 134 | struct ksqlstmt; 135 | 136 | __BEGIN_DECLS 137 | 138 | struct ksql *ksql_alloc(const struct ksqlcfg *) 139 | __attribute__((deprecated)); 140 | struct ksql *ksql_alloc_child(const struct ksqlcfg *, void(*)(void *), void *); 141 | enum ksqlc ksql_bind_blob(struct ksqlstmt *, 142 | size_t, const void *, size_t); 143 | enum ksqlc ksql_bind_double(struct ksqlstmt *, size_t, double); 144 | enum ksqlc ksql_bind_int(struct ksqlstmt *, size_t, int64_t); 145 | enum ksqlc ksql_bind_null(struct ksqlstmt *, size_t); 146 | enum ksqlc ksql_bind_str(struct ksqlstmt *, size_t, const char *); 147 | enum ksqlc ksql_bind_zblob(struct ksqlstmt *, size_t, size_t); 148 | void ksql_cfg_defaults(struct ksqlcfg *); 149 | enum ksqlc ksql_close(struct ksql *); 150 | enum ksqlc ksql_exec(struct ksql *, const char *, size_t); 151 | enum ksqlc ksql_free(struct ksql *); 152 | enum ksqlc ksql_lastid(struct ksql *, int64_t *); 153 | enum ksqlc ksql_open(struct ksql *, const char *); 154 | enum ksqlc ksql_result_blob(struct ksqlstmt *, const void **, size_t *, size_t); 155 | enum ksqlc ksql_result_blob_alloc(struct ksqlstmt *, void **, size_t *, size_t); 156 | enum ksqlc ksql_result_bytes(struct ksqlstmt *, size_t *, size_t); 157 | enum ksqlc ksql_result_double(struct ksqlstmt *, double *, size_t); 158 | enum ksqlc ksql_result_int(struct ksqlstmt *, int64_t *, size_t); 159 | enum ksqlc ksql_result_isnull(struct ksqlstmt *, int *, size_t); 160 | enum ksqlc ksql_result_str(struct ksqlstmt *, const char **, size_t); 161 | enum ksqlc ksql_result_str_alloc(struct ksqlstmt *, char **, size_t); 162 | void ksql_role(struct ksql *, size_t); 163 | enum ksqlc ksql_stmt_alloc(struct ksql *, 164 | struct ksqlstmt **, const char *, size_t); 165 | 166 | enum ksqlc ksql_stmt_step(struct ksqlstmt *); 167 | enum ksqlc ksql_stmt_cstep(struct ksqlstmt *); 168 | enum ksqlc ksql_stmt_reset(struct ksqlstmt *); 169 | enum ksqlc ksql_stmt_free(struct ksqlstmt *); 170 | 171 | const void *ksql_stmt_blob(struct ksqlstmt *, size_t) 172 | __attribute__((deprecated)); 173 | size_t ksql_stmt_bytes(struct ksqlstmt *, size_t) 174 | __attribute__((deprecated)); 175 | double ksql_stmt_double(struct ksqlstmt *, size_t) 176 | __attribute__((deprecated)); 177 | int64_t ksql_stmt_int(struct ksqlstmt *, size_t) 178 | __attribute__((deprecated)); 179 | int ksql_stmt_isnull(struct ksqlstmt *, size_t) 180 | __attribute__((deprecated)); 181 | const char *ksql_stmt_str(struct ksqlstmt *, size_t) 182 | __attribute__((deprecated)); 183 | 184 | enum ksqlc ksql_trans_commit(struct ksql *, size_t); 185 | enum ksqlc ksql_trans_exclopen(struct ksql *, size_t); 186 | enum ksqlc ksql_trans_open(struct ksql *, size_t); 187 | enum ksqlc ksql_trans_rollback(struct ksql *, size_t); 188 | enum ksqlc ksql_trans_singleopen(struct ksql *, size_t); 189 | 190 | enum ksqlc ksql_trace(struct ksql *); 191 | enum ksqlc ksql_untrace(struct ksql *); 192 | 193 | void ksqlitedbmsg(void *, int, int, const char *, const char *); 194 | void ksqlitemsg(void *, enum ksqlc, const char *, const char *); 195 | 196 | __END_DECLS 197 | 198 | #endif 199 | -------------------------------------------------------------------------------- /ksql_alloc.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016, 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_ALLOC 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_alloc 22 | .Nd allocate a ksql database handle 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft struct ksql * 30 | .Fo ksql_alloc 31 | .Fa "const struct ksqlcfg *cfg" 32 | .Fc 33 | .Sh DESCRIPTION 34 | .Em This function will soon be deprecated. 35 | See 36 | .Xr ksql_alloc_child 3 37 | for the replacement. 38 | .Pp 39 | The 40 | .Nm 41 | function creates a SQLite database handle. 42 | This must be matched by a call to 43 | .Xr ksql_free 3 44 | unless 45 | .Dv KSQL_SAFE_EXIT 46 | is specified, in which case it will be freed on exit. 47 | (It is still good practice to manually free.) 48 | It is usually followed by 49 | .Xr ksql_open 3 . 50 | The database is opened within the calling process. 51 | For a split-process model, see 52 | .Xr ksql_alloc_child 3 . 53 | .Pp 54 | If 55 | .Fa cfg 56 | is 57 | .Dv NULL , 58 | a configuration from 59 | .Xr ksql_cfg_defaults 3 60 | is used. 61 | .\" .Sh CONTEXT 62 | .\" For section 9 functions only. 63 | .\" .Sh IMPLEMENTATION NOTES 64 | .\" Not used in OpenBSD. 65 | .Sh RETURN VALUES 66 | This returns the allocated database handle or 67 | .Dv NULL 68 | if memory allocations failed. 69 | .\" For sections 2, 3, and 9 function return values only. 70 | .\" .Sh ENVIRONMENT 71 | .\" For sections 1, 6, 7, and 8 only. 72 | .\" .Sh FILES 73 | .\" .Sh EXIT STATUS 74 | .\" For sections 1, 6, and 8 only. 75 | .\" .Sh EXAMPLES 76 | .\" .Sh DIAGNOSTICS 77 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 78 | .\" .Sh ERRORS 79 | .\" For sections 2, 3, 4, and 9 errno settings only. 80 | .Sh SEE ALSO 81 | .Xr ksql_alloc_child 3 , 82 | .Xr ksql_cfg_defaults 3 , 83 | .Xr ksql_free 3 84 | .\" .Sh STANDARDS 85 | .\" .Sh HISTORY 86 | .\" .Sh AUTHORS 87 | .\" .Sh CAVEATS 88 | .\" .Sh BUGS 89 | .\" .Sh SECURITY CONSIDERATIONS 90 | .\" Not used in OpenBSD. 91 | -------------------------------------------------------------------------------- /ksql_alloc_child.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2017--2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_ALLOC_CHILD 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_alloc_child 22 | .Nd allocate a ksql database handle in split-process mode 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft struct ksql * 30 | .Fo ksql_alloc_child 31 | .Fa "const struct ksqlcfg *cfg" 32 | .Fa "void (*onfork)(void *arg)" 33 | .Fa "void *arg" 34 | .Fc 35 | .Sh DESCRIPTION 36 | The 37 | .Nm 38 | function creates a SQLite database handle. 39 | It must be matched by a call to 40 | .Xr ksql_free 3 41 | unless 42 | .Dv KSQL_SAFE_EXIT 43 | is specified, in which case it will be freed on exit. 44 | (It is still good practice to manually free.) 45 | It is usually followed by 46 | .Xr ksql_open 3 . 47 | .Pp 48 | The 49 | .Nm 50 | function differs from 51 | .Xr ksql_alloc 3 52 | in that the database is opened in a child process instead of within the 53 | current process, with all database queries running over a communication 54 | socket. 55 | This way, the caller can safely sandbox after the function returns. 56 | .Pp 57 | If 58 | .Fa cfg 59 | is 60 | .Dv NULL , 61 | a configuration from 62 | .Xr ksql_cfg_defaults 3 63 | is used. 64 | .Pp 65 | The 66 | .Fa onfork 67 | function, if specified, is invoked by the child after a successful 68 | .Xr fork 2 69 | with the 70 | .Fa arg 71 | variable. 72 | The process environment within which 73 | .Fa onfork 74 | is invoked is prior to its being sandboxed. 75 | .\" .Sh CONTEXT 76 | .\" For section 9 functions only. 77 | .\" .Sh IMPLEMENTATION NOTES 78 | .\" Not used in OpenBSD. 79 | .Sh RETURN VALUES 80 | This returns the allocated database handle or 81 | .Dv NULL 82 | if an error occurs. 83 | .\" For sections 2, 3, and 9 function return values only. 84 | .\" .Sh ENVIRONMENT 85 | .\" For sections 1, 6, 7, and 8 only. 86 | .\" .Sh FILES 87 | .\" .Sh EXIT STATUS 88 | .\" For sections 1, 6, and 8 only. 89 | .\" .Sh EXAMPLES 90 | .\" .Sh DIAGNOSTICS 91 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 92 | .\" .Sh ERRORS 93 | .\" For sections 2, 3, 4, and 9 errno settings only. 94 | .Sh SEE ALSO 95 | .Xr ksql_cfg_defaults 3 , 96 | .Xr ksql_free 3 97 | .\" .Sh STANDARDS 98 | .\" .Sh HISTORY 99 | .\" .Sh AUTHORS 100 | .\" .Sh CAVEATS 101 | .\" .Sh BUGS 102 | .\" .Sh SECURITY CONSIDERATIONS 103 | .\" Not used in OpenBSD. 104 | -------------------------------------------------------------------------------- /ksql_bind_double.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_BIND_DOUBLE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_bind_blob , 22 | .Nm ksql_bind_double , 23 | .Nm ksql_bind_int , 24 | .Nm ksql_bind_null , 25 | .Nm ksql_bind_str , 26 | .Nm ksql_bind_zblob 27 | .Nd bind values to a prepared statement 28 | .Sh LIBRARY 29 | .Lb ksql 30 | .Sh SYNOPSIS 31 | .In sys/types.h 32 | .In stdint.h 33 | .In ksql.h 34 | .Ft "enum ksqlc" 35 | .Fo ksql_bind_blob 36 | .Fa "struct ksqlstmt *stmt" 37 | .Fa "size_t column" 38 | .Fa "const void *val" 39 | .Fa "size_t valsz" 40 | .Fc 41 | .Ft "enum ksqlc" 42 | .Fo ksql_bind_double 43 | .Fa "struct ksqlstmt *stmt" 44 | .Fa "size_t column" 45 | .Fa "double val" 46 | .Fc 47 | .Ft "enum ksqlc" 48 | .Fo ksql_bind_int 49 | .Fa "struct ksqlstmt *stmt" 50 | .Fa "size_t column" 51 | .Fa "int64_t val" 52 | .Fc 53 | .Ft "enum ksqlc" 54 | .Fo ksql_bind_null 55 | .Fa "struct ksqlstmt *stmt" 56 | .Fa "size_t column" 57 | .Fc 58 | .Ft "enum ksqlc" 59 | .Fo ksql_bind_str 60 | .Fa "struct ksqlstmt *stmt" 61 | .Fa "size_t column" 62 | .Fa "const char *val" 63 | .Fc 64 | .Ft "enum ksqlc" 65 | .Fo ksql_bind_zblob 66 | .Fa "struct ksqlstmt *stmt" 67 | .Fa "size_t column" 68 | .Fa "size_t valsz" 69 | .Fc 70 | .Sh DESCRIPTION 71 | These functions bind data to a statement prepared with 72 | .Xr ksql_stmt_alloc 3 . 73 | They all accept the relevant 74 | .Fa column 75 | to bind, which starts at zero. 76 | .Pp 77 | In 78 | .Nm ksql_bind_blob 79 | and 80 | .Nm ksql_bind_str , 81 | the contents are internally copied. 82 | .\" .Sh CONTEXT 83 | .\" For section 9 functions only. 84 | .\" .Sh IMPLEMENTATION NOTES 85 | .\" Not used in OpenBSD. 86 | .Sh RETURN VALUES 87 | These functions return 88 | .Dv KSQL_BINDCOL 89 | if the requested 90 | .Fa column 91 | is not associated with a statement parameter, 92 | .Dv KSQL_DB 93 | if the underlying bind operation fails, or 94 | .Dv KSQL_OK 95 | on success. 96 | .\" For sections 2, 3, and 9 function return values only. 97 | .\" .Sh ENVIRONMENT 98 | .\" For sections 1, 6, 7, and 8 only. 99 | .\" .Sh FILES 100 | .\" .Sh EXIT STATUS 101 | .\" For sections 1, 6, and 8 only. 102 | .\" .Sh EXAMPLES 103 | .\" .Sh DIAGNOSTICS 104 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 105 | .\" .Sh ERRORS 106 | .\" For sections 2, 3, 4, and 9 errno settings only. 107 | .Sh SEE ALSO 108 | .Xr sqlite3_bind_blob 3 109 | .\" .Xr foobar 1 110 | .\" .Sh STANDARDS 111 | .\" .Sh HISTORY 112 | .\" .Sh AUTHORS 113 | .Sh CAVEATS 114 | In SQLite, binding occurs starting at column one. 115 | In these functions, for consistency with 116 | .Xr ksql_stmt_double 3 117 | and friends, it starts at zero. 118 | .\" .Sh BUGS 119 | .\" .Sh SECURITY CONSIDERATIONS 120 | .\" Not used in OpenBSD. 121 | -------------------------------------------------------------------------------- /ksql_cfg_defaults.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_CFG_DEFAULTS 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_cfg_defaults 22 | .Nd set defaults for a ksql configuration 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft void 30 | .Fo ksql_cfg_defaults 31 | .Fa "struct ksqlcfg *cfg" 32 | .Fc 33 | .Sh DESCRIPTION 34 | The 35 | .Nm 36 | function initialises 37 | .Fa cfg 38 | with useful defaults: the 39 | .Dv KSQL_EXIT_ON_ERR 40 | and 41 | .Dv KSQL_SAFE_EXIT 42 | flags are set, which means that any errors in using database routines 43 | will trigger an exit; and upon exiting in such a state, the database 44 | will be properly cleaned up. 45 | The 46 | .Fn ksqlitemsg 47 | and 48 | .Fn ksqlitedbmsg 49 | functions are set as error message loggers. 50 | These output to 51 | .Dv stderr 52 | the full error message and error code for both regular and database 53 | errors. 54 | .Pp 55 | The 56 | .Vt struct ksqlcfg 57 | structure consists of the following: 58 | .Bl -tag -width Ds 59 | .It Va void *arg 60 | The private argument passed to 61 | .Va err 62 | and 63 | .Va dberr . 64 | .It Va ksqldbmsg dberr 65 | A function that will be invoked upon a database error, for example, if 66 | .Xr sqlite3_step 3 67 | does not return an 68 | .Dv SQLITE_DONE 69 | or 70 | .Dv SQLITE_ROW 71 | code. 72 | .It Va ksqlmsg err 73 | Supply a function that will be invoked upon a non-database error, for 74 | example, memory allocation. 75 | .It Va unsigned int flags 76 | A bit-field which may consists of 77 | .Dv KSQL_EXIT_ON_ERR , 78 | which causes the system to 79 | .Xr exit 3 80 | if any database errors occur; 81 | .Dv KSQL_FOREIGN_KEYS , 82 | which causes the database to be subsequently opened with foreign key 83 | support; and 84 | .Dv KSQL_SAFE_EXIT , 85 | which causes the library to register an 86 | .Xr atexit 3 87 | hook to free the database if it hasn't be freed prior to exit. 88 | The 89 | .Dv KSQL_SAFE_EXIT 90 | flag will also cause the 91 | .Dv SIGABRT 92 | and 93 | .Dv SIGSEGV 94 | signals to be caught and 95 | .Xr siglongjmp 3 96 | into an exit handler, which will then close out open databases. 97 | .It Va struct ksqlroles roles 98 | Role-based access control configuration. 99 | Roles map a caller role to stored statements in 100 | .Fa stmts 101 | and set the availability of further role transition with 102 | .Xr ksql_role 3 . 103 | .It Va struct ksqlstmts stmts 104 | Structure containing stored statement information. 105 | If stored statements are provided, 106 | .Xr ksql_stmt_alloc 3 107 | and 108 | .Xr ksql_exec 3 109 | will only draw from the stored statements. 110 | .El 111 | .Pp 112 | The 113 | .Vt ksqlmsg 114 | function is invoked as 115 | .Ft void 116 | .Fo ksqlmsg 117 | .Fa "void *arg" 118 | .Fa "enum ksqlc code" 119 | .Fa "const char *file" 120 | .Fa "const char *msg" 121 | .Fc , 122 | with 123 | .Fa arg 124 | being the private argument, 125 | .Fa argc 126 | being the error code in question, 127 | the database 128 | .Fa file 129 | .Po which may be 130 | .Dv NULL Pc , 131 | and 132 | .Fa msg 133 | being an ASCII string describing the error (in English). 134 | .Pp 135 | The 136 | .Vt ksqldbmsg 137 | function is 138 | .Ft void 139 | .Fo ksqldbmsg 140 | .Fa "void *arg" 141 | .Fa "int sqlerr" 142 | .Fa "int sqlexterr" 143 | .Fa "const char *file" 144 | .Fa "const char *msg" 145 | .Fc , 146 | which also has the 147 | .Fa sqlerr 148 | and 149 | .Fa sqlexterr 150 | SQLite error and extended error code, and and the SQLite string error 151 | message 152 | .Fa msg . 153 | .Pp 154 | The 155 | .Fa stmts 156 | variable configures stored statements. 157 | These provide an extra measure of security for 158 | .Xr ksql_alloc_child 3 159 | contexts where the protected child process manages pre-set SQL 160 | statements that cannot be changed by the caller. 161 | It contains the following: 162 | .Bl -tag -width Ds 163 | .It Fa const char *const *stmts 164 | An array of SQL statement strings, none of which may be 165 | .Dv NULL . 166 | .It Fa size_t stmtsz 167 | The number of entries in 168 | .Fa stmts . 169 | .El 170 | .Pp 171 | The 172 | .Fa roles 173 | variable configures role-based access control mapping roles to stored 174 | statements set in 175 | .Fa stmts . 176 | Stored statements may be used without roles, but roles require stored 177 | statements. 178 | The structure consists of the following: 179 | .Bl -tag -width Ds 180 | .It Fa struct ksqlrole *roles 181 | The role array. 182 | Each 183 | .Fa struct ksqlrole 184 | entry consists of 185 | .Fa roles , 186 | a list of possible roles that may be subsequently set with 187 | .Xr ksql_role 3 188 | from the current role; 189 | .Fa flags , 190 | a bit-field consisting only of 191 | .Dv KSQLROLE_OPEN , 192 | which indicates that the role may open databases; and 193 | .Fa stmts , 194 | a list of all possible statements. 195 | The index of a statement in 196 | .Fa stmts 197 | and the role in 198 | .Fa roles 199 | corresponds to the 200 | .Fa id 201 | passed to 202 | .Xr ksql_stmt_alloc 3 203 | and 204 | .Xr ksql_exec 3 . 205 | If it zero if false (the role may not execute the statement, or the role 206 | may not be entered from the given role), non-zero if it may. 207 | .It Fa size_t rolesz 208 | The length of 209 | .Fa roles . 210 | .It Fa size_t defrole 211 | The index of the default role set upon 212 | .Xr ksql_alloc 3 213 | or 214 | .Xr ksql_alloc_child 3 . 215 | .El 216 | .\" .Sh CONTEXT 217 | .\" For section 9 functions only. 218 | .\" .Sh IMPLEMENTATION NOTES 219 | .\" Not used in OpenBSD. 220 | .\" .Sh RETURN VALUES 221 | .\" For sections 2, 3, and 9 function return values only. 222 | .\" .Sh ENVIRONMENT 223 | .\" For sections 1, 6, 7, and 8 only. 224 | .\" .Sh FILES 225 | .\" .Sh EXIT STATUS 226 | .\" For sections 1, 6, and 8 only. 227 | .Sh EXAMPLES 228 | In this simple example, a default configuration is extended with stored 229 | statements, then the connection is started in split-process mode and the 230 | caller sandboxes. 231 | (The sandboxing is only available on 232 | .Ox . ) 233 | For brevity, no error checking is performed. 234 | .Bd -literal 235 | struct ksqlcfg cfg; 236 | struct ksql *sql; 237 | const char *const stmts[] = { 238 | "INSERT INTO test (foo) VALUES (?)", 239 | "SELECT foo FROM test" 240 | }; 241 | 242 | ksql_cfg_defaults(&cfg); 243 | cfg.stmts.stmts = stmts; 244 | cfg.stmts.stmtsz = 2; 245 | 246 | sql = ksql_alloc_child(&cfg, NULL, NULL); 247 | pledge("stdio", NULL); 248 | .Ed 249 | .\" .Sh DIAGNOSTICS 250 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 251 | .\" .Sh ERRORS 252 | .\" For sections 2, 3, 4, and 9 errno settings only. 253 | .Sh SEE ALSO 254 | .Xr ksql_alloc 3 , 255 | .Xr ksql_alloc_child 3 , 256 | .Xr ksql_exec 3 , 257 | .Xr ksql_stmt_alloc 3 258 | .\" .Sh STANDARDS 259 | .\" .Sh HISTORY 260 | .\" .Sh AUTHORS 261 | .\" .Sh CAVEATS 262 | .\" .Sh BUGS 263 | .\" .Sh SECURITY CONSIDERATIONS 264 | .\" Not used in OpenBSD. 265 | -------------------------------------------------------------------------------- /ksql_close.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_CLOSE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_close 22 | .Nd close a ksql database connection 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_close 31 | .Fa "struct ksql *sql" 32 | .Fc 33 | .Sh DESCRIPTION 34 | The 35 | .Nm 36 | function closes a database connection opened with 37 | .Xr ksql_open 3 . 38 | If the connection was not opened, is already closed, or 39 | .Fa sql 40 | is 41 | .Dv NULL , 42 | this function is a no-op. 43 | .Pp 44 | This function will begin by calling 45 | .Xr sqlite3_finalize 3 46 | on all active statements (it is an error to have any open connections), 47 | then it will roll back any open transactions (also triggering an error) 48 | if found, 49 | then it will close out the database itself with 50 | .Xr sqlite3_close 3 . 51 | In the event of statements still being open, all statements are 52 | finalised and the database closed prior to calling the error handler. 53 | If 54 | .Dv KSQL_FAIL_ON_EXIT 55 | was specified on 56 | .Fa sql , 57 | the 58 | .Xr exit 2 59 | will only be invoked after all resources have been closed and freed. 60 | .\" .Sh CONTEXT 61 | .\" For section 9 functions only. 62 | .\" .Sh IMPLEMENTATION NOTES 63 | .\" Not used in OpenBSD. 64 | .Sh RETURN VALUES 65 | This returns 66 | .Dv KSQL_STMT 67 | if there were open statements, 68 | .Dv KSQL_TRANS 69 | if there was an open transaction 70 | (if there were both open statements and a transaction, this is 71 | returned), 72 | .Dv KSQL_DB 73 | if there were database-level errors (this overrides whether there were 74 | open transactions and/or a transaction), or 75 | .Dv KSQL_OK 76 | on success. 77 | .\" For sections 2, 3, and 9 function return values only. 78 | .\" .Sh ENVIRONMENT 79 | .\" For sections 1, 6, 7, and 8 only. 80 | .\" .Sh FILES 81 | .\" .Sh EXIT STATUS 82 | .\" For sections 1, 6, and 8 only. 83 | .\" .Sh EXAMPLES 84 | .\" .Sh DIAGNOSTICS 85 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 86 | .\" .Sh ERRORS 87 | .\" For sections 2, 3, 4, and 9 errno settings only. 88 | .Sh SEE ALSO 89 | .Xr sqlite3_close 3 , 90 | .Xr sqlite3_exec 3 , 91 | .Xr sqlite3_finalize 3 92 | .\" .Xr foobar 1 93 | .\" .Sh STANDARDS 94 | .\" .Sh HISTORY 95 | .\" .Sh AUTHORS 96 | .\" .Sh CAVEATS 97 | .\" .Sh BUGS 98 | .\" .Sh SECURITY CONSIDERATIONS 99 | .\" Not used in OpenBSD. 100 | -------------------------------------------------------------------------------- /ksql_exec.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016, 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_EXEC 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_exec 22 | .Nd execute a standalone statement not returning any values 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_exec 31 | .Fa "struct ksql *sql" 32 | .Fa "const char *stmt" 33 | .Fa "size_t id" 34 | .Fc 35 | .Sh DESCRIPTION 36 | The 37 | .Nm 38 | function executes 39 | .Fa stmt 40 | on an open database connection 41 | .Fa sql . 42 | The 43 | .Fa id 44 | is used to identify the statement in error messages and should uniquely 45 | identify the statement. 46 | .Pp 47 | If a non-empty 48 | .Fa cfg->stmts 49 | structure documented in 50 | .Xr ksql_cfg_defaults 3 51 | is passed to 52 | .Xr ksql_alloc 3 53 | or 54 | .Xr ksql_alloc_child 3 , 55 | the stored statement is looked up by 56 | .Fa id 57 | and the 58 | .Fa stmt 59 | value is ignored. 60 | If 61 | .Fa id 62 | is not a valid index of 63 | .Fa cfg->stmts.stmtsz , 64 | the program is immediately terminated. 65 | .Pp 66 | This function handles a locked database (specifically, 67 | .Dv SQLITE_BUSY , 68 | .Dv SQLITE_LOCKED , 69 | or 70 | .Dv SQLITE_PROTOCOL ) 71 | by sleeping for a random interval, then trying again infinitely. 72 | .\" .Sh CONTEXT 73 | .\" For section 9 functions only. 74 | .\" .Sh IMPLEMENTATION NOTES 75 | .\" Not used in OpenBSD. 76 | .Sh RETURN VALUES 77 | This function returns 78 | .Dv KSQL_NOTOPEN 79 | if the database connection has not been opeend, 80 | .Dv KSQL_DB 81 | on database errors, or 82 | .Dv KSQL_OK 83 | otherwise. 84 | .\" For sections 2, 3, and 9 function return values only. 85 | .\" .Sh ENVIRONMENT 86 | .\" For sections 1, 6, 7, and 8 only. 87 | .\" .Sh FILES 88 | .\" .Sh EXIT STATUS 89 | .\" For sections 1, 6, and 8 only. 90 | .\" .Sh EXAMPLES 91 | .\" .Sh DIAGNOSTICS 92 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 93 | .\" .Sh ERRORS 94 | .\" For sections 2, 3, 4, and 9 errno settings only. 95 | .Sh SEE ALSO 96 | .Xr sqlite3_exec 3 97 | .\" .Xr foobar 1 98 | .\" .Sh STANDARDS 99 | .\" .Sh HISTORY 100 | .\" .Sh AUTHORS 101 | .\" .Sh CAVEATS 102 | .\" .Sh BUGS 103 | .\" .Sh SECURITY CONSIDERATIONS 104 | .\" Not used in OpenBSD. 105 | -------------------------------------------------------------------------------- /ksql_free.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_FREE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_free 22 | .Nd free a ksql database handle 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_free 31 | .Fa "struct ksql *sql" 32 | .Fc 33 | .Sh DESCRIPTION 34 | The 35 | .Nm 36 | function frees a database handle 37 | .Fa sql , 38 | previously allocated with 39 | .Xr ksql_alloc 3 . 40 | This internally invokes 41 | .Xr ksql_close 3 42 | then frees all internal memory. 43 | If the handle was allocated with 44 | .Dv KSQL_SAFE_EXIT , 45 | the handle is removed from the queue of handles that would be freed on 46 | exit (so it is not double-freed). 47 | .Pp 48 | If 49 | .Fa sql 50 | is 51 | .Dv NULL , 52 | no action occurs. 53 | .Pp 54 | This function is invoked from the 55 | .Xr atexit 3 56 | handler if 57 | .Dv KSQL_SAFE_EXIT 58 | was specified and the handle was not freed prior to exiting. 59 | In this case, the 60 | .Dv KSQL_EXIT_ON_ERR 61 | flag is temporarily suppressed. 62 | .\" .Sh CONTEXT 63 | .\" For section 9 functions only. 64 | .\" .Sh IMPLEMENTATION NOTES 65 | .\" Not used in OpenBSD. 66 | .Sh RETURN VALUES 67 | This returns the error code of 68 | .Xr ksql_close 3 . 69 | .\" For sections 2, 3, and 9 function return values only. 70 | .\" .Sh ENVIRONMENT 71 | .\" For sections 1, 6, 7, and 8 only. 72 | .\" .Sh FILES 73 | .\" .Sh EXIT STATUS 74 | .\" For sections 1, 6, and 8 only. 75 | .\" .Sh EXAMPLES 76 | .\" .Sh DIAGNOSTICS 77 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 78 | .\" .Sh ERRORS 79 | .\" For sections 2, 3, 4, and 9 errno settings only. 80 | .\" .Sh SEE ALSO 81 | .\" .Xr foobar 1 82 | .\" .Sh STANDARDS 83 | .\" .Sh HISTORY 84 | .\" .Sh AUTHORS 85 | .\" .Sh CAVEATS 86 | .\" .Sh BUGS 87 | .\" .Sh SECURITY CONSIDERATIONS 88 | .\" Not used in OpenBSD. 89 | -------------------------------------------------------------------------------- /ksql_lastid.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016, 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_LASTID 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_lastid 22 | .Nd get the last insertion row identifier 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft "enum ksqlc" 30 | .Fo ksql_lastid 31 | .Fa "struct ksql *sql" 32 | .Fa "int64_t *id" 33 | .Fc 34 | .Sh DESCRIPTION 35 | The 36 | .Nm 37 | function fills in 38 | .Fa id 39 | with the row identifier of the last insertion. 40 | If 41 | .Fa id 42 | is 43 | .Dv NULL , 44 | then this does nothing, except that the return value still indicates 45 | whether the database is open. 46 | If no row was successfully inserted since the database 47 | .Fa sql 48 | was opened, 49 | .Pf * Fa id 50 | is set to 0. 51 | .\" .Sh CONTEXT 52 | .\" For section 9 functions only. 53 | .\" .Sh IMPLEMENTATION NOTES 54 | .\" Not used in OpenBSD. 55 | .Sh RETURN VALUES 56 | The 57 | .Nm 58 | function returns 59 | .Dv KSQL_NOTOPEN 60 | if the database is not open or 61 | .Dv KSQL_OK 62 | otherwise. 63 | .\" For sections 2, 3, and 9 function return values only. 64 | .\" .Sh ENVIRONMENT 65 | .\" For sections 1, 6, 7, and 8 only. 66 | .\" .Sh FILES 67 | .\" .Sh EXIT STATUS 68 | .\" For sections 1, 6, and 8 only. 69 | .Sh EXAMPLES 70 | The following assumes an open database 71 | .Va sql . 72 | It inserts a row and checks its row identifier. 73 | For brevity, it performs no error checking. 74 | .Bd -literal 75 | int64_t id; 76 | struct ksqlstmt *stmt; 77 | 78 | ksql_stmt_alloc(sql, &stmt, 79 | "INSERT INTO test (foo) VALUES (?)", 0); 80 | ksql_bind_int(stmt, 0, 42); 81 | ksql_stmt_step(stmt); 82 | ksql_lastid(sql, &id); 83 | 84 | printf("The last rowid = %" PRId64 "\en", id); 85 | .Ed 86 | .\" .Sh DIAGNOSTICS 87 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 88 | .\" .Sh ERRORS 89 | .\" For sections 2, 3, 4, and 9 errno settings only. 90 | .Sh SEE ALSO 91 | .Xr sqlite3_last_insert_rowid 3 92 | .\" .Xr foobar 1 93 | .\" .Sh STANDARDS 94 | .\" .Sh HISTORY 95 | .\" .Sh AUTHORS 96 | .\" .Sh CAVEATS 97 | .\" .Sh BUGS 98 | .\" .Sh SECURITY CONSIDERATIONS 99 | .\" Not used in OpenBSD. 100 | -------------------------------------------------------------------------------- /ksql_open.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016, 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_OPEN 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_open 22 | .Nd open a ksql database connection 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_open 31 | .Fa "struct ksql *sql" 32 | .Fa "const char *dbfile" 33 | .Fc 34 | .Sh DESCRIPTION 35 | The 36 | .Nm 37 | function opens a database connection to 38 | .Fa dbfile 39 | (which must exist) on a handle allocated with 40 | .Xr ksql_alloc 3 . 41 | If the handle was allocated with 42 | .Dv KSQL_FOREIGN_KEYS , 43 | a successful open will be followed by a 44 | .Xr ksql_exec 3 45 | call to enable foreign keys. 46 | If the current role does not permit opening databases, an error message 47 | is emitted on 48 | .Dv stderr 49 | and the program is immediately terminated. 50 | .Pp 51 | The 52 | .Nm 53 | function handles a locked database (specifically, 54 | .Dv SQLITE_BUSY , 55 | .Dv SQLITE_LOCKED , 56 | or 57 | .Dv SQLITE_PROTOCOL ) 58 | by sleeping for a random interval, then trying again infinitely. 59 | .\" .Sh CONTEXT 60 | .\" For section 9 functions only. 61 | .\" .Sh IMPLEMENTATION NOTES 62 | .\" Not used in OpenBSD. 63 | .Sh RETURN VALUES 64 | This returns 65 | .Dv KSQL_ALREADYOPEN 66 | if a database connection is already open, 67 | .Dv KSQL_MEM 68 | if memory allocation failed, 69 | .Dv KSQL_DB 70 | if the 71 | .Xr sqlite3_open 3 72 | function failed, the exit code of 73 | .Xr ksql_exec 3 74 | if it failed to enable foreign keys, or otherwise 75 | .Dv KSQL_OK 76 | on success. 77 | .\" For sections 2, 3, and 9 function return values only. 78 | .\" .Sh ENVIRONMENT 79 | .\" For sections 1, 6, 7, and 8 only. 80 | .\" .Sh FILES 81 | .\" .Sh EXIT STATUS 82 | .\" For sections 1, 6, and 8 only. 83 | .Sh EXAMPLES 84 | The following opens a database 85 | .Pa test.db 86 | in split-process mode. 87 | For illustrative purpose, it uses 88 | .Xr pledge 2 89 | to sandbox the controlling parent process. 90 | (This security feature is only available on 91 | .Ox . ) 92 | For brevity, this does not report an errors. 93 | .Bd -literal 94 | struct ksql *sql; 95 | 96 | sql = ksql_alloc_child(NULL, NULL, NULL); 97 | pledge("stdio", NULL); 98 | ksql_open(sql, "test.db"); 99 | /* Do stuff */ 100 | ksql_close(sql); 101 | .Ed 102 | .\" .Sh DIAGNOSTICS 103 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 104 | .\" .Sh ERRORS 105 | .\" For sections 2, 3, 4, and 9 errno settings only. 106 | .Sh SEE ALSO 107 | .Xr sqlite3_exec 3 , 108 | .Xr sqlite3_open 3 109 | .\" .Xr foobar 1 110 | .\" .Sh STANDARDS 111 | .\" .Sh HISTORY 112 | .\" .Sh AUTHORS 113 | .\" .Sh CAVEATS 114 | .\" .Sh BUGS 115 | .\" .Sh SECURITY CONSIDERATIONS 116 | .\" Not used in OpenBSD. 117 | -------------------------------------------------------------------------------- /ksql_result_double.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_RESULT_DOUBLE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_result_blob , 22 | .Nm ksql_result_blob_alloc , 23 | .Nm ksql_result_bytes , 24 | .Nm ksql_result_double , 25 | .Nm ksql_result_int , 26 | .Nm ksql_result_isnull , 27 | .Nm ksql_result_str , 28 | .Nm ksql_result_str_alloc 29 | .Nd get the result columns of a statement 30 | .Sh LIBRARY 31 | .Lb ksql 32 | .Sh SYNOPSIS 33 | .In sys/types.h 34 | .In stdint.h 35 | .In ksql.h 36 | .Ft enum ksqlc 37 | .Fo ksql_result_blob 38 | .Fa "struct ksqlstmt *stmt" 39 | .Fa "const void **p" 40 | .Fa "size_t *sz" 41 | .Fa "size_t column" 42 | .Fc 43 | .Ft enum ksqlc 44 | .Fo ksql_result_blob_alloc 45 | .Fa "struct ksqlstmt *stmt" 46 | .Fa "void **p" 47 | .Fa "size_t *sz" 48 | .Fa "size_t column" 49 | .Fc 50 | .Ft enum ksqlc 51 | .Fo ksql_result_bytes 52 | .Fa "struct ksqlstmt *stmt" 53 | .Fa "int *p" 54 | .Fa "size_t column" 55 | .Fc 56 | .Ft enum ksqlc 57 | .Fo ksql_result_double 58 | .Fa "struct ksqlstmt *stmt" 59 | .Fa "double *p" 60 | .Fa "size_t column" 61 | .Fc 62 | .Ft enum ksqlc 63 | .Fo ksql_result_int 64 | .Fa "struct ksqlstmt *stmt" 65 | .Fa "int64_t *p" 66 | .Fa "size_t column" 67 | .Fc 68 | .Ft enum ksqlc 69 | .Fo ksql_result_isnull 70 | .Fa "struct ksqlstmt *stmt" 71 | .Fa "int *p" 72 | .Fa "size_t column" 73 | .Fc 74 | .Ft enum ksqlc 75 | .Fo ksql_result_str 76 | .Fa "struct ksqlstmt *stmt" 77 | .Fa "const char **p" 78 | .Fa "size_t column" 79 | .Fc 80 | .Ft enum ksqlc 81 | .Fo ksql_result_str_alloc 82 | .Fa "struct ksqlstmt *stmt" 83 | .Fa "char **p" 84 | .Fa "size_t column" 85 | .Fc 86 | .Sh DESCRIPTION 87 | These functions return results following 88 | .Xr ksql_stmt_step 3 . 89 | They all accept 90 | .Fa stmt , 91 | the statement allocated with 92 | .Xr ksql_stmt_alloc 3 ; 93 | the 94 | .Fa column 95 | to query, which starts at zero; and 96 | .Fa p , 97 | which is set to the resulting data. 98 | The 99 | .Fn ksql_result_blob 100 | function additionaly accepts 101 | .Fa sz , 102 | which is set to the length of 103 | .Fa p . 104 | .Pp 105 | The 106 | .Fn ksql_result_str 107 | and 108 | .Fn ksql_result_blob 109 | functions return memory that must be copied prior to a subsequent 110 | .Xr ksql_stmt_step 3 , 111 | .Xr ksql_stmt_reset 3 , 112 | or 113 | .Xr ksql_stmt_free 3 . 114 | The 115 | .Fn ksql_result_blob 116 | may fill in a 117 | .Dv NULL 118 | pointer in the event of an empty buffer; 119 | .Fn ksql_result_str 120 | will always return a NUL-terminated string on success. 121 | For the caller to manage memory, use the 122 | .Fn ksql_result_blob_alloc 123 | and 124 | .Fn ksql_result_str_alloc 125 | variants to pass back memory that must be released with 126 | .Xr free 3 . 127 | .\" .Sh CONTEXT 128 | .\" For section 9 functions only. 129 | .\" .Sh IMPLEMENTATION NOTES 130 | .\" Not used in OpenBSD. 131 | .Sh RETURN VALUES 132 | There are a number of error conditions returned by these functions. 133 | .Bl -tag -width Ds 134 | .It Dv KSQL_MEM 135 | Memory allocation failure. 136 | .It Dv KSQL_NORESULTS 137 | There is no result row available. 138 | This happens when the prior 139 | .Xr ksql_stmt_step 3 140 | did not return 141 | .Dv KSQL_ROW . 142 | .It Dv KSQL_NULL 143 | The column has no data. 144 | This is not returned by 145 | .Fn ksql_result_isnull . 146 | .It Dv KSQL_RESULTCOL 147 | The column exceeds the maximum number of available result columns. 148 | .It Dv KSQL_SYSTEM 149 | An internal system error occurred. 150 | .El 151 | .Pp 152 | If any of these errors occur, the passed-in result pointers 153 | .Fa p 154 | and 155 | .Fa sz 156 | are undefined. 157 | .Pp 158 | On success, 159 | .Dv KSQL_OK 160 | is returned and the result pointers are filled in. 161 | .\" For sections 2, 3, and 9 function return values only. 162 | .\" .Sh ENVIRONMENT 163 | .\" For sections 1, 6, 7, and 8 only. 164 | .\" .Sh FILES 165 | .\" .Sh EXIT STATUS 166 | .\" For sections 1, 6, and 8 only. 167 | .\" .Sh EXAMPLES 168 | .\" .Sh DIAGNOSTICS 169 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 170 | .\" .Sh ERRORS 171 | .\" For sections 2, 3, 4, and 9 errno settings only. 172 | .Sh SEE ALSO 173 | .Xr sqlite3_column_blob 3 174 | .\" .Xr foobar 1 175 | .\" .Sh STANDARDS 176 | .\" .Sh HISTORY 177 | .\" .Sh AUTHORS 178 | .\" .Sh CAVEATS 179 | .\" .Sh BUGS 180 | .\" .Sh SECURITY CONSIDERATIONS 181 | .\" Not used in OpenBSD. 182 | -------------------------------------------------------------------------------- /ksql_role.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_ROLE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_role 22 | .Nd set role in ksql context 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft void 30 | .Fo ksql_role 31 | .Fa "struct ksql *sql" 32 | .Fa "size_t role" 33 | .Fc 34 | .Sh DESCRIPTION 35 | The 36 | .Nm 37 | function sets the current role of 38 | .Fa sql . 39 | The 40 | .Fa role 41 | is the index of a role defined in 42 | .Fa cfg->roles 43 | as passed to 44 | .Xr ksql_alloc 3 45 | or 46 | .Xr ksql_alloc_child 3 . 47 | The role affects all subsequent 48 | .Xr ksql_exec 3 49 | and 50 | .Xr ksql_stmt_alloc 3 51 | calls. 52 | .Pp 53 | The new role must be allowed by having a non-zero value in the 54 | .Fa roles 55 | array within the current role's 56 | .Ft struct ksqlrole 57 | object. 58 | Otherwise, the situation is logged to 59 | .Dv stderr 60 | and the program is immediately terminated. 61 | .Pp 62 | In split-process mode, 63 | .Fn ksql_role 64 | automatically sets 65 | .Dv KSQL_EXIT_ON_ERR 66 | on 67 | .Fa cfg->flags 68 | and 69 | .Fa cfg->err 70 | to 71 | .Dv NULL , 72 | restoring both if/when it returns. 73 | These guarantee that the function will never return without having properly set 74 | the new role. 75 | .\" .Sh CONTEXT 76 | .\" For section 9 functions only. 77 | .\" .Sh IMPLEMENTATION NOTES 78 | .\" Not used in OpenBSD. 79 | .\" .Sh RETURN VALUES 80 | .\" For sections 2, 3, and 9 function return values only. 81 | .\" .Sh ENVIRONMENT 82 | .\" For sections 1, 6, 7, and 8 only. 83 | .\" .Sh FILES 84 | .\" .Sh EXIT STATUS 85 | .\" For sections 1, 6, and 8 only. 86 | .\" .Sh EXAMPLES 87 | .\" .Sh DIAGNOSTICS 88 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 89 | .\" .Sh ERRORS 90 | .\" For sections 2, 3, 4, and 9 errno settings only. 91 | .Sh SEE ALSO 92 | .Xr ksql_alloc 3 , 93 | .Xr ksql_alloc_child 3 , 94 | .Xr ksql_exec 3 , 95 | .Xr ksql_stmt_alloc 3 96 | .\" .Sh STANDARDS 97 | .\" .Sh HISTORY 98 | .\" .Sh AUTHORS 99 | .\" .Sh CAVEATS 100 | .\" .Sh BUGS 101 | .\" .Sh SECURITY CONSIDERATIONS 102 | .\" Not used in OpenBSD. 103 | -------------------------------------------------------------------------------- /ksql_stmt_alloc.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016, 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_STMT_ALLOC 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_stmt_alloc 22 | .Nd allocate a statement on a database connection 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_stmt_alloc 31 | .Fa "struct ksql *sql" 32 | .Fa "struct ksqlstmt **stmt" 33 | .Fa "const char *sqlstmt" 34 | .Fa "size_t id" 35 | .Fc 36 | .Sh DESCRIPTION 37 | The 38 | .Nm 39 | function allocates a SQL statement 40 | .Fa sqlstmt 41 | on a database connection 42 | .Fa sql 43 | previously opened with 44 | .Xr ksql_open 3 . 45 | It must be matched by a call to 46 | .Xr ksql_stmt_free 3 . 47 | Upon success, this will fill in the 48 | .Fa stmt 49 | pointer. 50 | The 51 | .Fa id 52 | is used to identify the statement in error messages and should uniquely 53 | identify the statement. 54 | .Pp 55 | If a non-empty 56 | .Fa cfg->stmts 57 | structure documented in 58 | .Xr ksql_cfg_defaults 3 59 | is passed to 60 | .Xr ksql_alloc 3 61 | or 62 | .Xr ksql_alloc_child 3 , 63 | the stored statement is looked up by 64 | .Fa id 65 | and the 66 | .Fa sqlstmt 67 | value is ignored. 68 | If 69 | .Fa id 70 | is not a valid index of 71 | .Fa cfg->stmts.stmtsz 72 | or if the current role does not permit access to the statement, 73 | an error message is emitted on 74 | .Dv stderr 75 | and the program is immediately terminated. 76 | .Pp 77 | The 78 | .Nm 79 | function handles a locked database (specifically, 80 | .Dv SQLITE_BUSY , 81 | .Dv SQLITE_LOCKED , 82 | or 83 | .Dv SQLITE_PROTOCOL ) 84 | by sleeping for a random interval, then trying again infinitely. 85 | .\" .Sh CONTEXT 86 | .\" For section 9 functions only. 87 | .\" .Sh IMPLEMENTATION NOTES 88 | .\" Not used in OpenBSD. 89 | .Sh RETURN VALUES 90 | This returns 91 | .Dv KSQL_MEM 92 | on allocation failure, 93 | .Dv KSQL_NOTOPEN 94 | if the database isn't connected, or 95 | .Dv KSQL_DB 96 | if there were errors invoking 97 | .Xr sqlite3_prepare_v2 3 , 98 | otherwise 99 | .Dv KSQL_OK 100 | on success. 101 | .\" For sections 2, 3, and 9 function return values only. 102 | .\" .Sh ENVIRONMENT 103 | .\" For sections 1, 6, 7, and 8 only. 104 | .\" .Sh FILES 105 | .\" .Sh EXIT STATUS 106 | .\" For sections 1, 6, and 8 only. 107 | .\" .Sh EXAMPLES 108 | .\" .Sh DIAGNOSTICS 109 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 110 | .\" .Sh ERRORS 111 | .\" For sections 2, 3, 4, and 9 errno settings only. 112 | .Sh SEE ALSO 113 | .Xr sqlite3_prepare_v2 3 114 | .\" .Xr foobar 1 115 | .\" .Sh STANDARDS 116 | .\" .Sh HISTORY 117 | .\" .Sh AUTHORS 118 | .\" .Sh CAVEATS 119 | .\" .Sh BUGS 120 | .\" .Sh SECURITY CONSIDERATIONS 121 | .\" Not used in OpenBSD. 122 | -------------------------------------------------------------------------------- /ksql_stmt_double.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016, 2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_STMT_DOUBLE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_stmt_blob , 22 | .Nm ksql_stmt_bytes , 23 | .Nm ksql_stmt_double , 24 | .Nm ksql_stmt_int , 25 | .Nm ksql_stmt_isnull , 26 | .Nm ksql_stmt_str 27 | .Nd get the result columns of a statement 28 | .Sh LIBRARY 29 | .Lb ksql 30 | .Sh SYNOPSIS 31 | .In sys/types.h 32 | .In stdint.h 33 | .In ksql.h 34 | .Ft "const void *" 35 | .Fo ksql_stmt_blob 36 | .Fa "struct ksqlstmt *stmt" 37 | .Fa "size_t column" 38 | .Fc 39 | .Ft size_t 40 | .Fo ksql_stmt_bytes 41 | .Fa "struct ksqlstmt *stmt" 42 | .Fa "size_t column" 43 | .Fc 44 | .Ft double 45 | .Fo ksql_stmt_double 46 | .Fa "struct ksqlstmt *stmt" 47 | .Fa "size_t column" 48 | .Fc 49 | .Ft int64_t 50 | .Fo ksql_stmt_int 51 | .Fa "struct ksqlstmt *stmt" 52 | .Fa "size_t column" 53 | .Fc 54 | .Ft int 55 | .Fo ksql_stmt_isnull 56 | .Fa "struct ksqlstmt *stmt" 57 | .Fa "size_t column" 58 | .Fc 59 | .Ft "const char *" 60 | .Fo ksql_stmt_str 61 | .Fa "struct ksqlstmt *stmt" 62 | .Fa "size_t column" 63 | .Fc 64 | .Sh DESCRIPTION 65 | .Em All of these functions will soon be deprecated. 66 | See 67 | .Xr ksql_result_double 3 68 | for replacements. 69 | .Pp 70 | These functions 71 | functions return results following 72 | .Xr ksql_stmt_step 3 . 73 | They all accept 74 | .Fa stmt , 75 | the statement allocated with 76 | .Xr ksql_stmt_alloc 3 ; 77 | and the 78 | .Fa column 79 | to query, which starts at zero. 80 | The results are not defined for a statement without a resulting row or 81 | for columns beyond the prepared statement. 82 | .Pp 83 | The 84 | .Fn ksql_stmt_str 85 | and 86 | .Fn ksql_stmt_blob 87 | functions return memory tha tmust be copied prior to subsequent 88 | .Xr ksql_stmt_step 3 , 89 | .Xr ksql_stmt_reset 3 , 90 | or 91 | .Xr ksql_stmt_free 3 . 92 | .\" .Sh CONTEXT 93 | .\" For section 9 functions only. 94 | .\" .Sh IMPLEMENTATION NOTES 95 | .\" Not used in OpenBSD. 96 | .\" .Sh RETURN VALUES 97 | .\" For sections 2, 3, and 9 function return values only. 98 | .\" .Sh ENVIRONMENT 99 | .\" For sections 1, 6, 7, and 8 only. 100 | .\" .Sh FILES 101 | .\" .Sh EXIT STATUS 102 | .\" For sections 1, 6, and 8 only. 103 | .\" .Sh EXAMPLES 104 | .\" .Sh DIAGNOSTICS 105 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 106 | .\" .Sh ERRORS 107 | .\" For sections 2, 3, 4, and 9 errno settings only. 108 | .Sh SEE ALSO 109 | .Xr sqlite3_column_blob 3 110 | .\" .Xr foobar 1 111 | .\" .Sh STANDARDS 112 | .\" .Sh HISTORY 113 | .\" .Sh AUTHORS 114 | .\" .Sh CAVEATS 115 | .\" .Sh BUGS 116 | .\" .Sh SECURITY CONSIDERATIONS 117 | .\" Not used in OpenBSD. 118 | -------------------------------------------------------------------------------- /ksql_stmt_free.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016--2017 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_STMT_FREE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_stmt_free 22 | .Nd free a statement on a database connection 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft "enum ksqlc" 30 | .Fo ksql_stmt_free 31 | .Fa "struct ksqlstmt *stmt" 32 | .Fc 33 | .Sh DESCRIPTION 34 | The 35 | .Nm 36 | function frees a statement allocated with 37 | .Xr ksql_stmt_alloc 3 , 38 | invoking 39 | .Xr sqlite3_finalize 3 40 | on the underlying statement. 41 | This does nothing if 42 | .Fa stmt 43 | is 44 | .Dv NULL . 45 | .\" .Sh CONTEXT 46 | .\" For section 9 functions only. 47 | .\" .Sh IMPLEMENTATION NOTES 48 | .\" Not used in OpenBSD. 49 | .Sh RETURN VALUES 50 | Returns 51 | .Dv KSQL_OK 52 | on success or another error on failure. 53 | .Em Note : 54 | this does not return the code of 55 | .Xr sqlite3_finalize 3 , 56 | which itself returns the code of the last step and not the failure of 57 | the resource de-allocation. 58 | .\" For sections 2, 3, and 9 function return values only. 59 | .\" .Sh ENVIRONMENT 60 | .\" For sections 1, 6, 7, and 8 only. 61 | .\" .Sh FILES 62 | .\" .Sh EXIT STATUS 63 | .\" For sections 1, 6, and 8 only. 64 | .\" .Sh EXAMPLES 65 | .\" .Sh DIAGNOSTICS 66 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 67 | .\" .Sh ERRORS 68 | .\" For sections 2, 3, 4, and 9 errno settings only. 69 | .Sh SEE ALSO 70 | .Xr sqlite3_finalize 3 71 | .\" .Xr foobar 1 72 | .\" .Sh STANDARDS 73 | .\" .Sh HISTORY 74 | .\" .Sh AUTHORS 75 | .\" .Sh CAVEATS 76 | .\" .Sh BUGS 77 | .\" .Sh SECURITY CONSIDERATIONS 78 | .\" Not used in OpenBSD. 79 | -------------------------------------------------------------------------------- /ksql_stmt_reset.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016--2017 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_STMT_RESET 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_stmt_reset 22 | .Nd resets a statement on a database connection 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft "enum ksqlc" 30 | .Fo ksql_stmt_reset 31 | .Fa "struct ksqlstmt *stmt" 32 | .Fc 33 | .Sh DESCRIPTION 34 | The 35 | .Nm 36 | function resets a statement allocated with 37 | .Xr ksql_stmt_alloc 3 . 38 | .Em Note : 39 | as documented in 40 | .Xr sqlite3_reset 3 , 41 | this does not unbind statements made with 42 | .Xr ksql_bind_double 3 43 | and friends. 44 | .\" .Sh CONTEXT 45 | .\" For section 9 functions only. 46 | .\" .Sh IMPLEMENTATION NOTES 47 | .\" Not used in OpenBSD. 48 | .Sh RETURN VALUES 49 | This function returns 50 | .Dv KSQL_OK 51 | on success or another error on failure. 52 | Currently, it can only fail due to communication failures in 53 | split-process mode. 54 | .Pp 55 | The return value of the underlying 56 | .Xr sqlite3_reset 3 57 | is ignored because resource deallocation cannot fail, and because 58 | .Xr sqlite3_reset 3 59 | instead returns the return value of the most recent step of the 60 | execution of the statement. 61 | Even if the last step of the execution failed, 62 | .Fn ksql_stmt_reset 63 | will usually succeed. 64 | .\" For sections 2, 3, and 9 function return values only. 65 | .\" .Sh ENVIRONMENT 66 | .\" For sections 1, 6, 7, and 8 only. 67 | .\" .Sh FILES 68 | .\" .Sh EXIT STATUS 69 | .\" For sections 1, 6, and 8 only. 70 | .\" .Sh EXAMPLES 71 | .\" .Sh DIAGNOSTICS 72 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 73 | .\" .Sh ERRORS 74 | .\" For sections 2, 3, 4, and 9 errno settings only. 75 | .Sh SEE ALSO 76 | .Xr sqlite3_reset 3 77 | .\" .Xr foobar 1 78 | .\" .Sh STANDARDS 79 | .\" .Sh HISTORY 80 | .\" .Sh AUTHORS 81 | .\" .Sh CAVEATS 82 | .\" .Sh BUGS 83 | .\" .Sh SECURITY CONSIDERATIONS 84 | .\" Not used in OpenBSD. 85 | -------------------------------------------------------------------------------- /ksql_stmt_step.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_STMT_STEP 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_stmt_cstep , 22 | .Nm ksql_stmt_step 23 | .Nd get the next row of results from a statement 24 | .Sh LIBRARY 25 | .Lb ksql 26 | .Sh SYNOPSIS 27 | .In sys/types.h 28 | .In stdint.h 29 | .In ksql.h 30 | .Ft enum ksqlc 31 | .Fo ksql_stmt_step 32 | .Fa "struct ksqlstmt *stmt" 33 | .Fc 34 | .Ft enum ksqlc 35 | .Fo ksql_stmt_cstep 36 | .Fa "struct ksqlstmt *stmt" 37 | .Fc 38 | .Sh DESCRIPTION 39 | The 40 | .Fn ksql_stmt_step 41 | and 42 | .Fn ksql_stmt_cstep 43 | functions query the next row of results from a statement prepared with 44 | .Xr ksql_stmt_alloc 3 . 45 | The two functions differ in that 46 | .Fn ksql_stmt_step 47 | fails when a constraint is violated, while 48 | .Fn ksql_stmt_cstep 49 | returns 50 | .Dv KSQL_CONSTRAINT 51 | instead. 52 | .Pp 53 | These functions handle a locked database (specifically, 54 | .Dv SQLITE_BUSY , 55 | .Dv SQLITE_LOCKED , 56 | or 57 | .Dv SQLITE_PROTOCOL ) 58 | by sleeping for a random interval, then trying again infinitely. 59 | .\" .Sh CONTEXT 60 | .\" For section 9 functions only. 61 | .\" .Sh IMPLEMENTATION NOTES 62 | .\" Not used in OpenBSD. 63 | .Sh RETURN VALUES 64 | These functions return several error conditions: 65 | .Dv KSQL_NOTOPEN 66 | if the database connection has not been opened; 67 | .Dv KSQL_DB 68 | on database errors, including constraint violations with 69 | .Fn ksql_stmt_step ; 70 | and 71 | .Dv KSQL_MEM 72 | on memory allocation failure. 73 | .Pp 74 | It also returns the following non-error conditions: 75 | .Dv KSQL_ROW 76 | if a row is available for examining, 77 | .Dv KSQL_DONE 78 | if no more rows are available, or 79 | .Dv KSQL_CONSTRAINT 80 | if 81 | .Fn ksql_stmt_cstep 82 | was used and a constraint was violated, 83 | .\" For sections 2, 3, and 9 function return values only. 84 | .\" .Sh ENVIRONMENT 85 | .\" For sections 1, 6, 7, and 8 only. 86 | .\" .Sh FILES 87 | .\" .Sh EXIT STATUS 88 | .\" For sections 1, 6, and 8 only. 89 | .\" .Sh EXAMPLES 90 | .\" .Sh DIAGNOSTICS 91 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 92 | .\" .Sh ERRORS 93 | .\" For sections 2, 3, 4, and 9 errno settings only. 94 | .Sh SEE ALSO 95 | .Xr sqlite3_step 3 96 | .\" .Xr foobar 1 97 | .\" .Sh STANDARDS 98 | .\" .Sh HISTORY 99 | .\" .Sh AUTHORS 100 | .\" .Sh CAVEATS 101 | .\" .Sh BUGS 102 | .\" .Sh SECURITY CONSIDERATIONS 103 | .\" Not used in OpenBSD. 104 | -------------------------------------------------------------------------------- /ksql_trace.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2017--2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_TRACE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_trace 22 | .Nd enable SQLite logging 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_trace 31 | .Fa "struct ksql *sql" 32 | .Fc 33 | .Sh DESCRIPTION 34 | This function causes SQLite to emit error and debug messages. 35 | Messages are passed to the error message handler set during 36 | .Xr ksql_alloc 3 37 | or 38 | .Xr ksql_alloc_child 3 . 39 | It may be disabled with 40 | .Xr ksql_untrace 3 . 41 | .Pp 42 | The 43 | .Fn ksql_trace 44 | function may only be called prior to 45 | .Xr ksql_open 3 . 46 | .\" .Sh CONTEXT 47 | .\" For section 9 functions only. 48 | .\" .Sh IMPLEMENTATION NOTES 49 | .\" Not used in OpenBSD. 50 | .Sh RETURN VALUES 51 | This returns 52 | .Dv KSQL_ALREADYOPEN 53 | if invoked after 54 | .Xr ksql_open 3 55 | or 56 | .Dv KSQL_SYSTEM 57 | on system error. 58 | Otherwise, it returns 59 | .Dv KSQL_OK . 60 | .\" For sections 2, 3, and 9 function return values only. 61 | .\" .Sh ENVIRONMENT 62 | .\" For sections 1, 6, 7, and 8 only. 63 | .\" .Sh FILES 64 | .\" .Sh EXIT STATUS 65 | .\" For sections 1, 6, and 8 only. 66 | .\" .Sh EXAMPLES 67 | .\" .Sh DIAGNOSTICS 68 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 69 | .\" .Sh ERRORS 70 | .\" For sections 2, 3, 4, and 9 errno settings only. 71 | .Sh SEE ALSO 72 | .Xr ksql_untrace 3 73 | .\" .Sh STANDARDS 74 | .\" .Sh HISTORY 75 | .\" .Sh AUTHORS 76 | .\" .Sh CAVEATS 77 | .\" .Sh BUGS 78 | .\" .Sh SECURITY CONSIDERATIONS 79 | .\" Not used in OpenBSD. 80 | -------------------------------------------------------------------------------- /ksql_trans_commit.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_TRANS_COMMIT 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_trans_commit , 22 | .Nm ksql_trans_rollback 23 | .Nd close a database transaction 24 | .Sh LIBRARY 25 | .Lb ksql 26 | .Sh SYNOPSIS 27 | .In sys/types.h 28 | .In stdint.h 29 | .In ksql.h 30 | .Ft enum ksqlc 31 | .Fo ksql_trans_commit 32 | .Fa "struct ksql *sql" 33 | .Fa "size_t id" 34 | .Fc 35 | .Ft enum ksqlc 36 | .Fo ksql_trans_rollback 37 | .Fa "struct ksql *sql" 38 | .Fa "size_t id" 39 | .Fc 40 | .Sh DESCRIPTION 41 | The 42 | .Nm ksql_trans_commit 43 | and 44 | .Nm ksql_trans_functions 45 | functions commit or roll-back active database transactions as opened with 46 | .Xr ksql_trans_open 3 . 47 | It is an error to try to close a transaction that has not been opened. 48 | This follows the same underlying execution logic of 49 | .Xr ksql_exec 3 . 50 | The 51 | .Fa id 52 | value is used in reporting errors and making sure that transaction calls 53 | are symmetric. 54 | .\" .Sh CONTEXT 55 | .\" For section 9 functions only. 56 | .\" .Sh IMPLEMENTATION NOTES 57 | .\" Not used in OpenBSD. 58 | .Sh RETURN VALUES 59 | This returns 60 | .Dv KSQL_NOTOPEN 61 | if the database connection is not open, 62 | .Dv KSQL_TRANS 63 | if a transaction is not yet open, 64 | .Dv KSQL_DB 65 | if errors occurred on the database, or 66 | .Dv KSQL_OK 67 | on success. 68 | .\" For sections 2, 3, and 9 function return values only. 69 | .\" .Sh ENVIRONMENT 70 | .\" For sections 1, 6, 7, and 8 only. 71 | .\" .Sh FILES 72 | .\" .Sh EXIT STATUS 73 | .\" For sections 1, 6, and 8 only. 74 | .\" .Sh EXAMPLES 75 | .\" .Sh DIAGNOSTICS 76 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 77 | .\" .Sh ERRORS 78 | .\" For sections 2, 3, 4, and 9 errno settings only. 79 | .Sh SEE ALSO 80 | .Xr sqlite3_exec 3 81 | .\" .Xr foobar 1 82 | .\" .Sh STANDARDS 83 | .\" .Sh HISTORY 84 | .\" .Sh AUTHORS 85 | .\" .Sh CAVEATS 86 | .\" .Sh BUGS 87 | .\" .Sh SECURITY CONSIDERATIONS 88 | .\" Not used in OpenBSD. 89 | -------------------------------------------------------------------------------- /ksql_trans_open.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2016--2017 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_TRANS_OPEN 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_trans_exclopen , 22 | .Nm ksql_trans_open , 23 | .Nm ksql_trans_singleopen 24 | .Nd open a database transaction 25 | .Sh LIBRARY 26 | .Lb ksql 27 | .Sh SYNOPSIS 28 | .In sys/types.h 29 | .In stdint.h 30 | .In ksql.h 31 | .Ft enum ksqlc 32 | .Fo ksql_trans_exclopen 33 | .Fa "struct ksql *sql" 34 | .Fa "size_t id" 35 | .Fc 36 | .Ft enum ksqlc 37 | .Fo ksql_trans_open 38 | .Fa "struct ksql *sql" 39 | .Fa "size_t id" 40 | .Fc 41 | .Ft enum ksqlc 42 | .Fo ksql_trans_singleopen 43 | .Fa "struct ksql *sql" 44 | .Fa "size_t id" 45 | .Fc 46 | .Sh DESCRIPTION 47 | The 48 | .Fn ksql_trans_exclopen , 49 | .Fn ksql_trans_open , 50 | and 51 | .Fn ksql_trans_singleopen 52 | functions open transactions with different lock states. 53 | All functions accept 54 | .Fa sql , 55 | the database connection; and 56 | .Fa id , 57 | an identifier used in error reporting and in making sure that 58 | transaction calls are symmetric. 59 | Each of these functions must be followed by 60 | .Xr ksql_trans_commit 3 61 | or 62 | .Xr ksql_trans_rollback 3 . 63 | .Pp 64 | If 65 | .Fn ksql_trans_open 66 | is used 67 | .Pq a Do DEFERRED Dc transaction, in SQLite terms , 68 | the database is locked only when the first database operation 69 | is invoked. 70 | The lock is read-shared (multiple readers, no writers) for a read 71 | operation and is upgraded to write-single (multiple readers, single 72 | writer) on a write. 73 | .Pp 74 | If 75 | .Fn ksql_trans_singleopen 76 | is used 77 | .Pq Dq IMMEDIATE , 78 | the database is immediately locked in write-single mode. 79 | .Pp 80 | If 81 | .Fn ksql_trans_exclopen 82 | .Pq Dq EXCLUSIVE , 83 | the database is opened in a write-exclusive lock, where only the calling 84 | process may write or read from the database. 85 | .Pp 86 | Recursive transactions are disallowed. 87 | .\" .Sh CONTEXT 88 | .\" For section 9 functions only. 89 | .\" .Sh IMPLEMENTATION NOTES 90 | .\" Not used in OpenBSD. 91 | .Sh RETURN VALUES 92 | This returns 93 | .Dv KSQL_NOTOPEN 94 | if the database connection is not open, 95 | .Dv KSQL_TRANS 96 | if a transaction is already open, 97 | .Dv KSQL_DB 98 | if errors occurred on the database, or 99 | .Dv KSQL_OK 100 | on success. 101 | .\" For sections 2, 3, and 9 function return values only. 102 | .\" .Sh ENVIRONMENT 103 | .\" For sections 1, 6, 7, and 8 only. 104 | .\" .Sh FILES 105 | .\" .Sh EXIT STATUS 106 | .\" For sections 1, 6, and 8 only. 107 | .\" .Sh EXAMPLES 108 | .\" .Sh DIAGNOSTICS 109 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 110 | .\" .Sh ERRORS 111 | .\" For sections 2, 3, 4, and 9 errno settings only. 112 | .Sh SEE ALSO 113 | .Xr sqlite3_exec 3 114 | .\" .Xr foobar 1 115 | .\" .Sh STANDARDS 116 | .\" .Sh HISTORY 117 | .\" .Sh AUTHORS 118 | .\" .Sh CAVEATS 119 | .\" .Sh BUGS 120 | .\" .Sh SECURITY CONSIDERATIONS 121 | .\" Not used in OpenBSD. 122 | -------------------------------------------------------------------------------- /ksql_untrace.3: -------------------------------------------------------------------------------- 1 | .\" $Id$ 2 | .\" 3 | .\" Copyright (c) 2017--2018 Kristaps Dzonsons 4 | .\" 5 | .\" Permission to use, copy, modify, and distribute this software for any 6 | .\" purpose with or without fee is hereby granted, provided that the above 7 | .\" copyright notice and this permission notice appear in all copies. 8 | .\" 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | .\" 17 | .Dd $Mdocdate$ 18 | .Dt KSQL_UNTRACE 3 19 | .Os 20 | .Sh NAME 21 | .Nm ksql_untrace 22 | .Nd disable SQLite logging 23 | .Sh LIBRARY 24 | .Lb ksql 25 | .Sh SYNOPSIS 26 | .In sys/types.h 27 | .In stdint.h 28 | .In ksql.h 29 | .Ft enum ksqlc 30 | .Fo ksql_untrace 31 | .Fa "struct ksql *sql" 32 | .Fc 33 | .Sh DESCRIPTION 34 | The 35 | .Nm 36 | function disables SQLite's internal error logging facility enabled by 37 | .Xr ksql_trace 3 . 38 | .Pp 39 | The 40 | .Fn ksql_untrace 41 | function may only be called prior to 42 | .Xr ksql_open 3 . 43 | .\" .Sh CONTEXT 44 | .\" For section 9 functions only. 45 | .\" .Sh IMPLEMENTATION NOTES 46 | .\" Not used in OpenBSD. 47 | .Sh RETURN VALUES 48 | This returns 49 | .Dv KSQL_ALREADYOPEN 50 | if invoked after 51 | .Xr ksql_open 3 52 | or 53 | .Dv KSQL_SYSTEM 54 | on system error. 55 | Otherwise, it returns 56 | .Dv KSQL_OK . 57 | .\" For sections 2, 3, and 9 function return values only. 58 | .\" .Sh ENVIRONMENT 59 | .\" For sections 1, 6, 7, and 8 only. 60 | .\" .Sh FILES 61 | .\" .Sh EXIT STATUS 62 | .\" For sections 1, 6, and 8 only. 63 | .\" .Sh EXAMPLES 64 | .\" .Sh DIAGNOSTICS 65 | .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. 66 | .\" .Sh ERRORS 67 | .\" For sections 2, 3, 4, and 9 errno settings only. 68 | .Sh SEE ALSO 69 | .Xr ksql_trace 3 70 | .\" .Sh STANDARDS 71 | .\" .Sh HISTORY 72 | .\" .Sh AUTHORS 73 | .\" .Sh CAVEATS 74 | .\" .Sh BUGS 75 | .\" .Sh SECURITY CONSIDERATIONS 76 | .\" Not used in OpenBSD. 77 | -------------------------------------------------------------------------------- /log.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #if HAVE_SYS_QUEUE 20 | # include 21 | #endif 22 | 23 | #include 24 | #if HAVE_ERR 25 | # include 26 | #endif 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "ksql.h" 34 | #include "extern.h" 35 | 36 | /* 37 | * Hard-coded string name for errors. 38 | * Those without a value aren't errors, just conditions. 39 | */ 40 | static const char * const ksqlcs[] = { 41 | NULL, /* KSQL_OK */ 42 | NULL, /* KSQL_DONE */ 43 | NULL, /* KSQL_ROW */ 44 | NULL, /* KSQL_CONSTRAINT */ 45 | "memory exhausted", /* KSQL_MEM */ 46 | "database not open", /* KSQL_NOTOPEN */ 47 | "database already open", /* KSQL_ALREADYOPEN */ 48 | "database error", /* KSQL_DB */ 49 | "transaction already open or not yet open", /* KSQL_TRANS */ 50 | "statement(s) open on exit", /* KSQL_STMT */ 51 | "closing on exit", /* KSQL_EXIT */ 52 | "system error", /* KSQL_SYSTEM */ 53 | NULL, /* KSQL_EOF */ 54 | "security violation", /* KSQL_SECURITY */ 55 | "invalid bind column index", /* KSQL_BINDCOL */ 56 | "invalid result column index", /* KSQL_RESULTCOL */ 57 | "NULL value when requesting result", /* KSQL_NULL */ 58 | "request for results without any rows", /* KSQL_NORESULTS */ 59 | }; 60 | 61 | /* 62 | * If there's an error function, use it to print "msg". 63 | * Don't do anything else (i.e., don't exit). 64 | * This can be used to simply print errors and messages. 65 | * Does nothing if there's no error handler. 66 | */ 67 | void 68 | ksql_err_noexit(struct ksql *p, enum ksqlc erc, const char *msg) 69 | { 70 | 71 | if (NULL == msg) 72 | msg = ksqlcs[erc]; 73 | assert(NULL != msg); 74 | if (NULL != p->cfg.err) 75 | p->cfg.err(p->cfg.arg, erc, p->dbfile, msg); 76 | } 77 | 78 | /* 79 | * See ksql_dberr(). 80 | */ 81 | enum ksqlc 82 | ksql_err(struct ksql *p, enum ksqlc erc, const char *msg) 83 | { 84 | 85 | ksql_err_noexit(p, erc, msg); 86 | if (KSQL_EXIT_ON_ERR & p->cfg.flags) 87 | exit(EXIT_FAILURE); 88 | return(erc); 89 | } 90 | 91 | /* 92 | * This pass-through to ksql_err() accepts variable parameters. 93 | * They must resolve to less than 1024 characters before truncation. 94 | */ 95 | enum ksqlc 96 | ksql_verr(struct ksql *p, enum ksqlc erc, const char *fmt, ...) 97 | { 98 | va_list ap; 99 | char buf[1024]; 100 | 101 | va_start(ap, fmt); 102 | vsnprintf(buf, sizeof(buf), fmt, ap); 103 | va_end(ap); 104 | return ksql_err(p, erc, buf); 105 | } 106 | 107 | /* 108 | * Pass a database error from SQLite to the error printing function. 109 | * If no database error has occurred, this will print something 110 | * harmless. 111 | * Does nothing if there's no database-error handler. 112 | */ 113 | void 114 | ksql_dberr_noexit(struct ksql *p) 115 | { 116 | 117 | if (NULL == p->cfg.dberr) 118 | return; 119 | p->cfg.dberr(p->cfg.arg, 120 | sqlite3_errcode(p->db), 121 | sqlite3_extended_errcode(p->db), 122 | p->dbfile, sqlite3_errmsg(p->db)); 123 | } 124 | 125 | /* 126 | * Pass an error to the error handler, if found. 127 | * Then if we're exiting on errors, do it here. 128 | */ 129 | enum ksqlc 130 | ksql_dberr(struct ksql *p) 131 | { 132 | 133 | ksql_dberr_noexit(p); 134 | if (KSQL_EXIT_ON_ERR & p->cfg.flags) 135 | exit(EXIT_FAILURE); 136 | return(KSQL_DB); 137 | } 138 | 139 | void 140 | ksqlitedbmsg(void *arg, int sql3, 141 | int esql3, const char *file, const char *msg) 142 | { 143 | 144 | (void)arg; 145 | if (NULL != file) 146 | warnx("%s: %s (error code %d, extended %d)", 147 | file, msg, sql3, esql3); 148 | else 149 | warnx("%s (error code %d, extended %d)", 150 | msg, sql3, esql3); 151 | } 152 | 153 | void 154 | ksqlitemsg(void *arg, enum ksqlc code, 155 | const char *file, const char *msg) 156 | { 157 | 158 | (void)arg; 159 | if (NULL != file) 160 | warnx("%s: %s (error code %d)", file, msg, code); 161 | else 162 | warnx("%s (error code %d)", msg, code); 163 | } 164 | 165 | /* 166 | * Like ksqlitemsg() but accepting variable arguments. 167 | * Internally this will invoke vsnprintf(), which will truncate the 168 | * message to 1023 Bytes. 169 | * MAKE SURE YOUR MESSAGES ARE BOUND IN SIZE. 170 | */ 171 | void 172 | ksqlitevmsg(const struct ksql *p, 173 | enum ksqlc code, const char *fmt, ...) 174 | { 175 | va_list ap; 176 | char msg[1024]; 177 | 178 | va_start(ap, fmt); 179 | vsnprintf(msg, sizeof(msg), fmt, ap); 180 | va_end(ap); 181 | 182 | if (NULL != p->dbfile) 183 | warnx("%s: %s (error code %d)", p->dbfile, msg, code); 184 | else 185 | warnx("%s (error code %d)", msg, code); 186 | } 187 | 188 | -------------------------------------------------------------------------------- /mandoc.css: -------------------------------------------------------------------------------- 1 | html { width: 80%; 2 | max-width: 900px; 3 | margin-left: auto; 4 | margin-right: auto; } 5 | body { font-size: 12pt; 6 | font-family: Calibri, Candara, Segoe, Segoe UI, Optima, Arial, sans-serif; } 7 | h1 { font-size: 15pt; 8 | color: #135; 9 | font-family: Georgia, Times, Times New Roman, serif; 10 | font-weight: 300; } 11 | h2 { font-size: 14pt; 12 | font-family: Georgia, Times, Times New Roman, serif; 13 | font-weight: 200; } 14 | div.section > h1 { } /* Section header (Sh, SH). */ 15 | table { width: 100%; 16 | margin-top: 0ex; 17 | margin-bottom: 0ex; } /* All tables. */ 18 | td { vertical-align: top; } /* All table cells. */ 19 | p { } /* Paragraph: Pp, Lp. */ 20 | blockquote { margin-left: 5ex; margin-top: 0ex; margin-bottom: 0ex; } /* D1. */ 21 | div.section { margin-bottom: 20pt; } /* Sections (Sh, SH). */ 22 | div.subsection { } /* Sub-sections (Ss, SS). */ 23 | table.synopsis { } /* SYNOPSIS section table. */ 24 | div.spacer { margin: 1em 0; } 25 | code, pre { font-size: 11pt; } 26 | 27 | /* Preamble structure. */ 28 | 29 | table.foot { border-spacing: 0; 30 | color: #68a; 31 | font-variant: small-caps; 32 | margin-top: 14pt; 33 | border-top: thin solid #eee; } /* Document footer. */ 34 | td.foot-date { width: 50%; } /* Document footer: date. */ 35 | td.foot-os { width: 50%; } /* Document footer: OS/source. */ 36 | table.head { border-spacing: 0; 37 | color: #68a; 38 | font-variant: small-caps; 39 | margin-bottom: 14pt; 40 | padding-bottom: 4pt; 41 | border-bottom: thin solid #dee; } /* Document header. */ 42 | td.head-ltitle { width: 10%; } /* Document header: left-title. */ 43 | td.head-vol { width: 80%; } /* Document header: volume. */ 44 | td.head-rtitle { width: 10%; } /* Document header: right-title. */ 45 | 46 | /* General font modes. */ 47 | 48 | i { } /* Italic: BI, IB, I, (implicit). */ 49 | .emph { font-style: italic; font-weight: normal; } /* Emphasis: Em, Bl -emphasis. */ 50 | b { font-weight: 600; } /* Bold: SB, BI, IB, BR, RB, B, (implicit). */ 51 | .symb { font-style: normal; font-weight: bold; } /* Symbolic: Sy, Ms, Bf -symbolic. */ 52 | small { } /* Small: SB, SM. */ 53 | .lit { font-style: normal; font-weight: normal; font-family: monospace; } /* Literal: Dl, Li, Ql, Bf -literal, Bl -literal, Bl -unfilled. */ 54 | 55 | /* Block modes. */ 56 | 57 | .display { } /* Top of all Bd, D1, Dl. */ 58 | .list { } /* Top of all Bl. */ 59 | 60 | /* Context-specific modes. */ 61 | 62 | i.addr { font-weight: normal; } /* Address (Ad). */ 63 | i.arg { font-weight: normal; } /* Command argument (Ar). */ 64 | span.author { } /* Author name (An). */ 65 | b.cmd { font-style: normal; } /* Command (Cm). */ 66 | b.config { font-style: normal; } /* Config statement (Cd). */ 67 | span.define { } /* Defines (Dv). */ 68 | span.desc { } /* Nd. After em-dash. */ 69 | b.diag { font-style: normal; } /* Diagnostic (Bl -diag). */ 70 | span.env { } /* Environment variables (Ev). */ 71 | span.errno { } /* Error string (Er). */ 72 | i.farg { font-weight: normal; } /* Function argument (Fa, Fn). */ 73 | i.file { font-weight: normal; } /* File (Pa). */ 74 | b.flag { font-style: normal; } /* Flag (Fl, Cm). */ 75 | b.fname { font-style: normal; } /* Function name (Fa, Fn, Rv). */ 76 | i.ftype { font-weight: normal; } /* Function types (Ft, Fn). */ 77 | b.includes { font-style: normal; } /* Header includes (In). */ 78 | span.lib { } /* Library (Lb). */ 79 | i.link-sec { font-weight: normal; } /* Section links (Sx). */ 80 | b.macro { font-style: normal; } /* Macro-ish thing (Fd). */ 81 | b.name { font-style: normal; } /* Name of utility (Nm). */ 82 | span.opt { } /* Options (Op, Oo/Oc). */ 83 | span.ref { } /* Citations (Rs). */ 84 | span.ref-auth { } /* Reference author (%A). */ 85 | i.ref-book { font-weight: normal; } /* Reference book (%B). */ 86 | span.ref-city { } /* Reference city (%C). */ 87 | span.ref-date { } /* Reference date (%D). */ 88 | i.ref-issue { font-weight: normal; } /* Reference issuer/publisher (%I). */ 89 | i.ref-jrnl { font-weight: normal; } /* Reference journal (%J). */ 90 | span.ref-num { } /* Reference number (%N). */ 91 | span.ref-opt { } /* Reference optionals (%O). */ 92 | span.ref-page { } /* Reference page (%P). */ 93 | span.ref-corp { } /* Reference corporate/foreign author (%Q). */ 94 | span.ref-rep { } /* Reference report (%R). */ 95 | span.ref-title { text-decoration: underline; } /* Reference title (%T). */ 96 | span.ref-vol { } /* Reference volume (%V). */ 97 | span.type { font-style: italic; font-weight: normal; } /* Variable types (Vt). */ 98 | span.unix { } /* Unices (Ux, Ox, Nx, Fx, Bx, Bsx, Dx). */ 99 | b.utility { font-style: normal; } /* Name of utility (Ex). */ 100 | b.var { font-style: normal; } /* Variables (Rv). */ 101 | 102 | a { text-decoration: none; } 103 | a.link-ext { } /* Off-site link (Lk). */ 104 | a.link-includes { } /* Include-file link (In). */ 105 | a.link-mail { } /* Mailto links (Mt). */ 106 | a.link-man { } /* Manual links (Xr). */ 107 | a.link-ref { } /* Reference section links (%Q). */ 108 | a.link-sec { } /* Section links (Sx). */ 109 | 110 | /* Formatting for lists. See mdoc(7). */ 111 | 112 | dl.list-diag { } 113 | dt.list-diag { } 114 | dd.list-diag { } 115 | 116 | dl.list-hang { } 117 | dt.list-hang { } 118 | dd.list-hang { } 119 | 120 | dl.list-inset { } 121 | dt.list-inset { } 122 | dd.list-inset { } 123 | 124 | dl.list-ohang { } 125 | dt.list-ohang { } 126 | dd.list-ohang { margin-left: 0ex; } 127 | 128 | dl.list-tag { } 129 | dt.list-tag { } 130 | dd.list-tag { } 131 | 132 | table.list-col { } 133 | tr.list-col { } 134 | td.list-col { } 135 | 136 | ul.list-bul { list-style-type: disc; padding-left: 1em; } 137 | li.list-bul { } 138 | 139 | ul.list-dash { list-style-type: none; padding-left: 0em; } 140 | li.list-dash:before { content: "\2014 "; } 141 | 142 | ul.list-hyph { list-style-type: none; padding-left: 0em; } 143 | li.list-hyph:before { content: "\2013 "; } 144 | 145 | ul.list-item { list-style-type: none; padding-left: 0em; } 146 | li.list-item { } 147 | 148 | ol.list-enum { padding-left: 2em; } 149 | li.list-enum { } 150 | 151 | /* Equation modes. See eqn(7). */ 152 | 153 | span.eqn { } 154 | 155 | /* Table modes. See tbl(7). */ 156 | 157 | table.tbl { } 158 | 159 | @media only screen and (max-width: 768px), only screen and (max-device-width: 768px) { 160 | html { width: 98%; } 161 | } 162 | -------------------------------------------------------------------------------- /manpage.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | ${sblg-base} 13 | 14 | 15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /notugly.xsl: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | font-size:10px; font-family:sans-serif; 9 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | fill- 219 | 220 | 221 | stop-color:;stop-opacity:1 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | shadow 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 391 | 393 | 395 | black 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | fill: black; stroke: none; fill-opacity:0.3 527 | translate(3,3) 528 | 529 | 530 | 531 | 532 | 533 | 534 | fill: none; stroke: black; stroke-opacity:0.3 535 | translate(3,3) 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 545 | 547 | 549 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 562 | 564 | 566 | black 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | fill: url(#); 593 | 594 | 595 | fill: none; 596 | 597 | 598 | stroke: ; 599 | stroke: ; 600 | stroke: black; 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | fill: url(#fill-); 609 | fill: url(#); 610 | 611 | 612 | 613 | 614 | -------------------------------------------------------------------------------- /result.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #if HAVE_SYS_QUEUE 20 | # include 21 | #endif 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | #include "ksql.h" 30 | #include "extern.h" 31 | 32 | static int 33 | ksql_result_isfatal(enum ksqlc c) 34 | { 35 | 36 | return KSQL_OK != c; 37 | } 38 | 39 | /* 40 | * Make sure the result at column "col" is accessible to the current 41 | * statement. 42 | * Returns KSQL_OK if it is or an error otherwise. 43 | */ 44 | static enum ksqlc 45 | ksql_result_check(struct ksqlstmt *stmt, size_t col) 46 | { 47 | 48 | if ( ! stmt->hasrow) 49 | return ksql_verr(stmt->sql, KSQL_NORESULTS, 50 | "result index %zu without any rows", col); 51 | if (col >= stmt->rcols) 52 | return ksql_verr(stmt->sql, KSQL_RESULTCOL, 53 | "result index %zu exceeds maximum " 54 | "index %zu", col, stmt->rcols - 1); 55 | if (SQLITE_NULL == sqlite3_column_type(stmt->stmt, col)) 56 | return ksql_verr(stmt->sql, KSQL_NULL, 57 | "result requested for null column %zu", col); 58 | 59 | return KSQL_OK; 60 | } 61 | 62 | /* 63 | * Write the full message required for a ksql_result_xxx function when in 64 | * the parent of a parent-child daemon scenario. 65 | * This checks the return code of the child *before* trying to read the 66 | * result, so we never pass a bogus result from the child to the parent. 67 | */ 68 | static enum ksqlc 69 | ksql_readres(struct ksqlstmt *stmt, enum ksqlop op, 70 | size_t col, void *buf, size_t bufsz) 71 | { 72 | enum ksqlc c, code; 73 | 74 | assert(KSQLSRV_ISPARENT(stmt->sql)); 75 | 76 | if (KSQL_OK != (c = ksql_writeop(stmt->sql, op)) || 77 | KSQL_OK != (c = ksql_writeptr(stmt->sql, stmt->ptr)) || 78 | KSQL_OK != (c = ksql_writesz(stmt->sql, col)) || 79 | KSQL_OK != (c = ksql_readcode(stmt->sql, &code))) 80 | return c; 81 | if (ksql_result_isfatal(code)) 82 | return code; 83 | if (KSQL_OK != (c = ksql_readbuf(stmt->sql, buf, bufsz, 0))) 84 | return c; 85 | 86 | return code; 87 | } 88 | 89 | enum ksqlc 90 | ksqlsrv_result_blob(struct ksql *p) 91 | { 92 | enum ksqlc c, cc; 93 | struct ksqlstmt *stmt; 94 | size_t col, sz = 0; 95 | const void *val = NULL; 96 | 97 | if (KSQL_OK != (c = ksql_readptr(p, &stmt)) || 98 | KSQL_OK != (c = ksql_readsz(p, &col))) 99 | return c; 100 | 101 | if (KSQL_OK == (c = ksql_result_check(stmt, col))) { 102 | sz = sqlite3_column_bytes(stmt->stmt, col); 103 | val = sqlite3_column_blob(stmt->stmt, col); 104 | } 105 | 106 | if (KSQL_OK != (cc = ksql_writecode(p, c))) 107 | return cc; 108 | else if (ksql_result_isfatal(c)) 109 | return c; 110 | 111 | /* Check code *before* writing result. */ 112 | if (KSQL_OK != (c = ksql_writebuf(p, &sz, sizeof(size_t)))) 113 | return c; 114 | return ksql_writebuf(p, val, sz); 115 | } 116 | 117 | enum ksqlc 118 | ksqlsrv_result_isnull(struct ksql *p) 119 | { 120 | enum ksqlc c, cc; 121 | struct ksqlstmt *stmt; 122 | size_t col; 123 | int val; 124 | 125 | if (KSQL_OK != (c = ksql_readptr(p, &stmt)) || 126 | KSQL_OK != (c = ksql_readsz(p, &col))) 127 | return c; 128 | c = ksql_result_isnull(stmt, &val, col); 129 | if (KSQL_OK != (cc = ksql_writecode(p, c))) 130 | return cc; 131 | else if (ksql_result_isfatal(c)) 132 | return c; 133 | 134 | /* Check code *before* writing result. */ 135 | return ksql_writebuf(p, &val, sizeof(int)); 136 | } 137 | 138 | enum ksqlc 139 | ksqlsrv_result_bytes(struct ksql *p) 140 | { 141 | enum ksqlc c, cc; 142 | struct ksqlstmt *stmt; 143 | size_t col, val; 144 | 145 | if (KSQL_OK != (c = ksql_readptr(p, &stmt)) || 146 | KSQL_OK != (c = ksql_readsz(p, &col))) 147 | return c; 148 | c = ksql_result_bytes(stmt, &val, col); 149 | if (KSQL_OK != (cc = ksql_writecode(p, c))) 150 | return cc; 151 | else if (ksql_result_isfatal(c)) 152 | return c; 153 | 154 | /* Check code *before* writing result. */ 155 | return ksql_writebuf(p, &val, sizeof(size_t)); 156 | } 157 | 158 | enum ksqlc 159 | ksqlsrv_result_double(struct ksql *p) 160 | { 161 | enum ksqlc c, cc; 162 | struct ksqlstmt *stmt; 163 | size_t col; 164 | double val; 165 | 166 | if (KSQL_OK != (c = ksql_readptr(p, &stmt)) || 167 | KSQL_OK != (c = ksql_readsz(p, &col))) 168 | return c; 169 | c = ksql_result_double(stmt, &val, col); 170 | if (KSQL_OK != (cc = ksql_writecode(p, c))) 171 | return cc; 172 | else if (ksql_result_isfatal(c)) 173 | return c; 174 | 175 | /* Check code *before* writing result. */ 176 | return ksql_writebuf(p, &val, sizeof(double)); 177 | } 178 | 179 | enum ksqlc 180 | ksqlsrv_result_int(struct ksql *p) 181 | { 182 | enum ksqlc c, cc; 183 | struct ksqlstmt *stmt; 184 | size_t col; 185 | int64_t val; 186 | 187 | if (KSQL_OK != (c = ksql_readptr(p, &stmt)) || 188 | KSQL_OK != (c = ksql_readsz(p, &col))) 189 | return c; 190 | c = ksql_result_int(stmt, &val, col); 191 | if (KSQL_OK != (cc = ksql_writecode(p, c))) 192 | return cc; 193 | else if (ksql_result_isfatal(c)) 194 | return c; 195 | 196 | /* Check code *before* writing result. */ 197 | return ksql_writebuf(p, &val, sizeof(int64_t)); 198 | } 199 | 200 | enum ksqlc 201 | ksqlsrv_result_str(struct ksql *p) 202 | { 203 | enum ksqlc c, cc; 204 | struct ksqlstmt *stmt; 205 | size_t col; 206 | const char *val = NULL; 207 | 208 | if (KSQL_OK != (c = ksql_readptr(p, &stmt)) || 209 | KSQL_OK != (c = ksql_readsz(p, &col))) 210 | return c; 211 | 212 | if (KSQL_OK == (c = ksql_result_check(stmt, col))) { 213 | val = (const char *)sqlite3_column_text(stmt->stmt, col); 214 | if (NULL == val) 215 | c = ksql_err(stmt->sql, KSQL_MEM, NULL); 216 | } 217 | 218 | if (KSQL_OK != (cc = ksql_writecode(p, c))) 219 | return cc; 220 | else if (ksql_result_isfatal(c)) 221 | return c; 222 | 223 | /* Check code *before* writing result. */ 224 | 225 | assert(NULL != val); 226 | return ksql_writestr(p, val); 227 | } 228 | 229 | enum ksqlc 230 | ksql_result_isnull(struct ksqlstmt *stmt, int *p, size_t col) 231 | { 232 | 233 | *p = 0; 234 | 235 | if (KSQLSRV_ISPARENT(stmt->sql)) 236 | return ksql_readres(stmt, KSQLOP_RESULT_ISNULL, 237 | col, p, sizeof(int)); 238 | 239 | /* 240 | * Do this differently from the others by not performing the 241 | * SQLITE_NULL check. 242 | * Eventually, the ksql_result_check() function will need to be 243 | * modified so that it doesn't perform that one check for this 244 | * function, else we'll end up duplicating code. 245 | */ 246 | 247 | if (col >= stmt->rcols) 248 | return ksql_verr(stmt->sql, KSQL_RESULTCOL, 249 | "result index %zu exceeds maximum " 250 | "index %zu", col, stmt->rcols - 1); 251 | 252 | if (SQLITE_NULL == sqlite3_column_type(stmt->stmt, col)) 253 | *p = 1; 254 | 255 | return KSQL_OK; 256 | } 257 | 258 | enum ksqlc 259 | ksql_result_bytes(struct ksqlstmt *stmt, size_t *p, size_t col) 260 | { 261 | enum ksqlc c; 262 | 263 | *p = 0; 264 | 265 | if (KSQLSRV_ISPARENT(stmt->sql)) 266 | return ksql_readres(stmt, KSQLOP_RESULT_BYTES, 267 | col, p, sizeof(size_t)); 268 | if (KSQL_OK == (c = ksql_result_check(stmt, col))) 269 | *p = sqlite3_column_bytes(stmt->stmt, col); 270 | 271 | return c; 272 | } 273 | 274 | enum ksqlc 275 | ksql_result_double(struct ksqlstmt *stmt, double *p, size_t col) 276 | { 277 | enum ksqlc c; 278 | 279 | *p = 0.0; 280 | 281 | if (KSQLSRV_ISPARENT(stmt->sql)) 282 | return ksql_readres(stmt, KSQLOP_RESULT_DOUBLE, 283 | col, p, sizeof(double)); 284 | if (KSQL_OK == (c = ksql_result_check(stmt, col))) 285 | *p = sqlite3_column_double(stmt->stmt, col); 286 | 287 | return c; 288 | } 289 | 290 | enum ksqlc 291 | ksql_result_int(struct ksqlstmt *stmt, int64_t *p, size_t col) 292 | { 293 | enum ksqlc c; 294 | 295 | *p = 0; 296 | 297 | if (KSQLSRV_ISPARENT(stmt->sql)) 298 | return ksql_readres(stmt, KSQLOP_RESULT_INT, 299 | col, p, sizeof(int64_t)); 300 | if (KSQL_OK == (c = ksql_result_check(stmt, col))) 301 | *p = sqlite3_column_int64(stmt->stmt, col); 302 | 303 | return c; 304 | } 305 | 306 | enum ksqlc 307 | ksql_result_str(struct ksqlstmt *stmt, const char **p, size_t col) 308 | { 309 | char *cp = NULL; 310 | size_t sz; 311 | struct kcache *cache; 312 | enum ksqlc c; 313 | 314 | *p = NULL; 315 | 316 | assert(KSQLSRV_ISPARENT(stmt->sql)); 317 | 318 | c = ksql_readres 319 | (stmt, KSQLOP_RESULT_STR, 320 | col, &sz, sizeof(size_t)); 321 | if (KSQL_OK != c) 322 | return c; 323 | 324 | if (NULL == (cp = malloc(sz + 1))) 325 | return ksql_err(stmt->sql, KSQL_MEM, NULL); 326 | cp[sz] = '\0'; 327 | 328 | if (KSQL_OK != (c = ksql_readbuf(stmt->sql, cp, sz, 0))) { 329 | free(cp); 330 | return c; 331 | } 332 | 333 | if (NULL == (cache = calloc(1, sizeof(struct kcache)))) { 334 | free(cp); 335 | return ksql_err(stmt->sql, KSQL_MEM, NULL); 336 | } 337 | 338 | TAILQ_INSERT_TAIL(&stmt->cache, cache, entries); 339 | *p = cache->s = cp; 340 | return KSQL_OK; 341 | } 342 | 343 | enum ksqlc 344 | ksql_result_str_alloc(struct ksqlstmt *stmt, char **p, size_t col) 345 | { 346 | char *cp = NULL; 347 | size_t sz; 348 | enum ksqlc c; 349 | 350 | *p = NULL; 351 | 352 | assert(KSQLSRV_ISPARENT(stmt->sql)); 353 | 354 | c = ksql_readres 355 | (stmt, KSQLOP_RESULT_STR, 356 | col, &sz, sizeof(size_t)); 357 | if (KSQL_OK != c) 358 | return c; 359 | 360 | if (NULL == (cp = malloc(sz + 1))) 361 | return ksql_err(stmt->sql, KSQL_MEM, NULL); 362 | cp[sz] = '\0'; 363 | 364 | if (KSQL_OK != (c = ksql_readbuf(stmt->sql, cp, sz, 0))) { 365 | free(cp); 366 | return c; 367 | } 368 | 369 | *p = cp; 370 | return KSQL_OK; 371 | } 372 | 373 | enum ksqlc 374 | ksql_result_blob(struct ksqlstmt *stmt, 375 | const void **p, size_t *sz, size_t col) 376 | { 377 | struct kcache *cache; 378 | void *cp = NULL; 379 | enum ksqlc c; 380 | size_t rsz; 381 | 382 | *p = NULL; 383 | *sz = 0; 384 | 385 | assert(KSQLSRV_ISPARENT(stmt->sql)); 386 | 387 | c = ksql_readres 388 | (stmt, KSQLOP_RESULT_BLOB, 389 | col, &rsz, sizeof(size_t)); 390 | if (KSQL_OK != c) 391 | return c; 392 | 393 | /* Don't allocate for zero-sized buffers. */ 394 | 395 | if (0 == rsz) 396 | return KSQL_OK; 397 | if (NULL == (cp = malloc(rsz))) 398 | return ksql_err(stmt->sql, KSQL_MEM, NULL); 399 | 400 | if (KSQL_OK != (c = ksql_readbuf(stmt->sql, cp, rsz, 0))) { 401 | free(cp); 402 | return c; 403 | } 404 | 405 | if (NULL == (cache = calloc(1, sizeof(struct kcache)))) { 406 | free(cp); 407 | return ksql_err(stmt->sql, KSQL_MEM, NULL); 408 | } 409 | 410 | TAILQ_INSERT_TAIL(&stmt->cache, cache, entries); 411 | *p = cache->s = cp; 412 | *sz = rsz; 413 | return KSQL_OK; 414 | } 415 | 416 | enum ksqlc 417 | ksql_result_blob_alloc(struct ksqlstmt *stmt, 418 | void **p, size_t *sz, size_t col) 419 | { 420 | enum ksqlc c; 421 | void *cp; 422 | size_t rsz; 423 | 424 | *p = NULL; 425 | *sz = 0; 426 | 427 | assert(KSQLSRV_ISPARENT(stmt->sql)); 428 | 429 | c = ksql_readres 430 | (stmt, KSQLOP_RESULT_BLOB, 431 | col, &rsz, sizeof(size_t)); 432 | if (KSQL_OK != c) 433 | return c; 434 | 435 | /* Don't allocate for zero-sized buffers. */ 436 | 437 | if (0 == rsz) 438 | return KSQL_OK; 439 | if (NULL == (cp = malloc(rsz))) 440 | return ksql_err(stmt->sql, KSQL_MEM, NULL); 441 | 442 | if (KSQL_OK != (c = ksql_readbuf(stmt->sql, cp, rsz, 0))) { 443 | free(cp); 444 | return c; 445 | } 446 | 447 | *sz = rsz; 448 | *p = cp; 449 | return KSQL_OK; 450 | } 451 | -------------------------------------------------------------------------------- /stmt.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #if HAVE_SYS_QUEUE 20 | # include 21 | #endif 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | #include "ksql.h" 30 | #include "extern.h" 31 | 32 | /* 33 | * Write the full message required for a ksql_stmt_xxx function when in 34 | * the parent of a parent-child daemon scenario. 35 | * Returns zero on failure, non-zero on success. 36 | * (We don't need to return the codes because the SQLite functions don't 37 | * as well.) 38 | */ 39 | static int 40 | ksql_writecol(struct ksqlstmt *stmt, enum ksqlop op, 41 | size_t col, void *buf, size_t bufsz) 42 | { 43 | 44 | assert(KSQLSRV_ISPARENT(stmt->sql)); 45 | 46 | if (KSQL_OK != ksql_writeop(stmt->sql, op)) 47 | return(0); 48 | if (KSQL_OK != ksql_writeptr(stmt->sql, stmt->ptr)) 49 | return(0); 50 | if (KSQL_OK != ksql_writesz(stmt->sql, col)) 51 | return(0); 52 | if (KSQL_OK != ksql_readbuf(stmt->sql, buf, bufsz, 0)) 53 | return(0); 54 | 55 | return(1); 56 | } 57 | 58 | enum ksqlc 59 | ksqlsrv_stmt_bytes(struct ksql *p) 60 | { 61 | enum ksqlc c; 62 | struct ksqlstmt *stmt; 63 | size_t col; 64 | size_t val; 65 | 66 | if (KSQL_OK != (c = ksql_readptr(p, &stmt))) 67 | return(c); 68 | if (KSQL_OK != (c = ksql_readsz(p, &col))) 69 | return(c); 70 | val = ksql_stmt_bytes(stmt, col); 71 | return(ksql_writebuf(p, &val, sizeof(size_t))); 72 | } 73 | 74 | enum ksqlc 75 | ksqlsrv_stmt_double(struct ksql *p) 76 | { 77 | enum ksqlc c; 78 | struct ksqlstmt *stmt; 79 | size_t col; 80 | double val; 81 | 82 | if (KSQL_OK != (c = ksql_readptr(p, &stmt))) 83 | return(c); 84 | if (KSQL_OK != (c = ksql_readsz(p, &col))) 85 | return(c); 86 | val = ksql_stmt_double(stmt, col); 87 | return(ksql_writebuf(p, &val, sizeof(double))); 88 | } 89 | 90 | enum ksqlc 91 | ksqlsrv_stmt_isnull(struct ksql *p) 92 | { 93 | enum ksqlc c; 94 | struct ksqlstmt *stmt; 95 | size_t col; 96 | int val; 97 | 98 | if (KSQL_OK != (c = ksql_readptr(p, &stmt))) 99 | return(c); 100 | if (KSQL_OK != (c = ksql_readsz(p, &col))) 101 | return(c); 102 | val = ksql_stmt_isnull(stmt, col); 103 | return(ksql_writebuf(p, &val, sizeof(int))); 104 | } 105 | 106 | enum ksqlc 107 | ksqlsrv_stmt_int(struct ksql *p) 108 | { 109 | enum ksqlc c; 110 | struct ksqlstmt *stmt; 111 | size_t col; 112 | int64_t val; 113 | 114 | if (KSQL_OK != (c = ksql_readptr(p, &stmt))) 115 | return(c); 116 | if (KSQL_OK != (c = ksql_readsz(p, &col))) 117 | return(c); 118 | val = ksql_stmt_int(stmt, col); 119 | return(ksql_writebuf(p, &val, sizeof(int64_t))); 120 | } 121 | 122 | enum ksqlc 123 | ksqlsrv_stmt_blob(struct ksql *p) 124 | { 125 | enum ksqlc c; 126 | struct ksqlstmt *stmt; 127 | size_t col, sz; 128 | const char *val; 129 | 130 | if (KSQL_OK != (c = ksql_readptr(p, &stmt))) 131 | return(c); 132 | if (KSQL_OK != (c = ksql_readsz(p, &col))) 133 | return(c); 134 | 135 | /* Get both the size and pointer first. */ 136 | 137 | sz = ksql_stmt_bytes(stmt, col); 138 | val = ksql_stmt_str(stmt, col); 139 | 140 | /* 141 | * If val is NULL, SQLite couldn't allocate OR the size of the 142 | * buffer was zero. 143 | * We want to handle both conditions, so construe the number of 144 | * bytes as zero either way and don't transmit. 145 | */ 146 | 147 | if (NULL == val) 148 | sz = 0; 149 | if (KSQL_OK != (c = ksql_writesz(p, sz))) 150 | return(c); 151 | if (0 != sz) 152 | c = ksql_writebuf(p, val, sz); 153 | return(c); 154 | } 155 | 156 | enum ksqlc 157 | ksqlsrv_stmt_str(struct ksql *p) 158 | { 159 | enum ksqlc c; 160 | struct ksqlstmt *stmt; 161 | size_t col, sz; 162 | const char *val; 163 | 164 | if (KSQL_OK != (c = ksql_readptr(p, &stmt))) 165 | return(c); 166 | if (KSQL_OK != (c = ksql_readsz(p, &col))) 167 | return(c); 168 | 169 | /* 170 | * SQLite returns NULL on allocation failure. 171 | * So we do something special here. 172 | * Send the string *buffer* length, which is always non-zero to 173 | * account for the nil terminator. 174 | * If that's zero, then we know that we have a NULL. 175 | * If that's one, then it's a zero-length string. 176 | */ 177 | 178 | val = ksql_stmt_str(stmt, col); 179 | sz = NULL == val ? 0 : strlen(val) + 1; 180 | 181 | if (KSQL_OK != (c = ksql_writesz(p, sz))) 182 | return(c); 183 | if (sz > 1) 184 | c = ksql_writebuf(p, val, sz - 1); 185 | return(c); 186 | } 187 | 188 | const void * 189 | ksql_stmt_blob(struct ksqlstmt *stmt, size_t col) 190 | { 191 | size_t sz; 192 | char *cp; 193 | struct kcache *c; 194 | 195 | if ( ! KSQLSRV_ISPARENT(stmt->sql)) 196 | return(sqlite3_column_blob(stmt->stmt, col)); 197 | 198 | if (KSQL_OK != ksql_writeop(stmt->sql, KSQLOP_COL_BLOB)) 199 | return(NULL); 200 | if (KSQL_OK != ksql_writeptr(stmt->sql, stmt->ptr)) 201 | return(NULL); 202 | if (KSQL_OK != ksql_writesz(stmt->sql, col)) 203 | return(NULL); 204 | 205 | /* 206 | * Note: ksql_stmt_blob doesn't return the byte size. 207 | * We do so in ksqlsrv_stmt_blob instead. 208 | * If the byte size is zero, then SQLite had a memory failure or 209 | * is returning nothing in that space. 210 | */ 211 | 212 | if (KSQL_OK != ksql_readsz(stmt->sql, &sz)) 213 | return(NULL); 214 | if (0 == sz) 215 | return(NULL); 216 | 217 | if (NULL == (cp = malloc(sz))) { 218 | ksql_err(stmt->sql, KSQL_MEM, NULL); 219 | return(NULL); 220 | } 221 | 222 | if (KSQL_OK != ksql_readbuf(stmt->sql, cp, sz, 0)) { 223 | free(cp); 224 | return(NULL); 225 | } 226 | 227 | /* Put into our pointer cache. */ 228 | 229 | if (NULL == (c = calloc(1, sizeof(struct kcache)))) { 230 | ksql_err(stmt->sql, KSQL_MEM, NULL); 231 | free(cp); 232 | return(NULL); 233 | } 234 | TAILQ_INSERT_TAIL(&stmt->cache, c, entries); 235 | c->s = cp; 236 | return(c->s); 237 | } 238 | 239 | size_t 240 | ksql_stmt_bytes(struct ksqlstmt *stmt, size_t col) 241 | { 242 | size_t val; 243 | 244 | if ( ! KSQLSRV_ISPARENT(stmt->sql)) 245 | return(sqlite3_column_bytes(stmt->stmt, col)); 246 | return(ksql_writecol(stmt, KSQLOP_COL_BYTES, 247 | col, &val, sizeof(size_t)) ? val : 0); 248 | } 249 | 250 | double 251 | ksql_stmt_double(struct ksqlstmt *stmt, size_t col) 252 | { 253 | double val; 254 | 255 | if ( ! KSQLSRV_ISPARENT(stmt->sql)) 256 | return(sqlite3_column_double(stmt->stmt, col)); 257 | return(ksql_writecol(stmt, KSQLOP_COL_DOUBLE, 258 | col, &val, sizeof(double)) ? val : 0.0); 259 | } 260 | 261 | int 262 | ksql_stmt_isnull(struct ksqlstmt *stmt, size_t col) 263 | { 264 | int val; 265 | 266 | if ( ! KSQLSRV_ISPARENT(stmt->sql)) 267 | return(SQLITE_NULL == 268 | sqlite3_column_type(stmt->stmt, col)); 269 | return(ksql_writecol(stmt, KSQLOP_COL_ISNULL, 270 | col, &val, sizeof(int)) ? val : 0); 271 | } 272 | 273 | int64_t 274 | ksql_stmt_int(struct ksqlstmt *stmt, size_t col) 275 | { 276 | int64_t val; 277 | 278 | if ( ! KSQLSRV_ISPARENT(stmt->sql)) 279 | return(sqlite3_column_int64(stmt->stmt, col)); 280 | return(ksql_writecol(stmt, KSQLOP_COL_INT, 281 | col, &val, sizeof(int64_t)) ? val : 0); 282 | } 283 | 284 | const char * 285 | ksql_stmt_str(struct ksqlstmt *stmt, size_t col) 286 | { 287 | char *cp; 288 | size_t sz; 289 | struct kcache *c; 290 | 291 | if ( ! KSQLSRV_ISPARENT(stmt->sql)) 292 | return((char *)sqlite3_column_text(stmt->stmt, col)); 293 | 294 | /* 295 | * This is a little tricky because we need to handle 296 | * out-of-memory conditions for ourselves, our child, and SQLite 297 | * itself. 298 | */ 299 | 300 | if (KSQL_OK != ksql_writeop(stmt->sql, KSQLOP_COL_STR)) 301 | return(NULL); 302 | if (KSQL_OK != ksql_writeptr(stmt->sql, stmt->ptr)) 303 | return(NULL); 304 | if (KSQL_OK != ksql_writesz(stmt->sql, col)) 305 | return(NULL); 306 | 307 | /* 308 | * If we get a zero-sized buffer, that means that SQLite 309 | * couldn't allocate for the string and returned NULL. 310 | */ 311 | 312 | if (KSQL_OK != ksql_readsz(stmt->sql, &sz)) 313 | return(NULL); 314 | if (0 == sz) 315 | return(NULL); 316 | 317 | /* Allocate and nil-terminate, then fill. */ 318 | 319 | if (NULL == (cp = malloc(sz))) { 320 | ksql_err(stmt->sql, KSQL_MEM, NULL); 321 | return(NULL); 322 | } 323 | cp[sz - 1] = '\0'; 324 | 325 | /* 326 | * If we have a one-length string, it's empty so don't pass into 327 | * the readbuf function (it will assert). 328 | */ 329 | 330 | if (sz > 1 && 331 | KSQL_OK != ksql_readbuf(stmt->sql, cp, sz - 1, 0)) { 332 | free(cp); 333 | return(NULL); 334 | } 335 | 336 | /* Put into our pointer cache. */ 337 | 338 | if (NULL == (c = calloc(1, sizeof(struct kcache)))) { 339 | ksql_err(stmt->sql, KSQL_MEM, NULL); 340 | free(cp); 341 | return(NULL); 342 | } 343 | TAILQ_INSERT_TAIL(&stmt->cache, c, entries); 344 | c->s = cp; 345 | return(c->s); 346 | } 347 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2017--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #include 20 | 21 | #include 22 | #if HAVE_ERR 23 | # include 24 | #endif 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "ksql.h" 32 | 33 | static const size_t insertsz = 50; 34 | 35 | static const char *const stmts[] = { 36 | "INSERT INTO test (foo,bar,baz,xyzzy) VALUES (?,?,?,?)", 37 | "SELECT foo,bar,baz,xyzzy,id FROM test", 38 | "DELETE FROM test", 39 | }; 40 | 41 | /* 42 | * This is not a regression test. 43 | * It's just a playground to test functionality as I develop it. 44 | */ 45 | 46 | static void 47 | insert(struct ksql *sql, struct ksqlstmt *stmt, size_t *index, int *nulls) 48 | { 49 | uint32_t val; 50 | char buf[64]; 51 | int64_t id; 52 | 53 | #if HAVE_ARC4RANDOM 54 | val = arc4random(); 55 | #else 56 | val = random(); 57 | #endif 58 | snprintf(buf, sizeof(buf), "%" PRIu32, val); 59 | 60 | if (0 == (val % 2)) { 61 | nulls[*index] = 0; 62 | if (KSQL_OK != ksql_bind_int(stmt, 0, val)) 63 | errx(EXIT_FAILURE, "ksql_bind_int"); 64 | if (KSQL_OK != ksql_bind_str(stmt, 1, buf)) 65 | errx(EXIT_FAILURE, "ksql_bind_str"); 66 | if (KSQL_OK != ksql_bind_blob 67 | (stmt, 2, buf, strlen(buf) + 1)) 68 | errx(EXIT_FAILURE, "ksql_bind_str"); 69 | if (KSQL_OK != ksql_bind_double(stmt, 3, val * 0.5)) 70 | errx(EXIT_FAILURE, "ksql_bind_double"); 71 | } else { 72 | nulls[*index] = 1; 73 | if (KSQL_OK != ksql_bind_null(stmt, 0)) 74 | errx(EXIT_FAILURE, "ksql_bind_null"); 75 | if (KSQL_OK != ksql_bind_null(stmt, 1)) 76 | errx(EXIT_FAILURE, "ksql_bind_null"); 77 | if (KSQL_OK != ksql_bind_zblob 78 | (stmt, 2, strlen(buf) + 1)) 79 | errx(EXIT_FAILURE, "ksql_bind_str"); 80 | if (KSQL_OK != ksql_bind_null(stmt, 3)) 81 | errx(EXIT_FAILURE, "ksql_bind_null"); 82 | } 83 | if (KSQL_DONE != ksql_stmt_step(stmt)) 84 | errx(EXIT_FAILURE, "ksql_stmt_step"); 85 | if (KSQL_OK != ksql_lastid(sql, &id)) 86 | errx(EXIT_FAILURE, "ksql_lastid"); 87 | if (KSQL_OK != ksql_stmt_reset(stmt)) 88 | errx(EXIT_FAILURE, "ksql_stmt_reset"); 89 | 90 | (*index)++; 91 | } 92 | 93 | int 94 | main(void) 95 | { 96 | struct ksql *sql; 97 | struct ksqlcfg cfg; 98 | struct ksqlstmt *stmt; 99 | int nulls[insertsz * 2], isnull; 100 | const char *valstr, *valstr2; 101 | size_t i, inserted = 0, valblobsz, valblobsz2; 102 | const void *valblob, *valblob2; 103 | double valdouble, valdouble2; 104 | int64_t valint, valint2; 105 | const int stmts0[] = { 1, 1, 0 }; 106 | const int stmts1[] = { 1, 1, 0 }; 107 | const int stmts2[] = { 1, 1, 0 }; 108 | const int stmts3[] = { 0, 0, 1 }; 109 | const int roles0[] = { 1, 0, 0, 0 }; /* To noone. */ 110 | const int roles1[] = { 1, 1, 0, 0 }; /* Only to root. */ 111 | const int roles2[] = { 0, 1, 1, 0 }; /* Only to above. */ 112 | const int roles3[] = { 1, 1, 1, 1 }; /* To all. */ 113 | struct ksqlrole roles[4] = { 114 | { roles0, stmts0, 0 }, 115 | { roles1, stmts1, 0 }, 116 | { roles2, stmts2, 0 }, 117 | { roles3, stmts3, 1 }, 118 | }; 119 | 120 | #if HAVE_PLEDGE 121 | if (-1 == pledge("stdio rpath cpath wpath flock fattr proc", NULL)) 122 | err(EXIT_FAILURE, "pledge"); 123 | #endif 124 | #if ! HAVE_ARC4RANDOM 125 | srandom(getpid()); 126 | #endif 127 | memset(&cfg, 0, sizeof(struct ksqlcfg)); 128 | cfg.flags = KSQL_EXIT_ON_ERR | KSQL_SAFE_EXIT; 129 | cfg.err = ksqlitemsg; 130 | cfg.dberr = ksqlitedbmsg; 131 | cfg.stmts.stmts = stmts; 132 | cfg.stmts.stmtsz = 3; 133 | cfg.roles.roles = roles; 134 | cfg.roles.rolesz = 4; 135 | cfg.roles.defrole = 3; 136 | 137 | if (NULL == (sql = ksql_alloc_child(&cfg, NULL, NULL))) 138 | errx(EXIT_FAILURE, "ksql_alloc_child"); 139 | #if HAVE_PLEDGE 140 | if (-1 == pledge("stdio", NULL)) 141 | err(EXIT_FAILURE, "pledge"); 142 | #endif 143 | if (KSQL_OK != ksql_open(sql, "test.db")) 144 | errx(EXIT_FAILURE, "ksql_open"); 145 | 146 | if (KSQL_OK != ksql_exec(sql, NULL, 2)) 147 | errx(EXIT_FAILURE, "ksql_exec"); 148 | 149 | /* Play around with roles. */ 150 | 151 | ksql_role(sql, 2); 152 | ksql_role(sql, 1); 153 | 154 | /* 155 | * Perform a whole bunch of insertions. 156 | * Some will be NULL (randomly), others won't. 157 | * We record which are which in "nulls". 158 | */ 159 | 160 | if (KSQL_OK != ksql_trans_open(sql, 0)) 161 | errx(EXIT_FAILURE, "ksql_trans_open"); 162 | if (KSQL_OK != ksql_stmt_alloc(sql, &stmt, NULL, 0)) 163 | errx(EXIT_FAILURE, "ksql_stmt_alloc"); 164 | for (i = 0; i < insertsz; i++) 165 | insert(sql, stmt, &inserted, nulls); 166 | if (KSQL_OK != ksql_stmt_free(stmt)) 167 | errx(EXIT_FAILURE, "ksql_stmt_free"); 168 | if (KSQL_OK != ksql_trans_commit(sql, 0)) 169 | errx(EXIT_FAILURE, "ksql_trans_open"); 170 | 171 | /* Now do the same thing, but without a transaction. */ 172 | 173 | if (KSQL_OK != ksql_stmt_alloc(sql, &stmt, NULL, 0)) 174 | errx(EXIT_FAILURE, "ksql_stmt_alloc"); 175 | for (i = 0; i < insertsz; i++) 176 | insert(sql, stmt, &inserted, nulls); 177 | if (KSQL_OK != ksql_stmt_free(stmt)) 178 | errx(EXIT_FAILURE, "ksql_stmt_free"); 179 | 180 | /* Now we're going to get data out. */ 181 | 182 | if (KSQL_OK != ksql_stmt_alloc(sql, &stmt, NULL, 1)) 183 | errx(EXIT_FAILURE, "ksql_stmt_alloc"); 184 | 185 | i = 0; 186 | while (KSQL_ROW == ksql_stmt_step(stmt)) { 187 | valint = ksql_stmt_int(stmt, 0); 188 | valstr = ksql_stmt_str(stmt, 1); 189 | valblob = ksql_stmt_blob(stmt, 2); 190 | valblobsz = ksql_stmt_bytes(stmt, 2); 191 | valdouble = ksql_stmt_double(stmt, 3); 192 | 193 | printf("Step (%zu:1): %" PRId64 "\n", i, valint); 194 | printf("Step (%zu:2): %s\n", i, 195 | NULL == valstr ? "(null)" : valstr); 196 | if (NULL == valblob) 197 | printf("Step (%zu:3): null\n", i); 198 | else 199 | printf("Step (%zu:3): [%.*s] (%zu)\n", i, 200 | (int)valblobsz, 201 | (const char *)valblob, valblobsz); 202 | printf("Step (%zu:4): %f\n", i, valdouble); 203 | printf("Step (%zu): expecting null: %d\n", i, nulls[i]); 204 | 205 | assert(i < insertsz * 2); 206 | 207 | if (nulls[i++]) 208 | continue; 209 | 210 | if (KSQL_OK != ksql_result_isnull(stmt, &isnull, 0)) 211 | errx(EXIT_FAILURE, "ksql_result_isnull"); 212 | if (isnull) 213 | errx(EXIT_FAILURE, "ksql_result_isnull wrong"); 214 | if (KSQL_OK != ksql_result_isnull(stmt, &isnull, 1)) 215 | errx(EXIT_FAILURE, "ksql_result_isnull"); 216 | if (isnull) 217 | errx(EXIT_FAILURE, "ksql_result_isnull wrong"); 218 | if (KSQL_OK != ksql_result_isnull(stmt, &isnull, 3)) 219 | errx(EXIT_FAILURE, "ksql_result_isnull"); 220 | if (isnull) 221 | errx(EXIT_FAILURE, "ksql_result_isnull wrong"); 222 | 223 | if (KSQL_OK != ksql_result_int(stmt, &valint2, 0)) 224 | errx(EXIT_FAILURE, "ksql_result_int"); 225 | if (valint != valint2) 226 | errx(EXIT_FAILURE, "ksql_result_int inequality"); 227 | if (KSQL_OK != ksql_result_str(stmt, &valstr2, 1)) 228 | errx(EXIT_FAILURE, "ksql_result_str"); 229 | if ((NULL == valstr && NULL != valstr2) || 230 | (NULL != valstr && NULL == valstr2) || 231 | (NULL != valstr && strcmp(valstr, valstr2))) 232 | errx(EXIT_FAILURE, "ksql_result_str inequality"); 233 | if (KSQL_OK != ksql_result_blob(stmt, &valblob2, &valblobsz2, 2)) 234 | errx(EXIT_FAILURE, "ksql_result_blob"); 235 | if ((NULL == valblob && NULL != valblob2) || 236 | (NULL != valblob && NULL == valblob2) || 237 | (valblobsz != valblobsz2) || 238 | (NULL != valblob && memcmp(valblob, valblob2, valblobsz2))) 239 | errx(EXIT_FAILURE, "ksql_result_blob inequality"); 240 | if (KSQL_OK != ksql_result_double(stmt, &valdouble2, 3)) 241 | errx(EXIT_FAILURE, "ksql_result_double"); 242 | if (valdouble != valdouble2) 243 | errx(EXIT_FAILURE, "ksql_result_double inequality"); 244 | } 245 | 246 | if (KSQL_OK != ksql_stmt_free(stmt)) 247 | errx(EXIT_FAILURE, "ksql_stmt_free"); 248 | if (KSQL_OK != ksql_close(sql)) 249 | errx(EXIT_FAILURE, "ksql_close"); 250 | 251 | ksql_free(sql); 252 | return EXIT_SUCCESS; 253 | } 254 | -------------------------------------------------------------------------------- /test.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE test ( 2 | foo INTEGER, 3 | bar TEXT, 4 | baz BLOB, 5 | xyzzy DOUBLE, 6 | id INTEGER PRIMARY KEY 7 | ); 8 | -------------------------------------------------------------------------------- /tests.c: -------------------------------------------------------------------------------- 1 | #if TEST___PROGNAME 2 | int 3 | main(void) 4 | { 5 | extern char *__progname; 6 | 7 | return !__progname; 8 | } 9 | #endif /* TEST___PROGNAME */ 10 | #if TEST_ARC4RANDOM 11 | #include 12 | 13 | int 14 | main(void) 15 | { 16 | return (arc4random() + 1) ? 0 : 1; 17 | } 18 | #endif /* TEST_ARC4RANDOM */ 19 | #if TEST_B64_NTOP 20 | #include 21 | #include 22 | 23 | int 24 | main(void) 25 | { 26 | const char *src = "hello world"; 27 | char output[1024]; 28 | 29 | return b64_ntop((const unsigned char *)src, 11, output, sizeof(output)) > 0 ? 0 : 1; 30 | } 31 | #endif /* TEST_B64_NTOP */ 32 | #if TEST_CAPSICUM 33 | #include 34 | 35 | int 36 | main(void) 37 | { 38 | cap_enter(); 39 | return(0); 40 | } 41 | #endif /* TEST_CAPSICUM */ 42 | #if TEST_ERR 43 | /* 44 | * Copyright (c) 2015 Ingo Schwarze 45 | * 46 | * Permission to use, copy, modify, and distribute this software for any 47 | * purpose with or without fee is hereby granted, provided that the above 48 | * copyright notice and this permission notice appear in all copies. 49 | * 50 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 51 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 52 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 53 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 54 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 55 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 56 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 57 | */ 58 | 59 | #include 60 | 61 | int 62 | main(void) 63 | { 64 | warnx("%d. warnx", 1); 65 | warn("%d. warn", 2); 66 | err(0, "%d. err", 3); 67 | /* NOTREACHED */ 68 | return 1; 69 | } 70 | #endif /* TEST_ERR */ 71 | #if TEST_EXPLICIT_BZERO 72 | #include 73 | 74 | int 75 | main(void) 76 | { 77 | char foo[10]; 78 | 79 | explicit_bzero(foo, sizeof(foo)); 80 | return(0); 81 | } 82 | #endif /* TEST_EXPLICIT_BZERO */ 83 | #if TEST_GETPROGNAME 84 | #include 85 | 86 | int 87 | main(void) 88 | { 89 | const char * progname; 90 | 91 | progname = getprogname(); 92 | return progname == NULL; 93 | } 94 | #endif /* TEST_GETPROGNAME */ 95 | #if TEST_INFTIM 96 | /* 97 | * Linux doesn't (always?) have this. 98 | */ 99 | 100 | #include 101 | #include 102 | 103 | int 104 | main(void) 105 | { 106 | printf("INFTIM is defined to be %ld\n", (long)INFTIM); 107 | return 0; 108 | } 109 | #endif /* TEST_INFTIM */ 110 | #if TEST_MD5 111 | #include 112 | #include 113 | 114 | int main(void) 115 | { 116 | MD5_CTX ctx; 117 | 118 | MD5Init(&ctx); 119 | MD5Update(&ctx, "abcd", 4); 120 | 121 | return 0; 122 | } 123 | #endif /* TEST_MD5 */ 124 | #if TEST_MEMMEM 125 | #define _GNU_SOURCE 126 | #include 127 | 128 | int 129 | main(void) 130 | { 131 | char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world")); 132 | return(NULL == a); 133 | } 134 | #endif /* TEST_MEMMEM */ 135 | #if TEST_MEMRCHR 136 | #if defined(__linux__) || defined(__MINT__) 137 | #define _GNU_SOURCE /* See test-*.c what needs this. */ 138 | #endif 139 | #include 140 | 141 | int 142 | main(void) 143 | { 144 | const char *buf = "abcdef"; 145 | void *res; 146 | 147 | res = memrchr(buf, 'a', strlen(buf)); 148 | return(NULL == res ? 1 : 0); 149 | } 150 | #endif /* TEST_MEMRCHR */ 151 | #if TEST_MEMSET_S 152 | #include 153 | 154 | int main(void) 155 | { 156 | char buf[10]; 157 | memset_s(buf, 0, 'c', sizeof(buf)); 158 | return 0; 159 | } 160 | #endif /* TEST_MEMSET_S */ 161 | #if TEST_PATH_MAX 162 | /* 163 | * POSIX allows PATH_MAX to not be defined, see 164 | * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html; 165 | * the GNU Hurd is an example of a system not having it. 166 | * 167 | * Arguably, it would be better to test sysconf(_SC_PATH_MAX), 168 | * but since the individual *.c files include "config.h" before 169 | * , overriding an excessive value of PATH_MAX from 170 | * "config.h" is impossible anyway, so for now, the simplest 171 | * fix is to provide a value only on systems not having any. 172 | * So far, we encountered no system defining PATH_MAX to an 173 | * impractically large value, even though POSIX explicitly 174 | * allows that. 175 | * 176 | * The real fix would be to replace all static buffers of size 177 | * PATH_MAX by dynamically allocated buffers. But that is 178 | * somewhat intrusive because it touches several files and 179 | * because it requires changing struct mlink in mandocdb.c. 180 | * So i'm postponing that for now. 181 | */ 182 | 183 | #include 184 | #include 185 | 186 | int 187 | main(void) 188 | { 189 | printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX); 190 | return 0; 191 | } 192 | #endif /* TEST_PATH_MAX */ 193 | #if TEST_PLEDGE 194 | #include 195 | 196 | int 197 | main(void) 198 | { 199 | return !!pledge("stdio", NULL); 200 | } 201 | #endif /* TEST_PLEDGE */ 202 | #if TEST_PROGRAM_INVOCATION_SHORT_NAME 203 | #define _GNU_SOURCE /* See feature_test_macros(7) */ 204 | #include 205 | 206 | int 207 | main(void) 208 | { 209 | 210 | return !program_invocation_short_name; 211 | } 212 | #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */ 213 | #if TEST_REALLOCARRAY 214 | #include 215 | 216 | int 217 | main(void) 218 | { 219 | return !reallocarray(NULL, 2, 2); 220 | } 221 | #endif /* TEST_REALLOCARRAY */ 222 | #if TEST_RECALLOCARRAY 223 | #include 224 | 225 | int 226 | main(void) 227 | { 228 | return !recallocarray(NULL, 0, 2, 2); 229 | } 230 | #endif /* TEST_RECALLOCARRAY */ 231 | #if TEST_SANDBOX_INIT 232 | #include 233 | 234 | int 235 | main(void) 236 | { 237 | char *ep; 238 | int rc; 239 | 240 | rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep); 241 | if (-1 == rc) 242 | sandbox_free_error(ep); 243 | return(-1 == rc); 244 | } 245 | #endif /* TEST_SANDBOX_INIT */ 246 | #if TEST_SECCOMP_FILTER 247 | #include 248 | #include 249 | #include 250 | 251 | int 252 | main(void) 253 | { 254 | 255 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0); 256 | return(EFAULT == errno ? 0 : 1); 257 | } 258 | #endif /* TEST_SECCOMP_FILTER */ 259 | #if TEST_SOCK_NONBLOCK 260 | /* 261 | * Linux doesn't (always?) have this. 262 | */ 263 | 264 | #include 265 | 266 | int 267 | main(void) 268 | { 269 | int fd[2]; 270 | socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd); 271 | return 0; 272 | } 273 | #endif /* TEST_SOCK_NONBLOCK */ 274 | #if TEST_STRLCAT 275 | #include 276 | 277 | int 278 | main(void) 279 | { 280 | char buf[3] = "a"; 281 | return ! (strlcat(buf, "b", sizeof(buf)) == 2 && 282 | buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0'); 283 | } 284 | #endif /* TEST_STRLCAT */ 285 | #if TEST_STRLCPY 286 | #include 287 | 288 | int 289 | main(void) 290 | { 291 | char buf[2] = ""; 292 | return ! (strlcpy(buf, "a", sizeof(buf)) == 1 && 293 | buf[0] == 'a' && buf[1] == '\0'); 294 | } 295 | #endif /* TEST_STRLCPY */ 296 | #if TEST_STRNDUP 297 | #include 298 | 299 | int 300 | main(void) 301 | { 302 | const char *foo = "bar"; 303 | char *baz; 304 | 305 | baz = strndup(foo, 1); 306 | return(0 != strcmp(baz, "b")); 307 | } 308 | #endif /* TEST_STRNDUP */ 309 | #if TEST_STRNLEN 310 | #include 311 | 312 | int 313 | main(void) 314 | { 315 | const char *foo = "bar"; 316 | size_t sz; 317 | 318 | sz = strnlen(foo, 1); 319 | return(1 != sz); 320 | } 321 | #endif /* TEST_STRNLEN */ 322 | #if TEST_STRTONUM 323 | /* 324 | * Copyright (c) 2015 Ingo Schwarze 325 | * 326 | * Permission to use, copy, modify, and distribute this software for any 327 | * purpose with or without fee is hereby granted, provided that the above 328 | * copyright notice and this permission notice appear in all copies. 329 | * 330 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 331 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 332 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 333 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 334 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 335 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 336 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 337 | */ 338 | 339 | #include 340 | 341 | int 342 | main(void) 343 | { 344 | const char *errstr; 345 | 346 | if (strtonum("1", 0, 2, &errstr) != 1) 347 | return 1; 348 | if (errstr != NULL) 349 | return 2; 350 | if (strtonum("1x", 0, 2, &errstr) != 0) 351 | return 3; 352 | if (errstr == NULL) 353 | return 4; 354 | if (strtonum("2", 0, 1, &errstr) != 0) 355 | return 5; 356 | if (errstr == NULL) 357 | return 6; 358 | if (strtonum("0", 1, 2, &errstr) != 0) 359 | return 7; 360 | if (errstr == NULL) 361 | return 8; 362 | return 0; 363 | } 364 | #endif /* TEST_STRTONUM */ 365 | #if TEST_SYS_QUEUE 366 | #include 367 | #include 368 | 369 | struct foo { 370 | int bar; 371 | TAILQ_ENTRY(foo) entries; 372 | }; 373 | 374 | TAILQ_HEAD(fooq, foo); 375 | 376 | int 377 | main(void) 378 | { 379 | struct fooq foo_q; 380 | 381 | TAILQ_INIT(&foo_q); 382 | return 0; 383 | } 384 | #endif /* TEST_SYS_QUEUE */ 385 | #if TEST_SYSTRACE 386 | #include 387 | #include 388 | 389 | #include 390 | 391 | int 392 | main(void) 393 | { 394 | 395 | return(0); 396 | } 397 | #endif /* TEST_SYSTRACE */ 398 | #if TEST_ZLIB 399 | #include 400 | #include 401 | 402 | int 403 | main(void) 404 | { 405 | gzFile gz; 406 | 407 | if (NULL == (gz = gzopen("/dev/null", "w"))) 408 | return(1); 409 | gzputs(gz, "foo"); 410 | gzclose(gz); 411 | return(0); 412 | } 413 | #endif /* TEST_ZLIB */ 414 | -------------------------------------------------------------------------------- /trace.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #if HAVE_SYS_QUEUE 20 | # include 21 | #endif 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include "ksql.h" 29 | #include "extern.h" 30 | 31 | /* 32 | * Used by SQLite when tracing events. 33 | * Passes right through to ksql_err_noexit(). 34 | */ 35 | static void 36 | ksql_tracemsg(void *pArg, int iErrCode, const char *zMsg) 37 | { 38 | struct ksql *p = pArg; 39 | 40 | (void)iErrCode; 41 | 42 | ksql_err_noexit(p, KSQL_OK, zMsg); 43 | } 44 | 45 | enum ksqlc 46 | ksql_trace(struct ksql *p) 47 | { 48 | enum ksqlc c, cc; 49 | int rc; 50 | 51 | if (KSQLSRV_ISPARENT(p)) { 52 | ksql_writeop(p, KSQLOP_TRACE); 53 | if (KSQL_OK != (c = ksql_readcode(p, &cc))) 54 | return(c); 55 | return(cc); 56 | } 57 | 58 | rc = sqlite3_config 59 | (SQLITE_CONFIG_LOG, ksql_tracemsg, p); 60 | 61 | if (SQLITE_MISUSE == rc) 62 | cc = KSQL_ALREADYOPEN; 63 | else if (SQLITE_OK != rc) 64 | cc = KSQL_SYSTEM; 65 | else 66 | cc = KSQL_OK; 67 | 68 | return(ksql_writecode(p, cc)); 69 | } 70 | 71 | enum ksqlc 72 | ksql_untrace(struct ksql *p) 73 | { 74 | enum ksqlc c, cc; 75 | int rc; 76 | 77 | if (KSQLSRV_ISPARENT(p)) { 78 | ksql_writeop(p, KSQLOP_UNTRACE); 79 | if (KSQL_OK != (c = ksql_readcode(p, &cc))) 80 | return(c); 81 | return(cc); 82 | } 83 | 84 | rc = sqlite3_config 85 | (SQLITE_CONFIG_LOG, NULL, NULL); 86 | 87 | if (SQLITE_MISUSE == rc) 88 | cc = KSQL_ALREADYOPEN; 89 | else if (SQLITE_OK != rc) 90 | cc = KSQL_SYSTEM; 91 | else 92 | cc = KSQL_OK; 93 | 94 | return(ksql_writecode(p, cc)); 95 | } 96 | -------------------------------------------------------------------------------- /trans.c: -------------------------------------------------------------------------------- 1 | /* $Id$ */ 2 | /* 3 | * Copyright (c) 2016--2018 Kristaps Dzonsons 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | #include "config.h" 18 | 19 | #if HAVE_SYS_QUEUE 20 | # include 21 | #endif 22 | 23 | #include 24 | #if HAVE_ERR 25 | # include 26 | #endif 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "ksql.h" 34 | #include "extern.h" 35 | 36 | static enum ksqlc 37 | ksql_trans_close_inner(struct ksql *p, size_t mode, size_t id) 38 | { 39 | enum ksqlc c, cc; 40 | char buf[1024]; 41 | 42 | if (KSQLSRV_ISPARENT(p)) { 43 | c = ksql_writeop(p, KSQLOP_TRANS_CLOSE); 44 | if (KSQL_OK != c) 45 | return c; 46 | if (KSQL_OK != (c = ksql_writesz(p, mode))) 47 | return c; 48 | if (KSQL_OK != (c = ksql_writesz(p, id))) 49 | return c; 50 | if (KSQL_OK != (c = ksql_readcode(p, &cc))) 51 | return c; 52 | return cc; 53 | } 54 | 55 | if (NULL == p->db) 56 | return ksql_err(p, KSQL_NOTOPEN, NULL); 57 | 58 | if ( ! (KSQLFL_TRANS & p->flags)) { 59 | snprintf(buf, sizeof(buf), 60 | "transaction %zu not open", id); 61 | return ksql_err(p, KSQL_TRANS, buf); 62 | } else if (id != p->trans) { 63 | snprintf(buf, sizeof(buf), 64 | "transaction %zu pending on close of %zu", 65 | p->trans, id); 66 | return ksql_err(p, KSQL_TRANS, buf); 67 | } 68 | 69 | c = mode ? 70 | ksql_exec_private(p, "ROLLBACK TRANSACTION") : 71 | ksql_exec_private(p, "COMMIT TRANSACTION"); 72 | 73 | /* Set this only if the exec succeeded.*/ 74 | 75 | if (KSQL_OK == c) 76 | p->flags &= ~KSQLFL_TRANS; 77 | 78 | return c; 79 | } 80 | 81 | static enum ksqlc 82 | ksql_trans_open_inner(struct ksql *p, size_t mode, size_t id) 83 | { 84 | enum ksqlc c, cc; 85 | char buf[1024]; 86 | 87 | if (KSQLSRV_ISPARENT(p)) { 88 | c = ksql_writeop(p, KSQLOP_TRANS_OPEN); 89 | if (KSQL_OK != c) 90 | return c; 91 | if (KSQL_OK != (c = ksql_writesz(p, mode))) 92 | return c; 93 | if (KSQL_OK != (c = ksql_writesz(p, id))) 94 | return c; 95 | if (KSQL_OK != (c = ksql_readcode(p, &cc))) 96 | return c; 97 | return cc; 98 | } 99 | 100 | if (NULL == p->db) 101 | return ksql_err(p, KSQL_NOTOPEN, NULL); 102 | 103 | if (KSQLFL_TRANS & p->flags) { 104 | snprintf(buf, sizeof(buf), 105 | "transaction %zu still open", p->trans); 106 | return ksql_err(p, KSQL_TRANS, buf); 107 | } 108 | 109 | assert(mode <= 2); 110 | 111 | if (2 == mode) 112 | c = ksql_exec_private(p, "BEGIN EXCLUSIVE"); 113 | else if (1 == mode) 114 | c = ksql_exec_private(p, "BEGIN IMMEDIATE"); 115 | else 116 | c = ksql_exec_private(p, "BEGIN DEFERRED"); 117 | 118 | /* Set this only if the exec succeeded.*/ 119 | 120 | if (KSQL_OK == c) { 121 | p->flags |= KSQLFL_TRANS; 122 | p->trans = id; 123 | } 124 | return c; 125 | } 126 | 127 | enum ksqlc 128 | ksqlsrv_trans_close(struct ksql *p) 129 | { 130 | enum ksqlc c, cc; 131 | size_t type, id; 132 | 133 | if (KSQL_OK != (c = ksql_readsz(p, &type))) 134 | return c; 135 | if (KSQL_OK != (c = ksql_readsz(p, &id))) 136 | return c; 137 | cc = ksql_trans_close_inner(p, type, id); 138 | return ksql_writecode(p, cc); 139 | } 140 | 141 | enum ksqlc 142 | ksqlsrv_trans_open(struct ksql *p) 143 | { 144 | enum ksqlc c, cc; 145 | size_t type, id; 146 | 147 | if (KSQL_OK != (c = ksql_readsz(p, &type))) 148 | return c; 149 | if (KSQL_OK != (c = ksql_readsz(p, &id))) 150 | return c; 151 | cc = ksql_trans_open_inner(p, type, id); 152 | return ksql_writecode(p, cc); 153 | } 154 | 155 | enum ksqlc 156 | ksql_trans_open(struct ksql *p, size_t id) 157 | { 158 | 159 | return ksql_trans_open_inner(p, 0, id); 160 | } 161 | 162 | enum ksqlc 163 | ksql_trans_exclopen(struct ksql *p, size_t id) 164 | { 165 | 166 | return ksql_trans_open_inner(p, 2, id); 167 | } 168 | 169 | enum ksqlc 170 | ksql_trans_singleopen(struct ksql *p, size_t id) 171 | { 172 | 173 | return ksql_trans_open_inner(p, 1, id); 174 | } 175 | 176 | enum ksqlc 177 | ksql_trans_commit(struct ksql *p, size_t id) 178 | { 179 | 180 | return ksql_trans_close_inner(p, 0, id); 181 | } 182 | 183 | enum ksqlc 184 | ksql_trans_rollback(struct ksql *p, size_t id) 185 | { 186 | 187 | return ksql_trans_close_inner(p, 1, id); 188 | } 189 | -------------------------------------------------------------------------------- /versions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 |

0.3.5

7 |
Kristaps Dzonsons
8 | 9 |
10 | 25 |
26 |
27 |
28 |

0.3.4

29 |
Kristaps Dzonsons
30 | 31 |
32 | 39 |
40 |
41 |
42 |

0.3.3

43 |
Kristaps Dzonsons
44 | 45 |
46 | 55 |
56 |
57 |
58 |

0.3.2

59 |
Kristaps Dzonsons
60 | 61 |
62 | 75 |
76 |
77 |
78 |

0.3.1

79 |
Kristaps Dzonsons
80 | 81 |
82 | 112 |
113 |
114 |
115 |

0.3.0

116 |
Kristaps Dzonsons
117 | 118 |
119 | 164 |
165 |
166 |
167 |

0.2.5

168 |
Kristaps Dzonsons
169 | 170 |
171 | 185 |
186 |
187 |
188 |

0.2.4

189 |
Kristaps Dzonsons
190 | 191 |
192 | 202 |
203 |
204 |
205 |

0.2.3

206 |
Kristaps Dzonsons
207 | 208 |
209 | 215 |
216 |
217 |
218 |

0.2.2

219 |
Kristaps Dzonsons
220 | 221 |
222 | 231 |
232 |
233 |
234 |

0.2.1

235 |
Kristaps Dzonsons
236 | 237 |
238 | 248 |
249 |
250 |
251 |

0.2.0

252 |
Kristaps Dzonsons
253 | 254 |
255 | 276 |
277 |
278 |
279 |

0.1.4

280 |
Kristaps Dzonsons
281 | 282 |
283 | 296 |
297 |
298 |
299 |

0.1.3

300 |
Kristaps Dzonsons
301 | 302 |
303 | 309 |
310 |
311 |
312 |

0.1.2

313 |
Kristaps Dzonsons
314 | 315 |
316 | 324 |
325 |
326 |
327 |

0.1.1

328 |
Kristaps Dzonsons
329 | 330 |
331 | 343 |
344 |
345 |
346 |

0.1.0

347 |
Kristaps Dzonsons
348 | 349 |
350 | 370 |
371 |
372 |
373 |

0.0.6

374 |
Kristaps Dzonsons
375 | 376 |
377 | 384 |
385 |
386 |
387 |

0.0.7

388 |
Kristaps Dzonsons
389 | 390 |
391 | 397 |
398 |
399 |
400 |

0.0.8

401 |
Kristaps Dzonsons
402 | 403 |
404 | 410 |
411 |
412 |
413 |

0.0.9

414 |
Kristaps Dzonsons
415 | 416 |
417 | 422 |
423 |
424 | --------------------------------------------------------------------------------