This code example shows how to use OTL with a CLOB table column that has "IS JSON" constraint. It's not really that different from any other CLOB column. See the Oracle manual for more detail on how JSON columns can be selected from the underlying table.
#include <iostream> #include <string> using namespace std;
#define OTL_ORA12C // Compile OTL 4.0/OCI12 #define OTL_STL // Enable STL compatibility mode #include <otlv4.h> // include the OTL 4.0 header file otl_connect db; // connect object
const char* json_sample=
"{ \"PONumber\" : 1600,"
" \"Reference\" : \"ABULL-20140421\","
" \"Requestor\" : \"Alexis Bull\","
" \"User\" : \"ABULL\","
" \"CostCenter\" : \"A50\","
" \"ShippingInstructions\" : { \"name\" : \"Alexis Bull\","
" \"Address\": { \"street\" : \"200 Sporting Green\","
" \"city\" : \"South San Francisco\","
" \"state\" : \"CA\","
" \"zipCode\" : 99236,"
" \"country\" : \"United States of America\" },"
" \"Phone\" : [ { \"type\" : \"Office\", \"number\" : \"909-555-7307\" },"
" { \"type\" : \"Mobile\", \"number\" : \"415-555-1234\" } ] },"
" \"Special Instructions\" : null,"
" \"AllowPartialShipment\" : false,"
" \"LineItems\" : [ { \"ItemNumber\" : 1,"
" \"Part\" : { \"Description\" : \"One Magic Christmas\","
" \"UnitPrice\" : 19.95,"
" \"UPCCode\" : 13131092899 },"
" \"Quantity\" : 9.0 },"
" { \"ItemNumber\" : 2,"
" \"Part\" : { \"Description\" : \"Lethal Weapon\","
" \"UnitPrice\" : 19.95,"
" \"UPCCode\" : 85391628927 },"
" \"Quantity\" : 5.0 } ] }";
void insert() // insert rows into table {
string f2(json_sample);
db.set_max_long_size(70000); // set maximum long string size for connect object otl_stream o(1, // buffer size has to be set to 1 for operations with LOBs "insert into test_tab values(:f1<int>,'{}') " "returning f2 into :f2<clob>", // SQL statement db // connect object ); o<<1<<f2;
o<<2<<f2;
} void select() { string f2; db.set_max_long_size(70000); // set maximum long string size for connect object otl_stream i(1, // buffer size needs to be set to 1 for operations with LOBs "select * from test_tab where f1>=:f<int> and f1<=:f*2", // SELECT statement db // connect object ); // create select stream int f1; i<<1; // assigning :f = 1 // SELECT automatically executes when all input variables are // assigned. First portion of output rows is fetched to the buffer while(!i.eof()){ // while not end-of-data
i>>f1>>f2;
cout<<"f1="<<f1<<", f2="<<f2<<endl;
cout<<"======================================================"<<endl;
}
} int main() { otl_connect::otl_initialize(); // initialize OCI environment try{ db.rlogon("scott/tiger"); // connect to Oracle otl_cursor::direct_exec ( db, "drop table test_tab", otl_exception::disabled // disable OTL exceptions ); // drop table otl_cursor::direct_exec ( db, "create table test_tab(f1 number, f2 clob, check(f2 is json))" ); // create table insert(); // insert records into table select(); // select records from table } catch(otl_exception& p){ // intercept OTL exceptions cerr<<p.msg<<endl; // print out error message cerr<<p.stm_text<<endl; // print out SQL that caused the error cerr<<p.var_info<<endl; // print out the variable that caused the error } db.logoff(); // disconnect from Oracle return 0; }
f1=1, f2={ "PONumber" : 1600, "Reference" : "ABULL-20140421", "Requestor" : "Alexis Bull", "User" : "ABULL", "CostCenter" : "A50", "ShippingInstructions" : { "name" : "Alexis Bull", "Address": { "street" : "200 Sporting Green", "city" : "South San Francisco", "state" : "CA", "zipCode" : 99236, "country" : "United States of America" }, "Phone" : [ { "type" : "Office", "number" : "909-555-7307" }, { "type" : "Mobile", "number" : "415-555-1234" } ] }, "Special Instructions" : null, "AllowPartialShipment" : false, "LineItems" : [ { "ItemNumber" : 1, "Part" : { "Description" : "One Magic Christmas", "UnitPrice" : 19.95, "UPCCode" : 13131092899 }, "Quantity" : 9.0 }, { "ItemNumber" : 2, "Part" : { "Description" : "Lethal Weapon", "UnitPrice" : 19.95, "UPCCode" : 85391628927 }, "Quantity" : 5.0 } ] }
======================================================
f1=2, f2={ "PONumber" : 1600, "Reference" : "ABULL-20140421", "Requestor" : "Alexis Bull", "User" : "ABULL", "CostCenter" : "A50", "ShippingInstructions" : { "name" : "Alexis Bull", "Address": { "street" : "200 Sporting Green", "city" : "South San Francisco", "state" : "CA", "zipCode" : 99236, "country" : "United States of America" }, "Phone" : [ { "type" : "Office", "number" : "909-555-7307" }, { "type" : "Mobile", "number" : "415-555-1234" } ] }, "Special Instructions" : null, "AllowPartialShipment" : false, "LineItems" : [ { "ItemNumber" : 1, "Part" : { "Description" : "One Magic Christmas", "UnitPrice" : 19.95, "UPCCode" : 13131092899 }, "Quantity" : 9.0 }, { "ItemNumber" : 2, "Part" : { "Description" : "Lethal Weapon", "UnitPrice" : 19.95, "UPCCode" : 85391628927 }, "Quantity" : 5.0 } ] }
======================================================
Copyright © 1996-2024, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com.
Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.