Skip to content

Instantly share code, notes, and snippets.

View stellingsimon's full-sized avatar

Simon Stelling stellingsimon

View GitHub Profile
import org.slf4j.Logger
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
/**
* Use as follows:
* ```
* class SomeClass {
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
// A `Catalog` is similar to an `enum class` in the sense that
// * every property is available through a static identifier
// * you can list the `values()`
// A `Catalog` is different from an `enum class` in the sense that
// * its properties are of a different type
// * you can distribute properties among several catalog objects
@stellingsimon
stellingsimon / first_last.sql
Created August 22, 2019 05:41
Simulation of first_value(x IGNORE NULLS) on Postgres
-- found here: https://wiki.postgresql.org/wiki/First/last_(aggregate)
-- Create a function that always returns the first non-NULL item
CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement )
RETURNS anyelement LANGUAGE SQL IMMUTABLE STRICT AS $$
SELECT $1;
$$;
-- And then wrap an aggregate around it
CREATE AGGREGATE public.FIRST (
@stellingsimon
stellingsimon / mock_fk_dependencies.sql
Last active August 26, 2020 12:04
mock FK dependencies that are in safe distance
-- Example usage:
-- ==============
--
-- (first, see http://www.jooq.org/sakila for an overview of the Sakila DB schema)
--
-- Assuming you are writing a test for 'returnRentalAndPayImmediately()':
--
-- Then, tables under test are 'rental' and 'payment'.
-- You want to keep the FKs between the following tables, since they may point you to crucial bugs:
-- payment, rental, staff, customer, inventory
@stellingsimon
stellingsimon / Calculator.java
Last active July 19, 2018 13:54
Plain Java emulation of Kotlin's `sealed` classes: https://kotlinlang.org/docs/reference/sealed-classes.html
public class Calculator {
public static void main(String[] args) {
Calculator calculator = new Calculator();
MathExpression onePlusTwo = calculator.parseSimpleToken(MathExpressionType.SUM, "1+2");
MathExpression three = calculator.parseSimpleToken(MathExpressionType.CONSTANT, "3");
System.out.println("evaluating 1+2: " + calculator.evaluate(onePlusTwo));
System.out.println("evaluationg 3: " + calculator.evaluate(three));
@stellingsimon
stellingsimon / postgres-immediate-unique-constraint.sql
Created March 29, 2018 15:25
Postgres unique constraints are checked immediately not at the end of the statement
CREATE TABLE test (
val TEXT,
sort_order INTEGER NOT NULL UNIQUE
);
-- works fine:
TRUNCATE test;
INSERT INTO test
VALUES ('B', 2),
('A', 1);
@stellingsimon
stellingsimon / SkippingRule.java
Created February 21, 2018 17:48
Skipping Tests are Failing Tests
// Usage in Test Class:
// @Rule
// public SkippingRule skippingTests = failingTests();
public class SkippingRule extends TestWatcher {
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
Assert.fail("Test has assumptions that are violated. Please fix them.");
}
@stellingsimon
stellingsimon / normalize_sort_order.sql
Last active January 9, 2018 22:30
Normalize SORT_ORDER column of a user-sortable Postgres table after DELETE/INSERTs
CREATE TABLE tbl (
id INT,
sort_order INT
);
WITH updated_order(id, new_sort_order) AS (
SELECT
id,
row_number() OVER (ORDER BY tbl.sort_order NULLS FIRST)
FROM tbl
@stellingsimon
stellingsimon / update_sort_order.sql
Last active January 9, 2018 22:29
Reordering user-sortable Postgres tables in one `UPDATE` statement
CREATE TABLE tbl (
id UUID,
sort_order INT
);
-- 1 param of type UUID[]: pass in all IDs in the desired order
WITH updated_order(id, new_sort_order) AS (
SELECT tbl.id, u.new_sort_order
FROM tbl
LEFT OUTER JOIN unnest(? :: UUID []) WITH ORDINALITY AS u(id, new_sort_order) on tbl.id = u.id
@stellingsimon
stellingsimon / AuditingConnection.java
Last active September 9, 2023 08:50
Recording row-level auditing information from an application's user session context in a Postgres DB (9.5+)
package example.postgres.audit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;