Skip to content
This repository
Browse code

METRICS-3198: Postgres sql parser fails to parse EDB connection urls

  • Loading branch information...
commit a8980eaff1bea18a1b386ee57d97b23b92e77069 1 parent 40f9ddb
anchlovi authored December 30, 2012
50  ...on-plugins/jdbc/src/main/java/com/springsource/insight/plugin/jdbc/parser/parsers/PostgresSqlParser.java
@@ -16,7 +16,7 @@
16 16
 
17 17
 package com.springsource.insight.plugin.jdbc.parser.parsers;
18 18
 
19  
-import com.springsource.insight.plugin.jdbc.parser.SimpleSqlUrlParser;
  19
+import com.springsource.insight.plugin.jdbc.parser.AbstractSqlPatternParser;
20 20
 
21 21
 /**
22 22
  * <P>The following formats are supported:</P></BR>
@@ -35,11 +35,55 @@
35 35
  *		jdbc:postgresql://neptune.acme.com:5432/test 
36 36
  *		jdbc:postgresql:test - equivalent to jdbc:postgresql://localhost:5432/test
37 37
  */
38  
-public class PostgresSqlParser extends SimpleSqlUrlParser {
  38
+public class PostgresSqlParser extends AbstractSqlPatternParser {
  39
+	/**
  40
+	 * Defines a non-capturing group for postgres or edb
  41
+	 */
  42
+	private static final String VENDOR_LIST  =  JDBC_PREFIX + ":(?:postgres|edb)";
  43
+	
  44
+	/**
  45
+	 * Defines a group for IPv6, IPv4 IPs and host names<br/>
  46
+	 * Will match the next addresses:
  47
+	 * <ol>
  48
+	 * <li>IPv6 localhost - [::1]
  49
+	 * <li>IPv6 ip address - [2001:0db8:85a3:0042:1000:8a2e:0370:7334]
  50
+	 * </ol>
  51
+	 */
  52
+	private static final String IPv6_PATTERN =  "\\[(::1|[0-9a-f]{4}(?:[:][0-9a-f]{4}){7})\\]";
  53
+	
  54
+	/**
  55
+	 * Matches jdbc:postgres:database and jdbc:edb:database  
  56
+	 */
  57
+	private static final String PATTERN1 = VENDOR_LIST+":([^:/?]+)";
  58
+	
  59
+	/**
  60
+	 * Matches jdbc:postgres://host/database and jdbc:edb://host/database (IPv4 and host names)  
  61
+	 */
  62
+	private static final String PATTERN2 = VENDOR_LIST+"://([^:/?]+)/([^:/?]+).*";
  63
+	
  64
+	/**
  65
+	 * Matches jdbc:postgres://host:port/database and jdbc:edb://host:port/database (IPv4 and host names)  
  66
+	 */
  67
+	private static final String PATTERN3 = VENDOR_LIST+"://([^:/?]+):(\\d+)/([^:/?]+).*";
  68
+	
  69
+	/**
  70
+	 * Matches jdbc:postgres://host/database and jdbc:edb://host/database (IPv6)  
  71
+	 */
  72
+	private static final String PATTERN4 = VENDOR_LIST+"://"+IPv6_PATTERN+"/([^:/?]+).*";
  73
+	
  74
+	/**
  75
+	 * Matches jdbc:postgres://host:port/database and jdbc:edb://host:port/database (IPv6)  
  76
+	 */
  77
+	private static final String PATTERN5 = VENDOR_LIST+"://"+IPv6_PATTERN+":(\\d+)/([^:/?]+).*";
  78
+	
39 79
 	public static final int	DEFAULT_CONNECTION_PORT=5432;
40 80
 	public static final String	VENDOR="postgresql";
41 81
 
42 82
 	public PostgresSqlParser () {
43  
-		super(VENDOR, DEFAULT_CONNECTION_PORT);
  83
+		super(VENDOR, DEFAULT_CONNECTION_PORT, create(PATTERN1, -1, -1, 1),
  84
+				                               create(PATTERN2, 1, -1, 2),
  85
+				                               create(PATTERN3, 1, 2, 3),
  86
+				                               create(PATTERN4, 1, -1, 2),
  87
+				                               create(PATTERN5, 1, 2, 3));
44 88
 	}
45 89
 }
125  ...ction-plugins/jdbc/src/test/java/com/springsource/insight/plugin/jdbc/parsers/PostgresSqlParserTest.java
@@ -17,13 +17,134 @@
17 17
 package com.springsource.insight.plugin.jdbc.parsers;
18 18
 
19 19
 import com.springsource.insight.plugin.jdbc.parser.DatabaseType;
  20
+import com.springsource.insight.plugin.jdbc.parser.JdbcUrlParser;
20 21
 import com.springsource.insight.plugin.jdbc.parser.parsers.PostgresSqlParser;
21 22
 
22 23
 /**
23 24
  * 
24 25
  */
25  
-public class PostgresSqlParserTest extends SimpleSqlUrlParserTestSupport<PostgresSqlParser> {
  26
+public class PostgresSqlParserTest extends SqlParserTestImpl<PostgresSqlParser> {
26 27
 	public PostgresSqlParserTest () {
27  
-		super(DatabaseType.POSTGRESQL, new PostgresSqlParser());
  28
+		super(DatabaseType.POSTGRESQL, new PostgresSqlParser(),
  29
+		  new SqlTestEntry("jdbc:postgres:database",
  30
+				 JdbcUrlParser.DEFAULT_HOST,
  31
+				 PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  32
+				 "database")
  33
+		 ,
  34
+		 new SqlTestEntry("jdbc:edb:database",
  35
+				 JdbcUrlParser.DEFAULT_HOST,
  36
+				 PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  37
+				 "database")
  38
+		,
  39
+		new SqlTestEntry("jdbc:postgres:data123",
  40
+				JdbcUrlParser.DEFAULT_HOST,
  41
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  42
+				"data123")
  43
+		,
  44
+		new SqlTestEntry("jdbc:edb:data123",
  45
+				JdbcUrlParser.DEFAULT_HOST,
  46
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  47
+				"data123")
  48
+		,
  49
+		new SqlTestEntry("jdbc:postgres://[::1]/database",
  50
+				"::1",
  51
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  52
+				"database")
  53
+		,
  54
+		new SqlTestEntry("jdbc:edb://[::1]/database",
  55
+				"::1",
  56
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  57
+				"database")
  58
+		,
  59
+		new SqlTestEntry("jdbc:postgres://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]/database",
  60
+				"2001:0db8:85a3:0042:1000:8a2e:0370:7334",
  61
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  62
+				"database")
  63
+		,
  64
+		new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]/database",
  65
+				"2001:0db8:85a3:0042:1000:8a2e:0370:7334",
  66
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  67
+				"database")
  68
+		,
  69
+		new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]/database?someprop=somevalue",
  70
+				"2001:0db8:85a3:0042:1000:8a2e:0370:7334",
  71
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  72
+				"database")
  73
+		,
  74
+		new SqlTestEntry("jdbc:postgres://myhost/database",
  75
+				"myhost",
  76
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  77
+				"database")
  78
+		,
  79
+		new SqlTestEntry("jdbc:edb://myhost/database",
  80
+				"myhost",
  81
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  82
+				"database")
  83
+		,
  84
+		new SqlTestEntry("jdbc:postgres://10.23.197.110/database",
  85
+				"10.23.197.110",
  86
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  87
+				"database")
  88
+		,
  89
+		new SqlTestEntry("jdbc:edb://10.23.197.110/database",
  90
+				"10.23.197.110",
  91
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  92
+				"database")
  93
+		,
  94
+		new SqlTestEntry("jdbc:edb://10.23.197.110/database?someprop=somevalue",
  95
+				"10.23.197.110",
  96
+				PostgresSqlParser.DEFAULT_CONNECTION_PORT,
  97
+				"database")
  98
+		,
  99
+		new SqlTestEntry("jdbc:postgres://[::1]:9090/database",
  100
+				"::1",
  101
+				9090,
  102
+				"database")
  103
+		,
  104
+		new SqlTestEntry("jdbc:edb://[::1]:9090/database",
  105
+				"::1",
  106
+				9090,
  107
+				"database")
  108
+		,
  109
+		new SqlTestEntry("jdbc:postgres://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:9090/database",
  110
+				"2001:0db8:85a3:0042:1000:8a2e:0370:7334",
  111
+				9090,
  112
+				"database")
  113
+		,
  114
+		new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:9090/database",
  115
+				"2001:0db8:85a3:0042:1000:8a2e:0370:7334",
  116
+				9090,
  117
+				"database")
  118
+		,
  119
+		new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:9090/database?someprop=somevalue",
  120
+				"2001:0db8:85a3:0042:1000:8a2e:0370:7334",
  121
+				9090,
  122
+				"database")
  123
+		,
  124
+		new SqlTestEntry("jdbc:postgres://myhost:8080/database",
  125
+				"myhost",
  126
+				8080,
  127
+				"database")
  128
+		,
  129
+		new SqlTestEntry("jdbc:edb://myhost:8080/database",
  130
+				"myhost",
  131
+				8080,
  132
+				"database")
  133
+		,
  134
+		new SqlTestEntry("jdbc:postgres://10.23.197.110:8080/database",
  135
+				"10.23.197.110",
  136
+				8080,
  137
+				"database")
  138
+		,
  139
+		new SqlTestEntry("jdbc:edb://10.23.197.110:8080/database",
  140
+				"10.23.197.110",
  141
+				8080,
  142
+				"database")
  143
+		,
  144
+		new SqlTestEntry("jdbc:edb://10.23.197.110:8080/database?someprop=somevalue",
  145
+				"10.23.197.110",
  146
+				8080,
  147
+				"database")
  148
+		);
28 149
 	}
29 150
 }

0 notes on commit a8980ea

Please sign in to comment.
Something went wrong with that request. Please try again.