-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mysql adapter for sqlite and legacy/modern mysql and container …
…test
- Loading branch information
Fabian Phan
committed
Apr 23, 2024
1 parent
7c00b72
commit 230ffaf
Showing
25 changed files
with
475 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
ndatabase-core/src/main/java/com/nivixx/ndatabase/core/dao/mysql/adapter/MySQLAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.nivixx.ndatabase.core.dao.mysql.adapter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public abstract class MySQLAdapter { | ||
|
||
public abstract void createIndexIfNotExist( | ||
String tableName, String columnName, Class<?> fieldType, Connection connection) throws SQLException; | ||
} |
50 changes: 50 additions & 0 deletions
50
...-core/src/main/java/com/nivixx/ndatabase/core/dao/mysql/adapter/MySQLAdapterResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.nivixx.ndatabase.core.dao.mysql.adapter; | ||
|
||
import java.sql.*; | ||
|
||
public class MySQLAdapterResolver { | ||
|
||
public static MySQLAdapter resolveMySQLAdapter(Connection connection) throws SQLException { | ||
|
||
DatabaseMetaData metaData = connection.getMetaData(); | ||
String dbName = metaData.getDatabaseProductName(); | ||
|
||
if (dbName.equalsIgnoreCase("SQLite")) { | ||
return new MySQLAdapter_SQLITE(); | ||
} | ||
|
||
String version = getMySQLVersion(connection); | ||
boolean isMySQL57OrBelow = isMySQL57OrBelow(version); | ||
|
||
if (isMySQL57OrBelow) { | ||
return new MySQLAdapter_MYSQL_5_7(); | ||
} | ||
|
||
// Add more if needed | ||
|
||
return new MySQLAdapter_MYSQL_MODERN(); | ||
|
||
} | ||
|
||
|
||
private static String getMySQLVersion(Connection conn) throws SQLException { | ||
String version = null; | ||
try (Statement stmt = conn.createStatement()) { | ||
ResultSet rs = stmt.executeQuery("SELECT VERSION()"); | ||
if (rs.next()) { | ||
version = rs.getString(1); | ||
} | ||
} | ||
return version; | ||
} | ||
|
||
private static boolean isMySQL57OrBelow(String version) { | ||
if (version != null && version.matches("^\\d+\\.\\d+\\.\\d+")) { | ||
String[] parts = version.split("\\."); | ||
int major = Integer.parseInt(parts[0]); | ||
int minor = Integer.parseInt(parts[1]); | ||
return major < 6 || (major == 5 && minor <= 7); | ||
} | ||
return false; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...-core/src/main/java/com/nivixx/ndatabase/core/dao/mysql/adapter/MySQLAdapter_MARIADB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.nivixx.ndatabase.core.dao.mysql.adapter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.text.MessageFormat; | ||
|
||
public class MySQLAdapter_MARIADB extends MySQLAdapter { | ||
|
||
@Override | ||
public void createIndexIfNotExist(String tableName, String columnName, Class<?> fieldType, Connection connection) throws SQLException { | ||
|
||
String createIndexQuery = MessageFormat.format( | ||
"CREATE INDEX IF NOT EXISTS {0}_index ON {1}({2})", | ||
columnName, tableName, columnName); | ||
try (PreparedStatement ps = connection.prepareStatement(createIndexQuery)) { | ||
ps.execute(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ore/src/main/java/com/nivixx/ndatabase/core/dao/mysql/adapter/MySQLAdapter_MYSQL_5_7.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.nivixx.ndatabase.core.dao.mysql.adapter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.text.MessageFormat; | ||
|
||
public class MySQLAdapter_MYSQL_5_7 extends MySQLAdapter { | ||
|
||
@Override | ||
public void createIndexIfNotExist(String tableName, String columnName, Class<?> fieldType, Connection connection) throws SQLException { | ||
// Check if the index exists | ||
boolean indexExists = indexExists(tableName, columnName, connection); | ||
|
||
// If the index doesn't exist, create it | ||
if (!indexExists) { | ||
// In Legacy sql version, we need to specify index length for TEXT | ||
String createIndexQuery = | ||
fieldType.isAssignableFrom(String.class) ? | ||
"CREATE INDEX " + columnName + "_index ON " + tableName + "(" + columnName + "(255))" : | ||
"CREATE INDEX " + columnName + "_index ON " + tableName + "(" + columnName + ")"; | ||
try (PreparedStatement ps = connection.prepareStatement(createIndexQuery)) { | ||
ps.executeUpdate(); | ||
} | ||
} | ||
} | ||
|
||
|
||
private boolean indexExists(String tableName, String columnName, Connection connection) throws SQLException { | ||
String indexExistsQuery = "SHOW INDEX FROM " + tableName + " WHERE Key_name = ?"; | ||
try (PreparedStatement ps = connection.prepareStatement(indexExistsQuery)) { | ||
ps.setString(1, columnName + "_index"); | ||
try (ResultSet rs = ps.executeQuery()) { | ||
return rs.next(); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../src/main/java/com/nivixx/ndatabase/core/dao/mysql/adapter/MySQLAdapter_MYSQL_MODERN.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.nivixx.ndatabase.core.dao.mysql.adapter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.text.MessageFormat; | ||
|
||
public class MySQLAdapter_MYSQL_MODERN extends MySQLAdapter { | ||
|
||
@Override | ||
public void createIndexIfNotExist(String tableName, String columnName, Class<?> fieldType, Connection connection) throws SQLException { | ||
// Check if the index exists | ||
boolean indexExists = indexExists(tableName, columnName, connection); | ||
|
||
// If the index doesn't exist, create it | ||
if (!indexExists) { | ||
// In Legacy sql version, we need to specify index length for TEXT | ||
String createIndexQuery = | ||
fieldType.isAssignableFrom(String.class) ? | ||
"CREATE INDEX " + columnName + "_index ON " + tableName + "(" + columnName + "(255))" : | ||
"CREATE INDEX " + columnName + "_index ON " + tableName + "(" + columnName + ")"; | ||
try (PreparedStatement ps = connection.prepareStatement(createIndexQuery)) { | ||
ps.executeUpdate(); | ||
} | ||
} | ||
} | ||
|
||
|
||
private boolean indexExists(String tableName, String columnName, Connection connection) throws SQLException { | ||
String indexExistsQuery = "SHOW INDEX FROM " + tableName + " WHERE Key_name = ?"; | ||
try (PreparedStatement ps = connection.prepareStatement(indexExistsQuery)) { | ||
ps.setString(1, columnName + "_index"); | ||
try (ResultSet rs = ps.executeQuery()) { | ||
return rs.next(); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...e-core/src/main/java/com/nivixx/ndatabase/core/dao/mysql/adapter/MySQLAdapter_SQLITE.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.nivixx.ndatabase.core.dao.mysql.adapter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.text.MessageFormat; | ||
|
||
public class MySQLAdapter_SQLITE extends MySQLAdapter { | ||
|
||
@Override | ||
public void createIndexIfNotExist(String tableName, String columnName, Class<?> fieldType, Connection connection) throws SQLException { | ||
|
||
String createIndexQuery = MessageFormat.format( | ||
"CREATE INDEX IF NOT EXISTS {0}_index ON {1}({2})", | ||
columnName, tableName, columnName); | ||
try (PreparedStatement ps = connection.prepareStatement(createIndexQuery)) { | ||
ps.execute(); | ||
} | ||
} | ||
} |
Oops, something went wrong.