Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.sql2o.reflection.Setter;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
* User: dimzon
Expand Down Expand Up @@ -61,7 +62,9 @@ public void testAllTypes() throws IllegalAccessException {

assertFalse(pojo1.equals(pojo2));

Field[] fields = pojo1.getClass().getDeclaredFields();
Field[] fields = Arrays.stream(pojo1.getClass().getDeclaredFields())
.filter(f -> !f.isSynthetic())
.toArray(Field[]::new);
for (Field field : fields) {
Setter setter = fsf.newSetter(field);
assertSame(field.getType(),setter.getType());
Expand Down
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<!--<module>oracle</module>-->
<!--<module>oracle-joda-time</module>-->
<module>db2</module>
<module>sql2o-mysql</module>
</modules>

<dependencies>
Expand Down
6 changes: 6 additions & 0 deletions extensions/postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<version>9.1-901.jdbc4</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.17.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.impossibl.pgjdbc-ng</groupId>
<artifactId>pgjdbc-ng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public void testIssue10StatementsOnPostgres_noTransaction(){
try {
try (Connection connection = sql2o.open()) {
connection.createQuery("create table test_table(id SERIAL, val varchar(20))").executeUpdate();
}

try (Connection connection = sql2o.open()) {
Long key = connection.createQuery("insert into test_table (val) values(:val)", true)
.addParameter("val", "something").executeUpdate().getKey(Long.class);
assertNotNull(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PostgresTestSupport {
@Parameterized.Parameters(name = "{index} - {4}")
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{"jdbc:postgresql:testdb", "test", "testtest", "Official postgres driver"},
{"jdbc:tc:postgresql:9.6.8:///testdb", "test", "testtest", "Official postgres driver"},
// {"jdbc:pgsql://localhost/testdb", "test", "testtest", "Impossibl postgres driver"}
});
}
Expand Down
39 changes: 39 additions & 0 deletions extensions/sql2o-mysql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
~ Copyright (c) 2014 Lars Aaberg
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>sql2o-mysql</artifactId>
<packaging>jar</packaging>
<name>sql2o-mysql</name>
<description>MySQL extension for sql2o</description>
<url>http://www.sql2o.org</url>
<parent>
<groupId>org.sql2o.extensions</groupId>
<artifactId>extensions-parent</artifactId>
<version>1.6.1-SNAPSHOT</version>
</parent>

<dependencies>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.17.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sql2o.quirks;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQLQuirks extends NoQuirks {
public MySQLQuirks() {
super();
}

@Override
public Object getRSVal(ResultSet rs, int idx) throws SQLException {
Object o = rs.getObject(idx);
if (o != null && o.getClass().getCanonicalName().startsWith("java.time.LocalDateTime")){
o = rs.getObject(idx, java.sql.Timestamp.class);
}
return o;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sql2o.quirks;

/**
* Created by davidlzs on 25.11.22.
*/
public class MySQLQuirksProvider implements QuirksProvider {


@Override
public Quirks provide() {
return new MySQLQuirks();
}

@Override
public boolean isUsableForUrl(String url) {
return url.startsWith("jdbc:mysql:");
}

@Override
public boolean isUsableForClass(String className) {
return className.startsWith("com.mysql.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Library-Name: Sql2o.extensions.mysql
Library-Description: MySQL plugin for sql2o
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright (c) 2014 Lars Aaberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

org.sql2o.quirks.MySQLQuirksProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.sql2o.extensions;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sql2o.Connection;
import org.sql2o.Sql2o;
import org.sql2o.data.Row;
import org.sql2o.data.Table;
import org.sql2o.quirks.MySQLQuirks;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class MySQLTest {
private final Sql2o sql2o;
private final Logger logger = LoggerFactory.getLogger(MySQLTest.class);

public MySQLTest(String url, String user, String pass, String testName) {
logger.info(testName);
sql2o = new Sql2o(url, user, pass, new MySQLQuirks());
logger.info("starting MySQLTest");
}

@Parameterized.Parameters()
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{"jdbc:tc:mysql:5.7.34:///testdb", "test", "testtest", "Official MySQL driver"},
});
}

@Test
public void testMySQL_whenJDBCReturnLocalDateTime() {
try (Connection connection = sql2o.open()) {
connection.createQuery("create table test_table(created_datetime datetime(6))").executeUpdate();
//MySQL date(6) stores value to microseconds (6 digits) precision
Timestamp now = Timestamp.valueOf("2021-10-20 21:29:42.345678");
connection.createQuery("insert into test_table (created_datetime) values(:created_datetime)")
.addParameter("created_datetime", now).executeUpdate();

String selectSql = "select created_datetime from test_table";
Table resultTable = connection.createQuery(selectSql).executeAndFetchTable();

assertThat(resultTable.rows().size(), is(1));
Row resultRow = resultTable.rows().get(0);

assertThat(resultRow.getObject("created_datetime", Timestamp.class), equalTo(now));
}
}
}