-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Staketab/dev
create config for connect to minascan db
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/com/staketab/minanames/config/MinascanDbConfig.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 @@ | ||
package com.staketab.minanames.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@ConditionalOnProperty(value = "spring.minascan-datasource.enabled", havingValue = "true") | ||
@PropertySource({"classpath:application.yaml"}) | ||
@EnableJpaRepositories( | ||
basePackages = "com.staketab.minanames.minascan", | ||
entityManagerFactoryRef = "minascanEntityManager", | ||
transactionManagerRef = "minascanTransactionManager" | ||
) | ||
public class MinascanDbConfig { | ||
|
||
private static final String MINASCAN_ENTITIES_PACKAGE = "com.staketab.minanames.minascan"; | ||
private static final String MINASCAN_PERSISTENCE_UNIT_NAME = "minascan"; | ||
|
||
@Value(value = "${hibernate.minascan.hbm2ddl.auto}") | ||
private String dllAuto; | ||
@Value(value = "${hibernate.minascan.show_sql}") | ||
private String showSql; | ||
@Value(value = "${hibernate.minascan.dialect}") | ||
private String dialect; | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.minascan-datasource") | ||
public DataSource minascanDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
public Map<String, Object> minascanJpaProperties() { | ||
Map<String, Object> properties = new HashMap<>(); | ||
properties.put("hibernate.hbm2ddl.auto", dllAuto); | ||
properties.put("hibernate.show_sql", showSql); | ||
properties.put("hibernate.dialect", dialect); | ||
return properties; | ||
} | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean minascanEntityManager() { | ||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); | ||
em.setPersistenceUnitName(MINASCAN_PERSISTENCE_UNIT_NAME); | ||
em.setDataSource(minascanDataSource()); | ||
em.setPackagesToScan(MINASCAN_ENTITIES_PACKAGE); | ||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); | ||
em.setJpaPropertyMap(minascanJpaProperties()); | ||
return em; | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager minascanTransactionManager() { | ||
JpaTransactionManager transactionManager = new JpaTransactionManager(); | ||
transactionManager.setEntityManagerFactory(minascanEntityManager().getObject()); | ||
return transactionManager; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/staketab/minanames/minascan/TransactionRepository.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,21 @@ | ||
package com.staketab.minanames.minascan; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@ConditionalOnProperty(value = "spring.minascan-datasource.enabled", havingValue = "true") | ||
public interface TransactionRepository extends JpaRepository<UserCommandsEntity, Integer> { | ||
|
||
@Query(nativeQuery = true, | ||
value = """ | ||
SELECT buc.status as status | ||
FROM user_commands uc | ||
JOIN blocks_user_commands buc ON uc.id = buc.user_command_id | ||
JOIN a_canonical_blocks cb ON cb.id = buc.block_id | ||
WHERE uc.hash = :txHash | ||
""") | ||
String getTxStatusByTxHash(String txHash); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/staketab/minanames/minascan/UserCommandsEntity.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,21 @@ | ||
package com.staketab.minanames.minascan; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Entity | ||
@Table(name = "user_commands") | ||
public class UserCommandsEntity { | ||
|
||
@Id | ||
private int id; | ||
private String type; | ||
private long nonce; | ||
private Long validUntil; | ||
private String memo; | ||
private String hash; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0"?> | ||
<configuration> | ||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> | ||
|
||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<file>logs/application.log</file> | ||
|
||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
<fileNamePattern>logs/application.%d{yyyy-MM-dd}.log</fileNamePattern> | ||
<maxHistory>30</maxHistory> | ||
</rollingPolicy> | ||
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> | ||
<maxFileSize>150MB</maxFileSize> | ||
</triggeringPolicy> | ||
|
||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
<append>false</append> | ||
</appender> | ||
|
||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>${CONSOLE_LOG_PATTERN}</pattern> | ||
<charset>utf8</charset> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="FILE"/> | ||
<appender-ref ref="CONSOLE"/> | ||
</root> | ||
|
||
</configuration> |