-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10c2777
commit 28af87f
Showing
17 changed files
with
349 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.apache.ibatis.binding; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.mapping.SqlCommandType; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public class MapperMethod { | ||
|
||
private final SqlCommand command; | ||
|
||
public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) { | ||
this.command = new SqlCommand(config, mapperInterface, method); | ||
} | ||
|
||
public Object execute(SqlSession sqlSession, Object[] args) { | ||
Object result; | ||
switch (command.getType()) { | ||
case INSERT: { | ||
result = sqlSession.insert(command.getName(), args); | ||
break; | ||
} | ||
default: | ||
throw new BindingException(command.getType() + " not support"); | ||
} | ||
return result; | ||
} | ||
|
||
public static class SqlCommand { | ||
|
||
private final String name; | ||
private final SqlCommandType type; | ||
|
||
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) { | ||
final String methodName = method.getName(); | ||
String statementId = mapperInterface.getName() + "." + methodName; | ||
MappedStatement ms = configuration.getMappedStatement(statementId); | ||
if (Objects.isNull(ms)) { | ||
throw new BindingException(statementId + " statement not found"); | ||
} | ||
this.name = ms.getId(); | ||
this.type = ms.getSqlCommandType(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public SqlCommandType getType() { | ||
return type; | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.apache.ibatis.executor; | ||
|
||
import java.sql.SQLException; | ||
|
||
import org.apache.ibatis.mapping.MappedStatement; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public interface Executor { | ||
|
||
int update(MappedStatement ms, Object parameter) throws SQLException; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/apache/ibatis/executor/ExecutorException.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,27 @@ | ||
package org.apache.ibatis.executor; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public class ExecutorException extends RuntimeException { | ||
|
||
public ExecutorException() { | ||
super(); | ||
} | ||
|
||
public ExecutorException(String message) { | ||
super(message); | ||
} | ||
|
||
public ExecutorException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ExecutorException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected ExecutorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/org/apache/ibatis/executor/SimpleExecutor.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,47 @@ | ||
package org.apache.ibatis.executor; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.executor.statement.StatementHandler; | ||
import org.apache.ibatis.executor.statement.StatementUtil; | ||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public class SimpleExecutor implements Executor { | ||
|
||
private final Configuration configuration; | ||
|
||
public SimpleExecutor(Configuration configuration) { | ||
if (Objects.isNull(configuration.getDataSource())) { | ||
throw new ExecutorException("dataSource in Configuration cannot be empty"); | ||
} | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public int update(MappedStatement ms, Object parameter) throws SQLException { | ||
Statement stmt = null; | ||
try { | ||
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, null); | ||
stmt = prepareStatement(handler); | ||
return handler.update(stmt); | ||
} finally { | ||
StatementUtil.closeStatement(stmt); | ||
} | ||
} | ||
|
||
private Statement prepareStatement(StatementHandler handler) throws SQLException { | ||
Statement stmt; | ||
Connection connection = configuration.getDataSource().getConnection(); | ||
connection.setAutoCommit(true); | ||
stmt = handler.prepare(connection, null); | ||
return stmt; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/apache/ibatis/executor/statement/PrepareStatementHandler.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,45 @@ | ||
package org.apache.ibatis.executor.statement; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.executor.Executor; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public class PrepareStatementHandler implements StatementHandler { | ||
|
||
protected final Configuration configuration; | ||
protected final Executor executor; | ||
protected final MappedStatement mappedStatement; | ||
protected BoundSql boundSql; | ||
|
||
public PrepareStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { | ||
this.configuration = mappedStatement.getConfiguration(); | ||
this.executor = executor; | ||
this.mappedStatement = mappedStatement; | ||
if (Objects.isNull(boundSql)) { | ||
boundSql = mappedStatement.getBoundSql(parameterObject); | ||
} | ||
this.boundSql = boundSql; | ||
} | ||
|
||
@Override | ||
public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException { | ||
return connection.prepareStatement(boundSql.getSql()); | ||
} | ||
|
||
@Override | ||
public int update(Statement statement) throws SQLException { | ||
PreparedStatement ps = (PreparedStatement) statement; | ||
ps.execute(); | ||
return ps.getUpdateCount(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/apache/ibatis/executor/statement/StatementHandler.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,15 @@ | ||
package org.apache.ibatis.executor.statement; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public interface StatementHandler { | ||
|
||
Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException; | ||
|
||
int update(Statement statement) throws SQLException; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/apache/ibatis/executor/statement/StatementUtil.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,21 @@ | ||
package org.apache.ibatis.executor.statement; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
/** | ||
* @author furious 2024/4/8 | ||
*/ | ||
public enum StatementUtil { | ||
; | ||
|
||
public static void closeStatement(Statement statement) { | ||
if (statement != null) { | ||
try { | ||
statement.close(); | ||
} catch (SQLException ignore) { | ||
} | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.