Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
hoehrmann / sqlite3_profile.c
Last active March 15, 2023 23:02
Quick and dirty LD_PRELOAD SQLite query logger/profiler
// This is now https://github.com/federlieb/federprof
@hoehrmann
hoehrmann / force-prefix-init-script.bash
Created August 16, 2021 21:22
Bash: prefix output of all commands with timestamp
function process_command() {
if [ "$$" -eq "$BASHPID" ]; then
if [[ "$HANDLER_INSTALLED" -ne "1" ]]; then
exec > >(
trap "" INT TERM;
awk '{ print strftime("%Y-%m-%d %H:%M:%S ", systime()) $0; fflush(stdout) }'
)
exec 2> >(
trap "" INT TERM;
awk '{ print strftime("%Y-%m-%d %H:%M:%S ", systime()) $0; fflush(stdout) }' >&2
@hoehrmann
hoehrmann / sqlite.abnf
Created June 9, 2019 18:57
ABNF for SQLite 3.28 SQL
; FIXME: The grammar has been transformed so that `w` appears after a
; token, but there is no way in ABNF to define it as token-separator
; that can optionally contain a mix of comments and white-space. Take
; `;;` as an example, for that to match `sql-stmt-list` `w` would
; have to match the empty string. But if `w` matches the empty string
; then `ISNOT` is the same as `IS NOT`.
sql-stmt-list = [ sql-stmt ] *( ";" w [ sql-stmt ] )
sql-stmt = [ "EXPLAIN" w [ "QUERY" w "PLAN" w ] ] ( alter-table-stmt / analyze-stmt / attach-stmt / begin-stmt / commit-stmt / create-index-stmt / create-table-stmt / create-trigger-stmt / create-view-stmt / create-virtual-table-stmt / delete-stmt / delete-stmt-limited / detach-stmt / drop-index-stmt / drop-table-stmt / drop-trigger-stmt / drop-view-stmt / insert-stmt / pragma-stmt / reindex-stmt / release-stmt / rollback-stmt / savepoint-stmt / select-stmt / update-stmt / update-stmt-limited / vacuum-stmt )
alter-table-stmt = "ALTER" w "TABLE" w [ schema-name w "." w ] table-na
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use JSON;
use YAML::XS;
my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:');
our $Arg;
WITH
bytes AS (
SELECT 0x00 AS byte
UNION ALL
SELECT byte+1 FROM bytes WHERE byte < 0xFF
),
base AS (
SELECT 0x0000 AS cp
UNION ALL
SELECT cp+1 FROM base WHERE cp < 0x10FFFF
@hoehrmann
hoehrmann / set-intspan-partition.sql
Created May 5, 2019 21:44
SQL alternative for Set::IntSpan::Partition
WITH
args AS (
SELECT
'
[
[[1,3]],
[[3,5],[7,8]],
[[8,8]],
[[0,100]]
]
@hoehrmann
hoehrmann / vowels-vs-consonants.ts
Created April 19, 2019 22:21
NodeJS backpressure streams with prioritisation
const through2 = require('through2');
const split2 = require('split2');
var vowels = through2({ objectMode: true });
var consonants = through2({ objectMode: true });
var muxed = through2({ objectMode: true, highWaterMark: 1 });
[vowels, consonants].forEach(x => x.pipe(muxed));
muxed.pipe(process.stderr);
@hoehrmann
hoehrmann / evolveProtocol.ts
Created April 18, 2019 23:51
evolveProtocol
export namespace api {
export interface Request<
> {
method: string;
params: any;
result: any;
}
export interface Notification<
@hoehrmann
hoehrmann / evolveProtocol.ts
Last active April 18, 2019 21:15
Idea for JSON-RPC 2.0 interface definitions using TypeScript
export namespace api {
export interface Request<T extends string> {
params: any;
result: any;
__name: {
[name in T]: T
}
}
@hoehrmann
hoehrmann / fdpassing.ts
Created April 14, 2019 21:49
node <> Perl communication over additional fds
import { spawn } from 'child_process';
import { Writable, Readable } from 'stream';
const child = spawn(
'perl',
['-e', `
use IO::Handle;
my $in = IO::Handle->new_from_fd($ENV{JIPE_RHS_STDIN_FD}, 'r');
my $out = IO::Handle->new_from_fd($ENV{JIPE_RHS_STDOUT_FD}, 'w');