Package com.killcoding.datasource
Class DriverExecutor
java.lang.Object
com.killcoding.datasource.DriverExecutor
- Direct Known Subclasses:
CacheDriverExecutor
This class is execute sql base class.
Support database replication.
Support database multi-activity.
Support database CRUD
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanstatic final Stringstatic Stringstatic final Stringstatic final Stringprotected Connectionprotected Loggerprivate static CacheArrayprotected static final Map<Integer,List<DriverExecutor>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Abort connectionprotected static voidabortSyncConnection(Connection masterConn) For database replicationprivate static voidarchiveLog(int archiveDays, File sqlLogFolder) Archive Logprivate static voidbackupLog(File logFolder, File logFile, String logFileNamePrefix, int logIndex, String header, String msg) Backup log filebooleanExecute stored proc (and return boolean)booleancallAndReturnBoolean(String sql, List<Object> params) Execute stored proc (and return boolean)booleancallAndReturnBoolean(String sql, Map<String, Object> params) Execute stored proc (and return boolean)protected static voidcallAndReturnBooleanSync(Connection masterConn, long seq, String sql, List<Object> params, boolean returnResult) For database replicationcallAndReturnList(int cursorStart, int maxRows, String sql) Execute stored proc (and return List)callAndReturnList(int cursorStart, int maxRows, String sql, List<Object> params) Execute stored proc (and return List)callAndReturnList(int cursorStart, int maxRows, String sql, Map<String, Object> params) Execute stored proc (and return List)callAndReturnList(String sql) Execute stored proc (and return List)protected static voidcallAndReturnListSync(Connection masterConn, int cursorStart, int maxRows, String sql, List<Object> params) For database replicationintcallAndReturnRows(String sql) Execute stored proc (and return rows)intcallAndReturnRows(String sql, List<Object> params) Execute stored proc (and return rows)intcallAndReturnRows(String sql, Map<String, Object> params) Execute stored proc (and return rows)protected static voidcallAndReturnRowsSync(Connection masterConn, long seq, String sql, List<Object> params, int returnRows) For database replicationprotected booleancheckSqlAvailable(String sql) Check available sql to executeprivate static booleanCheck available sql to executeprotected static booleancheckSqlAvailable(Connection connection, String sql) Check available sql to executeprotected static booleancheckSqlLogAvailable(Connection connection, String sql) Check available sql to write to logvoidclose()Close connectionprotected static voidcloseSyncConnection(Connection masterConn) For database replicationvoidcommit()Commit connectionprotected static voidcommitSyncConnection(Connection masterConn) For database replicationprotected StringconverCase(String s) Covner to upper or lowerconverParams(String sql, Map<String, Object> map) Conver param from map to listprotected StringConver sql from ':column_name' to '?'private static voiddeleteFilesOlderThan(File directory, long archiveDaysMs) Delete old archive logsShow column data type and java type mappingintExecute sqlintExecute sqlintExecute sqlintExecute batchintexecuteBatchList(String sql, List<List<Object>> records) Execute batchprotected static voidexecuteBatchListSync(Connection masterConn, long seq, String sql, List<List<Object>> records, int returnRows) For database replicationprotected static voidexecuteSync(Connection masterConn, long seq, String sql, List<Object> params, int returnRows) For database replicationQuery all matched recordsQuery all matched recordsQuery all matched recordsQuery all matched recordsQuery all matched recordsQuery first recordQuery first recordQuery first recordGet all tablesgetAllTables(String _schema) Get all tables by schemagetColumnClasses(String tableName) Get column classes by table namegetColumnTypes(String tableName) Get column db data types by table nameGet current Connectionprivate StringGet data source thread nameprivate static intgetLogFileIndex(ConfigProperties configProperties, File folder, String prefix, String suffix) Get log file index For backup log file useprivate IntegerGet origin connection hash codegetPrimaryKeys(String _tableName) Get primary Keys by table nameprivate static StringhandleParams(int paramMaxLength, Object params) Hahdle Params For write csv logbooleanisClosed()Check connection is closedprotected static booleanopenSyncConnection(Connection masterConn) For database replicationprivate static StringreplaceToSigleLine(String msg) Replace to sigle line For write csv logvoidrollback()Rollback connectionprotected static voidrollbackSyncConnection(Connection masterConn) For database replicationprotected static booleanwriteSqlLog(int deHashCode, Connection connection, String type, long seq, String sql, Object params) Write sql logprotected booleanwriteSqlLog(String type, long seq, String sql, Object params) Write sql log
-
Field Details
-
SYNC_EXECUTOR_MARK
-
SYNC_CONN_ERROR_TIME
-
SQL_LOG_MSG_MAPPING
-
SQL_LOG_OVERSPEND_MAPPING
-
log
-
COLUMN_NAME_CASE_UPPER
- See Also:
-
COLUMN_NAME_CASE_LOWER
- See Also:
-
COLUMN_NAME_CASE_ORIGINAL
- See Also:
-
COLUMN_NAME_CASE_MODE
-
closed
-
connection
-
sqlLogCacheArray
-
-
Constructor Details
-
DriverExecutor
New a DriverExecutor object- Parameters:
connection- - JDBC connection
-
-
Method Details
-
getConnection
Get current Connection- Returns:
- Connection
-
getColumnClasses
Get column classes by table name- Parameters:
tableName- - table name- Returns:
- Map<String,Object> - column and java type class mapping
- Throws:
SQLException
-
getColumnTypes
Get column db data types by table name- Parameters:
tableName- - Table name- Returns:
- Map<String, Object> - column and db data type mapping
- Throws:
SQLException
-
desc
Show column data type and java type mapping- Parameters:
tableName- - Table name- Returns:
- List<Map<String, Object>> - Mapping list
- Throws:
SQLException
-
getPrimaryKeys
Get primary Keys by table name- Parameters:
_tableName- - Table name- Returns:
- List
- Primary Keys
-
getAllTables
Get all tables- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
getAllTables
Get all tables by schema- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
first
Query first record- Parameters:
sql- - Condition use format ':column_name'params-- Returns:
- Map<String,Object> - First result
- Throws:
SQLException
-
first
Query first record- Parameters:
sql-- Returns:
- Map<String,Object> - First result
- Throws:
SQLException
-
first
Query first record- Parameters:
sql- - Condition use format '?'params-- Returns:
- Map<String,Object> - First result
- Throws:
SQLException
-
find
Query all matched records- Parameters:
sql-params-- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
find
Query all matched records- Parameters:
cursorStart- - JDBC result Cursor start indexmaxRows- - JDBC result max rows (JDBC limited rows 50,000,000)sql-params-- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
find
Query all matched records- Parameters:
sql- - Condition use format ':column_name'params-- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
find
public List<Map<String,Object>> find(int cursorStart, int maxRows, String sql, Map<String, Object> params) throws SQLExceptionQuery all matched records- Parameters:
cursorStart- - JDBC result Cursor start indexmaxRows- - JDBC result max rows (JDBC limited rows 50,000,000)sql- - Condition use format ':column_name'params-- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
find
public List<Map<String,Object>> find(int cursorStart, int maxRows, String sql, List<Object> params) throws SQLException Query all matched records- Parameters:
cursorStart- - JDBC result Cursor start indexmaxRows- - JDBC result max rows (JDBC limited rows 50,000,000)sql- - Condition use format '?'params-- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
callAndReturnBoolean
Execute stored proc (and return boolean)- Parameters:
sql- - Query sql- Returns:
- boolean
- Throws:
SQLException
-
callAndReturnBoolean
Execute stored proc (and return boolean)- Parameters:
sql- - Query sql, condition use format ':column_name'params-- Returns:
- boolean
- Throws:
SQLException
-
callAndReturnBoolean
Execute stored proc (and return boolean)- Parameters:
sql- - Query sql,condition use format '?'params-- Returns:
- boolean
- Throws:
SQLException
-
callAndReturnRows
Execute stored proc (and return rows)- Parameters:
sql- - Query sql- Returns:
- int
- Throws:
SQLException
-
callAndReturnRows
Execute stored proc (and return rows)- Parameters:
sql- - Query sql,condition use format ':column_name'params-- Returns:
- int
- Throws:
SQLException
-
callAndReturnRows
Execute stored proc (and return rows)- Parameters:
sql- - Query sql,condition use format '?'params-- Returns:
- int
- Throws:
SQLException
-
callAndReturnList
Execute stored proc (and return List)- Parameters:
sql- - Query sql- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
callAndReturnList
public List<Map<String,Object>> callAndReturnList(int cursorStart, int maxRows, String sql) throws SQLException Execute stored proc (and return List)- Parameters:
cursorStart- - JDBC result Cursor start indexmaxRows- - JDBC result max rows (JDBC limited rows 50,000,000)sql- - Query sql- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
callAndReturnList
public List<Map<String,Object>> callAndReturnList(int cursorStart, int maxRows, String sql, Map<String, Object> params) throws SQLExceptionExecute stored proc (and return List)- Parameters:
cursorStart- - JDBC result Cursor start indexmaxRows- - JDBC result max rows (JDBC limited rows 50,000,000)sql- - Query sql, condition use format ':column_name'- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
callAndReturnList
public List<Map<String,Object>> callAndReturnList(int cursorStart, int maxRows, String sql, List<Object> params) throws SQLException Execute stored proc (and return List)- Parameters:
cursorStart- - JDBC result Cursor start indexmaxRows- - JDBC result max rows (JDBC limited rows 50,000,000)sql- - Query sql, condition use format '?'- Returns:
- List<Map<String, Object>>
- Throws:
SQLException
-
execute
Execute sql- Parameters:
sql-- Returns:
- int
- Throws:
SQLException
-
execute
Execute sql- Parameters:
sql- - Condition use format ':column_name'params-- Returns:
- int
- Throws:
SQLException
-
execute
Execute sql- Parameters:
sql- - Condition use format '?'params-- Returns:
- int
- Throws:
SQLException
-
executeBatch
Execute batch- Parameters:
sql- - Condition use format ':column_name'records-- Returns:
- int - Return rows
- Throws:
SQLException
-
executeBatchList
Execute batch- Parameters:
sql- - Condition use format '?'records-- Returns:
- int - Return rows
- Throws:
SQLException
-
isClosed
Check connection is closed- Returns:
- boolean
-
abort
Abort connection- Throws:
SQLException
-
close
Close connection- Throws:
SQLException
-
commit
Commit connection- Throws:
SQLException
-
rollback
Rollback connection- Throws:
SQLException
-
getOriginConnectionHashCode
Get origin connection hash code- Returns:
- Integer - hash code
-
getDataSourceName
Get data source thread name- Returns:
- String - DataSource thread name
-
converSql
Conver sql from ':column_name' to '?'- Returns:
- String
-
converParams
Conver param from map to list- Parameters:
sql-map-- Returns:
- List
-
converCase
Covner to upper or lower- Parameters:
s- - Column name or table name- Returns:
- String
-
checkSqlLogAvailable
Check available sql to write to log- Parameters:
connection- - Connectionsql-- Returns:
- boolean
-
checkSqlAvailable
Check available sql to execute- Parameters:
sql-- Returns:
- boolean
-
checkSqlAvailable
Check available sql to execute- Parameters:
connection-sql-- Returns:
- boolean
-
checkSqlAvailable
private static boolean checkSqlAvailable(String sql, List<String> sqlAllowed, List<String> sqlIgnored) Check available sql to execute- Parameters:
sql-sqlAllowed- - From DataSources.propertiessqlIgnored- - From DataSources.properties- Returns:
- boolean
-
writeSqlLog
Write sql log- Parameters:
type-seq-sql-params-- Returns:
- boolean
-
writeSqlLog
protected static boolean writeSqlLog(int deHashCode, Connection connection, String type, long seq, String sql, Object params) Write sql log- Parameters:
connection-type-seq-sql-params-- Returns:
- boolean
-
backupLog
private static void backupLog(File logFolder, File logFile, String logFileNamePrefix, int logIndex, String header, String msg) throws Exception Backup log file- Throws:
Exception
-
getLogFileIndex
private static int getLogFileIndex(ConfigProperties configProperties, File folder, String prefix, String suffix) Get log file index For backup log file use -
archiveLog
Archive Log -
deleteFilesOlderThan
Delete old archive logs- Throws:
IOException
-
replaceToSigleLine
Replace to sigle line For write csv log -
handleParams
Hahdle Params For write csv log -
callAndReturnBooleanSync
protected static void callAndReturnBooleanSync(Connection masterConn, long seq, String sql, List<Object> params, boolean returnResult) throws SQLException For database replication- Throws:
SQLException
-
callAndReturnListSync
protected static void callAndReturnListSync(Connection masterConn, int cursorStart, int maxRows, String sql, List<Object> params) throws SQLException For database replication- Throws:
SQLException
-
callAndReturnRowsSync
protected static void callAndReturnRowsSync(Connection masterConn, long seq, String sql, List<Object> params, int returnRows) throws SQLException For database replication- Throws:
SQLException
-
executeBatchListSync
protected static void executeBatchListSync(Connection masterConn, long seq, String sql, List<List<Object>> records, int returnRows) throws SQLException For database replication- Throws:
SQLException
-
executeSync
protected static void executeSync(Connection masterConn, long seq, String sql, List<Object> params, int returnRows) throws SQLException For database replication- Throws:
SQLException
-
openSyncConnection
For database replication- Throws:
SQLException
-
closeSyncConnection
For database replication- Throws:
SQLException
-
commitSyncConnection
For database replication- Throws:
SQLException
-
rollbackSyncConnection
For database replication- Throws:
SQLException
-
abortSyncConnection
For database replication- Throws:
SQLException
-