Skip to content
Permalink
Browse files

refactor: make HostChooser implement Iterable

make HostChooser implement Iterable. Also refactoring of usage of Iterators

fixes #645
  • Loading branch information
AlexElin authored and vlsi committed Nov 12, 2016
1 parent 0275d40 commit 3d37db78ae4a82cab8809bac86e3b55537e91d95
@@ -39,7 +39,7 @@ public static void authenticate(PGStream pgStream, String host, String user, Str
kerberosServerName = "postgres";
}

Exception result = null;
Exception result;
try {
boolean performAuthentication = true;
GSSCredential gssCredential = null;
@@ -12,11 +12,12 @@
/**
* Lists connections in preferred order.
*/
public interface HostChooser {
public interface HostChooser extends Iterable<HostSpec> {
/**
* Lists connection hosts in preferred order.
*
* @return connection hosts in preferred order.
*/
@Override
Iterator<HostSpec> iterator();
}
@@ -37,6 +37,7 @@ protected MultiHostChooser(HostSpec[] hostSpecs, HostRequirement targetServerTyp
loadBalance = parseBoolean(info.getProperty("loadBalanceHosts", "false"));
}

@Override
public Iterator<HostSpec> iterator() {
List<HostSpecStatus> candidates =
GlobalHostStatusTracker.getCandidateHosts(hostSpecs, targetServerType, hostRecheckTime);
@@ -21,6 +21,7 @@ public SingleHostChooser(HostSpec hostSpec) {
this.hostSpec = Collections.singletonList(hostSpec);
}

@Override
public Iterator<HostSpec> iterator() {
return hostSpec.iterator();
}
@@ -24,6 +24,7 @@
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -1288,6 +1289,7 @@ protected ResultSet getProcedureColumns(int jdbcVersion, String catalog,
return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
}

@Override
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern,
String types[]) throws SQLException {
String select;
@@ -1500,6 +1502,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
ht.put("NOSCHEMAS", "c.relkind = 'm'");
}

@Override
public ResultSet getSchemas() throws SQLException {
return getSchemas(getJDBCMajorVersion(), null, null);
}
@@ -1546,6 +1549,7 @@ protected ResultSet getSchemas(int jdbcVersion, String catalog, String schemaPat
* PostgreSQL does not support multiple catalogs from a single connection, so to reduce confusion
* we only return the current catalog. {@inheritDoc}
*/
@Override
public ResultSet getCatalogs() throws SQLException {
Field f[] = new Field[1];
List<byte[][]> v = new ArrayList<byte[][]>();
@@ -1557,21 +1561,17 @@ public ResultSet getCatalogs() throws SQLException {
return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
}

@Override
public ResultSet getTableTypes() throws SQLException {
String types[] = new String[tableTypeClauses.size()];
Iterator<String> e = tableTypeClauses.keySet().iterator();
int i = 0;
while (e.hasNext()) {
types[i++] = e.next();
}
sortStringArray(types);
String types[] = tableTypeClauses.keySet().toArray(new String[0]);
Arrays.sort(types);

Field f[] = new Field[1];
List<byte[][]> v = new ArrayList<byte[][]>();
f[0] = new Field("TABLE_TYPE", Oid.VARCHAR);
for (i = 0; i < types.length; i++) {
for (String type : types) {
byte[][] tuple = new byte[1][];
tuple[0] = connection.encodeString(types[i]);
tuple[0] = connection.encodeString(type);
v.add(tuple);
}

@@ -1803,12 +1803,14 @@ protected ResultSet getColumns(int jdbcVersion, String catalog, String schemaPat
return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
}

@Override
public ResultSet getColumns(String catalog, String schemaPattern,
String tableNamePattern, String columnNamePattern) throws SQLException {
return getColumns(getJDBCMajorVersion(), catalog, schemaPattern, tableNamePattern,
columnNamePattern);
}

@Override
public ResultSet getColumnPrivileges(String catalog, String schema, String table,
String columnNamePattern) throws SQLException {
Field f[] = new Field[8];
@@ -1878,32 +1880,22 @@ public ResultSet getColumnPrivileges(String catalog, String schema, String table
String owner = rs.getString("rolname");
String relAcl = rs.getString("relacl");

// For instance: SELECT -> user1 -> list of [grantor, grantable]
Map<String, Map<String, List<String[]>>> permissions = parseACL(relAcl, owner);

if (connection.haveMinimumServerVersion(ServerVersion.v8_4)) {
String acl = rs.getString("attacl");
Map<String, Map<String, List<String[]>>> relPermissions = parseACL(acl, owner);
permissions.putAll(relPermissions);
}
String permNames[] = new String[permissions.size()];
Iterator<String> e = permissions.keySet().iterator();
int i = 0;
while (e.hasNext()) {
permNames[i++] = e.next();
}
sortStringArray(permNames);
for (i = 0; i < permNames.length; i++) {
byte[] privilege = connection.encodeString(permNames[i]);
Map<String, List<String[]>> grantees = permissions.get(permNames[i]);
String granteeUsers[] = new String[grantees.size()];
Iterator<String> g = grantees.keySet().iterator();
int k = 0;
while (g.hasNext()) {
granteeUsers[k++] = g.next();
}
for (int j = 0; j < grantees.size(); j++) {
List<String[]> grantor = grantees.get(granteeUsers[j]);
String grantee = granteeUsers[j];
String permNames[] = permissions.keySet().toArray(new String[0]);
Arrays.sort(permNames);
for (String permName : permNames) {
byte[] privilege = connection.encodeString(permName);
Map<String, List<String[]>> grantees = permissions.get(permName);
for (Map.Entry<String, List<String[]>> userToGrantable : grantees.entrySet()) {
List<String[]> grantor = userToGrantable.getValue();
String grantee = userToGrantable.getKey();
for (String[] grants : grantor) {
String grantable = owner.equals(grantee) ? "YES" : grants[1];
byte[][] tuple = new byte[8][];
@@ -1926,6 +1918,7 @@ public ResultSet getColumnPrivileges(String catalog, String schema, String table
return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
}

@Override
public ResultSet getTablePrivileges(String catalog, String schemaPattern,
String tableNamePattern) throws SQLException {
Field f[] = new Field[7];
@@ -1969,24 +1962,14 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern,
String owner = rs.getString("rolname");
String acl = rs.getString("relacl");
Map<String, Map<String, List<String[]>>> permissions = parseACL(acl, owner);
String permNames[] = new String[permissions.size()];
Iterator<String> e = permissions.keySet().iterator();
int i = 0;
while (e.hasNext()) {
permNames[i++] = e.next();
}
sortStringArray(permNames);
for (i = 0; i < permNames.length; i++) {
byte[] privilege = connection.encodeString(permNames[i]);
Map<String, List<String[]>> grantees = permissions.get(permNames[i]);
String granteeUsers[] = new String[grantees.size()];
Iterator<String> g = grantees.keySet().iterator();
int k = 0;
while (g.hasNext()) {
granteeUsers[k++] = g.next();
}
for (String granteeUser : granteeUsers) {
List<String[]> grants = grantees.get(granteeUser);
String permNames[] = permissions.keySet().toArray(new String[0]);
Arrays.sort(permNames);
for (String permName : permNames) {
byte[] privilege = connection.encodeString(permName);
Map<String, List<String[]>> grantees = permissions.get(permName);
for (Map.Entry<String, List<String[]>> userToGrantable : grantees.entrySet()) {
List<String[]> grants = userToGrantable.getValue();
String granteeUser = userToGrantable.getKey();
for (String[] grantTuple : grants) {
// report the owner as grantor if it's missing
String grantor = grantTuple[0] == null ? owner : grantTuple[0];
@@ -2012,18 +1995,6 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern,
return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
}

private static void sortStringArray(String s[]) {
for (int i = 0; i < s.length - 1; i++) {
for (int j = i + 1; j < s.length; j++) {
if (s[i].compareTo(s[j]) > 0) {
String tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
}

/**
* Parse an String of ACLs into a List of ACLs.
*/
@@ -2164,6 +2135,7 @@ private static void addACLPrivileges(String acl, Map<String, Map<String, List<St
/**
* Take the a String representing an array of ACLs and return a Map mapping the SQL permission
* name to a List of usernames who have that permission.
* For instance: SELECT -> user1 -> list of [grantor, grantable]
*
* @param aclArray ACL array
* @param owner owner
@@ -2610,7 +2582,7 @@ protected ResultSet getImportedExportedKeys(String primaryCatalog, String primar

int action = java.sql.DatabaseMetaData.importedKeyNoAction;

if (rule == null || "noaction".equals(rule)) {
if ("noaction".equals(rule)) {
action = java.sql.DatabaseMetaData.importedKeyNoAction;
}
if ("cascade".equals(rule)) {
@@ -2896,7 +2868,7 @@ public ResultSet getIndexInfo(String catalog, String schema, String tableName,
} else {
String select;
String from;
String where = "";
String where;

if (connection.haveMinimumServerVersion(ServerVersion.v7_3)) {
select = "SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM, ";
@@ -3126,7 +3098,8 @@ public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePa
}


public java.sql.Connection getConnection() throws SQLException {
@Override
public Connection getConnection() throws SQLException {
return connection;
}

@@ -3140,7 +3113,7 @@ public boolean rowChangesAreVisible(int type) throws SQLException {
return false;
}

protected java.sql.Statement createMetaDataStatement() throws SQLException {
protected Statement createMetaDataStatement() throws SQLException {
return connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
@@ -3153,22 +3126,27 @@ public boolean supportsRefCursors() throws SQLException {
return true;
}

@Override
public RowIdLifetime getRowIdLifetime() throws SQLException {
throw org.postgresql.Driver.notImplemented(this.getClass(), "getRowIdLifetime()");
}

@Override
public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException {
return getSchemas(getJDBCMajorVersion(), catalog, schemaPattern);
}

@Override
public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException {
return true;
}

@Override
public boolean autoCommitFailureClosesAllResultSets() throws SQLException {
return false;
}

@Override
public ResultSet getClientInfoProperties() throws SQLException {
Field f[] = new Field[4];
f[0] = new Field("NAME", Oid.VARCHAR);

0 comments on commit 3d37db7

Please sign in to comment.
You can’t perform that action at this time.