52
@sthilaire wphilltech.com LOBS, BLOBS, CLOBS Dealing with Attachments in APEX Tim St. Hilaire Feb 2014

LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

  • Upload
    enkitec

  • View
    856

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

LOBS, BLOBS, CLOBS

Dealing with Attachments in APEX

Tim St. Hilaire

Feb 2014

Page 2: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

The Plan

• Intro

• The Goal

• APEX BLOB Wizard

• Making It Your Own

• Base Technology

• Advanced Database Concepts

• Questions

Page 3: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Introduction

Tim St. Hilaire

• System Integrator

• Problems Solver

• Pragmatist

• Technology Enthusiast

• APEX Fan

• Enkitec Product Developer

Page 4: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

The Goal

• Ability to accept file attachment input from users in an application

• Use the content in applications either as text, images, or downloadable to users

• Understand how the data is stored, retrieved, and managed inside the database

Page 5: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Demonstration – The Goal

Page 6: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

The Academics

Page 7: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

The Academics

• LOB – Large Objects – broken down into datatypes – BLOB - Stores unstructured binary data in the

database as a bit stream

– CLOB – Stores large amounts of text such as XML in the database character set

– NCLOB – National Character Set LOB – stores Unicode data

– BFILE – external LOB

– Secure File LOB – file data enabling advanced features such as compression and encryption

Page 8: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

BLOB vs. CLOB

• BLOB – Stream of data intended to be binary

– Chunks are returned in RAW format (substr)

• CLOB – Will translate between character sets

– Chunks are returned in VARCHAR2 format (substr)

SELECT * FROM

NLS_DATABASE_PARAMETERS WHERE

PARAMETER = 'NLS_CHARACTERSET';

Page 9: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

There are 10 types of people in the world…

Those that understand binary, and those that don’t.

-- The Internet

Page 10: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

APEX

Page 11: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Demonstration - APEX Submit Button

Page 12: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Where did the file go?

WWV_FLOW_FILES

APEX_APPLICATION_FILES

Page 13: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Table Context Sensitivity

• APEX_APPLICATION_FILES – limits to files available in the workspace.

• Includes files that were uploaded by all applications in a workspace

• Includes files that have been uploaded via the builder

• Includes Static Shared Component Files for the Application and the Workspace

Page 14: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Evaluate WWV_FLOW_FILES

Pros

• No Table Creations Required

• Consistent Implementation

• Storage in a known location

Cons

• Need Remote Key Storage

• Application Files combined

• Storage for all Workspaces is shared

• Some Files are publicly available

Page 15: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Custom Table

Page 16: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

My Table

• To configure your own table

• Minimum Requirements

• Adjust to Features and Business Requirements

ID NUMBER (your choice)

ACTUAL_FILE BLOB

MIME_TYPE VARCHAR2

FILE_NAME VARCHAR2

Page 17: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Custom Table CREATE TABLE MY_APEX_FILES(

BLOB_ID NUMBER NOT NULL,

NAME VARCHAR2(100 BYTE),

DESCRIPTION VARCHAR2(2000 BYTE),

CATEGORY VARCHAR2(200 BYTE),

PUBLIC_FLAG VARCHAR2(1 BYTE),

BLOB_CONTENT BLOB,

BLOB_SIZE NUMBER,

MIME_TYPE VARCHAR2(255 BYTE),

FILE_NAME VARCHAR2(255 BYTE),

CREATED_BY VARCHAR2(50 BYTE),

CREATED_ON DATE,

UPDATED_BY VARCHAR2(50 BYTE),

UPDATED_ON DATE);

Page 18: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Referencing

Page 19: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - File Browse Settings

Page 20: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - Display Images

Page 21: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - Display Images

Page 22: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - Display Images

Page 23: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - Display Images

Page 24: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - Display Images

Page 25: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Form - Difference

<img id="P210_IMAGE" src="apex_util.get_blob_file?a=500&amp;s=970034890998301&amp;p=210&amp;d=27553826455759041&amp;i=27566106354076381&amp;p_pk1=321230641481386783828770868593367722825&amp;p_pk2=&amp;p_ck=D164075B30411E1CF29B6B9ED9A57684" alt="Hunger" title="Hunger">

<img id="P210_IMAGE_SELECT" src="wwv_flow.show?p_request=NATIVE%3DC825C6CF697537EC53FBD9065B8B0827585481276038A327CEF5A150CAC67389&amp;p_instance=970034890998301&amp;p_flow_id=500&amp;p_flow_step_id=210">

Page 26: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report - Showing Contents

Page 27: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report – BLOB Field

• When writing SQL, the report column is NOT the BLOB content Field – as much as a reference to the content

Page 28: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report – Numbers Required

• The Report column is calculated as SIZE

• Using the APEX format mask / generator, it must be a number.

Page 29: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report Content = SIZE

• Note – You can store the size when the row is written for easy syntax

Page 30: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Notes From the Field

• The Mime Type and File Name is important for usability

Page 31: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Notes From the Field

• The report column containing the primary key must be named the same as the primary key field in the BLOB table – even if the BLOB table is not in the report

• Fields are ignored based on format selections (inline vs. download)

Page 32: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report - Include Download

Page 33: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Link Creation Result

<a href="apex_util.get_blob?s=8634949024800&amp;a=500&amp;c=3473415610798872&amp;p=200&amp;k1=3418001856547833&amp;k2=&amp;ck=BD47E34C8E5A3058299ECD457A4574B4&amp;rt=IR" alt="Download"

title="Download 58KB">Download</a>

Page 34: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Notes From the Field

• Case Sensitive Names

“Apex_application_files”

!=

APEX_APPLICATION_FILES

Page 35: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report - Include Image

Page 36: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Report - Include Image

As a Column in a Select Statement: CASE

WHEN MIME_TYPE LIKE 'image%' THEN '<img src="'||apex_util.get_blob_file_src(

'P210_BLOB_CONTENT',ID)||

'" height="75" width="75" />'

ELSE NULL

END as shrunk

Page 37: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Text

Page 38: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Quiz

What is the size limit of a VARCHAR2?

(A) 255

(B) 2000

(C) 4000

(D) 32767

(E) 8 Terabytes

(F) Whatever it is defined as….

Page 39: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Maximum Width

Page 40: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Rich Text Editor

Character Limit!

Or

The most descriptive error messages ever…

Page 41: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Demonstration – Text Region Error

Page 42: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Plugin – Rich Text

Page 43: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

The Next Level of Complexity

Page 44: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Once Upon a Time….

@apexins.sql

Tablespace_apex

tablespace_files

tablespace_temp

images

@apexins.sql

SYSAUX

SYSAUX

TEMP

/i/

Page 45: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Specify LOB Storage Location

CREATE TABLE ContainsLOB_tab (

n NUMBER,

c CLOB)

lob (c) STORE AS BASICFILE segname

(TABLESPACE lobtbs1

CHUNK 4096

PCTVERSION 5

NOCACHE LOGGING

STORAGE (MAXEXTENTS 5)

);

Page 46: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Demonstration – Text Region Plugin

Page 47: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Size of Tables

SELECT

l.table_name||'-lob',

s.bytes

FROM user_lobs l,

user_segments s

WHERE

s.segment_name = l.segment_name

AND

s.segment_type = 'LOBSEGMENT'

Page 48: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Size Matters

ALTER TABLE MY_TABLE DEALLOCATE UNUSED;

ALTER TABLE MY_TABLE enable ROW MOVEMENT;

ALTER TABLE MY_TABLE SHRINK SPACE cascade;

ALTER TABLE MY_TABLE disable ROW MOVEMENT;

Find a DBA to Partner With

Page 51: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

References Many thanks to all those that have come before me, and for those that take the time to help others grow and improve their skills by sharing their time an knowledge.

• Enkitec Plugin – http://www.enkitec.com/products/plugins

• Demonstration Application – http://wphilltech.com

• APEX Docs – BLOB in APEX 17.10 http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35125/advnc_blob.htm#HTMDB25906 – GET_BLOB_FILE_SRC http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/apex_util.htm#AEAPI129

• DBMS_LOB – http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_lob.htm

• Oracle SecureFiles and Large Objects – http://docs.oracle.com/cd/E11882_01/appdev.112/e18294/adlob_tables.htm – http://docs.oracle.com/cd/E11882_01/appdev.112/e18294/adlob_lob_ops.htm

• APEX Builder - BLOB Support in Forms and Reports – http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35125/advnc_blob.htm

• Icons – http://www.stdicon.com/

Page 52: LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX

@sthilaire wphilltech.com

Disclaimer

• Marks, images, brands and referenced content are the property of their respective owners. Usage is for discussion purposes only. No ownership assumed or implied.

• The comments and opinions expressed here are sole responsibility of the author and not of his employer or any other party.

• No trees were harmed during the creation of this presentation. However, a great number of electrons were terribly inconvenienced.