What is the sonar database structure?

I would suggest using the Sonar REST API to retrieve statistics.

The database schema is deliberately undocumented by the development team. This enables them to make changes which don't break dependent reporting applications. (Obviously this doesn't stop someone running SQL queries)


Example

Using the CXF project in nemo as an example.

The "resources" REST API returns the latest value of the metrics your requested

http://nemo.sonarsource.org/api/resources?resource=org.apache.cxf:cxf&verbose=true&metrics=ncloc,violations_density,comment_lines_density,public_documented_api_density,duplicated_lines_density,blocker_violations,critical_violations,major_violations,minor_violations

And "timemachine" REST API returns a raw CSV dump of the data:

http://nemo.sonarsource.org/api/timemachine?resource=org.apache.cxf:cxf&format=csv&metrics=ncloc,violations_density,comment_lines_density,public_documented_api_density,duplicated_lines_density,blocker_violations,critical_violations,major_violations,minor_violations

(My browser will conveniently launch a spreadsheet to read CSV data)


You can get The above Information Using The below table.

1)projects, snapshots , metrics and project_measures. where projects table contain the Projects name . And for each project one snapshot id is created in certain period of time in snapshot table. then from snapshot table take the snapshot id and search it projects_measure table. and search the value of the descibed attribute using metric id.

select distinct proj.name NAME_OF_PROJ, metric.description Description,
                projdesc.value, snap.created_at CREATED_DATE
  from projects proj
    inner join snapshots snap on snap.project_id=proj.id 
  inner join (select max(snap2.created_at) as date_of_creation,id from snapshots snap2 
              where Date(snap2.created_at)   in ('2011-12-20','2012-02-21') 
               and snap2.project_id  in (5507,35252,9807,38954,23018,32390) 
              GROUP BY DAY(snap2.created_at),snap2.project_id ) as Lookup on Lookup.id=snap.id 

  inner join project_measures projdesc on  projdesc.snapshot_id=snap.id 
  inner join metrics metric on  projdesc.metric_id =metric.id 
  where  metric.id in( 1,2...)

Tags:

Sonarqube