Skip to content

Commit

Permalink
Add ability to specify database port
Browse files Browse the repository at this point in the history
  • Loading branch information
blakemcbride committed Mar 15, 2024
1 parent e9412f7 commit 3e6593a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/main/backend/KissInit.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class KissInit {
static void init() {
MainServlet.setConnectionType Connection.ConnectionType.SQLite
MainServlet.setHost "localhost"
//MainServlet.setPort 5432 // specify the database port if not the database default
MainServlet.setDatabase "DB.sqlite" // the name of the database, leave blank for none (and no authentication)
MainServlet.setUser "" // database user (not application user login)
MainServlet.setPassword "" // database password (not application user password)
Expand Down
2 changes: 1 addition & 1 deletion src/main/core/org/kissweb/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class Main {

public static void main(String [] argv) throws Exception {
Connection db = new Connection(ConnectionType.PostgreSQL, "localhost", "waytogo", "postgres", "postgres");
Connection db = new Connection(ConnectionType.PostgreSQL, "localhost", null, "waytogo", "postgres", "postgres");
Command cmd = db.newCommand();
Cursor c = cmd.query("select * from hr_employee_event order by employee_id, event_date, summary, detail");
Record prec = null;
Expand Down
39 changes: 26 additions & 13 deletions src/main/core/org/kissweb/database/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,44 @@ public Connection(java.sql.Connection db) {
* Create a connection string appropriate for the indicated database type. This method is only used in special situations.
*
* @param type
* @param host
* @param host (can use null for localhost)
* @param port (use null for default)
* @param dbname
* @param user
* @param user (use null for integrated security)
* @param pw
* @return
*
* @see Connection(ConnectionType, String, String, String, String)
*/
public static String makeConnectionString(ConnectionType type, String host, String dbname, String user, String pw) {
public static String makeConnectionString(ConnectionType type, String host, Integer port, String dbname, String user, String pw) {
String connectionString;

if (host == null)
host = "localhost";
if (type == ConnectionType.PostgreSQL) {
connectionString = "jdbc:postgresql://" + host + "/" + dbname + "?user=" + user + "&password=" + pw;
if (port == null)
port = 5432;
connectionString = "jdbc:postgresql://" + host + ":" + port + "/" + dbname + "?user=" + user + "&password=" + pw;
} else if (type == ConnectionType.MicrosoftServer) {
connectionString = "jdbc:sqlserver://" + host + ";databaseName=" + dbname + ";";
if (port == null)
port = 1433;
connectionString = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbname + ";";
if (user != null && !"".equals(user))
connectionString += "user=" + user + ";password=" + pw + ";";
else
connectionString += "integratedSecurity=true;";
} else if (type == ConnectionType.MySQL) {
connectionString = "jdbc:mysql://" + host + "/" + dbname + "?user=" + user + "&password=" + pw;
if (port == null)
port = 3306;
connectionString = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?user=" + user + "&password=" + pw;
} else if (type == ConnectionType.SQLite) {
connectionString = "jdbc:sqlite:" + dbname;
if (pw != null && !pw.isEmpty())
connectionString += ";Password=" + pw + ";";
} else if (type == ConnectionType.Oracle) {
connectionString = "jdbc:oracle:thin:" + user + "/" + pw + "@" + dbname;
if (port == null)
port = 1521;
connectionString = "jdbc:oracle:thin:" + user + "/" + pw + "@//" + host + ":" + port + "/" + dbname;
} else
throw new UnsupportedOperationException();
return connectionString;
Expand Down Expand Up @@ -199,7 +210,8 @@ public Connection(ConnectionType type, String connectionString) throws ClassNotF
* This is the main method of forming a new database connection.
*
* @param type
* @param host
* @param host (null for localhost)
* @param port (null for default)
* @param dbname
* @param user
* @param pw
Expand All @@ -208,8 +220,8 @@ public Connection(ConnectionType type, String connectionString) throws ClassNotF
*
* @see Connection(ConnectionType, String, String)
*/
public Connection(ConnectionType type, String host, String dbname, String user, String pw) throws SQLException, ClassNotFoundException {
this(type, makeConnectionString(type, host, dbname, user, pw));
public Connection(ConnectionType type, String host, Integer port, String dbname, String user, String pw) throws SQLException, ClassNotFoundException {
this(type, makeConnectionString(type, host, port, dbname, user, pw));
ctype = type;
dmd = conn.getMetaData();
}
Expand All @@ -218,15 +230,16 @@ public Connection(ConnectionType type, String host, String dbname, String user,
* This is the main method of forming a new database connection when Windows authentication is used.
*
* @param type
* @param host
* @param host (null for localhost)
* @param port (null for default)
* @param dbname
* @throws SQLException
* @throws ClassNotFoundException
*
* @see Connection(ConnectionType, String, String, String, String)
*/
public Connection(ConnectionType type, String host, String dbname) throws SQLException, ClassNotFoundException {
this(type, makeConnectionString(type, host, dbname, null, null));
public Connection(ConnectionType type, String host, Integer port, String dbname) throws SQLException, ClassNotFoundException {
this(type, makeConnectionString(type, host, port, dbname, null, null));
ctype = type;
dmd = conn.getMetaData();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/core/org/kissweb/database/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void main(String [] argv) {
String pw;

try {
DB = new Connection(Connection.ConnectionType.PostgreSQL, "localhost", "fos", "postgres", "postgres");
DB = new Connection(Connection.ConnectionType.PostgreSQL, "localhost", null, "fos", "postgres", "postgres");
cmd = DB.newCommand();
Record rec;

Expand Down
16 changes: 14 additions & 2 deletions src/main/core/org/kissweb/restServer/MainServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class MainServlet extends HttpServlet {
private static final Logger logger = Logger.getLogger(MainServlet.class);
private static Connection.ConnectionType connectionType;
private static String host; // set by KissInit.groovy
private static Integer port; // optionally set by KissInit.groovy
private static String database; // set by KissInit.groovy
private static String user; // database username, set by KissInit.groovy
private static String password; // database password, set by KissInit.groovy
Expand Down Expand Up @@ -258,10 +259,13 @@ static void makeDatabaseConnection() throws PropertyVetoException, SQLException,
return;
}
if (cpds == null) {
logger.info("* * * Attempting to connect to database " + host + ":" + database + ":" + user);
if (port == null)
logger.info("* * * Attempting to connect to database " + host + ":" + database + ":" + user);
else
logger.info("* * * Attempting to connect to database " + host + ":" + port + ":" + database + ":" + user);
if (connectionType == Connection.ConnectionType.SQLite && database != null && !database.isEmpty() && database.charAt(0) != '/')
database = applicationPath + database;
String cstr = Connection.makeConnectionString(connectionType, host, database, user, password);
String cstr = Connection.makeConnectionString(connectionType, host, port, database, user, password);
Connection con;
try {
con = new Connection(connectionType, cstr);
Expand Down Expand Up @@ -299,10 +303,18 @@ public static void setHost(String hostp) {
host = hostp;
}

public static void setPort(int portp) {
port = portp;
}

public static String getHost() {
return host;
}

public static Integer getPort() {
return port;
}

public static String getUser() {
return user;
}
Expand Down

0 comments on commit 3e6593a

Please sign in to comment.