Skip to content

Commit

Permalink
fixed raw database url
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Nov 5, 2023
1 parent 79aa7f5 commit f3d94e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/osiris/jsqlgen/payhook/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It holds the database credentials (set by you at first run of jSQL-Gen).<br>
- Ensures optimal performance (cpu and memory usage) for any type of database from small to huge, with millions of queries per second.
- Connection status is checked before doing a query (since it could be closed or timed out and thus result in errors).*/
public class Database{
public static String rawUrl = "jdbc:mysql://localhost/";
public static String rawUrl = com.osiris.payhook.PayHook.databaseRawUrl;
public static String url = com.osiris.payhook.PayHook.databaseUrl;
public static String name = com.osiris.payhook.PayHook.databaseName;
public static String username = com.osiris.payhook.PayHook.databaseUsername;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/osiris/payhook/PayHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public final class PayHook {
* Always null, except when doing init of database.
*/
public static String databaseUrl = null;
public static String databaseRawUrl = null;
/**
* Always null, except when doing init of database.
*/
Expand Down Expand Up @@ -238,12 +239,14 @@ public static synchronized void init(String brandName, String databaseUrl, Strin
if (isSandbox && (!databaseUrl.contains("sandbox") && !databaseUrl.contains("test")))
throw new SQLException("You are running in sandbox mode, thus your database-url/name must contain 'sandbox' or 'test'!");
PayHook.databaseUrl = databaseUrl;
PayHook.databaseRawUrl = getRawDbUrlFrom(databaseUrl);
PayHook.databaseName = Objects.requireNonNull(databaseName);
PayHook.databaseUsername = Objects.requireNonNull(databaseUsername);
PayHook.databasePassword = Objects.requireNonNull(databasePassword);
com.osiris.jsqlgen.payhook.Database.create();
// Reset values since they aren't needed anymore
PayHook.databaseUrl = null;
PayHook.databaseRawUrl = null;
PayHook.databaseName = null;
PayHook.databaseUsername = null;
PayHook.databasePassword = null;
Expand Down Expand Up @@ -287,6 +290,26 @@ public static synchronized void init(String brandName, String databaseUrl, Strin
isInitialised = true;
}

/**
* Gets the raw database url without database name. <br>
* Before: "jdbc:mysql://localhost/my_database" <br>
* After: "jdbc:mysql://localhost" <br>
*/
private static String getRawDbUrlFrom(String databaseUrl) {
int index = 0;
int count = 0;
for (int i = 0; i < databaseUrl.length(); i++) {
char c = databaseUrl.charAt(i);
if(c == '/'){
index = i;
count++;
}
if(count == 3) break;
}
if(count != 3) return databaseUrl; // Means there is less than 3 "/", thus may already be raw url, or totally wrong url
return databaseUrl.substring(0, index);
}

public static void initCommandLineTool() {
if (commandLineThread != null) commandLineThread.interrupt();
commandLineThread = new Thread(() -> {
Expand Down

0 comments on commit f3d94e4

Please sign in to comment.