Skip to content

Commit a35a3a0

Browse files
committed
Correct documentation of getXXXSourceCode methods
- Fix typo in table name - Add tests
1 parent d934271 commit a35a3a0

4 files changed

Lines changed: 149 additions & 12 deletions

File tree

src/docs/asciidoc/release_notes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ The `schema` can be `null` or empty string for schemaless tables (i.e. Firebird
582582
[#other-fixes-and-changes]
583583
=== Other fixes and changes
584584
585+
* Changed the documentation of `FirebirdDatabaseMetaData` methods `getProcedureSourceCode`, `getTriggerSourceCode` and `getViewSourceCode` that the object not being found is reported by a `null` value, not a `SQLException`.
586+
With this change, the documentation reflects the actual behaviour.
585587
* ...
586588
587589
[#compatibility-changes]

src/main/org/firebirdsql/jdbc/FBDatabaseMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ public String getViewSourceCode(String schema, String viewName) throws SQLExcept
18981898

18991899
private enum SourceObjectType {
19001900
PROCEDURE("RDB$PROCEDURES", "RDB$PROCEDURE_SOURCE", "RDB$PROCEDURE_NAME"),
1901-
TRIGGER("RDB$TRIGGERS", "RDB$TRIGGER_SOUCE", "RDB$TRIGGER_NAME"),
1901+
TRIGGER("RDB$TRIGGERS", "RDB$TRIGGER_SOURCE", "RDB$TRIGGER_NAME"),
19021902
VIEW("RDB$RELATIONS", "RDB$VIEW_SOURCE", "RDB$RELATION_NAME"),
19031903
;
19041904

src/main/org/firebirdsql/jdbc/FirebirdDatabaseMetaData.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public interface FirebirdDatabaseMetaData extends DatabaseMetaData {
5454
*
5555
* @param procedureName
5656
* name of the stored procedure
57-
* @return source of the stored procedure
57+
* @return source of the stored procedure, or {@code null} if not found or if the source column is {@code NULL}
5858
* @throws SQLException
59-
* if specified procedure cannot be found
59+
* for database access errors
6060
* @deprecated use {@link DatabaseMetaData#getProcedures(String, String, String)}, column
6161
* {@code JB_PROCEDURE_SOURCE}; there are currently no plans to remove this method
6262
*/
@@ -72,9 +72,9 @@ public interface FirebirdDatabaseMetaData extends DatabaseMetaData {
7272
*
7373
* @param triggerName
7474
* name of the trigger
75-
* @return source of the trigger
75+
* @return source of the trigger, or {@code null} if not found or if the source column is {@code NULL}
7676
* @throws SQLException
77-
* if specified trigger cannot be found
77+
* for database access errors
7878
* @see #getTriggerSourceCode(String, String)
7979
*/
8080
String getTriggerSourceCode(String triggerName) throws SQLException;
@@ -86,9 +86,9 @@ public interface FirebirdDatabaseMetaData extends DatabaseMetaData {
8686
* schema of the trigger ({@code null} drops the schema from the search; ignored on Firebird 5.0 and older)
8787
* @param triggerName
8888
* name of the trigger
89-
* @return source of the trigger
89+
* @return source of the trigger, or {@code null} if not found or if the source column is {@code NULL}
9090
* @throws SQLException
91-
* if specified trigger cannot be found
91+
* for database access errors
9292
* @since 7
9393
*/
9494
String getTriggerSourceCode(String schema, String triggerName) throws SQLException;
@@ -105,9 +105,9 @@ public interface FirebirdDatabaseMetaData extends DatabaseMetaData {
105105
*
106106
* @param viewName
107107
* name of the view
108-
* @return source of the view
108+
* @return source of the view, or {@code null} if not found or if the source column is {@code NULL}
109109
* @throws SQLException
110-
* if specified view cannot be found
110+
* for database access errors
111111
* @see #getViewSourceCode(String, String)
112112
*/
113113
String getViewSourceCode(String viewName) throws SQLException;
@@ -119,9 +119,9 @@ public interface FirebirdDatabaseMetaData extends DatabaseMetaData {
119119
* schema of the trigger ({@code null} drops the schema from the search; ignored on Firebird 5.0 and older)
120120
* @param viewName
121121
* name of the view
122-
* @return source of the view
122+
* @return source of the view, or {@code null} if not found or if the source column is {@code NULL}
123123
* @throws SQLException
124-
* if specified view cannot be found
124+
* for database access errors
125125
* @since 7
126126
*/
127127
String getViewSourceCode(String schema, String viewName) throws SQLException;

src/test/org/firebirdsql/jdbc/FBDatabaseMetaDataTest.java

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@
2727

2828
import static java.lang.String.format;
2929
import static org.firebirdsql.common.FBTestProperties.*;
30+
import static org.firebirdsql.common.FbAssumptions.assumeSchemaSupport;
3031
import static org.firebirdsql.common.matchers.MatcherAssume.assumeThat;
3132
import static org.firebirdsql.common.matchers.RegexMatcher.matchesRegex;
33+
import static org.firebirdsql.gds.ISCConstants.isc_dsql_drop_trigger_failed;
34+
import static org.firebirdsql.gds.ISCConstants.isc_dsql_table_not_found;
35+
import static org.firebirdsql.gds.ISCConstants.isc_dsql_view_not_found;
36+
import static org.firebirdsql.gds.ISCConstants.isc_no_meta_update;
3237
import static org.firebirdsql.jdbc.FBDatabaseMetaData.*;
3338
import static org.firebirdsql.util.FirebirdSupportInfo.supportInfoFor;
3439
import static org.hamcrest.CoreMatchers.anyOf;
@@ -44,7 +49,6 @@
4449
*
4550
* @author David Jencks
4651
* @author Mark Rotteveel
47-
* @version 1.0
4852
*/
4953
class FBDatabaseMetaDataTest {
5054

@@ -802,6 +806,137 @@ void testSupportsSchemasInXXX() {
802806
);
803807
}
804808

809+
@SuppressWarnings("deprecation")
810+
@Test
811+
void testGetProcedureSourceCode_String() throws SQLException {
812+
var dbmd = connection.getMetaData().unwrap(FirebirdDatabaseMetaData.class);
813+
assertNull(dbmd.getProcedureSourceCode("TEST_PROC_1"), "Expected no procedure source");
814+
815+
final String procedureBody = """
816+
begin
817+
/* TEST_PROC_1 body */
818+
end""";
819+
try (var stmt = connection.createStatement()) {
820+
stmt.execute("create procedure TEST_PROC_1 (VARIN integer) as " + procedureBody);
821+
}
822+
823+
assertEquals(procedureBody, dbmd.getProcedureSourceCode("TEST_PROC_1"), "Procedure source");
824+
}
825+
826+
@Test
827+
void testGetTriggerSourceCode_String() throws SQLException {
828+
var dbmd = connection.getMetaData().unwrap(FirebirdDatabaseMetaData.class);
829+
assertNull(dbmd.getTriggerSourceCode("TEST_TRIG_1"), "Expected no trigger source");
830+
831+
final String triggerBody = """
832+
as
833+
begin
834+
/* TEST_TRIG_1 body */
835+
end""";
836+
837+
try (var stmt = connection.createStatement()) {
838+
connection.setAutoCommit(false);
839+
try {
840+
stmt.execute("create table TEST_1 (id integer)");
841+
stmt.execute("create trigger TEST_TRIG_1 before insert on TEST_1 " + triggerBody);
842+
connection.commit();
843+
844+
assertEquals(triggerBody, dbmd.getTriggerSourceCode("TEST_TRIG_1"), "Trigger source");
845+
} finally {
846+
DdlHelper.executeDDL(stmt, List.of("drop table TEST_1"), isc_no_meta_update, isc_dsql_table_not_found,
847+
isc_dsql_view_not_found, isc_dsql_drop_trigger_failed);
848+
}
849+
} finally {
850+
connection.setAutoCommit(false);
851+
}
852+
}
853+
854+
@Test
855+
void testGetTriggerSourceCode_String_String() throws SQLException {
856+
assumeSchemaSupport();
857+
var dbmd = connection.getMetaData().unwrap(FirebirdDatabaseMetaData.class);
858+
assertNull(dbmd.getTriggerSourceCode("TEST_SCHEMA_1", "TEST_TRIG_1"), "Expected no trigger source");
859+
860+
final String triggerBody = """
861+
as
862+
begin
863+
/* TEST_SCHEMA_1.TEST_TRIG_1 body */
864+
end""";
865+
866+
try (var stmt = connection.createStatement()) {
867+
connection.setAutoCommit(false);
868+
try {
869+
stmt.execute("create schema TEST_SCHEMA_1");
870+
stmt.execute("create table TEST_SCHEMA_1.TEST_1 (id integer)");
871+
stmt.execute("create trigger TEST_TRIG_1 before insert on TEST_SCHEMA_1.TEST_1 " + triggerBody);
872+
connection.commit();
873+
874+
assertEquals(triggerBody, dbmd.getTriggerSourceCode("TEST_SCHEMA_1", "TEST_TRIG_1"), "Trigger source");
875+
assertEquals(triggerBody, dbmd.getTriggerSourceCode("TEST_TRIG_1"), "Trigger source");
876+
assertNull(dbmd.getTriggerSourceCode("PUBLIC", "TEST_TRIG_1"), "Expected no trigger source");
877+
} finally {
878+
DdlHelper.executeDDL(stmt, List.of("drop table TEST_SCHEMA_1.TEST_1", "drop schema TEST_SCHEMA_1"),
879+
isc_no_meta_update, isc_dsql_table_not_found, isc_dsql_view_not_found,
880+
isc_dsql_drop_trigger_failed);
881+
// TODO Add schema support: error code for drop schema failure?
882+
}
883+
} finally {
884+
connection.setAutoCommit(true);
885+
}
886+
887+
888+
}
889+
890+
@Test
891+
void testGetViewSourceCode_String() throws SQLException {
892+
var dbmd = connection.getMetaData().unwrap(FirebirdDatabaseMetaData.class);
893+
assertNull(dbmd.getViewSourceCode("TEST_VIEW_1"), "Expected no view source");
894+
895+
final String viewBody = """
896+
select 'TEST_VIEW_1' as VIEW_NAME, 1 as SOME_COLUMN
897+
from RDB$DATABASE""";
898+
try (var stmt = connection.createStatement()) {
899+
try {
900+
stmt.execute("create view TEST_VIEW_1 as " + viewBody);
901+
902+
assertEquals(viewBody, dbmd.getViewSourceCode("TEST_VIEW_1"), "View source");
903+
} finally {
904+
DdlHelper.executeDDL(stmt, List.of("drop view TEST_VIEW_1"), isc_no_meta_update,
905+
isc_dsql_table_not_found, isc_dsql_view_not_found, isc_dsql_drop_trigger_failed);
906+
}
907+
}
908+
}
909+
910+
@Test
911+
void testGetViewSourceCode_String_String() throws SQLException {
912+
assumeSchemaSupport();
913+
var dbmd = connection.getMetaData().unwrap(FirebirdDatabaseMetaData.class);
914+
assertNull(dbmd.getViewSourceCode("TEST_SCHEMA_1", "TEST_VIEW_1"), "Expected no view source");
915+
916+
final String viewBody = """
917+
select 'TEST_SCHEMA_1' as VIEW_SCHEMA, 'TEST_VIEW_1' as VIEW_NAME, 1 as SOME_COLUMN
918+
from RDB$DATABASE""";
919+
try (var stmt = connection.createStatement()) {
920+
connection.setAutoCommit(false);
921+
try {
922+
stmt.execute("create schema TEST_SCHEMA_1");
923+
stmt.execute("create view TEST_SCHEMA_1.TEST_VIEW_1 as " + viewBody);
924+
connection.commit();
925+
926+
assertEquals(viewBody, dbmd.getViewSourceCode("TEST_SCHEMA_1", "TEST_VIEW_1"), "View source");
927+
assertEquals(viewBody, dbmd.getViewSourceCode("TEST_VIEW_1"), "View source");
928+
assertNull(dbmd.getViewSourceCode("PUBLIC", "TEST_VIEW_1"), "Expected no view source");
929+
} finally {
930+
DdlHelper.executeDDL(stmt, List.of("drop view TEST_SCHEMA_1.TEST_VIEW_1", "drop schema TEST_SCHEMA_1"),
931+
isc_no_meta_update, isc_dsql_table_not_found, isc_dsql_view_not_found,
932+
isc_dsql_drop_trigger_failed);
933+
// TODO Add schema support: error code for drop schema failure?
934+
}
935+
} finally {
936+
connection.setAutoCommit(true);
937+
}
938+
}
939+
805940
@SuppressWarnings("SameParameterValue")
806941
private void createPackage(String packageName, String procedureName) throws Exception {
807942
try (Statement stmt = connection.createStatement()) {

0 commit comments

Comments
 (0)