Search in sources :

Example 51 with DSLContext

use of org.jooq.DSLContext in project waltz by khartec.

the class SoftwarePackageGenerator method main.

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    SoftwarePackageDao softwarePackageDao = ctx.getBean(SoftwarePackageDao.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);
    List<SoftwarePackage> softwarePackages = ListUtilities.builder(SoftwarePackage.class).addAll(DatabaseSoftwarePackages.dbs).addAll(AppServerSoftwarePackages.appServers).addAll(MiddlewareSoftwarePackages.middleware).build();
    System.out.println("-- deleting all software packages");
    dsl.deleteFrom(SOFTWARE_PACKAGE).where(SOFTWARE_PACKAGE.PROVENANCE.eq(SampleDataUtilities.SAMPLE_DATA_PROVENANCE)).execute();
    System.out.println(" -- storing packages ( " + softwarePackages.size() + " )");
    softwarePackageDao.bulkStore(softwarePackages);
    System.out.println(" -- done");
}
Also used : SoftwarePackageDao(com.khartec.waltz.data.software_catalog.SoftwarePackageDao) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DSLContext(org.jooq.DSLContext) SoftwarePackage(com.khartec.waltz.model.software_catalog.SoftwarePackage)

Example 52 with DSLContext

use of org.jooq.DSLContext in project waltz by khartec.

the class SoftwareUsageGenerator method main.

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    SoftwarePackageDao softwarePackageDao = ctx.getBean(SoftwarePackageDao.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);
    ApplicationDao applicationDao = ctx.getBean(ApplicationDao.class);
    List<SoftwarePackage> allSoftware = softwarePackageDao.findAll();
    List<SoftwareUsageRecord> records = applicationDao.getAll().stream().flatMap(app -> IntStream.range(0, new Random().nextInt(4) + 1).mapToObj(x -> new SoftwareUsageRecord(app.id().get(), ListUtilities.randomPick(allSoftware).id().get(), "waltz-random"))).collect(Collectors.toList());
    System.out.println("-- deleting all software usages");
    dsl.deleteFrom(SOFTWARE_USAGE).where(SOFTWARE_USAGE.PROVENANCE.eq("waltz-sample")).execute();
    System.out.println(" -- storing usages ( " + records.size() + " )");
    dsl.batchInsert(records).execute();
    System.out.println(" -- done");
}
Also used : SoftwarePackageDao(com.khartec.waltz.data.software_catalog.SoftwarePackageDao) IntStream(java.util.stream.IntStream) SoftwarePackageDao(com.khartec.waltz.data.software_catalog.SoftwarePackageDao) ApplicationDao(com.khartec.waltz.data.application.ApplicationDao) SoftwareUsageRecord(com.khartec.waltz.schema.tables.records.SoftwareUsageRecord) ListUtilities(com.khartec.waltz.common.ListUtilities) Random(java.util.Random) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) List(java.util.List) DIConfiguration(com.khartec.waltz.service.DIConfiguration) SoftwarePackage(com.khartec.waltz.model.software_catalog.SoftwarePackage) DSLContext(org.jooq.DSLContext) SOFTWARE_USAGE(com.khartec.waltz.schema.tables.SoftwareUsage.SOFTWARE_USAGE) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Random(java.util.Random) DSLContext(org.jooq.DSLContext) SoftwareUsageRecord(com.khartec.waltz.schema.tables.records.SoftwareUsageRecord) ApplicationDao(com.khartec.waltz.data.application.ApplicationDao) SoftwarePackage(com.khartec.waltz.model.software_catalog.SoftwarePackage)

Example 53 with DSLContext

use of org.jooq.DSLContext in project jOOQ by jOOQ.

the class Example_1_5_ColumnExpressions method run.

@Test
public void run() {
    DSLContext dsl = DSL.using(connection());
    Tools.title("CONCAT() function with prefix notation");
    Tools.print(dsl.select(concat(AUTHOR.FIRST_NAME, val(" "), AUTHOR.LAST_NAME)).from(AUTHOR).orderBy(AUTHOR.ID).fetch());
    Tools.title("CONCAT() function with infix notation");
    Tools.print(dsl.select(AUTHOR.FIRST_NAME.concat(" ").concat(AUTHOR.LAST_NAME)).from(AUTHOR).orderBy(AUTHOR.ID).fetch());
}
Also used : DSLContext(org.jooq.DSLContext) Test(org.junit.Test)

Example 54 with DSLContext

use of org.jooq.DSLContext in project jOOQ by jOOQ.

the class LicenseService method run.

// -------------------------------------------------------------------------
// Utilities
// -------------------------------------------------------------------------
/**
     * This method encapsulates a transaction and initialises a jOOQ DSLcontext.
     * This could also be achieved with Spring and DBCP for connection pooling.
     */
private String run(CtxRunnable runnable) {
    Connection c = null;
    try {
        Class.forName("org.postgresql.Driver");
        c = getConnection("jdbc:postgresql:postgres", "postgres", System.getProperty("pw", "test"));
        DSLContext ctx = DSL.using(new DefaultConfiguration().set(new DefaultConnectionProvider(c)).set(SQLDialect.POSTGRES).set(new Settings().withExecuteLogging(false)));
        return runnable.run(ctx);
    } catch (Exception e) {
        e.printStackTrace();
        Response.status(Status.SERVICE_UNAVAILABLE);
        return "Service Unavailable - Please contact support@datageekery.com for help";
    } finally {
        JDBCUtils.safeClose(c);
    }
}
Also used : DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) Connection(java.sql.Connection) DriverManager.getConnection(java.sql.DriverManager.getConnection) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Settings(org.jooq.conf.Settings)

Example 55 with DSLContext

use of org.jooq.DSLContext in project jOOQ by jOOQ.

the class SparkCRUD method main.

public static void main(String[] args) throws Exception {
    final BasicDataSource ds = new BasicDataSource();
    final Properties properties = new Properties();
    properties.load(SparkCRUD.class.getResourceAsStream("/config.properties"));
    ds.setDriverClassName(properties.getProperty("db.driver"));
    ds.setUrl(properties.getProperty("db.url"));
    ds.setUsername(properties.getProperty("db.username"));
    ds.setPassword(properties.getProperty("db.password"));
    final DSLContext ctx = DSL.using(ds, SQLDialect.H2);
    // Creates a new book resource, will return the ID to the created resource
    // author and title are sent as query parameters e.g. /books?author=Foo&title=Bar
    post("/books", (request, response) -> {
        AuthorRecord author = upsertAuthor(ctx, request);
        BookRecord book = ctx.newRecord(BOOK);
        book.setAuthorId(author.getId());
        book.setTitle(request.queryParams("title"));
        book.store();
        // 201 Created
        response.status(201);
        return book.getId();
    });
    // Gets the book resource for the provided id
    get("/books/:id", (request, response) -> {
        Record2<String, String> book = ctx.select(BOOK.TITLE, AUTHOR.NAME).from(BOOK).join(AUTHOR).on(BOOK.AUTHOR_ID.eq(AUTHOR.ID)).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
        if (book != null) {
            return "Title: " + book.value1() + ", Author: " + book.value2();
        } else {
            // 404 Not found
            response.status(404);
            return "Book not found";
        }
    });
    // Updates the book resource for the provided id with new information
    // author and title are sent as query parameters e.g. /books/<id>?author=Foo&title=Bar
    put("/books/:id", (request, response) -> {
        BookRecord book = ctx.selectFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
        if (book != null) {
            AuthorRecord author = upsertAuthor(ctx, request);
            String newAuthor = request.queryParams("author");
            String newTitle = request.queryParams("title");
            if (newAuthor != null) {
                book.setAuthorId(author.getId());
            }
            if (newTitle != null) {
                book.setTitle(newTitle);
            }
            book.update();
            return "Book with id '" + book.getId() + "' updated";
        } else {
            // 404 Not found
            response.status(404);
            return "Book not found";
        }
    });
    // Deletes the book resource for the provided id
    delete("/books/:id", (request, response) -> {
        BookRecord book = ctx.deleteFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).returning().fetchOne();
        if (book != null) {
            return "Book with id '" + book.getId() + "' deleted";
        } else {
            // 404 Not found
            response.status(404);
            return "Book not found";
        }
    });
    // Gets all available book resources (id's)
    get("/books", (request, response) -> {
        return ctx.select(BOOK.ID).from(BOOK).fetch(BOOK.ID).stream().map(Object::toString).collect(Collectors.joining(" "));
    });
}
Also used : AuthorRecord(org.jooq.example.db.h2.tables.records.AuthorRecord) DSLContext(org.jooq.DSLContext) BookRecord(org.jooq.example.db.h2.tables.records.BookRecord) Properties(java.util.Properties) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Aggregations

DSLContext (org.jooq.DSLContext)109 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)55 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)17 List (java.util.List)17 DIConfiguration (com.khartec.waltz.service.DIConfiguration)14 Collectors (java.util.stream.Collectors)14 EntityKind (com.khartec.waltz.model.EntityKind)11 ArrayList (java.util.ArrayList)9 DSL (org.jooq.impl.DSL)9 EntityReference (com.khartec.waltz.model.EntityReference)8 Timestamp (java.sql.Timestamp)8 Random (java.util.Random)8 IntStream (java.util.stream.IntStream)8 Application (com.khartec.waltz.model.application.Application)7 LogicalFlowDao (com.khartec.waltz.data.logical_flow.LogicalFlowDao)6 OrganisationalUnit (com.khartec.waltz.model.orgunit.OrganisationalUnit)6 Field (org.jooq.Field)6 Record1 (org.jooq.Record1)6 Test (org.junit.Test)6