-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/manifold-systems/manifold/issues/608
- streamline duckdb's appender usage within manifold-sql
- Loading branch information
1 parent
b6ad6a0
commit 746a14d
Showing
8 changed files
with
439 additions
and
25 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
...-parent/manifold-sql-inproc-test/src/test/java/manifold/sql/schema/duckdb/AppendTest.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,86 @@ | ||
/* | ||
* Copyright (c) 2023 - Manifold Systems LLC | ||
* | ||
* 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 manifold.sql.schema.duckdb; | ||
|
||
import manifold.sql.schema.duckdb.base.DuckdbDdlServerTest; | ||
import manifold.sql.schema.simple.duckdb.DuckdbSakila; | ||
import manifold.sql.schema.simple.duckdb.DuckdbSakila.Country; | ||
import org.junit.Test; | ||
|
||
import java.sql.SQLException; | ||
import java.time.LocalDateTime; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class AppendTest extends DuckdbDdlServerTest | ||
{ | ||
@Test | ||
public void testAppendNoTransaction() throws SQLException | ||
{ | ||
LocalDateTime now = LocalDateTime.now(); | ||
Country.append( a -> { | ||
a.append( 1, "Canada", now ); | ||
a.append( 2, "Mexico", now ); | ||
a.append( 3, "Belize", now ); | ||
a.append( 4, "Brazil", now ); | ||
} ); | ||
StringBuilder sb = new StringBuilder(); | ||
for( Country country : Country.fetchAll() ) | ||
{ | ||
sb.append( country.getCountryId() ).append( ',' ) | ||
.append( country.getCountry() ).append( ',' ) | ||
.append( country.getLastUpdate() ).append( '\n' ); | ||
} | ||
assertEquals( | ||
"1,Canada," + now + "\n" + | ||
"2,Mexico," + now + "\n" + | ||
"3,Belize," + now + "\n" + | ||
"4,Brazil," + now + "\n", | ||
sb.toString() ); | ||
} | ||
|
||
@Test | ||
public void testAppendWithinTransaction() throws SQLException | ||
{ | ||
LocalDateTime now = LocalDateTime.now(); | ||
DuckdbSakila.commit( ctx -> { | ||
Country.builder( "first" ).withLastUpdate( now ).build(); | ||
"[.sql:DuckdbSakila/] insert into country (country, last_update) values('second', :now)".execute( ctx, now ); | ||
Country.append( a -> { | ||
a.append( 10, "Canada", now ); | ||
a.append( 20, "Mexico", now ); | ||
a.append( 30, "Belize", now ); | ||
a.append( 40, "Brazil", now ); | ||
} ); | ||
} ); | ||
StringBuilder sb = new StringBuilder(); | ||
for( Country country : Country.fetchAll() ) | ||
{ | ||
sb.append( country.getCountryId() ).append( ',' ) | ||
.append( country.getCountry() ).append( ',' ) | ||
.append( country.getLastUpdate() ).append( '\n' ); | ||
} | ||
assertEquals( | ||
"1,first," + now + "\n" + | ||
"2,second," + now + "\n" + | ||
"10,Canada," + now + "\n" + | ||
"20,Mexico," + now + "\n" + | ||
"30,Belize," + now + "\n" + | ||
"40,Brazil," + now + "\n", | ||
sb.toString() ); | ||
} | ||
} |
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
124 changes: 124 additions & 0 deletions
124
manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/api/SchemaAppender.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,124 @@ | ||
package manifold.sql.rt.api; | ||
|
||
import manifold.json.rt.api.DataBindings; | ||
import manifold.util.ManExceptionUtil; | ||
import org.duckdb.DuckDBAppender; | ||
import org.duckdb.DuckDBConnection; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
public abstract class SchemaAppender | ||
{ | ||
private final String _schema; | ||
private final String _table; | ||
private DuckDBAppender _appender; | ||
|
||
public SchemaAppender( String schema, String table ) | ||
{ | ||
_schema = schema; | ||
_table = table; | ||
} | ||
|
||
// called from generated code | ||
@SuppressWarnings( "unused" ) | ||
protected void appendRow( DataBindings bindings ) | ||
{ | ||
try | ||
{ | ||
_appender.beginRow(); | ||
appendBindings( _appender, bindings ); | ||
_appender.endRow(); | ||
} | ||
catch( SQLException e ) | ||
{ | ||
throw ManExceptionUtil.unchecked( e ); | ||
} | ||
} | ||
|
||
public <T extends SchemaAppender> void execute( Connection c, Consumer<T> consumer ) throws SQLException | ||
{ | ||
DuckDBConnection duckdbConnection = c.unwrap( DuckDBConnection.class ); | ||
try( DuckDBAppender appender = duckdbConnection.createAppender( _schema, _table ) ) | ||
{ | ||
_appender = appender; | ||
//noinspection unchecked | ||
consumer.accept( (T)this ); | ||
} | ||
catch( Exception e ) | ||
{ | ||
throw new SQLException( e ); | ||
} | ||
} | ||
|
||
private void appendBindings( DuckDBAppender appender, DataBindings bindings ) throws SQLException | ||
{ | ||
for( Map.Entry<String, Object> entry : bindings.entrySet() ) | ||
{ | ||
Object value = entry.getValue(); | ||
if( value == null ) | ||
{ | ||
appender.append( null ); | ||
} | ||
else if( value instanceof BigDecimal ) | ||
{ | ||
appender.appendBigDecimal( (BigDecimal)value ); | ||
} | ||
else if( value instanceof BigInteger ) | ||
{ | ||
appender.append( ((BigInteger)value).longValueExact() ); | ||
} | ||
else if( value instanceof Long ) | ||
{ | ||
appender.append( (Long)value ); | ||
} | ||
else if( value instanceof Integer ) | ||
{ | ||
appender.append( (Integer)value ); | ||
} | ||
else if( value instanceof Short ) | ||
{ | ||
appender.append( (Short)value ); | ||
} | ||
else if( value instanceof Byte ) | ||
{ | ||
appender.append( (Byte)value ); | ||
} | ||
else if( value instanceof Double ) | ||
{ | ||
appender.append( (Double)value ); | ||
} | ||
else if( value instanceof Float ) | ||
{ | ||
appender.append( (Float)value ); | ||
} | ||
else if( value instanceof Boolean ) | ||
{ | ||
appender.append( (Boolean)value ); | ||
} | ||
else if( value instanceof LocalDateTime ) | ||
{ | ||
appender.appendLocalDateTime( (LocalDateTime)value ); | ||
} | ||
else if( value instanceof OffsetDateTime ) | ||
{ | ||
appender.appendLocalDateTime( ((OffsetDateTime)value).toLocalDateTime() ); | ||
} | ||
else if( value instanceof LocalDate ) | ||
{ | ||
appender.appendLocalDateTime( ((LocalDate)value).atStartOfDay() ); | ||
} | ||
else | ||
{ | ||
appender.append( value.toString() ); | ||
} | ||
} | ||
} | ||
} |
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.