PostgresSchema { public static void create(final QueryStatement statement, final CollectionName collection, final IObject options) throws QueryBuildException { try { Writer body = statement.getBodyWriter(); CreateClauses.writeFunctions(body); body.write("CREATE TABLE "); body.write(collection.toString()); body.write(" ("); body.write(DOCUMENT_COLUMN); body.write(" jsonb NOT NULL"); CreateClauses.writeFullTextColumn(body, options); body.write(");\n"); CreateClauses.writePrimaryKey(body, collection); if (options != null) { IndexCreators.writeIndexes(body, collection, options); } } catch (Exception e) { throw new QueryBuildException("Failed to build create body", e); } } private  PostgresSchema(); static void create(final QueryStatement statement, final CollectionName collection, final IObject options); static FieldPath getIdFieldPath(final CollectionName collection); static void insert(final QueryStatement statement, final CollectionName collection); static void update(final QueryStatement statement, final CollectionName collection); static void getById(final QueryStatement statement, final CollectionName collection); static void search(final QueryStatement statement, final CollectionName collection, final IObject criteria); static void delete(final QueryStatement statement, final CollectionName collection); static void count(final QueryStatement statement, final CollectionName collection, final IObject criteria); static final String DOCUMENT_COLUMN; static final String FULLTEXT_COLUMN; static final String ID_FIELD_PATTERN; static final String FTS_DICTIONARY; static final int DEFAULT_PAGE_SIZE; }
@Test public void testCreate() throws QueryBuildException { PostgresSchema.create(statement, collection, null); assertEquals( "CREATE OR REPLACE FUNCTION parse_timestamp_immutable(source jsonb) RETURNS timestamptz AS $$ " + "BEGIN RETURN source::text::timestamptz; END; " + "$$ LANGUAGE 'plpgsql' IMMUTABLE;\n" + "CREATE OR REPLACE FUNCTION bigint_to_jsonb_immutable(source bigint) RETURNS jsonb AS $$ " + "BEGIN RETURN to_json(source)::jsonb; END; " + "$$ LANGUAGE 'plpgsql' IMMUTABLE;\n" + "CREATE TABLE test_collection (document jsonb NOT NULL);\n" + "CREATE UNIQUE INDEX test_collection_pkey ON test_collection USING BTREE ((document#>'{test_collectionID}'));\n", body.toString()); }
@Test public void testCreateWithIndexes() throws QueryBuildException, InvalidArgumentException { IObject options = new DSObject("{ \"ordered\": \"a\"," + "\"fulltext\": \"b\"," + "\"language\": \"english\"" + "}"); PostgresSchema.create(statement, collection, options); assertEquals( "CREATE OR REPLACE FUNCTION parse_timestamp_immutable(source jsonb) RETURNS timestamptz AS $$ " + "BEGIN RETURN source::text::timestamptz; END; " + "$$ LANGUAGE 'plpgsql' IMMUTABLE;\n" + "CREATE OR REPLACE FUNCTION bigint_to_jsonb_immutable(source bigint) RETURNS jsonb AS $$ " + "BEGIN RETURN to_json(source)::jsonb; END; " + "$$ LANGUAGE 'plpgsql' IMMUTABLE;\n" + "CREATE TABLE test_collection (document jsonb NOT NULL, fulltext tsvector);\n" + "CREATE UNIQUE INDEX test_collection_pkey ON test_collection USING BTREE ((document#>'{test_collectionID}'));\n" + "CREATE INDEX ON test_collection USING BTREE ((document#>'{a}'));\n" + "CREATE INDEX ON test_collection USING GIN (fulltext);\n" + "CREATE FUNCTION test_collection_fulltext_update_trigger() RETURNS trigger AS $$\n" + "begin\n" + "new.fulltext := " + "to_tsvector('english', coalesce((new.document#>'{b}')::text,''));\n" + "return new;\n" + "end\n" + "$$ LANGUAGE plpgsql;\n" + "CREATE TRIGGER test_collection_fulltext_update_trigger BEFORE INSERT OR UPDATE " + "ON test_collection FOR EACH ROW EXECUTE PROCEDURE " + "test_collection_fulltext_update_trigger();\n", body.toString()); }