|
| 1 | +#!/usr/bin/env nextflow |
| 2 | + |
| 3 | +/* |
| 4 | + * Example script demonstrating how to use the SQL sqlExecute function |
| 5 | + */ |
| 6 | + |
| 7 | +include { sqlExecute } from 'plugin/nf-sqldb' |
| 8 | +include { fromQuery } from 'plugin/nf-sqldb' |
| 9 | + |
| 10 | +// Define database configuration in nextflow.config file |
| 11 | +// sql.db.demo = [url: 'jdbc:h2:mem:demo', driver: 'org.h2.Driver'] |
| 12 | + |
| 13 | +workflow { |
| 14 | + log.info """ |
| 15 | + ========================================= |
| 16 | + SQL Execution Function Example |
| 17 | + ========================================= |
| 18 | + """ |
| 19 | + |
| 20 | + // Step 1: Create a table (DDL operation returns null) |
| 21 | + log.info "Creating a sample table..." |
| 22 | + def createResult = sqlExecute( |
| 23 | + db: 'demo', |
| 24 | + statement: ''' |
| 25 | + CREATE TABLE IF NOT EXISTS TEST_TABLE ( |
| 26 | + ID INTEGER PRIMARY KEY, |
| 27 | + NAME VARCHAR(100), |
| 28 | + VALUE DOUBLE |
| 29 | + ) |
| 30 | + ''' |
| 31 | + ) |
| 32 | + log.info "Create table result: $createResult" |
| 33 | + |
| 34 | + // Step 2: Insert some data (DML operation returns affected row count) |
| 35 | + log.info "Inserting data..." |
| 36 | + def insertCount = sqlExecute( |
| 37 | + db: 'demo', |
| 38 | + statement: ''' |
| 39 | + INSERT INTO TEST_TABLE (ID, NAME, VALUE) VALUES |
| 40 | + (1, 'alpha', 10.5), |
| 41 | + (2, 'beta', 20.7), |
| 42 | + (3, 'gamma', 30.2), |
| 43 | + (4, 'delta', 40.9); |
| 44 | + ''' |
| 45 | + ) |
| 46 | + log.info "Inserted $insertCount rows" |
| 47 | + |
| 48 | + // Step 3: Update some data (DML operation returns affected row count) |
| 49 | + log.info "Updating data..." |
| 50 | + def updateCount = sqlExecute( |
| 51 | + db: 'demo', |
| 52 | + statement: ''' |
| 53 | + UPDATE TEST_TABLE |
| 54 | + SET VALUE = VALUE * 2 |
| 55 | + WHERE ID = 2; |
| 56 | + ''' |
| 57 | + ) |
| 58 | + log.info "Updated $updateCount rows" |
| 59 | + |
| 60 | + // Step 4: Delete some data (DML operation returns affected row count) |
| 61 | + log.info "Deleting data..." |
| 62 | + def deleteCount = sqlExecute( |
| 63 | + db: 'demo', |
| 64 | + statement: ''' |
| 65 | + DELETE FROM TEST_TABLE |
| 66 | + WHERE ID = 4; |
| 67 | + ''' |
| 68 | + ) |
| 69 | + log.info "Deleted $deleteCount rows" |
| 70 | + |
| 71 | + // Step 5: Query results |
| 72 | + log.info "Querying results..." |
| 73 | + channel |
| 74 | + .fromQuery("SELECT * FROM TEST_TABLE ORDER BY ID", db: 'demo') |
| 75 | + .view { row -> "ID: ${row[0]}, Name: ${row[1]}, Value: ${row[2]}" } |
| 76 | +} |
| 77 | + |
| 78 | +workflow example { |
| 79 | + // Setup: create table (DDL operation) |
| 80 | + def createResult = sqlExecute( |
| 81 | + db: 'foo', |
| 82 | + statement: ''' |
| 83 | + CREATE TABLE IF NOT EXISTS testing ( |
| 84 | + id INTEGER PRIMARY KEY, |
| 85 | + name VARCHAR(100), |
| 86 | + value DOUBLE |
| 87 | + ) |
| 88 | + ''' |
| 89 | + ) |
| 90 | + println "Create table success: ${createResult.success}" // Should be true |
| 91 | + |
| 92 | + // Handle potential failure |
| 93 | + if (!createResult.success) { |
| 94 | + println "Failed to create table: ${createResult.error}" |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // Insert data using sqlInsert |
| 99 | + Channel |
| 100 | + .of([1, 'alpha', 10.5], [2, 'beta', 20.5]) |
| 101 | + .sqlInsert( |
| 102 | + db: 'foo', |
| 103 | + into: 'sample_table', |
| 104 | + columns: 'id, name, value' |
| 105 | + ) |
| 106 | + |
| 107 | + // Query data using fromQuery |
| 108 | + fromQuery('SELECT * FROM sample_table', db: 'foo') |
| 109 | + .view() |
| 110 | + |
| 111 | + // Update data using sqlExecute (DML operation returns affected row count in result field) |
| 112 | + def updateResult = sqlExecute( |
| 113 | + db: 'foo', |
| 114 | + statement: "UPDATE sample_table SET value = 30.5 WHERE name = 'beta'" |
| 115 | + ) |
| 116 | + |
| 117 | + if (updateResult.success) { |
| 118 | + println "Updated ${updateResult.result} row(s)" |
| 119 | + } else { |
| 120 | + println "Update failed: ${updateResult.error}" |
| 121 | + } |
| 122 | +} |
0 commit comments