6.9.1.4.7 別のユーザーのプロパティ・グラフの問合せ
データベースの適切な権限を付与されている場合は、別のユーザーのプロパティ・グラフのデータを問い合せることができます。たとえば、SCOTTのスキーマでGRAPH1を問い合せるには、SCOTT.GRAPH1GE$、SCOTT.GRAPH1VT$、SCOTT.GRAPH1GT$およびSCOTT.GRAPH1VD$に対するREAD権限が必要です。
例6-22 PgqlExample15.java
PgqlExample15.java
は、他のユーザーがSCOTTのスキーマ内のグラフを問い合せる方法を示しています。
import java.sql.Connection;
import oracle.pg.rdbms.pgql.PgqlConnection;
import oracle.pg.rdbms.pgql.PgqlResultSet;
import oracle.pg.rdbms.pgql.PgqlStatement;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
/**
* This example shows how to query a property graph located in another user's
* schema. READ privilege on GE$, VT$, GT$ and VD$ tables for the other user's
* property graph are required to avoid ORA-00942: table or view does not exist.
*/
public class PgqlExample15
{
public static void main(String[] args) throws Exception
{
int idx=0;
String host = args[idx++];
String port = args[idx++];
String sid = args[idx++];
String user = args[idx++];
String password = args[idx++];
String graph = args[idx++];
Connection conn = null;
PgqlStatement ps = null;
PgqlResultSet rs = null;
try {
//Get a jdbc connection
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setURL("jdbc:oracle:thin:@"+host+":"+port +":"+sid);
pds.setUser(user);
pds.setPassword(password);
conn = pds.getConnection();
// Get a PGQL connection
PgqlConnection pgqlConn = PgqlConnection.getConnection(conn);
pgqlConn.setGraph(graph);
// Set schema so that we can query Scott's graph
pgqlConn.setSchema("SCOTT");
// Create a PgqlStatement
ps = pgqlConn.createStatement();
// Execute query to get a ResultSet object
String pgql =
"SELECT v.\"fname\" AS fname, v.\"lname\" AS lname "+
"FROM MATCH (v)";
rs = ps.executeQuery(pgql, "");
// Print query results
rs.print();
}
finally {
// close the result set
if (rs != null) {
rs.close();
}
// close the statement
if (ps != null) {
ps.close();
}
// close the connection
if (conn != null) {
conn.close();
}
}
}
}
次のSQL文は、データベース・ユーザーUSER2を作成し、必要な権限を付与します。また、OraclePropertyGraph.grantAccess
Java APIを使用しても同じ効果を得ることができます。
SQL> grant connect, resource, unlimited tablespace to user2 identified by user2; Grant succeeded. SQL> grant read on scott.test_graphvt$ to user2; Grant succeeded. SQL> grant read on scott.test_graphge$ to user2; Grant succeeded. SQL> grant read on scott.test_graphgt$ to user2; Grant succeeded. SQL> grant read on scott.test_graphvd$ to user2; Grant succeeded.
データベースにUSER2として接続した場合のtest_graph
データ・セットのPgqlExample15.java
の出力は次のようになります。PgqlExample15
を実行する前に、test_graph
は(GraphLoaderExample.java
コードを使用して)ユーザーSCOTTによってGRAPH1としてすでにロードされている必要があることに注意してください。
+---------------+ | FNAME | LNAME | +---------------+ | Susan | Blue | | Bill | Brown | | Ray | Green | | John | Black | +---------------+