Skip to content

Instantly share code, notes, and snippets.

@little-brother
little-brother / index.html
Created August 1, 2020 20:46
How to use nunjucks in the browser/client-side
<!doctype html>
<html>
<head>
<meta charset = "UTF-8"/>
<title>Nunjucks example</title>
<script type = "module" src = "index.js"></script>
</head>
<body>
<div id = "page-content"></div>
</body>
@little-brother
little-brother / square.c
Created April 26, 2021 12:18
SQLite scalar function example
static void square (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
int type = sqlite3_value_type(argv[0]);
double d = sqlite3_value_double(argv[0]);
switch (type) {
case SQLITE_NULL:
sqlite3_result_null(ctx);
break;
case SQLITE_INTEGER:
@little-brother
little-brother / odd.c
Created April 26, 2021 12:29
SQLite scalar function with context example
void odd (sqlite3_context *ctx, int argc, sqlite3_value **argv){
int *pCounter = (int*)sqlite3_get_auxdata(ctx, 0);
if (pCounter == 0) {
pCounter = sqlite3_malloc(sizeof(*pCounter));
if (pCounter == 0)
return sqlite3_result_error_nomem(ctx);
*pCounter = sqlite3_value_type(argv[0]) == SQLITE_NULL ? 0 : sqlite3_value_int(argv[0]);
sqlite3_set_auxdata(ctx, 0, pCounter, sqlite3_free);
} else {
@little-brother
little-brother / main.c
Created April 26, 2021 12:38
Attach an user defined function to SQLite database example
// gcc main.c sqlite3.c -o main.exe
#include <stdio.h>
#include "sqlite3.h"
static void square (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
int type = sqlite3_value_type(argv[0]);
double d = sqlite3_value_double(argv[0]);
switch (type) {
case SQLITE_NULL:
@little-brother
little-brother / main.c
Created April 26, 2021 12:42
SQLite scalar functions extension example
// gcc main.c sqlite3.c -o main.exe
#include <stdio.h>
#include "sqlite3.h"
int main() {
sqlite3* db;
if (SQLITE_OK != sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL)) {
printf("Error: can't open a database");
return -1;
}
@little-brother
little-brother / aggsum.c
Last active April 26, 2021 13:13
SQLite aggregate function example
// gcc -shared aggsum.c -o aggsum.dll -s -static
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
static void sum_step (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
double* total = (double*)sqlite3_aggregate_context(ctx, sizeof(double));
if (total == NULL)
return sqlite3_result_error_nomem(ctx);
*total += sqlite3_value_double(argv[0]);
@little-brother
little-brother / aggavg.c
Last active April 26, 2021 13:13
SQLite aggregate function with complex state example
// gcc -shared aggavg.c -o aggavg.dll -s -static
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
typedef struct agg_state {
int cnt;
double sum;
} agg_state;
static void avg_step (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
@little-brother
little-brother / vtab.c
Last active May 7, 2021 15:34
SQLite minimal virtual table example
// gcc -shared vtab.c -o vtab.dll -s -static
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
typedef struct vtab_vtab vtab_vtab;
struct vtab_vtab {
sqlite3_vtab base;
};
@little-brother
little-brother / vtab.c
Last active July 13, 2021 09:39
SQLite "read file" virtual table example
// gcc -shared vtab.c -o vtab.dll -s -static
// select * from vtab('D:/1.txt')
// select line from vtab where path = 'D:/1.txt'
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_SIZE 1024
@little-brother
little-brother / vtab.c
Last active May 7, 2021 16:09
SQLite non-table-valued virtual table example
// gcc -shared vtab.c -o vtab.dll -s -static
// create virtual table t using vtab(4); -- 4 is a column count
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct vtab_vtab vtab_vtab;