-
Notifications
You must be signed in to change notification settings - Fork 119
Add comprehensive JDBC Guide #3784
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
Open
arnaud-lacurie
wants to merge
1
commit into
FoundationDB:main
Choose a base branch
from
arnaud-lacurie:docs/jdbc-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| ====================== | ||
| Advanced JDBC Features | ||
| ====================== | ||
|
|
||
| .. important:: | ||
| The JDBC interface is experimental and not production-ready at this stage. APIs and behaviors are subject to change. | ||
|
|
||
| This guide covers FoundationDB Record Layer-specific JDBC features for working with complex data types like STRUCTs and ARRAYs. | ||
|
|
||
| .. note:: | ||
| **Driver-Specific Classes**: When creating STRUCT and ARRAY values, use the appropriate builder for your JDBC driver: | ||
|
|
||
| - **Embedded Driver**: ``EmbeddedRelationalStruct`` and ``EmbeddedRelationalArray`` (from ``com.apple.foundationdb.relational.api``) | ||
| - **Server Driver**: ``JDBCRelationalStruct`` and ``JDBCRelationalArray`` (from ``com.apple.foundationdb.relational.jdbc``) | ||
|
|
||
| Both provide identical APIs, so the code examples in this guide work with either by changing the import and class name. | ||
|
|
||
| Working with STRUCT Types | ||
| ========================== | ||
|
|
||
| The Record Layer extends standard JDBC to support STRUCT types, which represent nested record structures similar to protobuf messages. STRUCTs allow you to model hierarchical data within your relational schema. | ||
|
|
||
| Reading STRUCT Values | ||
| --------------------- | ||
|
|
||
| To read STRUCT values from a query result, unwrap the ``ResultSet`` to ``RelationalResultSet`` which provides direct access to STRUCTs: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::read-struct[] | ||
| :end-before: // end::read-struct[] | ||
| :dedent: 8 | ||
|
|
||
| Accessing Nested STRUCT Fields | ||
| ------------------------------- | ||
|
|
||
| STRUCTs can contain other STRUCTs, allowing for deeply nested data structures. Here's how to read a STRUCT with a nested STRUCT: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/ComplexTypesEmbedded.java | ||
| :language: java | ||
| :start-after: // tag::query-struct[] | ||
| :end-before: // end::query-struct[] | ||
| :dedent: 4 | ||
| :lines: 1-26 | ||
|
|
||
| Creating STRUCT Values | ||
| ---------------------- | ||
|
|
||
| Use the appropriate builder depending on your JDBC driver. Both builders provide identical APIs. Here's an example inserting a STRUCT into a STRUCT column: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::create-struct-simple[] | ||
| :end-before: // end::create-struct-simple[] | ||
| :dedent: 8 | ||
|
|
||
| Creating Nested STRUCTs | ||
| ----------------------- | ||
|
|
||
| You can create STRUCTs that contain nested STRUCTs and insert them as a single value. Here's an example that creates a customer STRUCT with a nested address STRUCT: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/ComplexTypesEmbedded.java | ||
| :language: java | ||
| :start-after: // tag::insert-struct[] | ||
| :end-before: // end::insert-struct[] | ||
| :dedent: 4 | ||
| :lines: 1-20 | ||
|
|
||
| Handling NULL STRUCT Fields | ||
| ---------------------------- | ||
|
|
||
| You can set NULL values for individual fields within a STRUCT: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::create-struct-null[] | ||
| :end-before: // end::create-struct-null[] | ||
| :dedent: 8 | ||
|
|
||
| Working with ARRAY Types | ||
| ========================= | ||
|
|
||
| The Record Layer supports ARRAY types containing elements of any SQL type, including primitives, STRUCTs, and even nested ARRAYs. | ||
|
|
||
| Reading ARRAY Values | ||
| -------------------- | ||
|
|
||
| Use the ``getArray()`` method to retrieve ARRAY values from a ``ResultSet``: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::read-array-basic[] | ||
| :end-before: // end::read-array-basic[] | ||
| :dedent: 8 | ||
|
|
||
| Using ResultSet to Iterate Arrays | ||
| ---------------------------------- | ||
|
|
||
| You can also retrieve array elements as a ``ResultSet`` for more structured access: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::read-array-resultset[] | ||
| :end-before: // end::read-array-resultset[] | ||
| :dedent: 8 | ||
|
|
||
| Creating ARRAY Values | ||
| --------------------- | ||
|
|
||
| Use the appropriate builder depending on your JDBC driver. Both builders provide identical APIs: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::create-array-basic[] | ||
| :end-before: // end::create-array-basic[] | ||
| :dedent: 8 | ||
|
|
||
| Working with Different Array Element Types | ||
| ------------------------------------------- | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::create-array-types[] | ||
| :end-before: // end::create-array-types[] | ||
| :dedent: 8 | ||
|
|
||
| Handling NULL Arrays | ||
| -------------------- | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::array-null[] | ||
| :end-before: // end::array-null[] | ||
| :dedent: 8 | ||
|
|
||
| Complex Nested Types | ||
| ==================== | ||
|
|
||
| Arrays of STRUCTs | ||
| ----------------- | ||
|
|
||
| ARRAYs can contain STRUCT elements, allowing for collections of complex records: | ||
|
|
||
| .. tab-set:: | ||
|
|
||
| .. tab-item:: Embedded Driver | ||
| :sync: embedded | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::array-of-structs[] | ||
| :end-before: // end::array-of-structs[] | ||
| :dedent: 8 | ||
|
|
||
| .. tab-item:: Server Driver | ||
| :sync: server | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippetsServer.java | ||
| :language: java | ||
| :start-after: // tag::array-of-structs[] | ||
| :end-before: // end::array-of-structs[] | ||
| :dedent: 8 | ||
|
|
||
| Reading Arrays of STRUCTs | ||
| ------------------------- | ||
|
|
||
| When reading arrays that contain STRUCT elements, unwrap the array's ``ResultSet`` to ``RelationalResultSet``: | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/ComplexTypesEmbedded.java | ||
| :language: java | ||
| :start-after: // tag::query-struct[] | ||
| :end-before: // end::query-struct[] | ||
| :dedent: 4 | ||
| :lines: 28-47 | ||
|
|
||
| STRUCTs Containing Arrays | ||
| ------------------------- | ||
|
|
||
| STRUCTs can contain ARRAY fields. You can insert the entire STRUCT including its arrays as a single value: | ||
|
|
||
| .. tab-set:: | ||
|
|
||
| .. tab-item:: Embedded Driver | ||
| :sync: embedded | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::struct-containing-arrays[] | ||
| :end-before: // end::struct-containing-arrays[] | ||
| :dedent: 8 | ||
|
|
||
| .. tab-item:: Server Driver | ||
| :sync: server | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippetsServer.java | ||
| :language: java | ||
| :start-after: // tag::struct-containing-arrays[] | ||
| :end-before: // end::struct-containing-arrays[] | ||
| :dedent: 8 | ||
|
|
||
| Inspecting Array Metadata | ||
| ========================== | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::array-metadata[] | ||
| :end-before: // end::array-metadata[] | ||
| :dedent: 8 | ||
|
|
||
| Inspecting STRUCT Metadata | ||
| =========================== | ||
|
|
||
| .. literalinclude:: ../../../../examples/src/main/java/com/apple/foundationdb/relational/jdbc/examples/AdvancedSnippets.java | ||
| :language: java | ||
| :start-after: // tag::struct-metadata[] | ||
| :end-before: // end::struct-metadata[] | ||
| :dedent: 8 | ||
|
|
||
| See Also | ||
| ======== | ||
|
|
||
| - :doc:`basic` - Basic JDBC usage patterns | ||
| - :doc:`../SQL_Reference` - Complete SQL syntax reference | ||
| - :doc:`../reference/Databases_Schemas_SchemaTemplates` - Understanding the data model | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this true? Do we not have an API that works for both the embedded and server use cases? I would have expected that there'd be something like a
RelationalStructobject that the user could create (in the API package) and then something could translate that to the appropriate one.It almost seems like this is a deficiency in our API that is worth addressing before we try and document it too much, including the infrastructure to have parallel documentation for the different paths, but maybe that ship has sailed