1
+ /*
2
+ * Copyright 2018 Philipp Salvisberg <[email protected] >
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ package org.utplsql.sqldev.dal
17
+
18
+ import java.sql.Connection
19
+ import java.util.List
20
+ import org.springframework.dao.EmptyResultDataAccessException
21
+ import org.springframework.jdbc.core.BeanPropertyRowMapper
22
+ import org.springframework.jdbc.core.JdbcTemplate
23
+ import org.springframework.jdbc.datasource.SingleConnectionDataSource
24
+ import org.utplsql.sqldev.model.ut.Annotation
25
+
26
+ class UtplsqlDao {
27
+ public static val UTPLSQL_PACKAGE_NAME = " UT"
28
+ private var Connection conn
29
+ private var JdbcTemplate jdbcTemplate
30
+
31
+ new (Connection conn) {
32
+ this . conn = conn
33
+ this . jdbcTemplate = new JdbcTemplate (new SingleConnectionDataSource (conn, true ))
34
+ }
35
+
36
+ /**
37
+ * Gets the schema name of the utPLSQL installation.
38
+ *
39
+ * @return utPLSQL schema or null if no utPLSQL is not installed
40
+ * @throws DataAccessException if there is a problem
41
+ */
42
+ def String getUtplsqlSchema () {
43
+ val sql = ' ' '
44
+ SELECT table_owner
45
+ FROM all_synonyms
46
+ WHERE owner = ' PUBLIC '
47
+ AND synonym_name = ' «UTPLSQL_PACKAGE_NAME »'
48
+ AND table_name = ' «UTPLSQL_PACKAGE_NAME »'
49
+ ' ' '
50
+ try {
51
+ val schema = jdbcTemplate. queryForObject(sql, String )
52
+ return schema
53
+ } catch (EmptyResultDataAccessException e) {
54
+ return null
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Checks if the package ut_annotation_manager is installed.
60
+ * This package has been introduced with utPLSQL 3.0.4.
61
+ * This version is a prerequisite to identify
62
+ * utPLSQL unit test procedures.
63
+ *
64
+ * @return true if ut_annotation_manager package has been found
65
+ * @throws DataAccessException if there is a problem
66
+ */
67
+ def boolean isUtAnnotationManagerInstalled () {
68
+ if (utplsqlSchema !== null ) {
69
+ val sql = ' ' '
70
+ SELECT count(*)
71
+ FROM all_objects
72
+ WHERE owner = ' «utplsqlSchema»'
73
+ AND object_type = ' PACKAGE '
74
+ AND object_name = ' UT_ANNOTATION_MANAGER '
75
+ ' ' '
76
+ val found = jdbcTemplate. queryForObject(sql, Integer )
77
+ return found == 1
78
+ }
79
+ return false
80
+ }
81
+
82
+ /**
83
+ * Checks if utPLSQL tests exist
84
+ *
85
+ * @param owner schema name, mandatory, case-insensitive
86
+ * @param objectName name of the package or package body, optional, case-insensitive
87
+ * @param subobjectName name of the procedure, optional, case-insensitive
88
+ * @return true if at least one test has been found
89
+ * @throws DataAccessException if a utPLSQL version less than 3.0.4 is installed or if there are other problems
90
+ */
91
+ def boolean containsUtplsqlTest (String owner , String objectName , String subobjectName ) {
92
+ try {
93
+ val sql = ' ' '
94
+ SELECT count(
95
+ CASE
96
+ WHEN a.name = ' test'
97
+ AND (upper(a.subobject_name) = upper(?) OR ? IS NULL)
98
+ THEN
99
+ 1
100
+ ELSE
101
+ NULL
102
+ END
103
+ )
104
+ FROM TABLE(«utplsqlSchema».ut_annotation_manager.get_annotated_objects(upper(?), ' PACKAGE ' )) o
105
+ CROSS JOIN TABLE(o.annotations) a
106
+ WHERE (o.object_name = upper(?) OR ? IS NULL)
107
+ AND a.name IN (' test' , ' suite' )
108
+ HAVING count(
109
+ CASE
110
+ WHEN a.name = ' suite' THEN
111
+ 1
112
+ ELSE
113
+ NULL
114
+ END
115
+ ) > 0
116
+ ' ' '
117
+ val found = jdbcTemplate. queryForObject(sql, Integer , #[subobjectName, subobjectName, owner, objectName, objectName])
118
+ return found > 0
119
+ } catch (EmptyResultDataAccessException e) {
120
+ return false
121
+ }
122
+ }
123
+
124
+ def boolean containsUtplsqlTest (String owner ) {
125
+ return containsUtplsqlTest(owner, null , null )
126
+ }
127
+
128
+ def boolean containsUtplsqlTest (String owner , String objectType ) {
129
+ return containsUtplsqlTest(owner, objectType, null )
130
+ }
131
+
132
+ /**
133
+ * Gets a list of utPLSQL annotations for a given PL/SQL package specification
134
+ *
135
+ * @param owner schema name, mandatory, case-insensitive
136
+ * @param objectName name of the package or package body, optional, case-insensitive
137
+ * @return list of Annotation with name 'suite' or 'test'
138
+ * @throws DataAccessException if a utPLSQL version less than 3.0.4 is installed or if there are other problems
139
+ */
140
+ def List<Annotation > annotations (String owner , String objectName ) {
141
+ val sql = ' ' '
142
+ SELECT o.object_owner, o.object_type, o.object_name, a.name, a.text, a.subobject_name
143
+ FROM TABLE(«utplsqlSchema».ut_annotation_manager.get_annotated_objects(upper(?), ' PACKAGE ' )) o
144
+ CROSS JOIN TABLE(o.annotations) a
145
+ WHERE o.object_name = upper(?)
146
+ ' ' '
147
+ val result = jdbcTemplate. query(sql, new BeanPropertyRowMapper<Annotation > (Annotation ), #[owner, objectName])
148
+ return result
149
+ }
150
+
151
+ }
0 commit comments