Skip to content

Commit

Permalink
Allow simpler configuration URL for PostgreSQL, and adapt the MySQL o…
Browse files Browse the repository at this point in the history
…ne to support same URL pattern (the old syntax for MySQL is still compatible)
  • Loading branch information
guillaumebort committed Aug 19, 2011
1 parent 04ea6df commit c0df25e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 19 additions & 2 deletions framework/src/play/db/DBPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,30 @@ private static boolean changed() {
p.put("db.destroyMethod", "close");
}

Matcher m = new jregex.Pattern("^mysql:(({user}[\\w]+)(:({pwd}[^@]+))?@)?({name}[\\w]+)$").matcher(p.getProperty("db", ""));
Matcher m = new jregex.Pattern("^mysql:(//)?(({user}[\\w]+)(:({pwd}[^@]+))?@)?(({host}[^/]+)/)?({name}[\\w]+)$").matcher(p.getProperty("db", ""));
if (m.matches()) {
String user = m.group("user");
String password = m.group("pwd");
String name = m.group("name");
String host = m.group("host");
p.put("db.driver", "com.mysql.jdbc.Driver");
p.put("db.url", "jdbc:mysql://localhost/" + name + "?useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci");
p.put("db.url", "jdbc:mysql://" + (host == null ? "localhost" : host) + "/" + name + "?useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci");
if (user != null) {
p.put("db.user", user);
}
if (password != null) {
p.put("db.pass", password);
}
}

m = new jregex.Pattern("^postgres:(//)?(({user}[\\w]+)(:({pwd}[^@]+))?@)?(({host}[^/]+)/)?({name}[\\w]+)$").matcher(p.getProperty("db", ""));
if (m.matches()) {
String user = m.group("user");
String password = m.group("pwd");
String name = m.group("name");
String host = m.group("host");
p.put("db.driver", "org.postgresql.Driver");
p.put("db.url", "jdbc:postgresql://" + (host == null ? "localhost" : host) + "/" + name);
if (user != null) {
p.put("db.user", user);
}
Expand Down
5 changes: 4 additions & 1 deletion resources/application-skel/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ date.format=yyyy-MM-dd
# db=mem
#
# To connect to a local MySQL5 database, use:
# db=mysql:user:pwd@database_name
# db=mysql://user:pwd@host/database
#
# To connect to a local PostgreSQL9 database, use:
# db=postgres://user:pwd@host/database
#
# If you need a full JDBC configuration use the following :
# db.url=jdbc:postgresql:database_name
Expand Down

0 comments on commit c0df25e

Please sign in to comment.