aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
blob: 102c9497a511baa925b052bb5688569193f36db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.sonar.db.component.SnapshotMapper">

  <sql id="snapshotColumns">
    s.id,
    s.uuid as uuid,
    s.component_uuid as componentUuId,
    s.created_at as createdAt,
    s.build_date as buildDate,
    s.status as status,
    s.purge_status as purgeStatus,
    s.islast as last,
    s.version as version,
    s.period1_mode as period1Mode,
    s.period2_mode as period2Mode,
    s.period3_mode as period3Mode,
    s.period4_mode as period4Mode,
    s.period5_mode as period5Mode,
    s.period1_param as period1Param,
    s.period2_param as period2Param,
    s.period3_param as period3Param,
    s.period4_param as period4Param,
    s.period5_param as period5Param,
    s.period1_date as period1Date,
    s.period2_date as period2Date,
    s.period3_date as period3Date,
    s.period4_date as period4Date,
    s.period5_date as period5Date
  </sql>

  <sql id="viewsSnapshotColumns">
    s.uuid,
    s.created_at as createdAt
  </sql>

  <select id="selectByKey" parameterType="Long" resultType="Snapshot">
    SELECT
    <include refid="snapshotColumns" />
    FROM snapshots s
    <where>
      AND s.id=#{key}
    </where>
  </select>

  <select id="selectByIds" parameterType="Long" resultType="Snapshot">
    SELECT
      <include refid="snapshotColumns" />
    FROM
      snapshots s
    WHERE
      s.id in
      <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
      </foreach>
  </select>

  <select id="selectByUuids" parameterType="List" resultType="Snapshot">
    SELECT
      <include refid="snapshotColumns"/>
    FROM
      snapshots s
    WHERE
      s.uuid in
      <foreach collection="uuids" item="uuid" separator="," open="(" close=")">
        #{uuid}
      </foreach>
  </select>

  <select id="selectLastSnapshotByComponentUuid" resultType="Snapshot">
    select <include refid="snapshotColumns" />
    from snapshots s
    inner join projects p on s.component_uuid = p.project_uuid
    where
      s.islast=${_true}
      and p.uuid = #{componentUuid}
  </select>

  <select id="selectLastSnapshotByRootComponentUuid" resultType="Snapshot">
    select <include refid="snapshotColumns" />
    from snapshots s
    where s.islast=${_true} and s.component_uuid = #{componentUuid}
  </select>

  <select id="selectLastSnapshotsByRootComponentUuids" resultType="Snapshot">
    select <include refid="snapshotColumns" />
    from snapshots s
    where
      s.islast=${_true}
      and s.component_uuid in
      <foreach collection="componentUuids" item="componentUuid" separator="," open="(" close=")">
        #{componentUuid}
      </foreach>
      </select>

  <select id="selectSnapshotsByQuery" parameterType="map" resultType="Snapshot">
    SELECT
    <include refid="snapshotColumns" />
    FROM snapshots s
    <if test="query.componentUuid != null">
      INNER JOIN projects p ON p.uuid=s.component_uuid AND p.enabled=${_true} AND s.component_uuid=#{query.componentUuid}
    </if>
    <where>
      <if test="query.status != null">
        AND s.status=#{query.status}
      </if>
      <if test="query.version != null">
        AND s.version=#{query.version}
      </if>
      <if test="query.isLast != null">
        AND s.islast=#{query.isLast}
      </if>
      <if test="query.createdAfter != null">
        AND s.created_at>=#{query.createdAfter}
      </if>
      <if test="query.createdBefore != null">
        AND s.created_at&lt;#{query.createdBefore}
      </if>
    </where>
    <if test="query.sortField != null">
      ORDER BY
      <if test="query.sortField == 'created_at'">
        s.created_at
      </if>
      <if test="query.sortOrder == 'asc'">
        asc
      </if>
      <if test="query.sortOrder == 'desc'">
        desc
      </if>
    </if>
  </select>

  <select id="selectPreviousVersionSnapshots" parameterType="map" resultType="Snapshot">
    SELECT
    <include refid="snapshotColumns" />
    FROM snapshots s
    INNER JOIN events e ON s.uuid = e.analysis_uuid AND e.name &lt;&gt; #{lastVersion} AND e.category='Version'
    <where>
      s.component_uuid=#{componentUuid}
    </where>
    ORDER BY e.event_date DESC
  </select>

  <select id="selectOldestSnapshots" parameterType="map" resultType="Snapshot">
    SELECT
    <include refid="snapshotColumns" />
    FROM snapshots s
    <where>
      s.component_uuid=#{componentUuid}
    </where>
    ORDER BY s.created_at ASC
  </select>

  <select id="selectSnapshotBefore" resultType="ViewsSnapshot">
    SELECT
    <include refid="viewsSnapshotColumns" />
    FROM snapshots s
    <where>
      and s.component_uuid = #{componentUuid}
      and s.status = 'P'
      and s.created_at &lt; #{date}
    </where>
    order by created_at desc
  </select>

  <update id="unsetIsLastFlagForComponentUuid" parameterType="map">
    update snapshots
    set islast = ${_false}
    where component_uuid = #{componentUuid}
    and islast = ${_true}
  </update>

  <update id="setIsLastFlagForAnalysisUuid" parameterType="map">
    update snapshots
    set islast = ${_true}, status = 'P'
    where uuid = #{analysisUuid}
  </update>

  <update id="update" parameterType="Snapshot">
    update snapshots
    set version = #{version, jdbcType=VARCHAR},
        status = #{status, jdbcType=VARCHAR}
    where uuid = #{uuid}
  </update>

  <insert id="insert" parameterType="Snapshot" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
    insert into snapshots (
    uuid,
    component_uuid,
    created_at,
    build_date,
    status,
    purge_status,
    islast,
    version,
    period1_mode,
    period2_mode,
    period3_mode,
    period4_mode,
    period5_mode,
    period1_param,
    period2_param,
    period3_param,
    period4_param,
    period5_param,
    period1_date,
    period2_date,
    period3_date,
    period4_date,
    period5_date)
    values (
    #{uuid, jdbcType=VARCHAR},
    #{componentUuid, jdbcType=VARCHAR},
    #{createdAt, jdbcType=BIGINT},
    #{buildDate, jdbcType=BIGINT},
    #{status, jdbcType=VARCHAR},
    #{purgeStatus, jdbcType=INTEGER},
    #{last, jdbcType=BOOLEAN},
    #{version, jdbcType=VARCHAR},
    #{period1Mode, jdbcType=VARCHAR},
    #{period2Mode, jdbcType=VARCHAR},
    #{period3Mode, jdbcType=VARCHAR},
    #{period4Mode, jdbcType=VARCHAR},
    #{period5Mode, jdbcType=VARCHAR},
    #{period1Param, jdbcType=VARCHAR},
    #{period2Param, jdbcType=VARCHAR},
    #{period3Param, jdbcType=VARCHAR},
    #{period4Param, jdbcType=VARCHAR},
    #{period5Param, jdbcType=VARCHAR},
    #{period1Date, jdbcType=BIGINT},
    #{period2Date, jdbcType=BIGINT},
    #{period3Date, jdbcType=BIGINT},
    #{period4Date, jdbcType=BIGINT},
    #{period5Date, jdbcType=BIGINT})
  </insert>
</mapper>