|
27 | 27 |
|
28 | 28 | import static java.lang.String.format; |
29 | 29 | import static org.firebirdsql.common.FBTestProperties.*; |
| 30 | +import static org.firebirdsql.common.FbAssumptions.assumeSchemaSupport; |
30 | 31 | import static org.firebirdsql.common.matchers.MatcherAssume.assumeThat; |
31 | 32 | 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; |
32 | 37 | import static org.firebirdsql.jdbc.FBDatabaseMetaData.*; |
33 | 38 | import static org.firebirdsql.util.FirebirdSupportInfo.supportInfoFor; |
34 | 39 | import static org.hamcrest.CoreMatchers.anyOf; |
|
44 | 49 | * |
45 | 50 | * @author David Jencks |
46 | 51 | * @author Mark Rotteveel |
47 | | - * @version 1.0 |
48 | 52 | */ |
49 | 53 | class FBDatabaseMetaDataTest { |
50 | 54 |
|
@@ -802,6 +806,137 @@ void testSupportsSchemasInXXX() { |
802 | 806 | ); |
803 | 807 | } |
804 | 808 |
|
| 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 | + |
805 | 940 | @SuppressWarnings("SameParameterValue") |
806 | 941 | private void createPackage(String packageName, String procedureName) throws Exception { |
807 | 942 | try (Statement stmt = connection.createStatement()) { |
|
0 commit comments