Skip to content

Commit

Permalink
Add Cron EXT (#5)
Browse files Browse the repository at this point in the history
* Add Cron Extension

* Prepare Changelog for 0.9.0
  • Loading branch information
mindreframer authored Oct 16, 2021
1 parent 85b24b3 commit 4725244
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### 0.9.0-3360

- Files: https://github.com/mindreframer/sqlean/releases/tag/0.9.0-3360
- EXT:
- cron (https://github.com/daschr/sqlite3_extensions/)
- pivot_vtab (https://github.com/jakethaw/pivot_vtab/)

### 0.8.9-3360

- Files: https://github.com/mindreframer/sqlean/releases/tag/0.8.9-3360
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ download-sqlite:
compile-linux:
gcc -fPIC -shared src/sqlite3-bfsvtab.c -o dist/bfsvtab.so -lm
gcc -fPIC -shared src/sqlite3-bloom_filter.c -o dist/bloom_filter.so -lm
gcc -fPIC -shared src/sqlite3-cron.c -o dist/cron.so -lm
gcc -fPIC -shared src/sqlite3-crypto.c src/crypto/*.c -o dist/crypto.so -lm
gcc -fPIC -shared src/sqlite3-ipaddr.c -o dist/ipaddr.so -lm
gcc -fPIC -shared src/sqlite3-json1.c -o dist/json1.so -lm
Expand All @@ -38,6 +39,7 @@ compile-linux:
compile-macos:
gcc -fPIC -dynamiclib -I src src/sqlite3-bfsvtab.c -o dist/bfsvtab.dylib -lm
gcc -fPIC -dynamiclib -I src src/sqlite3-bloom_filter.c -o dist/bloom_filter.dylib -lm
gcc -fPIC -dynamiclib -I src src/sqlite3-cron.c -o dist/cron.dylib -lm
gcc -fPIC -dynamiclib -I src src/sqlite3-crypto.c src/crypto/*.c -o dist/crypto.dylib -lm
gcc -fPIC -dynamiclib -I src src/sqlite3-ipaddr.c -o dist/ipaddr.dylib -lm
gcc -fPIC -dynamiclib -I src src/sqlite3-json1.c -o dist/json1.dylib -lm
Expand All @@ -56,6 +58,7 @@ compile-macos:
compile-windows:
gcc -shared -I. src/sqlite3-bfsvtab.c -o dist/bfsvtab.dll -lm
gcc -shared -I. src/sqlite3-bloom_filter.c -o dist/bloom_filter.dll -lm
gcc -shared -I. src/sqlite3-cron.c -o dist/cron.dll -lm
gcc -shared -I. src/sqlite3-crypto.c src/crypto/*.c -o dist/crypto.dll -lm
gcc -shared -I. src/sqlite3-json1.c -o dist/json1.dll -lm
gcc -shared -I. src/sqlite3-math.c -o dist/math.dll -lm
Expand All @@ -73,6 +76,7 @@ compile-windows:
compile-windows-32:
gcc -shared -I. src/sqlite3-bfsvtab.c -o dist/bfsvtab-win32.dll -lm
gcc -shared -I. src/sqlite3-bloom_filter.c -o dist/bloom_filter-win32.dll -lm
gcc -shared -I. src/sqlite3-cron.c -o dist/cron-win32.dll -lm
gcc -shared -I. src/sqlite3-crypto.c src/crypto/*.c -o dist/crypto-win32.dll -lm
gcc -shared -I. src/sqlite3-json1.c -o dist/json1-win32.dll -lm
gcc -shared -I. src/sqlite3-math.c -o dist/math-win32.dll -lm
Expand Down
4 changes: 3 additions & 1 deletion bin/vendorLibs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ patch -p0 < diffs/sqlite3-bloom_filter.diff
curl -L https://github.com/abetlen/sqlite3-bfsvtab-ext/raw/main/bfsvtab.c --output src/sqlite3-bfsvtab.c


curl -L https://raw.githubusercontent.com/jakethaw/pivot_vtab/main/pivot_vtab.c --output src/sqlite3-pivot_vtab.c
curl -L https://raw.githubusercontent.com/jakethaw/pivot_vtab/main/pivot_vtab.c --output src/sqlite3-pivot_vtab.c

curl -L https://raw.githubusercontent.com/daschr/sqlite3_extensions/master/cron.c --output src/sqlite3-cron.c
1 change: 1 addition & 0 deletions genMake/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { GenBase } = require("./genbase");
const activeExts = [
["src/sqlite3-bfsvtab.c", "bfsvtab"],
["src/sqlite3-bloom_filter.c", "bloom_filter"],
["src/sqlite3-cron.c", "cron"],
["src/sqlite3-crypto.c src/crypto/*.c", "crypto"],
["src/sqlite3-ipaddr.c", "ipaddr"],
["src/sqlite3-json1.c", "json1"],
Expand Down
137 changes: 137 additions & 0 deletions src/sqlite3-cron.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

// https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
static int dayofweek(int y, int m, int d) {
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

static int is_digit(const char *s) {
if(s[0]=='\0')
return 0;

for(int i=0; s[i]!='\0'; ++i)
if(s[i] < '0' || s[i] > '9')
return 0;

return 1;
}


static int compare_single(unsigned int v, const char *e) {
if(*e == '\0')
return 0;

if(e[0] == '*') {
if(e[1] == '\0')
return 1;
else if(e[1] == '/') {
if(!is_digit(e+2))
return 0;
else
return !(v % atoi(e+2));
}
}

int epos=0, cv;
while(e[epos]!='\0') {
int i=0;
for(; e[epos+i]>='0' && e[epos+i]<='9'; ++i);

if(i==0) return 0;

if(sscanf(e+epos,"%d*",&cv) != 1)
return 0;

if(cv==v)
return 1;

epos+=++i;
}

return 0;
}

static int compare(const char *date, char *cronentry) {
unsigned int d[5];

if(sscanf(date, "%u-%u-%u %u:%u:*", &d[0], &d[1], &d[2], &d[3], &d[4]) != 5)
return 0;

int pos=0;
const char *s=strtok(cronentry," ");

while(s != NULL && pos < 4) {
if(!compare_single(d[4-pos++], s))
return 0;
s=strtok(NULL, " ");
}

if(s== NULL || pos != 4)
return 0;

return compare_single(dayofweek(d[0], d[1], d[2]), s);
}

/*
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>
*/

static void cron_match_func(
sqlite3_context *context,
int argc,
sqlite3_value **argv
) {
const char *date;
char *cronentry;

assert( argc==2 );
if( sqlite3_value_type(argv[0])==SQLITE_NULL ||
sqlite3_value_type(argv[1])==SQLITE_NULL )
return;

date=(const char *) sqlite3_value_text(argv[0]);

sqlite3_value *cpy=sqlite3_value_dup(argv[1]);
cronentry=(char *) sqlite3_value_text(cpy);

sqlite3_result_int(context, compare(date, cronentry));

sqlite3_value_free(cpy);
}

#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_cron_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
) {
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
rc = sqlite3_create_function(db, "cron_match", 2,
#if SQLITE_VERSION_NUMBER > 3031000
SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
#else
SQLITE_UTF8|SQLITE_DETERMINISTIC,
#endif
0, cron_match_func, 0, 0);
return rc;
}

11 changes: 11 additions & 0 deletions test/cron-test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.read test/_settings.sql
.load dist/cron

select cron_match('2000-01-01 00:03:00', '*/3 * * * *');

create table test1(date TEXT);
insert into test1(date) values ('2000-01-04 10:27:00'), ('2000-01-04 10:36:00');
insert into test1(date) values ('2000-01-06 11:30:00');

select date from test1 where cron_match(date, '*/9 */10 * * *');
select date from test1;
16 changes: 16 additions & 0 deletions testout/cron-test-out.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.load dist/cron

select cron_match('2000-01-01 00:03:00', '*/3 * * * *');
[{"cron_match('2000-01-01 00:03:00', '*/3 * * * *')":1}]

create table test1(date TEXT);
insert into test1(date) values ('2000-01-04 10:27:00'), ('2000-01-04 10:36:00');
insert into test1(date) values ('2000-01-06 11:30:00');

select date from test1 where cron_match(date, '*/9 */10 * * *');
[{"date":"2000-01-04 10:27:00"},
{"date":"2000-01-04 10:36:00"}]
select date from test1;
[{"date":"2000-01-04 10:27:00"},
{"date":"2000-01-04 10:36:00"},
{"date":"2000-01-06 11:30:00"}]

0 comments on commit 4725244

Please sign in to comment.