Last active
August 14, 2020 01:27
-
-
Save nakamura-to/03be0523b284d393610b42393d17940d to your computer and use it in GitHub Desktop.
RedshiftのUNLOADコマンドをDomaで生成する案
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
package sample; | |
public class AppConfig implements Config { | |
@Override | |
public QueryImplementors getQueryImplementors() { | |
return new QueryImplementors() { | |
@Override | |
public SqlFileSelectQuery createSqlFileSelectQuery(Method method) { | |
if (method.isAnnotationPresent(Unload.class)) { | |
return new UnloadQuery(); | |
} | |
return QueryImplementors.super.createSqlFileSelectQuery(method); | |
} | |
}; | |
} | |
} |
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
package sample; | |
import org.seasar.doma.Entity; | |
@Entity | |
public class DataLog { | |
public String hoge; | |
public String foo; | |
public String bar; | |
} |
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
package sample; | |
import java.time.LocalDateTime; | |
import java.util.List; | |
import org.seasar.doma.Dao; | |
import org.seasar.doma.Select; | |
@Dao | |
public interface DataLogDao { | |
@Unload | |
@Select | |
List<DataLog> unload(LocalDateTime startAt, LocalDateTime endAt, | |
String s3bucket, String accessKey, String sercretKey); | |
} |
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
package sample; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Unload { | |
} |
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
SELECT | |
hoge,foo,bar | |
FROM | |
data_log_table | |
WHERE | |
created_at BETWEEN /* startAt */'2016-11-01 10:00:00' AND /* endAt */'2016-11-01 11:00:00' | |
/*%if s3bucket == null && accessKey == null && sercretKey == null *//*%end*/ |
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
package sample; | |
import java.util.List; | |
import java.util.function.BiFunction; | |
import java.util.function.Function; | |
import org.seasar.doma.internal.expr.ExpressionEvaluator; | |
import org.seasar.doma.internal.jdbc.sql.PreparedSql; | |
import org.seasar.doma.internal.jdbc.sql.node.ExpandNode; | |
import org.seasar.doma.jdbc.query.SqlFileSelectQuery; | |
public class UnloadQuery extends SqlFileSelectQuery { | |
@Override | |
protected void buildSql( | |
BiFunction<ExpressionEvaluator, Function<ExpandNode, List<String>>, PreparedSql> sqlBuilder) { | |
super.buildSql(sqlBuilder); | |
sql = new PreparedSql(sql.getKind(), | |
buildUnloadStatement(sql.getRawSql()), | |
buildUnloadStatement(sql.getFormattedSql()), | |
sql.getSqlFilePath(), sql.getParameters(), sql.getSqlLogType()); | |
} | |
protected String buildUnloadStatement(String sql) { | |
String s3bucket = (String) parameters.get("s3bucket").getValue(); | |
String accessKey = (String) parameters.get("accessKey").getValue(); | |
String sercretKey = (String) parameters.get("sercretKey").getValue(); | |
return String | |
.format("unload ('%s') to '%s' credentials 'aws_access_key_id=%s;aws_secret_access_key=%s' parallel off", | |
sql, s3bucket, accessKey, sercretKey); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment