diff --git a/lib/plsql/schema.rb b/lib/plsql/schema.rb index cef7d0d8..7086efdd 100644 --- a/lib/plsql/schema.rb +++ b/lib/plsql/schema.rb @@ -90,7 +90,7 @@ def logoff # Current Oracle schema name def schema_name return nil unless connection - @schema_name ||= select_first("SELECT SYS_CONTEXT('userenv','session_user') FROM dual")[0] + @schema_name ||= select_first("SELECT SYS_CONTEXT('userenv','current_schema') FROM dual")[0] end # Default timezone to which database values will be converted - :utc or :local diff --git a/spec/plsql/package_spec.rb b/spec/plsql/package_spec.rb index df748f07..63f05533 100644 --- a/spec/plsql/package_spec.rb +++ b/spec/plsql/package_spec.rb @@ -20,7 +20,6 @@ END test_procedure; END; SQL - end after(:all) do @@ -67,6 +66,44 @@ end end + context "with a user with execute privilege who is not the package owner" do + before(:all) do + plsql.execute("grant execute on TEST_PACKAGE to #{DATABASE_USERS_AND_PASSWORDS[1][0]}") + @original_connection = plsql.connection + @conn = get_connection(1) + end + + before(:each) do + # resetting connection clears cached package objects and schema name + plsql.connection = @conn + end + + after(:all) do + plsql.logoff + plsql.connection = @original_connection + end + + it "should not find existing package" do + expect(PLSQL::Package.find(plsql, :test_package)).to be_nil + end + + context "who sets current_schema to match the package owner" do + before(:all) do + plsql.execute "ALTER SESSION set current_schema=#{DATABASE_USERS_AND_PASSWORDS[0][0]}" + end + + it "should find existing package" do + expect(PLSQL::Package.find(plsql, :test_package)).not_to be_nil + end + + it "should report an existing procedure as existing" do + expect(plsql.test_package.procedure_defined?(:test_procedure)).to be_truthy + end + + end + + end + describe "variables" do it "should set and get package variable value" do plsql.test_package.test_variable = 1 diff --git a/spec/plsql/schema_spec.rb b/spec/plsql/schema_spec.rb index 927b3e4d..4865a0ee 100644 --- a/spec/plsql/schema_spec.rb +++ b/spec/plsql/schema_spec.rb @@ -37,6 +37,13 @@ expect(plsql.schema_name).to eq(DATABASE_USERS_AND_PASSWORDS[0][0].upcase) end + it 'should match altered current_schema in database session' do + plsql.connection = @conn + expected_current_schema = DATABASE_USERS_AND_PASSWORDS[1][0] + plsql.execute "ALTER SESSION set current_schema=#{expected_current_schema}" + expect(plsql.schema_name).to eq(expected_current_schema.upcase) + end + it "should return new schema name after reconnection" do plsql.connection = @conn expect(plsql.schema_name).to eq(DATABASE_USERS_AND_PASSWORDS[0][0].upcase)