Oracle Database Soda PL SQL Developers Guide
Oracle Database Soda PL SQL Developers Guide
Oracle Database Soda PL SQL Developers Guide
Release 18c
E84719-04
February 2019
Oracle Database SODA for PL/SQL Developer's Guide, Release 18c
E84719-04
Copyright © 2018, 2019, Oracle and/or its affiliates. All rights reserved.
This software and related documentation are provided under a license agreement containing restrictions on
use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your
license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify,
license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means.
Reverse engineering, disassembly, or decompilation of this software, unless required by law for
interoperability, is prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If
you find any errors, please report them to us in writing.
If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on
behalf of the U.S. Government, then the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software,
any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are
"commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-
specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the
programs, including any operating system, integrated software, any programs installed on the hardware,
and/or documentation, shall be subject to license terms and license restrictions applicable to the programs.
No other rights are granted to the U.S. Government.
This software or hardware is developed for general use in a variety of information management applications.
It is not developed or intended for use in any inherently dangerous applications, including applications that
may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you
shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its
safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this
software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are
used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron,
the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro
Devices. UNIX is a registered trademark of The Open Group.
This software or hardware and documentation may provide access to or information about content, products,
and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly
disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise
set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be
responsible for any loss, costs, or damages incurred due to your access to or use of third-party content,
products, or services, except as set forth in an applicable agreement between you and Oracle.
Contents
Preface
Audience vii
Documentation Accessibility vii
Related Documents vii
Conventions viii
iii
4.2 Creating a Collection That Has Custom Metadata 4-3
Index
iv
List of Examples
3-1 Getting Started Run-Through 3-4
3-2 Sample Output for Getting Started Run-Through 3-4
3-3 Creating a Collection That Has the Default Metadata 3-6
3-4 Opening an Existing Document Collection 3-7
3-5 Checking for a Collection with a Given Name 3-7
3-6 Printing the Names of All Existing Collections 3-8
3-7 Dropping a Document Collection 3-10
3-8 Creating a Document with JSON Content 3-13
3-9 Creating a Document with Document Key and JSON Content 3-14
3-10 Inserting a Document into a Collection 3-16
3-11 Inserting a Document into a Collection and Getting the Result Document 3-16
3-12 Finding All Documents in a Collection 3-19
3-13 Finding the Unique Document That Has a Given Document Key 3-20
3-14 Finding Multiple Documents with Specified Document Keys 3-20
3-15 Finding Documents with a Filter Specification 3-21
3-16 Specifying Pagination Queries with Methods skip() and limit() 3-22
3-17 Specifying Document Version 3-23
3-18 Counting the Number of Documents Found 3-23
3-19 Replacing a Document in a Collection, Given Its Key, and Getting the Result Document 3-25
3-20 Replacing a Particular Version of a Document 3-25
3-21 Removing a Document from a Collection Using a Document Key 3-27
3-22 Removing a Particular Version of a Document 3-28
3-23 Removing Documents from a Collection Using Document Keys 3-28
3-24 Removing JSON Documents from a Collection Using a Filter 3-29
3-25 Creating a B-Tree Index for a JSON Field with SODA for PL/SQL 3-30
3-26 JSON Search Indexing with SODA for PL/SQL 3-30
3-27 Dropping an Index with SODA for PL/SQL 3-31
3-28 Getting a Data Guide with SODA for PL/SQL 3-32
3-29 Transaction Involving SODA Document Insertion and Replacement 3-33
4-1 Getting the Metadata of a Collection 4-2
4-2 Default Collection Metadata 4-2
4-3 Creating a Collection That Has Custom Metadata 4-3
v
List of Tables
3-1 Getter Methods for Documents (SODA_DOCUMENT_T) 3-12
vi
Preface
This document describes how to use Simple Oracle Document Access (SODA) for C.
• Audience
• Documentation Accessibility
• Related Documents
• Conventions
Audience
This document is intended for users of Simple Oracle Document Access (SODA) for
PL/SQL.
Documentation Accessibility
For information about Oracle's commitment to accessibility, visit the Oracle
Accessibility Program website at http://www.oracle.com/pls/topic/lookup?
ctx=acc&id=docacc.
Related Documents
For more information, see these Oracle resources:
• https://docs.oracle.com/en/database/oracle/simple-oracle-document-access/ for
complete information about SODA and its implementations
• Oracle Database Introduction to Simple Oracle Document Access (SODA) for
general information about SODA
• Oracle as a Document Store for general information about using JSON data in
Oracle Database, including with SODA
• Oracle Database JSON Developer’s Guide for information about using SQL and
PL/SQL with JSON data stored in Oracle Database
To download free release notes, installation documentation, white papers, or other
collateral, please visit the Oracle Technology Network (OTN). You must register online
before using OTN; registration is free and can be done at OTN Registration.
vii
Preface
If you already have a user name and password for OTN then you can go directly to the
documentation section of the OTN Web site at OTN Documentation.
Conventions
The following text conventions are used in this document:
Convention Meaning
boldface Boldface type indicates graphical user interface elements associated
with an action, or terms defined in text or the glossary.
italic Italic type indicates book titles, emphasis, or placeholder variables for
which you supply particular values.
monospace Monospace type indicates commands within a paragraph, URLs, code
in examples, text that appears on the screen, or text that you enter.
viii
1
SODA for PL/SQL Prerequisites
SODA for PL/SQL is an integral part of Oracle Database, starting with Release 18c
(18.1). The database is the only prerequisite for using SODA for PL/SQL, but some
features are available only starting with particular database releases.
The following features were added to SODA for PL/SQL in Oracle Database Release
18.3. You need that database release or later to use them:
• Data-type SODA_OPERATION_T
• Indexing
• JSON data guide
1-1
2
SODA for PL/SQL Overview
SODA for PL/SQL is a PL/SQL API that implements Simple Oracle Document
Access (SODA). You can use it with PL/SQL to perform create, read (retrieve),
update, and delete (CRUD) operations on documents of any kind, and you can use it
to query JSON documents.
SODA is a set of NoSQL-style APIs that let you create and store collections of
documents in Oracle Database, retrieve them, and query them, without needing to
know Structured Query Language (SQL) or how the data in the documents is stored in
the database.
Oracle Database supports storing and querying JSON data. To access this
functionality, you need structured query language (SQL) with special JSON SQL
operators. SODA for PL/SQL hides the complexities of SQL/JSON programming.
The remaining topics of this document describe various features of SODA for PL/SQL.
Note:
See Also:
Oracle Database JSON Developer’s Guide for information about using SQL
and PL/SQL with JSON data stored in Oracle Database
2-1
3
Using SODA for PL/SQL
How to access SODA for PL/SQL is described, as well as how to use it to perform
create, read (retrieve), update, and delete (CRUD) operations on collections.
(CRUD operations are also called “read and write operations” in this document.)
• Getting Started with SODA for PL/SQL
How to access SODA for PL/SQL is described, as well as how to use it to create a
database collection, insert a document into a collection, and retrieve a document
from a collection.
• Creating a Document Collection with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.create_collection to create a
document collection with the default metadata.
• Opening an Existing Document Collection with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.open_collection to open an existing
document collection.
• Checking Whether a Given Collection Exists with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.open_collection to check for the
existence of a given collection. It returns a SQL NULL value if a collection with the
specified name does not exist; otherwise, it returns the collection object.
• Discovering Existing Collections with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.list_collection_names to discover
existing collections.
• Dropping a Document Collection with SODA for PL/SQL
You use PL/SQL function DBMS_SODA.drop_collection to drop a document
collection.
• Creating Documents with SODA for PL/SQL
You use a constructor for PL/SQL object type SODA_DOCUMENT_T to create SODA
documents.
• Inserting Documents into Collections with SODA for PL/SQL
To insert a document into a collection, you invoke SODA_COLLECTION_T method
(member function) insert_one() or insert_one_and_get(). These methods
create document keys automatically, unless the collection is configured with client-
assigned keys and the input document provides the key.
• SODA for PLSQL Read and Write Operations
A SODA_OPERATION_T instance is returned by method find() of
SODA_COLLECTION_T. You can chain together SODA_OPERATION_T methods, to
specify read and write operations against a collection.
• Finding Documents in Collections with SODA for PL/SQL
You can use SODA_OPERATION_T terminal method get_one() or get_cursor() to
find one or multiple documents in a collection, respectively. You can use terminal
method count() to count the documents in a collection. You can use nonterminal
methods, such as key(), keys(), and filter(), to specify conditions for a find
operation.
3-1
Chapter 3
Getting Started with SODA for PL/SQL
Note:
Don’t worry if not everything in this topic is clear to you on first reading. The
necessary concepts are developed in detail in other topics. This topic should
give you an idea of what is involved overall in using SODA.
3-2
Chapter 3
Getting Started with SODA for PL/SQL
Caution:
Do not use SQL to drop the database table that underlies a collection.
Dropping a collection involves more than just dropping its database
table. In addition to the documents that are stored in its table, a
collection has metadata, which is also persisted in Oracle Database.
Dropping the table underlying a collection does not also drop the
collection metadata.
Note:
If a PL/SQL subprogram that you write invokes subprograms that are in
package DBMS_SODA, and if your subprogram has definer (owner) rights, then
a database administrator (DBA) must grant role SODA_APP to your
subprogram. For example, this code grants role SODA_APP to procedure
my_soda_proc, which is owned by database schema (user) my_db_schema:
See Also:
Oracle Database Security Guide for information about role SODA_APP
3-3
Chapter 3
Getting Started with SODA for PL/SQL
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
foundDocument SODA_DOCUMENT_T;
result_document SODA_DOCUMENT_T;
docKey VARCHAR2(100);
status NUMBER;
BEGIN
-- Create a collection.
collection := DBMS_SODA.create_collection('myCollectionName');
3-4
Chapter 3
Creating a Document Collection with SODA for PL/SQL
Key: 96F35328CD3B4F96BF3CD01BCE9EBDF5
Content: {"name" : "Alexander"}
Creation timestamp: 2017-09-19T01:05:06.160289Z
Last-modified timestamp: 2017-09-19T01:05:06.160289Z
Version: FD69FB6ACE73FA735EC7922CA4A02DDE0690462583F9EA2AF754D7E342B3EE78
If you do not care about the details of collection configuration then pass only the
collection name to DBMS_SODA.create_collection — no second argument. That
creates a collection with the default configuration.
If a collection with the same name already exists then it is simply opened and its
handle is returned. If custom metadata is provided and it does not match the metadata
3-5
Chapter 3
Creating a Document Collection with SODA for PL/SQL
of the existing collection then the collection is not opened and an error is raised. (To
match, all metadata fields must have the same values.)
Note:
Unless otherwise stated, the remainder of this documentation assumes that
a collection has the default configuration.
See Also:
DECLARE
collection SODA_Collection_T;
BEGIN
collection := DBMS_SODA.create_collection('myCollectionName');
END;
/
Related Topics
• Getting the Metadata of an Existing Collection
You use SODA_COLLECTION_T method get_metadata() to get all of the metadata for
a collection, as a JSON document.
• Creating a Collection That Has Custom Metadata
To create a document collection that has custom metadata, you pass its metadata,
as JSON data, to PL/SQL function DBMS_SODA.create_collection.
• Checking Whether a Given Collection Exists with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.open_collection to check for the
existence of a given collection. It returns a SQL NULL value if a collection with the
specified name does not exist; otherwise, it returns the collection object.
3-6
Chapter 3
Opening an Existing Document Collection with SODA for PL/SQL
See Also:
Oracle Database PL/SQL Packages and Types Reference for information
about PL/SQL function DBMS_SODA.open_collection
DECLARE
collection SODA_COLLECTION_T;
BEGIN
collection := DBMS_SODA.open_collection('myCollectionName');
END;
/
See Also:
Oracle Database PL/SQL Packages and Types Reference for information
about PL/SQL function DBMS_SODA.open_collection
DECLARE
collection SODA_COLLECTION_T;
3-7
Chapter 3
Discovering Existing Collections with SODA for PL/SQL
BEGIN
collection := DBMS_SODA.open_collection('myCollectionName');
IF collection IS NULL THEN
DBMS_OUTPUT.put_line('Collection does not exist');
END IF;
END;
/
Related Topics
• Creating a Document Collection with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.create_collection to create a
document collection with the default metadata.
See Also:
Oracle Database PL/SQL Packages and Types Reference for information
about PL/SQL function DBMS_SODA.list_collection_names
DECLARE
coll_list SODA_COLLNAME_LIST_T;
BEGIN
coll_list := DBMS_SODA.list_collection_names;
DBMS_OUTPUT.put_line('Number of collections: ' ||
to_char(coll_list.count));
DBMS_OUTPUT.put_line('Collection List: ');
IF (coll_list.count > 0) THEN
-- Loop over the collection name list
FOR i IN
coll_list.first .. coll_list.last
LOOP
DBMS_OUTPUT.put_line(coll_list(i));
END LOOP;
ELSE
DBMS_OUTPUT.put_line('No collections found');
END IF;
END;
/
3-8
Chapter 3
Dropping a Document Collection with SODA for PL/SQL
Caution:
Do not use SQL to drop the database table that underlies a collection.
Dropping a collection involves more than just dropping its database table. In
addition to the documents that are stored in its table, a collection has
metadata, which is also persisted in Oracle Database. Dropping the table
underlying a collection does not also drop the collection metadata.
Note:
Day-to-day use of a typical application that makes use of SODA does not
require that you drop and re-create collections. But if you need to do that for
any reason then this guideline applies.
Do not drop a collection and then re-create it with different metadata if there
is any application running that uses the collection in any way. Shut down any
such applications before re-creating the collection, so that all live SODA
objects are released.
There is no problem just dropping a collection. Any read or write operation on
a dropped collection raises an error. And there is no problem dropping a
collection and then re-creating it with the same metadata. But if you re-create
a collection with different metadata, and if there are any live applications
using SODA objects, then there is a risk that a stale collection is accessed,
and no error is raised in this case.
Note:
Commit all writes to a collection before using DBMS_SODA.drop_collection.
For the drop to succeed, all uncommitted writes to the collection must first be
either committed or rolled back — you must explicitly use SQL COMMIT or
ROLLBACK.. Otherwise, an exception is raised.
See Also:
Oracle Database PL/SQL Packages and Types Reference for information
about PL/SQL function DBMS_SODA.drop_collection
3-9
Chapter 3
Creating Documents with SODA for PL/SQL
DECLARE
status NUMBER := 0;
BEGIN
status := DBMS_SODA.drop_collection('myCollectionName');
END;
/
Related Topics
• Handling Transactions with SODA for PL/SQL
As usual in PL/SQL and SQL, you can treat individual SODA read and write
operations, or groups of them, as a transaction. To commit a transaction, use a
SQL COMMIT statement. If you want to roll back changes, use a SQL ROLLBACK
statement.
• Inserting Documents into Collections with SODA for PL/SQL
To insert a document into a collection, you invoke SODA_COLLECTION_T method
(member function) insert_one() or insert_one_and_get(). These methods
create document keys automatically, unless the collection is configured with client-
assigned keys and the input document provides the key.
• Replacing Documents in a Collection with SODA for PL/SQL
You can chain together SODA_OPERATION_T replace-operation method
replace_one() or replace_one_and_get() with nonterminal method key() to
uniquely identify a document to be replaced. You can optionally make use of
additional nonterminal methods such as version() and filter().
{ "name" : "Alexander",
"address" : "1234 Main Street",
"city" : "Anytown",
"state" : "CA",
"zip" : "12345"
}
3-10
Chapter 3
Creating Documents with SODA for PL/SQL
You can write a document of a given content type only to a collection whose content
column has been defined for documents of that type. For example, you can write
(insert or replace) only a document with content type BLOB to a collection whose
contentColumn has a sqlType value of BLOB. (BLOB is the default content type for a
collection.)
There are different ways to invoke a document constructor:
• You can provide the document key, as the first argument.
In a collection, each document must have a key. You must provide the key when
you create the document only if you expect to insert the document into a collection
that does not automatically generate keys for inserted documents. By default,
collections are configured to automatically generate document keys.
• You must provide the document content. If you also provide the document key
then the content is the second argument to the constructor.
If you provide only the content then you must specify both the formal and actual
content parameters, separated by the association arrow (=>): v_content =>
actual, c_content => actual, or b_content => actual, for content of type
VARCHAR2, CLOB, or BLOB, respectively.
• You can provide the document media type, which defaults to "application/json".
Unless you provide all of the parameters (key, content, and media type) you must
specify both the formal and actual media-type parameters, , separated by the
association arrow (=>): media_type => actual.
Parameters that you do not provide explicitly default to NULL.
Providing only the content parameter can be useful for creating documents that you
insert into a collection that automatically generates document keys. Providing only the
key and content can be useful for creating documents that you insert into a collection
that has client-assigned keys. Providing (the content and) the media type can be
useful for creating non-JSON documents (using a media type other than
"application/json").
Object type SODA_DOCUMENT_T provides getter methods (also known as getters), which
each retrieve a particular component from a document. (Getter get_data_type()
3-11
Chapter 3
Creating Documents with SODA for PL/SQL
actually returns information about the content component, rather than the component
itself.)
Immediately after you create a document, the getter methods return these values:
• Values provided to the constructor
• "application/json", for method get_media_type(), if the media type was not
provided
• NULL for other components
Each content storage data type has an associated content-component getter method.
You must use the getter method that is appropriate to each content storage type:
get_varchar2() for VARCHAR2 storage, get_clob() for CLOB storage, and get_blob()
for BLOB storage. Otherwise, an error is raised.
Example 3-8 creates a SODA_DOCUMENT_T instance, providing only content. The media
type defaults to "application/json", and the other document components default to
NULL.
3-12
Chapter 3
Creating Documents with SODA for PL/SQL
Example 3-9 creates a SODA_DOCUMENT_T instance, providing the document key and
content. The media type defaults to "application/json", and the other document
components default to NULL.
See Also:
After creating each document, the example uses getter methods to get the document
content. Note that the getter method that is appropriate for each content storage type
is used: get_blob() for BLOB content, and so on.
The document with content type BLOB would be appropriate for writing to the collection
created in Example 3-3, because that collection (which has the default metadata)
accepts documents with (only) BLOB content. The other two documents would not be
appropriate for that collection; trying to insert them would raise an error.
DECLARE
v_doc SODA_DOCUMENT_T;
b_doc SODA_DOCUMENT_T;
c_doc SODA_DOCUMENT_T;
BEGIN
-- Create VARCHAR2 document
v_doc := SODA_DOCUMENT_T(v_content => '{"name" : "Alexander"}');
DBMS_OUTPUT.put_line('Varchar2 Doc content: ' || v_doc.get_varchar2);
3-13
Chapter 3
Creating Documents with SODA for PL/SQL
Example 3-9 Creating a Document with Document Key and JSON Content
This example is similar to Example 3-8, but it provides the document key (myKey) as
well as the document content.
DECLARE
v_doc SODA_DOCUMENT_T;
b_doc SODA_DOCUMENT_T;
c_doc SODA_DOCUMENT_T;
BEGIN
-- Create VARCHAR2 document
v_doc := SODA_DOCUMENT_T('myKey' , v_content => '{"name" :
"Alexander"}');
DBMS_OUTPUT.put_line('Varchar2 Doc key: ' || v_doc.get_key);
DBMS_OUTPUT.put_line('Varchar2 Doc content: ' || v_doc.get_varchar2);
Related Topics
• Inserting Documents into Collections with SODA for PL/SQL
To insert a document into a collection, you invoke SODA_COLLECTION_T method
(member function) insert_one() or insert_one_and_get(). These methods
create document keys automatically, unless the collection is configured with client-
assigned keys and the input document provides the key.
• Finding Documents in Collections with SODA for PL/SQL
You can use SODA_OPERATION_T terminal method get_one() or get_cursor() to
find one or multiple documents in a collection, respectively. You can use terminal
method count() to count the documents in a collection. You can use nonterminal
methods, such as key(), keys(), and filter(), to specify conditions for a find
operation.
• Replacing Documents in a Collection with SODA for PL/SQL
You can chain together SODA_OPERATION_T replace-operation method
replace_one() or replace_one_and_get() with nonterminal method key() to
3-14
Chapter 3
Inserting Documents into Collections with SODA for PL/SQL
Note:
If the collection is configured with client-assigned document keys (which is
not the default case), and the input document provides a key that identifies
an existing document in the collection, then these methods throw an
exception.
See Also:
3-15
Chapter 3
Inserting Documents into Collections with SODA for PL/SQL
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
status NUMBER;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
document := SODA_DOCUMENT_T(
b_content => utl_raw.cast_to_raw('{"name" :
"Alexander"}'));
-- Insert a document
status := collection.insert_one(document);
END;
/
Example 3-11 Inserting a Document into a Collection and Getting the Result
Document
This example creates a document and inserts it into a collection using method
insert_one_and_get(). It then gets (and prints) each of the generated components
from the result document (which contains them). To obtain the components it uses
SODA_DOCUMENT_T methods get_key(), get_created_on(), get_last_modified(), and
get_version().
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
ins_doc SODA_DOCUMENT_T;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
document := SODA_DOCUMENT_T(
b_content => utl_raw.cast_to_raw('{"name" :
"Alexander"}'));
ins_doc := collection.insert_one_and_get(document);
3-16
Chapter 3
SODA for PLSQL Read and Write Operations
Related Topics
• Handling Transactions with SODA for PL/SQL
As usual in PL/SQL and SQL, you can treat individual SODA read and write
operations, or groups of them, as a transaction. To commit a transaction, use a
SQL COMMIT statement. If you want to roll back changes, use a SQL ROLLBACK
statement.
• Dropping a Document Collection with SODA for PL/SQL
You use PL/SQL function DBMS_SODA.drop_collection to drop a document
collection.
• Replacing Documents in a Collection with SODA for PL/SQL
You can chain together SODA_OPERATION_T replace-operation method
replace_one() or replace_one_and_get() with nonterminal method key() to
uniquely identify a document to be replaced. You can optionally make use of
additional nonterminal methods such as version() and filter().
Note:
Data type SODA_OPERATION_T was added to SODA for PL/SQL in Oracle
Database 18.3. You need that database release or later to use it.
You typically use SODA_OPERATION_T to specify all SODA read and write operations
other than inserts. You chain together SODA_OPERATION_T nonterminal methods to
narrow the scope or otherwise condition or qualify a read or write operation.
Nonterminal methods return the same SODA_OPERATION_T instance on which they are
invoked, which allows you to chain them together. The nonterminal methods are
key(), keys(), filter(), version(), skip(), and limit().
A SODA_OPERATION_T terminal method at the end of the chain carries out the actual
read or write operation. The methods for read operations are get_cursor(),
get_one(), and count(). The methods for write operations are replace_one(),
replace_one_and_get(), and remove().
Unless documentation states otherwise, you can chain together any nonterminal
methods, and you can end the chain with any terminal method. However, not all
combinations make sense. For example, it does not make sense to chain method
version() together with methods that do not uniquely identify the document, such as
keys().
Related Topics
• Finding Documents in Collections with SODA for PL/SQL
You can use SODA_OPERATION_T terminal method get_one() or get_cursor() to
find one or multiple documents in a collection, respectively. You can use terminal
method count() to count the documents in a collection. You can use nonterminal
3-17
Chapter 3
Finding Documents in Collections with SODA for PL/SQL
methods, such as key(), keys(), and filter(), to specify conditions for a find
operation.
• Replacing Documents in a Collection with SODA for PL/SQL
You can chain together SODA_OPERATION_T replace-operation method
replace_one() or replace_one_and_get() with nonterminal method key() to
uniquely identify a document to be replaced. You can optionally make use of
additional nonterminal methods such as version() and filter().
• Removing Documents from a Collection with SODA for PL/SQL
You can remove documents from a collection by chaining together
SODA_OPERATION_T method remove() with nonterminal method key(), keys(), or
filter() to identify documents to be removed. You can optionally make use of
additional nonterminal methods such as version().
See Also:
Oracle Database PL/SQL Packages and Types Reference for information
about SODA_OPERATION_T, including each of its methods
Note:
Data type SODA_OPERATION_T was added to SODA for PL/SQL in Oracle
Database 18.3. You need that database release or later to use it.
See Also:
3-18
Chapter 3
Finding Documents in Collections with SODA for PL/SQL
Note:
To avoid resource leaks, close any cursor that you no longer need.
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
cur SODA_CURSOR_T;
status BOOLEAN;
BEGIN
-- Open the collection to be queried
collection := DBMS_SODA.open_collection('myCollectionName');
3-19
Chapter 3
Finding Documents in Collections with SODA for PL/SQL
Example 3-13 Finding the Unique Document That Has a Given Document Key
This example uses SODA_COLLECTION_T methods find(), key(), and get_one() to find
the unique document whose key is "key1".
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
cur SODA_CURSOR_T;
status BOOLEAN;
myKeys SODA_KEY_LIST_T;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
3-20
Chapter 3
Finding Documents in Collections with SODA for PL/SQL
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
cur SODA_CURSOR_T;
status BOOLEAN;
qbe VARCHAR2(128);
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
3-21
Chapter 3
Finding Documents in Collections with SODA for PL/SQL
See Also:
Example 3-16 Specifying Pagination Queries with Methods skip() and limit()
This example uses SODA_COLLECTION_T methods filter(), skip() and limit() in a
pagination query.
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
cur SODA_Cursor_T;
status BOOLEAN;
qbe VARCHAR2(128);
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
-- Find all documents that match the QBE, skip over the first 1000 of
them,
-- limit the number of returned documents to 100
cur := collection.find().filter(qbe).skip(1000).limit(100).get_cursor;
3-22
Chapter 3
Finding Documents in Collections with SODA for PL/SQL
DBMS_OUTPUT.put_line('Content: ' ||
JSON_QUERY(document.get_blob, '$' PRETTY));
DBMS_OUTPUT.put_line('Creation timestamp: ' ||
document.get_created_on);
DBMS_OUTPUT.put_line('Last modified timestamp: ' ||
document.get_last_modified);
DBMS_OUTPUT.put_line('Version: ' || document.get_version);
END IF;
END LOOP;
status := cur.close;
END;
/
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
DECLARE
collection SODA_COLLECTION_T;
3-23
Chapter 3
Replacing Documents in a Collection with SODA for PL/SQL
num_docs NUMBER;
qbe VARCHAR2(128);
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
Related Topics
• SODA for PLSQL Read and Write Operations
A SODA_OPERATION_T instance is returned by method find() of
SODA_COLLECTION_T. You can chain together SODA_OPERATION_T methods, to
specify read and write operations against a collection.
Note:
Data type SODA_OPERATION_T was added to SODA for PL/SQL in Oracle
Database 18.3. You need that database release or later to use it.
3-24
Chapter 3
Replacing Documents in a Collection with SODA for PL/SQL
See Also:
Example 3-19 Replacing a Document in a Collection, Given Its Key, and Getting
the Result Document
This example replaces a document in a collection, given its key. It then gets (and
prints) the key and the generated components from the result document. To obtain the
components it uses SODA_DOCUMENT_T methods get_key(), get_created_on(),
get_last_modified(), and get_version().
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
new_doc SODA_DOCUMENT_T;
BEGIN
collection := DBMS_SODA.open_collection('myCollectionName');
document := SODA_DOCUMENT_T(
b_content => utl_raw.cast_to_raw('{"name" : "Sriky"}'));
new_doc := collection.find().key('key1').replace_one_and_get(document);
DECLARE
collection SODA_COLLECTION_T;
3-25
Chapter 3
Replacing Documents in a Collection with SODA for PL/SQL
document SODA_DOCUMENT_T;
new_doc SODA_DOCUMENT_T;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
Related Topics
• SODA for PLSQL Read and Write Operations
A SODA_OPERATION_T instance is returned by method find() of
SODA_COLLECTION_T. You can chain together SODA_OPERATION_T methods, to
specify read and write operations against a collection.
Related Topics
• Handling Transactions with SODA for PL/SQL
As usual in PL/SQL and SQL, you can treat individual SODA read and write
operations, or groups of them, as a transaction. To commit a transaction, use a
SQL COMMIT statement. If you want to roll back changes, use a SQL ROLLBACK
statement.
• Dropping a Document Collection with SODA for PL/SQL
You use PL/SQL function DBMS_SODA.drop_collection to drop a document
collection.
• Inserting Documents into Collections with SODA for PL/SQL
To insert a document into a collection, you invoke SODA_COLLECTION_T method
(member function) insert_one() or insert_one_and_get(). These methods
create document keys automatically, unless the collection is configured with client-
assigned keys and the input document provides the key.
3-26
Chapter 3
Removing Documents from a Collection with SODA for PL/SQL
Note:
Data type SODA_OPERATION_T was added to SODA for PL/SQL in Oracle
Database 18.3. You need that database release or later to use it.
See Also:
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
status NUMBER;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
3-27
Chapter 3
Removing Documents from a Collection with SODA for PL/SQL
END IF;
END;
/
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
status NUMBER;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
DECLARE
collection SODA_COLLECTION_T;
document SODA_DOCUMENT_T;
cur SODA_CURSOR_T;
num_docs NUMBER;
myKeys SODA_KEY_LIST_T;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
3-28
Chapter 3
Indexing the Documents in a Collection with SODA for PL/SQL
DECLARE
collection SODA_COLLECTION_T;
num_docs NUMBER;
qbe VARCHAR2(128);
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
-- Get a count of all documents in the collection that match the QBE
num_docs := collection.find().filter(qbe).remove;
DBMS_OUTPUT.put_line('Number of documents removed: ' || num_docs);
END;
/
Related Topics
• SODA for PLSQL Read and Write Operations
A SODA_OPERATION_T instance is returned by method find() of
SODA_COLLECTION_T. You can chain together SODA_OPERATION_T methods, to
specify read and write operations against a collection.
Note:
SODA for PL/SQL support for indexing was added in Oracle Database 18.3.
You need that database release or later to use this SODA feature.
A JSON search index is used for full-text and ad hoc structural queries, and for
persistent recording and automatic updating of JSON data-guide information.
An Oracle Spatial and Graph index is used for GeoJSON (spatial) data.
3-29
Chapter 3
Indexing the Documents in a Collection with SODA for PL/SQL
See Also:
Example 3-25 Creating a B-Tree Index for a JSON Field with SODA for PL/SQL
This example creates a B-tree non-unique index for numeric field address.zip of the
JSON documents in collection myCollectionName.
DECLARE
collection SODA_COLLECTION_T;
spec VARCHAR2(700);
status NUMBER;
BEGIN
-- Open the collection
collection := DBMS_SODA.open_collection('myCollectionName');
DECLARE
collection SODA_COLLECTION_T;
spec VARCHAR2(700);
status NUMBER;
BEGIN
3-30
Chapter 3
Indexing the Documents in a Collection with SODA for PL/SQL
The simple index specification it uses is equivalent to this one, which makes explicit
the default values:
{"name" : "SEARCH_AND_DATA_GUIDE_IDX",
"dataguide" : "on",
"search_on" : "text_value"}
If you instead wanted only ad hoc (search) indexing then you would explicitly specify a
value of "off" for field dataguide. If you instead wanted only data-guide support then
you would explicitly specify a value of "none" for field search_on.
Note:
To create a data guide-enabled JSON search index, or to data guide-enable
an existing JSON search index, you need database privilege CTXAPP and
Oracle Database Release 12c (12.2.0.1) or later.
DECLARE
coll SODA_COLLECTION_T;
status NUMBER;
BEGIN
-- Open the collection
coll := dbms_soda.open_Collection('myCollectionName');
3-31
Chapter 3
Getting a Data Guide for a Collection with SODA for PL/SQL
Note:
SODA for PL/SQL support for JSON data guide was added in Oracle
Database 18.3. You need that database release or later to use this SODA
feature.
Before you can obtain a data guide for your collection you must create a data guide-
enabled JSON search index on it. Example 3-26 shows how to do that.
Example 3-28 Getting a Data Guide with SODA for PL/SQL
This example gets a data guide for collection MyCollectionName using
SODA_COLLECTION_T method get_Data_Guide(). It then uses SQL/JSON function
json_query to pretty-print the content of the data-guide document. Finally, it frees the
temporary LOB used for the data-guide document.
DECLARE
collection SODA_COLLECTION_T;
dataguide CLOB;
BEGIN
-- Open the collection.
collection := dbms_soda.open_Collection('myCollectionName');
3-32
Chapter 3
Handling Transactions with SODA for PL/SQL
See Also:
DECLARE
collection SODA_COLLECTION_T;
status NUMBER;
BEGIN
collection := DBMS_SODA.open_collection('myCollectionName');
status := collection.insert_one(
SODA_Document_T(
b_content =>
utl_raw.cast_to_raw('{"a":"aval","b":"bval","c":"cval"}'));
status := collection.replace_one('key1',
SODA_DOCUMENT_T('{"x":"xval","y":"yval"}'));
-- Commit the transaction
COMMIT;
DBMS_OUTPUT.put_line('Transaction is committed');
-- Catch exceptions and roll back if an error is raised
3-33
Chapter 3
Handling Transactions with SODA for PL/SQL
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line (SQLERRM);
ROLLBACK;
DBMS_OUTPUT.put_line('Transaction has been rolled back');
END;
/
Related Topics
• Dropping a Document Collection with SODA for PL/SQL
You use PL/SQL function DBMS_SODA.drop_collection to drop a document
collection.
• Inserting Documents into Collections with SODA for PL/SQL
To insert a document into a collection, you invoke SODA_COLLECTION_T method
(member function) insert_one() or insert_one_and_get(). These methods
create document keys automatically, unless the collection is configured with client-
assigned keys and the input document provides the key.
• Replacing Documents in a Collection with SODA for PL/SQL
You can chain together SODA_OPERATION_T replace-operation method
replace_one() or replace_one_and_get() with nonterminal method key() to
uniquely identify a document to be replaced. You can optionally make use of
additional nonterminal methods such as version() and filter().
3-34
4
SODA Collection Configuration Using
Custom Metadata
SODA collections are highly configurable. You can customize collection metadata, to
obtain different behavior from that provided by default.
Note:
You can customize collection metadata to obtain different behavior from that
provided by default. However, changing some components requires
familiarity with Oracle Database concepts, such as SQL data types. Oracle
recommends that you do not change such components unless you have a
compelling reason. Because SODA collections are implemented on top of
Oracle Database tables (or views), many collection configuration
components are related to the underlying table configuration.
For example, if you change the content column type from BLOB (the default
value) to VARCHAR2 then you must understand the implications (content size
for VARCHAR2 is limited to 32K bytes, character-set conversion can take place,
and so on).
See Also:
4-1
Chapter 4
Getting the Metadata of an Existing Collection
See Also:
DECLARE
collection SODA_COLLECTION_T;
BEGIN
collection := DBMS_SODA.open_collection('myCollectionName');
IF collection IS NULL THEN
DBMS_OUTPUT.put_line('Collection does not exist');
ELSE
DBMS_OUTPUT.put_line('Metadata: '
|| json_query(collection.get_metadata, '$'
PRETTY));
END IF;
END;
/
{
"schemaName" : "mySchemaName",
"tableName" : "myTableName",
"keyColumn" :
{
"name" : "ID",
"sqlType" : "VARCHAR2",
"maxLength" : 255,
"assignmentMethod" : "UUID"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENT",
"sqlType" : "BLOB",
4-2
Chapter 4
Creating a Collection That Has Custom Metadata
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STANDARD"
},
"versionColumn" :
{
"name" : "VERSION",
"method" : "SHA256"
},
"lastModifiedColumn" :
{
"name" : "LAST_MODIFIED"
},
"creationTimeColumn" :
{
"name" : "CREATED_ON"
},
"readOnly" : false
}
See Also:
4-3
Chapter 4
Creating a Collection That Has Custom Metadata
the complete metadata from the newly created collection, which it passes to SQL/
JSON function json_query to pretty-print (using keyword PRETTY).
DECLARE
collection SODA_COLLECTION_T;
metadata VARCHAR2(4000) :=
'{"keyColumn" : {"name" : "KEY", "assignmentMethod": "CLIENT" },
"contentColumn" : { "name" : "JSON", "sqlType": "VARCHAR2" } }';
BEGIN
collection := DBMS_SODA.create_collection('myCustomCollection',
metadata);
DBMS_OUTPUT.put_line('Collection specification: ' ||
json_query(collection.get_metadata, '$' PRETTY));
END;
/
This is the pretty-printed output. The values of fields for keyColumn and contentColumn
that are not specified in the collection specification are defaulted. The values of fields
other than those provided in the collection specification (keyColumn and
contentColumn) are also defaulted. The value of field tableName is defaulted from the
collection name. The value of field schemaName is the database schema (user) that is
current when the collection is created.
Collection specification: {
"schemaName" : "mySchemaName",
"tableName" : "myCustomCollection",
"keyColumn" :
{
"name" : "KEY",
"sqlType" : "VARCHAR2",
"maxLength" : 255,
"assignmentMethod" : "CLIENT"
},
"contentColumn" :
{
"name" : "JSON",
"sqlType" : "VARCHAR2",
"maxLength" : 4000,
"validation" : "STANDARD"
},
"readOnly" : false
}
Related Topics
• Creating a Document Collection with SODA for PL/SQL
You can use PL/SQL function DBMS_SODA.create_collection to create a
document collection with the default metadata.
4-4
Index
C discovering collections
checking existence, 3-7
chaining together SODA_OPERATION_T listing, 3-8
methods, 3-17 documents
collection configuration, 4-1 components, 3-10
collection metadata creating, 3-10
custom, 4-3 finding in collections, 3-18
getting, 4-2 inserting into collections, 3-15
collections metadata, 3-10
checking existence, 3-7 removing from collections, 3-27
creating, 3-5 replacing in collections, 3-24
with custom metadata, 4-3 drop_collection function
discovering, 3-8 example, 3-9
dropping, 3-9 transaction handling, 3-33
opening, 3-7 dropping collections, 3-9
during creation, 3-5
committing a transaction, 3-33
components of SODA documents, 3-10
E
create_collection function existing collection, checking for, 3-7
transaction handling, 3-33
creating collections, 3-5
with custom metadata, 4-3 F
creating documents, 3-10 filter() SODA_COLLECTION_T method, 3-18
find() SODA_COLLECTION_T method, 3-18
D finding documents in collections, 3-18
Index-1
Index
I
S
insert_one_and_get() SODA_COLLECTION_T
method, 3-15 SODA_APP database role, 3-2
insert_one() SODA_COLLECTION_T method, SODA_COLLECTION_T methods
3-15 filter(), 3-18
inserting documents into collections, 3-15 find(), 3-18
get_cursor(), 3-18
get_metadata(), 4-2
L get_one(), 3-18
list_collection_names function insert_one_and_get(), 3-15
example, 3-8 insert_one(), 3-15
listing collections, 3-8 SODA_DOCUMENT_T methods
get_blob(), 3-10
get_clob(), 3-10
M get_created_on(), 3-10
get_data_type(), 3-10
metadata of collections
get_key(), 3-10
getting, 4-2
get_last_modified(), 3-10
metadata of documents
get_media_type(), 3-10
getting, 3-10
get_varchar2(), 3-10
get_version(), 3-10
N SODA_DOCUMENT_T object type and
constructors, 3-10
nonterminal SODA methods, definition, 3-17 SODA_OPERATION_T methods, 3-17
remove(), 3-27
O replace_one_and_get(), 3-24
replace_one(), 3-24
open_collection function
example, 3-7
opening collections, 3-7
T
during creation, 3-5 terminal SODA methods, definition, 3-17
transaction handling, 3-33
P
prerequisites for using SODA for PL/SQL, 1-1
W
write and read operations, 3-17
Index-2