Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to return auto generated keys #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>ca.krasnay</groupId>
<artifactId>sqlbuilder</artifactId>
<packaging>jar</packaging>
<version>2.1-SNAPSHOT</version>
<version>2.2-SNAPSHOT</version>

<name>SQL Builder</name>
<description>A lightweight library for building and executing SQL statements.</description>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ca/krasnay/sqlbuilder/InsertCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce
return ppsc.createPreparedStatement(conn);
}

public PreparedStatement createPreparedStatement(Connection conn, boolean autoGeneratedKeys) throws SQLException {
ppsc.setSql(builder.toString());
return ppsc.createPreparedStatement(conn, autoGeneratedKeys);
}

public ParameterizedPreparedStatementCreator setParameter(String name, Object value) {
return ppsc.setParameter(name, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public ParameterizedPreparedStatementCreator clone() {
}

public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
return createPreparedStatement(con, null);
}

public PreparedStatement createPreparedStatement(Connection con, Boolean autoGeneratedKeys) throws SQLException {

log.debug("createPreparedStatement: {}", sql);
for (String key : parameterMap.keySet()) {
Expand All @@ -103,7 +107,16 @@ public PreparedStatement createPreparedStatement(Connection con) throws SQLExcep

SqlAndParams sap = createSqlAndParams();

PreparedStatement ps = con.prepareStatement(sap.getSql());
PreparedStatement ps;
if(autoGeneratedKeys != null) {
int autoGeneratedKeysFlag = autoGeneratedKeys
? PreparedStatement.RETURN_GENERATED_KEYS
: PreparedStatement.NO_GENERATED_KEYS;

ps = con.prepareStatement(sap.getSql(), autoGeneratedKeysFlag);
} else {
ps = con.prepareStatement(sap.getSql());
}

for (int i = 0; i < sap.getParams().size(); i++) {
Object paramValue = sap.getParams().get(i);
Expand Down