简体   繁体   English

声纳插件查询数据库

[英]Sonar plugin query the database

I'm developing a sonar plugin and I have some questions about it. 我正在开发声纳插件,对此有一些疑问。 My plugin need to retrieve data from the database to manipulate them and display them in a page. 我的插件需要从数据库中检索数据以对其进行操作并将其显示在页面中。

Currently, my plugin is querying the database using JDBC driver but I think it will be a problem in production. 当前,我的插件正在使用JDBC驱动程序查询数据库,但是我认为这在生产中会是一个问题。 So I want to find a method to connect and query the database (just select query) from my plugin using an API, or an sonar object... 所以我想找到一种使用API​​或声纳对象从我的插件连接和查询数据库的方法(只是选择查询)...

I know there are some WebService, but they don't give me all information that I need, so I have to make queries by myself. 我知道有一些WebService,但是它们没有提供我所需的所有信息,因此我必须自己进行查询。

My plugin is for Sonarqube 4.1. 我的插件适用于Sonarqube 4.1。

I hope my explication was clear. 我希望我的解释很清楚。

I think I found the solution: 我想我找到了解决方案:

I have a Class who retrieve the databaseSessionFactory in the constructor. 我有一个在构造函数中检索databaseSessionFactory的Class。 I read somewhere that Sonar uses dependency injection by constructor. 我读过某处Sonar使用构造函数依赖注入。

    public class MyClass implements WebService{

        privateDatabaseSessionFactory sessionFactory;

        public MyClass(DatabaseSessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    }

And after, in this Class, I can use SessionFactory variable to Query the database: 之后,在该类中,我可以使用SessionFactory变量来查询数据库:

        DatabaseSession s = sessionFactory.getSession();
        String sqlQuery ="SELECT d.data FROM " + SnapshotSource.class.getName() + " d "
                    + "WHERE d.id = :Id";
        Query query = s.createQuery(sqlQuery);

        query.setParameter("Id", 1);

        List<String> queryResult = (List<String>) query.getResultList();
        //queryResult.get(0) contains the source

In this example, I get the first snapshot source in the database. 在此示例中,我获得了数据库中的第一个快照源。 I hope it's a good solution and it can help anybody. 我希望这是一个很好的解决方案,它可以帮助任何人。

You shall not query the SonarQube database directly. 您不得直接查询SonarQube数据库。 The DB schema is private and may change without notice, plus you risk to generate side effects to SonarQube itself. 数据库模式是私有的,可能会随时更改,恕不另行通知,此外,您还可能对SonarQube本身产生副作用。

If you need to read some stuff from the DB you should use the Web Services API that allows to query most of the "objects" manipulated by SonarQube. 如果您需要从数据库中读取一些内容,则应该使用Web服务API,该API允许查询SonarQube操纵的大多数“对象”。 In such case this is not really a plugin, just an external client application. 在这种情况下,这实际上不是插件,而只是外部客户端应用程序。

You also have the alternative to use the SonarQube Java APIs if you want to write a SonarQube plugin that displays something in the SonarQube UI (like a custom widget). 如果要编写一个在SonarQube UI中显示某些内容的SonarQube插件(例如自定义小部件),则还可以使用SonarQube Java API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM