Skip to content

Commit 8edf783

Browse files
committed
update schema queries in mysql, postgres connectors
1 parent 928c500 commit 8edf783

3 files changed

Lines changed: 97 additions & 41 deletions

File tree

pom.xml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>gr.sqlbrowserfx</groupId>
66
<artifactId>sqlbrowserfx</artifactId>
7-
<version>4.0.1-SNAPSHOT</version>
7+
<version>3.11.0</version>
88

99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11-
<maven.compiler.source>25</maven.compiler.source>
12-
<maven.compiler.target>25</maven.compiler.target>
11+
<maven.compiler.source>21</maven.compiler.source>
12+
<maven.compiler.target>21</maven.compiler.target>
1313
<attach.version>4.0.10</attach.version>
14-
<javafx.version>25</javafx.version>
15-
<log4j.version>2.25.1</log4j.version>
14+
<javafx.version>21.0.2</javafx.version>
15+
<log4j.version>2.22.1</log4j.version>
1616
</properties>
1717

1818
<build>
@@ -75,25 +75,25 @@
7575
<dependency>
7676
<groupId>com.github.vertical-blank</groupId>
7777
<artifactId>sql-formatter</artifactId>
78-
<version>2.0.5</version>
78+
<version>2.0.4</version>
7979
</dependency>
8080
<!-- https://mvnrepository.com/artifact/org.fxmisc.richtext/richtextfx -->
8181
<dependency>
8282
<groupId>org.fxmisc.richtext</groupId>
8383
<artifactId>richtextfx</artifactId>
8484
<!-- 0.10.9 works better than 0.11.2 -->
85-
<version>0.11.6</version>
85+
<version>0.10.9</version>
8686
</dependency>
8787
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
8888
<dependency>
8989
<groupId>org.slf4j</groupId>
9090
<artifactId>slf4j-api</artifactId>
91-
<version>2.0.17</version>
91+
<version>1.7.36</version>
9292
</dependency>
9393
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
9494
<dependency>
9595
<groupId>org.apache.logging.log4j</groupId>
96-
<artifactId>log4j-slf4j2-impl</artifactId>
96+
<artifactId>log4j-slf4j-impl</artifactId>
9797
<version>${log4j.version}</version>
9898
</dependency>
9999
<!--
@@ -114,19 +114,19 @@
114114
<dependency>
115115
<groupId>io.javalin</groupId>
116116
<artifactId>javalin</artifactId>
117-
<version>6.7.0</version>
117+
<version>5.6.3</version>
118118
</dependency>
119119
<!-- https://mvnrepository.com/artifact/org.json/json -->
120120
<dependency>
121121
<groupId>org.json</groupId>
122122
<artifactId>json</artifactId>
123-
<version>20250517</version>
123+
<version>20240205</version>
124124
</dependency>
125125
<!-- https://mvnrepository.com/artifact/org.controlsfx/controlsfx -->
126126
<dependency>
127127
<groupId>org.controlsfx</groupId>
128128
<artifactId>controlsfx</artifactId>
129-
<version>11.2.2</version>
129+
<version>11.2.0</version>
130130
</dependency>
131131
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web -->
132132
<dependency>
@@ -149,44 +149,44 @@
149149
<dependency>
150150
<groupId>org.xerial</groupId>
151151
<artifactId>sqlite-jdbc</artifactId>
152-
<version>3.50.3.0</version>
152+
<version>3.45.1.0</version>
153153
</dependency>
154154
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
155155
<dependency>
156156
<groupId>com.mysql</groupId>
157157
<artifactId>mysql-connector-j</artifactId>
158-
<version>9.4.0</version>
158+
<version>8.3.0</version>
159159
</dependency>
160160
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
161161
<dependency>
162162
<groupId>org.postgresql</groupId>
163163
<artifactId>postgresql</artifactId>
164-
<version>42.7.8</version>
164+
<version>42.7.1</version>
165165
</dependency>
166166
<!--
167167
https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
168168
<dependency>
169169
<groupId>com.microsoft.sqlserver</groupId>
170170
<artifactId>mssql-jdbc</artifactId>
171-
<version>13.2.0.jre11</version>
171+
<version>12.6.0.jre11</version>
172172
</dependency>
173173
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
174174
<dependency>
175175
<groupId>org.apache.commons</groupId>
176176
<artifactId>commons-dbcp2</artifactId>
177-
<version>2.13.0</version>
177+
<version>2.11.0</version>
178178
</dependency>
179179
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
180180
<dependency>
181181
<groupId>org.apache.commons</groupId>
182182
<artifactId>commons-lang3</artifactId>
183-
<version>3.18.0</version>
183+
<version>3.14.0</version>
184184
</dependency>
185185
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
186186
<dependency>
187187
<groupId>commons-io</groupId>
188188
<artifactId>commons-io</artifactId>
189-
<version>2.20.0</version>
189+
<version>2.15.1</version>
190190
</dependency>
191191
</dependencies>
192192

src/main/java/gr/sqlbrowserfx/conn/MysqlConnector.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,66 @@ public String getContentsQuery() {
119119

120120
@Override
121121
public void getTableSchema(String name, ResultSetAction action) throws SQLException {
122-
this.executeQuery("SHOW CREATE TABLE " + database + "." + name, action);
123-
}
122+
// FIXME: cannot make it work triple quoted
123+
String sql = "SELECT CONCAT('CREATE TABLE `', t.TABLE_SCHEMA, '`.`', t.TABLE_NAME, '` ( ', GROUP_CONCAT(CONCAT(' `', c.COLUMN_NAME, '` ', c.COLUMN_TYPE, IF(c.IS_NULLABLE='NO',' NOT NULL',''), IF(c.COLUMN_DEFAULT IS NOT NULL, CONCAT(' DEFAULT \\'', REPLACE(c.COLUMN_DEFAULT,'\\\\','\\\\\\\\'), '\\''),''), IF(c.EXTRA<>'', CONCAT(' ', c.EXTRA),''), IF(c.COLUMN_COMMENT<>'', CONCAT(' COMMENT \\'', REPLACE(c.COLUMN_COMMENT,'\\'','\\\\\\''), '\\''),'') ) ORDER BY c.ORDINAL_POSITION SEPARATOR ', '), IF(pk.primary_key IS NOT NULL, CONCAT(', PRIMARY KEY (', pk.primary_key, ')'), ''), IF(uk.unique_keys IS NOT NULL, CONCAT(', ', uk.unique_keys), ''), IF(fk.foreign_keys IS NOT NULL, CONCAT(', ', fk.foreign_keys), ''), ' ) ENGINE=', MAX(t.ENGINE), ' DEFAULT CHARSET=', MAX(SUBSTRING_INDEX(t.TABLE_COLLATION,'_',1)), ';') AS create_table_statement FROM information_schema.TABLES t JOIN information_schema.COLUMNS c ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME LEFT JOIN (SELECT kcu.TABLE_SCHEMA, kcu.TABLE_NAME, GROUP_CONCAT(CONCAT('`', kcu.COLUMN_NAME, '`') ORDER BY kcu.ORDINAL_POSITION) AS primary_key FROM information_schema.TABLE_CONSTRAINTS tc JOIN information_schema.KEY_COLUMN_USAGE kcu ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME AND tc.TABLE_SCHEMA = kcu.TABLE_SCHEMA AND tc.TABLE_NAME = kcu.TABLE_NAME WHERE tc.CONSTRAINT_TYPE='PRIMARY KEY' GROUP BY kcu.TABLE_SCHEMA, kcu.TABLE_NAME) pk ON t.TABLE_SCHEMA = pk.TABLE_SCHEMA AND t.TABLE_NAME = pk.TABLE_NAME LEFT JOIN (SELECT uk_sub.TABLE_SCHEMA, uk_sub.TABLE_NAME, GROUP_CONCAT(CONCAT('UNIQUE KEY `', uk_sub.CONSTRAINT_NAME, '` (', uk_sub.column_list, ')') SEPARATOR ', ') AS unique_keys FROM (SELECT kcu.TABLE_SCHEMA, kcu.TABLE_NAME, kcu.CONSTRAINT_NAME, GROUP_CONCAT(CONCAT('`', kcu.COLUMN_NAME, '`') ORDER BY kcu.ORDINAL_POSITION) AS column_list FROM information_schema.TABLE_CONSTRAINTS tc JOIN information_schema.KEY_COLUMN_USAGE kcu ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME AND tc.TABLE_SCHEMA = kcu.TABLE_SCHEMA AND tc.TABLE_NAME = kcu.TABLE_NAME WHERE tc.CONSTRAINT_TYPE='UNIQUE' GROUP BY kcu.TABLE_SCHEMA, kcu.TABLE_NAME, kcu.CONSTRAINT_NAME) AS uk_sub GROUP BY uk_sub.TABLE_SCHEMA, uk_sub.TABLE_NAME) uk ON t.TABLE_SCHEMA = uk.TABLE_SCHEMA AND t.TABLE_NAME = uk.TABLE_NAME LEFT JOIN (SELECT fk_sub.TABLE_SCHEMA, fk_sub.TABLE_NAME, GROUP_CONCAT(CONCAT('CONSTRAINT `', fk_sub.CONSTRAINT_NAME, '` FOREIGN KEY (`', fk_sub.COLUMN_NAME, '`) REFERENCES `', fk_sub.REFERENCED_TABLE_NAME, '`(`', fk_sub.REFERENCED_COLUMN_NAME, '`)') SEPARATOR ', ') AS foreign_keys FROM information_schema.KEY_COLUMN_USAGE fk_sub JOIN information_schema.TABLE_CONSTRAINTS tc ON fk_sub.CONSTRAINT_NAME = tc.CONSTRAINT_NAME AND fk_sub.TABLE_SCHEMA = tc.TABLE_SCHEMA AND fk_sub.TABLE_NAME = tc.TABLE_NAME WHERE tc.CONSTRAINT_TYPE='FOREIGN KEY' GROUP BY fk_sub.TABLE_SCHEMA, fk_sub.TABLE_NAME) fk ON t.TABLE_SCHEMA = fk.TABLE_SCHEMA AND t.TABLE_NAME = fk.TABLE_NAME WHERE 1=1 ";
124+
this.executeQuery(
125+
sql +
126+
" AND t.TABLE_SCHEMA = '" + database + "' " +
127+
" AND t.TABLE_NAME = '" + name + "' " +
128+
"""
129+
GROUP BY
130+
t.TABLE_SCHEMA,
131+
t.TABLE_NAME
132+
"""
133+
,
134+
action);
135+
}
124136

125137
@Override
126138
public void getViewSchema(String name, ResultSetAction action) throws SQLException {
127-
this.executeQuery("SHOW CREATE VIEW " + database + "." + name, action);
139+
this.executeQuery(
140+
"""
141+
SELECT CONCAT(
142+
'CREATE ',
143+
CASE WHEN IS_UPDATABLE = 'NO' THEN 'ALGORITHM=UNDEFINED ' ELSE '' END,
144+
'DEFINER=`', DEFINER, '` ',
145+
'SQL SECURITY ', SECURITY_TYPE, ' ',
146+
'VIEW `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` AS ',
147+
VIEW_DEFINITION,
148+
CASE WHEN CHECK_OPTION != 'NONE' THEN CONCAT(' WITH ', CHECK_OPTION, ' CHECK OPTION') ELSE '' END,
149+
';'
150+
) AS create_view_statement
151+
FROM information_schema.VIEWS
152+
WHERE 1=1
153+
AND TABLE_SCHEMA = ?
154+
AND TABLE_NAME = ?
155+
""",
156+
Arrays.asList(database, name),
157+
action);
128158
}
129159

130160
@Override
131161
public void getIndexSchema(String name, ResultSetAction action) throws SQLException {
162+
this.executeQuery(
163+
"""
164+
SELECT DISTINCT
165+
CONCAT(
166+
'CREATE ',
167+
CASE WHEN s.NON_UNIQUE = 0 AND s.INDEX_NAME <> 'PRIMARY' THEN 'UNIQUE ' ELSE '' END,
168+
CASE WHEN s.INDEX_NAME = 'PRIMARY' THEN 'PRIMARY KEY ' ELSE CONCAT('INDEX `', s.INDEX_NAME, '` ') END,
169+
'(',
170+
GROUP_CONCAT(CONCAT('`', s.COLUMN_NAME, '`') ORDER BY s.SEQ_IN_INDEX SEPARATOR ', '),
171+
') USING ', s.INDEX_TYPE, ';'
172+
) AS create_index_statement
173+
FROM information_schema.STATISTICS s
174+
WHERE s.TABLE_SCHEMA = ?
175+
AND s.TABLE_NAME = ?
176+
GROUP BY s.TABLE_NAME, s.INDEX_NAME, s.INDEX_TYPE, s.NON_UNIQUE
177+
""",
178+
Arrays.asList(database, name),
179+
action);
132180
throw new RuntimeException("No implemented");
181+
133182
}
134183

135184
@Override

src/main/java/gr/sqlbrowserfx/conn/PostgreSqlConnector.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ else if (table.getColumnsMap().get(label).contains("stamp") && value != null &&
6161

6262
@Override
6363
public String getContentsQuery() {
64-
return "select tb.table_name, tb.table_type from INFORMATION_SCHEMA.tables as tb WHERE tb.table_schema = ANY (current_schemas(false))"
65-
+ " union "
66-
+ "select indexname as table_name , 'INDEX'as table_type from pg_indexes where schemaname = 'public'";
64+
return
65+
"""
66+
select tb.table_name, tb.table_type from INFORMATION_SCHEMA.tables as tb WHERE tb.table_schema = ANY (current_schemas(false))
67+
union
68+
select indexname as table_name , 'INDEX'as table_type from pg_indexes where schemaname = 'public'
69+
""";
6770
}
6871

6972
@Override
@@ -73,20 +76,22 @@ public String getDbSchema() {
7376

7477
@Override
7578
public void getTriggers(String table, ResultSetAction action) throws SQLException {
76-
String query =
77-
"select event_object_schema as table_schema, "
78-
+ " event_object_table as table_name, "
79-
+ " trigger_schema, "
80-
+ " trigger_name as TRIGGER_NAME, "
81-
+ " string_agg(event_manipulation, ',') as event, "
82-
+ " action_timing as activation, "
83-
+ " action_condition as condition, "
84-
+ " action_statement as ACTION_STATEMENT "
85-
+ "from information_schema.triggers "
86-
+ "where event_object_table = ? "
87-
+ "group by 1,2,3,4,6,7,8 "
88-
+ "order by table_schema, "
89-
+ " table_name;";
79+
String query =
80+
"""
81+
select
82+
event_object_schema as table_schema,
83+
event_object_table as table_name,
84+
trigger_schema,
85+
trigger_name as TRIGGER_NAME,
86+
string_agg(event_manipulation, ',') as event,
87+
action_timing as activation,
88+
action_condition as condition,
89+
action_statement as ACTION_STATEMENT
90+
from information_schema.triggers
91+
where event_object_table = ?
92+
group by 1,2,3,4,6,7,8
93+
order by table_schema, table_name
94+
""";
9095
this.executeQuery(query, Arrays.asList(table), action);
9196

9297
}
@@ -109,8 +114,10 @@ public void getIndexSchema(String name, ResultSetAction action) throws SQLExcept
109114

110115
private void getSchema(String name, ResultSetAction action) throws SQLException {
111116
this.executeQuery(
112-
"select table_name, view_definition as schema from INFORMATION_SCHEMA.views "
113-
+ "WHERE table_schema = ANY (current_schemas(false)) and table_name = ?",
117+
"""
118+
select table_name, view_definition as schema from INFORMATION_SCHEMA.views
119+
where table_schema = ANY (current_schemas(false)) and table_name = ?
120+
""",
114121
Arrays.asList(name), action);
115122
}
116123

0 commit comments

Comments
 (0)