Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teking into account samples review comments #126

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ojdbc-provider-samples/example-configuration.properties
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ azure_redirect_url=http://localhost:7071
# Google Cloud Platform
gcp_secret_version_name=projects/138028249883/secrets/test-secret/versions/2
gcp_secret_version_name_config=projects/138028249883/secrets/config-secret/versions/4
gcp_cloud_storage_properties=project=onyx-eye-426013-i5;bucket=fm-test-bucket-123564;object=testObjectStorage.json
gcp_cloud_storage_properties=project=onyx-eye-426013-i5;bucket=fm-test-bucket-123564;object=testObjectStorage.json

# Jackson OSON samples properties
jackson_oson_url=jdbc:oracle:thin:@...
jackson_oson_username=USERNAME
jackson_oson_password=***********
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
/**
* <p>
* A standalone example that inserts and retrieves JSON data to the Oracle database
* using an {@link OsonFactory} and {@link ObjectNode}.
* using an {@link OsonFactory} and {@link ObjectNode}. Recommended for applications
* that already consume {@link ObjectNode}, for other application it is recommended
* to use a POJO instead.
* </p><p>
* The JSON object is created using Jackson API's {@link ObjectNode}. The {@link OsonFactory}
* and {@link OsonGenerator} are used to write the JSON Object to an OutputStream. The data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleType;
import oracle.jdbc.provider.oson.JacksonOsonProvider;
import oracle.jdbc.provider.oson.sample.model.Emp;

import java.sql.*;
import java.util.Properties;

/**
* <p>
Expand Down Expand Up @@ -86,10 +86,10 @@ public static void retrieveFromDatabase(Connection conn) throws SQLException {

public static void main(String[] args) {
try {
// set the system property to the class implementing the "oracle.jdbc.spi.JsonProvider interface"
System.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, JacksonOsonProvider.PROVIDER_NAME);
Properties properties = new Properties();
properties.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, "jackson-json-provider");

Connection conn = JacksonOsonSampleUtil.createConnection();
Connection conn = JacksonOsonSampleUtil.createConnectionWithProperties(properties);
JacksonOsonSampleUtil.createTable(conn);
insertIntoDatabase(conn);
retrieveFromDatabase(conn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class JacksonOsonSampleUtil {

Expand All @@ -66,10 +67,19 @@ public static Connection createConnection() throws SQLException {
return ods.getConnection();
}

public static Connection createConnectionWithProperties(Properties properties) throws SQLException {
OracleDataSource ods = new OracleDataSource();
ods.setURL(URL);
ods.setUser(USER);
ods.setPassword(PASSWORD);
ods.setConnectionProperties(properties);
return ods.getConnection();
}

public static void createTable(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.addBatch("drop table if exists jackson_oson_sample");
stmt.addBatch("create table jackson_oson_sample(id number, json_value JSON) tablespace tbs1");
stmt.addBatch("create table jackson_oson_sample(id number, json_value JSON)");
stmt.executeBatch();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
** Copyright (c) 2024 Oracle and/or its affiliates.
**
** The Universal Permissive License (UPL), Version 1.0
**
** Subject to the condition set forth below, permission is hereby granted to any
** person obtaining a copy of this software, associated documentation and/or data
** (collectively the "Software"), free of charge and under any and all copyright
** rights in the Software, and any and all patent rights owned or freely
** licensable by each licensor hereunder covering either (i) the unmodified
** Software as contributed to or provided by such licensor, or (ii) the Larger
** Works (as defined below), to deal in both
**
** (a) the Software, and
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
** one is included with the Software (each a "Larger Work" to which the Software
** is contributed by such licensors),
**
** without restriction, including without limitation the rights to copy, create
** derivative works of, display, perform, and distribute the Software and make,
** use, sell, offer for sale, import, export, have made, and have sold the
** Software and the Larger Work(s), and to sublicense the foregoing rights on
** either these or other terms.
**
** This license is subject to the following condition:
** The above copyright notice and either this complete permission notice or at
** a minimum a reference to the UPL must be included in all copies or
** substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
*/

package oracle.jdbc.provider.oson.sample;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleType;
import oracle.jdbc.provider.oson.sample.model.Movie;
import oracle.jdbc.provider.oson.sample.model.Image;

public class NestedStructurePOJO {
public static void main(String[] args) throws SQLException {
Properties properties = new Properties();
properties.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, "jackson-json-provider");
properties.setProperty(OracleConnection.CONNECTION_PROPERTY_JSON_DEFAULT_GET_OBJECT_TYPE, "oracle.sql.json.OracleJsonValue");
try (Connection con = JacksonOsonSampleUtil.createConnectionWithProperties(properties)) {
Statement stmt = con.createStatement();
stmt.execute("drop table if exists movie");
stmt.execute("create json collection table movie");

Movie matrix = new Movie(
123,
"The Matrix",
"Sci-fi",
BigDecimal.valueOf(353212323),
OffsetDateTime.now(),
Arrays.asList(
new Image("poster.png", "The Matrix"),
new Image("showtimes.png", "Matrix Showtimes")
)
);

Movie shawshank = new Movie(
456,
"The Shawshank Redemption",
"Drama",
BigDecimal.valueOf(453212345),
OffsetDateTime.now(),
null
);

PreparedStatement insert = con.prepareStatement("insert into movie values (:1)");
insert.setObject(1, matrix, OracleType.JSON);
insert.execute();

insert.setObject(1, shawshank, OracleType.JSON);
insert.execute();

ResultSet rs = stmt.executeQuery("select * from movie m where m.data.title = 'The Matrix'");
rs.next();
Movie result = rs.getObject(1, Movie.class);
System.out.println("Objects are equal: " + matrix.equals(result));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package oracle.jdbc.provider.oson.sample.model;

import java.util.Objects;

public class Image {
public String location;
public String description;

public Image() {}
public Image (String location, String description) {
this.description = description;
this.location = location;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Image image = (Image) o;
return location.equals(image.location) && description.equals(image.description);
}

@Override
public int hashCode() {
return Objects.hash(location, description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
** Copyright (c) 2024 Oracle and/or its affiliates.
**
** The Universal Permissive License (UPL), Version 1.0
**
** Subject to the condition set forth below, permission is hereby granted to any
** person obtaining a copy of this software, associated documentation and/or data
** (collectively the "Software"), free of charge and under any and all copyright
** rights in the Software, and any and all patent rights owned or freely
** licensable by each licensor hereunder covering either (i) the unmodified
** Software as contributed to or provided by such licensor, or (ii) the Larger
** Works (as defined below), to deal in both
**
** (a) the Software, and
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
** one is included with the Software (each a "Larger Work" to which the Software
** is contributed by such licensors),
**
** without restriction, including without limitation the rights to copy, create
** derivative works of, display, perform, and distribute the Software and make,
** use, sell, offer for sale, import, export, have made, and have sold the
** Software and the Larger Work(s), and to sublicense the foregoing rights on
** either these or other terms.
**
** This license is subject to the following condition:
** The above copyright notice and either this complete permission notice or at
** a minimum a reference to the UPL must be included in all copies or
** substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
*/
package oracle.jdbc.provider.oson.sample.model;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Movie {
@JsonProperty("_id")
public int id;
public String title;
public String genre;
public BigDecimal gross;
public OffsetDateTime released;
public List<Image> image;

public Movie() {}
public Movie(int id, String title, String genre, BigDecimal gross, OffsetDateTime released, List<Image> image) {
this.id = id;
this.title = title;
this.genre = genre;
this.gross = gross;
this.released = released;
this.image = image;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
return id == movie.id
&& title.equals(movie.title)
&& genre.equals(movie.genre)
&& gross.compareTo(movie.gross) == 0
&& released.isEqual(movie.released)
&& image.equals(movie.image);
}

@Override
public int hashCode() {
return Objects.hash(id, title, genre, gross, released, image);
}
}


Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
/*
** Copyright (c) 2024 Oracle and/or its affiliates.
**
** The Universal Permissive License (UPL), Version 1.0
**
** Subject to the condition set forth below, permission is hereby granted to any
** person obtaining a copy of this software, associated documentation and/or data
** (collectively the "Software"), free of charge and under any and all copyright
** rights in the Software, and any and all patent rights owned or freely
** licensable by each licensor hereunder covering either (i) the unmodified
** Software as contributed to or provided by such licensor, or (ii) the Larger
** Works (as defined below), to deal in both
**
** (a) the Software, and
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
** one is included with the Software (each a "Larger Work" to which the Software
** is contributed by such licensors),
**
** without restriction, including without limitation the rights to copy, create
** derivative works of, display, perform, and distribute the Software and make,
** use, sell, offer for sale, import, export, have made, and have sold the
** Software and the Larger Work(s), and to sublicense the foregoing rights on
** either these or other terms.
**
** This license is subject to the following condition:
** The above copyright notice and either this complete permission notice or at
** a minimum a reference to the UPL must be included in all copies or
** substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
*/
package oracle.jdbc.provider.oson.sample.model;

import java.util.Objects;
Expand Down
Loading