protected String buildQuery(String tableName, String[] requestedColumnNames, String[] conditionColumnNames, String[] conditionValues) throws SQLException {
    if (!tableExists(tableName)) {
      throw new IllegalArgumentException(String.format("%s table does not exist", tableName));
    }
    StringBuilder builder = new StringBuilder();
    builder.append("SELECT ");

    // Append the requested column names:
    if ((requestedColumnNames == null) || (requestedColumnNames.length == 0)) {
      throw new IllegalArgumentException("no columns for the select have been set");
    }
    for (String name : requestedColumnNames) {
      if (!tableHasColumn(tableName, name)) {
        throw new IllegalArgumentException(String.format("%s table does not contain %s column", tableName, name));
      }
    }
    builder.append(requestedColumnNames[0]);
    for (int i = 1; i < requestedColumnNames.length; i++) {
      builder.append(", ").append(requestedColumnNames[i]);
    }

    // Append the source table
    builder.append(" FROM ").append(tableName);

    // Add the WHERE clause using the conditionColumnNames and the conditionValues
    if (conditionColumnNames != null && conditionColumnNames.length > 0) {
      for (String name : conditionColumnNames) {
        if (!tableHasColumn(tableName, name)) {
          throw new IllegalArgumentException(String.format("%s table does not contain %s column", tableName, name));
        }
      }
      if (conditionColumnNames.length != conditionValues.length) {
        throw new IllegalArgumentException("number of columns should be equal to number of values");
      }
      builder.append(" WHERE ").append(conditionColumnNames[0]).append("='").append(conditionValues[0]).append("'");
      for (int i = 1; i < conditionColumnNames.length; i++) {
        builder.append(" AND ").append(conditionColumnNames[i]).append("='").append(conditionValues[i]).append("'");
      }
    }

    return builder.toString();
  }
Build a SELECT statement using the supplied table name, request columns and conditional column/value pairs.
<p>
The conditional pairs are optional but multiple pairs will be ANDed together.
<p>
Examples:
<ul>
<li>SELECT id FROM table1</li>
<li>SELECT id FROM table1 WHERE name='value1'</li>
<li>SELECT id FROM table1 WHERE name='value1' AND key='key1'</li>
<li>SELECT id, name FROM table1 WHERE key='key1'</li>
<li>SELECT id, name FROM table1 WHERE key='key1' AND allowed='1'</li>
</ul>

@param tableName            the table name
@param requestedColumnNames an array of column names to select
@param conditionColumnNames an array of column names to use in the where clause
@param conditionValues      an array of value to pair with the column names in conditionColumnNames
@return a query string
@throws SQLException
ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java