Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ......

35
Taking Batch Scripting to the Next Level with SQLcl 21 st September 2017 Robert Marz Technical Architect

Transcript of Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ......

Page 1: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

Taking Batch Scripting to the Next Level with SQLcl

21st September 2017

Robert MarzTechnical Architect

Page 2: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Robert Marz

Senior Technical Architectwith database centric view of the worldClient

Portfolio Manager Database Technologies

Blog Editorits-people

Active Member Database Communityin charge of Cloud topicsDOAG

@RobbieDatabee [email protected]

Page 3: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

What´s in Store for you?

What to expect:

• Discover new Possibilities

• Examples

Not included:

• Introduction to JavaScript

• Complete Feature Overview

Page 4: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

What is SQLcl?Oracle SQL Developer

Command Line

• The new SQL*Plus

• Modern Command Line

• Production Release since OOW

• Included in 12cR2 $ORACLE_HOME/bin

Page 5: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Scripting Vintage StyleSQL*Plus

Stable for decades

SequentialSQL & PL/SQL

Not too dynamic„DEFINE“

Error Handling„whenever sqlerror“

Page 6: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Scripting in SQLcl

New Possibilities

Languages(JSR-223)

SQL*Plus„plus“

Page 7: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Scripting in SQLcl – Basics

GitHub Readme

https://github.com/oracle/oracle-db-tools/blob/master/sqlcl/README.md

ctx

ctx.write(<string>)

print();

sqlcl

sqlcl.setStmt(<string>)

sqlcl.run()

util

execute(<string>,binds)

executeReturnOneCol(<string>,binds)

executeReturnListofLists(<string>,binds)

executeReturnList(<string>,binds)

Page 8: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up

Invoking

sql usr[/pwd[@connect]] [as sysdba]

Page 9: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up

Starting Scripts

SQL @scriptname[.sql]

Script

script scriptname.js -- (one line)

script -- (linebreak)

type your javascript on the fly

Page 10: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.1

SQLclPersistence

Objects outlast the End of the Script

Demo Object Persistance Assign Variables

Access from next Script

Page 11: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.1 cont.

Page 12: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.2

Alias Suitable for small, reoccuring tasks

May contain SQL, PL/SQL or Script

Definition is persistent

Demo Restore Data + RememberMe

Page 13: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.2 cont.

Page 14: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.3

login.sql Customise your command lineusing JavaScript

SQLPATH set

show

Demo Switching betweenprompt layouts

Page 15: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.3 cont.

Page 16: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.4

Output Streams

ctx.write() Needs trailing "\n" to produce output

print() Works like expected

Uses Different output Stream

Demo Mixed ctx.write() & print() statements

Page 17: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Warm-Up – Demo 01.4 cont.

Page 18: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Flow Control – Demo 02

SQL*Plus Sequential SQL Blocks

DEFINE, VARIABLE

WorkaroundsSpool „new Script.sql“

PL/SQL execute immediate

SQLcl Modern Scripting Languages

JDBC ConnectionBind Variables

Result Evaluation

Access to SQLcl Interpreter

Dynamic Loading of Scripts

Page 19: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Flow Control – Demo 02 (cont.)

Conditions If – then – else

Case

Demo Create table if not exists

Loops For / While

Demo Looping through Results

Assembling Script-Calls

Page 20: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Flow Control – Demo 02a If-Then-Else

Page 21: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Flow Control – Demo 02b Loops

Page 22: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Loading Blobs – Demo 03

Blob Transfer Blob from File into Database

Use Java Classes Access Files viajava.nio.files.Files

Demo Copy Image to Table

Page 23: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Loading Blobs – Demo 03

Page 24: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Array Magic – Demo 04

Arrays Powerful structures Simple Types

Objects

Nested Arrays

Demo Deploy Framework

List of Credentials

Connection Check

Execute Scripts with Credentials

Page 25: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Array Magic – Demo 04a – SQL-Define

Page 26: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Array Magic – Demo 04b – JS Arrays

Page 27: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Remote Control – Demo 05

Pipes Copy Data No Export File or DB Link

SQLcl „sqlformat insert“

Named Pipes Linux / Unix

Mac OS X

No CLI for Windows

Demo Copy Data between twoDatabases

Page 28: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – Background Sessions – „noDemo“ 06Parallelise Tasks

Java Threads GitHuboracle-db-tools

bg.js

longops.js

its-people Blog yet to be written

Additional JDBC connections

Connection metadata availableexcept credentialsSQLcl connection canbe cloned

„n“ threads

Page 29: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – As you like it – Demo 07

JSR-223 e.g. JavaScript, Lua or Python

Java Implementations„nashorn“, „LuaJ“, „Jython“

Embedding SQLcl Use SQLcl JARs in your Apps

Demo Running lua & python from SQlcl

Using SQLcl from python

ECMA Script 5 & 6

Page 30: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – As you like it – Demo 07a

Page 31: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

SQLcl – As you like it – Demo 07c

Page 32: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

The JavaScript

The JavaScriptEdvard Munch

1910

Page 33: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Reading List

Get

tin

gSt

arte

d SQLcl Scripting: Docs

SQLcl Community

Bas

ics List of JVM languages

JavaScript Cheat Sheet

Nashorn Tutorial

Page 34: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Reading List

Git

Hu

b

SQLcl Examples (oracle-db-tools/sqlcl)

its-people(sqlclscripts)

Blo

gsBarry McGillin

Kris Rice

Jeff Smith

Page 35: Taking Batch Scripting to the Next Level with SQLcl - … · SQL*Plus Sequential SQL Blocks ... Basics List of JVM languages JavaScript Cheat Sheet Nashorn Tutorial. 21st September

21st September 2017 Robert Marz @RobbieDatabee

Conclusion

PLEASE

DO

TRY THIS

AT HOME