1

我正在努力处理 JQL 查询。

我们有一个名为“Build Reported”的自定义字段,它是一个文本字段。它具有“4.7.323H”、“5.1.123L”、“3.1.456E”等值。

我需要编写一个简单的查询,它会给我在用户指定的版本之后报告的所有问题。

JQL 函数原型:searchIssues('Build Integrated', '>', '4.7.323B')

为此,我正在触发一个 JQL 查询,它为我提供所有问题的 Build Reported,然后我遍历每个问题并执行逐字符比较以确定当前问题的 Build Reported 版本是否大于用户指定的那个。这似乎需要很长时间才能执行,因为我必须从 jira 数据库中检索所有问题。

有没有更快的方法来实现这一目标?这是我到目前为止所拥有的:

// Get all the arguments
java.util.List args = operand.getArgs();
CustomField cf = customFieldManager.getCustomFieldObjectByName((String)args.get(0));
Long cfID = cf.getIdAsLong();
String operator = (String)args.get(1);
String userVersion = (String)args.get(2);
String jiraVersion = "";

java.util.List issues;
Iterator issuesIterator;
Issue issue;

issues = getAllIssues(user, interestedInVersion, cfID);
issuesIterator = issues.iterator();

// Iterate over all the issues
while(issuesIterator.hasNext())
{
    issue = (Issue)issuesIterator.next();

    // Get the Build reported value
    jiraVersion = (String)issue.getCustomFieldValue(cf);

    if(jiraVersion != null &&
       !jiraVersion.equals(""))
    {
        // Compare user-specified version to the one retrieved from database
        if(compareVersions(jiraVersion, userVersion, operator))
        {
            // Add the issue to the result set
            literals.add(new QueryLiteral(operand, issue.getId()));
        }
    }
}

// cfID is the ID for the custom field Build Reported
private java.util.List getAllIssues(User user, Long cfID) throws SearchException, ParseException
{
    JqlQueryBuilder builder = JqlQueryBuilder.newBuilder();
    builder.where().project("SDEV").and().customField(cfID).isNotEmpty();
    Query query = builder.buildQuery();
    SearchResults results = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
    return results.getIssues();
}

请注意,我没有任何其他过滤器可用于 JQL 查询构建器来帮助我减小结果集的大小。

4

1 回答 1

1

我找到了我在问题中描述的问题的替代方案。我最终没有使用 JQL,而是直接触发了 SELECT,结果证明这要快得多。下面的函数是 JQL 插件的一部分。这是代码:

这就是我最终做的事情:

public java.util.List getValues(@NotNull QueryCreationContext queryCreationContext, @NotNull FunctionOperand operand, @NotNull TerminalClause terminalClause)
{
    try
    {
        // User
        User user = queryCreationContext.getUser();

        // Args
        java.util.List args = operand.getArgs();
        CustomField cf = customFieldManager.getCustomFieldObjectByName((String)args.get(0));
        Long cfID = cf.getIdAsLong();
        String operator = (String)args.get(1);
        String userVersion = (String)args.get(2);

        // Locals
        java.util.List literals = new java.util.LinkedList();
        MutableIssue issue = null;
        String issueId = "";
        String jiraVersion = "";

        // DB
        Connection conn = null;
        String url = "jdbc:jtds:sqlserver://*****:*****/jiradb";
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        String userName = "*******";
        String password = "*******";
        String sqlQuery = null;
        Statement statement = null;
        ResultSet resultSet = null;

        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url, userName, password);

        // Get all the issues that has the custom field set
        sqlQuery = " SELECT t2.id AS IssueId, t1.stringvalue AS JiraVersion " + "\n" +
                        " FROM   jiradb.jiraschema.customfieldvalue t1 " + "\n" +
                        " INNER JOIN jiradb.jiraschema.jiraissue t2 " + "\n" +
                        " ON     t1.issue = t2.id " + "\n" +
                        " WHERE  t1.customfield = " + Long.toString(cfID) + " " + "\n" +
                        " AND    t1.stringvalue IS NOT NULL " + "\n" +
                        " AND    t1.stringvalue != '' " + "\n" +
                        " AND    t2.pkey LIKE 'SDEV-%' ";

        // Iterate over the result set
        statement = conn.createStatement();
        resultSet = statement.executeQuery(sqlQuery);
        while (resultSet.next()) 
        {  
            issueId = resultSet.getString("IssueId");  
            jiraVersion = resultSet.getString("JiraVersion");

            // Compare the version from jira with the user provided version
            // This is my own function that does char-by-char comparison
            if(compareVersions(jiraVersion, userVersion, operator))
            {
                // Get the issue object to add to the result
                issue = ComponentManager.getInstance().getIssueManager().getIssueObject(Long.parseLong(issueId));

                // Add the issue to the result
                literals.add(new QueryLiteral(operand, issue.getId()));
            }
        }

        // Return all the matching issues here
        return literals;
    }
    catch(Exception e)
    {
        // Exception handling
    }

    return null;
}

这就是插件的使用方式:

issue in searchIssues('Build Reported', '>', '5.1.104');
于 2012-11-12T21:20:23.890 回答