This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger | |
import kotlin.properties.ReadOnlyProperty | |
import kotlin.reflect.KClass | |
import kotlin.reflect.KProperty | |
/** | |
* Use as follows: | |
* ``` | |
* class SomeClass { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE test ( | |
val TEXT, | |
sort_order INTEGER NOT NULL UNIQUE | |
); | |
-- works fine: | |
TRUNCATE test; | |
INSERT INTO test | |
VALUES ('B', 2), | |
('A', 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |