-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
A new type handler for SQLXML data type. #1221
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/org/apache/ibatis/type/SqlxmlTypeHandler.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,72 @@ | ||
/** | ||
* Copyright 2009-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.ibatis.type; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.SQLXML; | ||
|
||
/** | ||
* Convert <code>String</code> to/from <code>SQLXML</code>. | ||
* | ||
* @since 3.4.5 | ||
* @author Iwao AVE! | ||
*/ | ||
public class SqlxmlTypeHandler extends BaseTypeHandler<String> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add JavaDoc(description, since, author...). |
||
|
||
@Override | ||
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) | ||
throws SQLException { | ||
SQLXML sqlxml = ps.getConnection().createSQLXML(); | ||
try { | ||
sqlxml.setString(parameter); | ||
ps.setSQLXML(i, sqlxml); | ||
} finally { | ||
sqlxml.free(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getNullableResult(ResultSet rs, String columnName) throws SQLException { | ||
return sqlxmlToString(rs.getSQLXML(columnName)); | ||
} | ||
|
||
@Override | ||
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | ||
return sqlxmlToString(rs.getSQLXML(columnIndex)); | ||
} | ||
|
||
@Override | ||
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | ||
return sqlxmlToString(cs.getSQLXML(columnIndex)); | ||
} | ||
|
||
protected String sqlxmlToString(SQLXML sqlxml) throws SQLException { | ||
if (sqlxml == null) { | ||
return null; | ||
} | ||
try { | ||
String result = sqlxml.getString(); | ||
return result; | ||
} finally { | ||
sqlxml.free(); | ||
} | ||
} | ||
|
||
} |
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
167 changes: 167 additions & 0 deletions
167
src/test/java/org/apache/ibatis/type/SqlxmlTypeHandlerTest.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,167 @@ | ||
/** | ||
* Copyright 2009-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.ibatis.type; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.Reader; | ||
import java.nio.file.Paths; | ||
import java.sql.Connection; | ||
import java.util.Collections; | ||
|
||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; | ||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.mapping.Environment; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.apache.ibatis.test.EmbeddedPostgresqlTests; | ||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; | ||
import ru.yandex.qatools.embed.postgresql.util.SocketUtil; | ||
|
||
@Category(EmbeddedPostgresqlTests.class) | ||
public class SqlxmlTypeHandlerTest { | ||
private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
// Launch PostgreSQL server. Download / unarchive if necessary. | ||
String url = postgres.start( | ||
EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", | ||
SocketUtil.findFreePort(), "postgres_sqlxml", "postgres", "root", Collections.emptyList()); | ||
|
||
Configuration configuration = new Configuration(); | ||
Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( | ||
"org.postgresql.Driver", url, null)); | ||
configuration.setEnvironment(environment); | ||
configuration.setUseGeneratedKeys(true); | ||
configuration.addMapper(Mapper.class); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); | ||
|
||
try (SqlSession session = sqlSessionFactory.openSession(); | ||
Connection conn = session.getConnection(); | ||
Reader reader = Resources | ||
.getResourceAsReader("org/apache/ibatis/type/SqlxmlTypeHandlerTest.sql")) { | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() { | ||
postgres.stop(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnXmlAsString() throws Exception { | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = session.getMapper(Mapper.class); | ||
XmlBean bean = mapper.select(1); | ||
assertEquals("<title>XML data</title>", | ||
bean.getContent()); | ||
} finally { | ||
session.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnNull() throws Exception { | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = session.getMapper(Mapper.class); | ||
XmlBean bean = mapper.select(2); | ||
assertNull(bean.getContent()); | ||
} finally { | ||
session.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldInsertXmlString() throws Exception { | ||
final Integer id = 100; | ||
final String content = "<books><book><title>Save XML</title></book><book><title>Get XML</title></book></books>"; | ||
// Insert | ||
{ | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = session.getMapper(Mapper.class); | ||
XmlBean bean = new XmlBean(); | ||
bean.setId(id); | ||
bean.setContent(content); | ||
mapper.insert(bean); | ||
session.commit(); | ||
} finally { | ||
session.close(); | ||
} | ||
} | ||
// Select to verify | ||
{ | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = session.getMapper(Mapper.class); | ||
XmlBean bean = mapper.select(id); | ||
assertEquals(content, bean.getContent()); | ||
} finally { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
interface Mapper { | ||
@Select("select id, content from mbtest.test_sqlxml where id = #{id}") | ||
XmlBean select(Integer id); | ||
|
||
@Insert("insert into mbtest.test_sqlxml (id, content) values (#{id}, #{content,jdbcType=SQLXML})") | ||
void insert(XmlBean bean); | ||
} | ||
|
||
public static class XmlBean { | ||
private Integer id; | ||
|
||
private String content; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/org/apache/ibatis/type/SqlxmlTypeHandlerTest.sql
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,28 @@ | ||
-- | ||
-- Copyright 2009-2018 the original author or authors. | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
CREATE SCHEMA mbtest; | ||
|
||
CREATE TABLE mbtest.test_sqlxml ( | ||
id serial PRIMARY KEY, | ||
content XML | ||
); | ||
|
||
INSERT INTO mbtest.test_sqlxml (id, content) | ||
VALUES (1, '<title>XML data</title>'); | ||
|
||
INSERT INTO mbtest.test_sqlxml (id, content) | ||
VALUES (2, NULL); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.5.0 or 3.4.7 ?