Craig Raw
3 years ago
10 changed files with 118 additions and 27 deletions
@ -1 +1 @@ |
|||
Subproject commit ee732fb2235fbe242d75366fc37d3f53e2082519 |
|||
Subproject commit de87ab1102db12cad8bbfe814a1346078cf957a5 |
@ -0,0 +1,38 @@ |
|||
package com.sparrowwallet.sparrow.io.db; |
|||
|
|||
import com.sparrowwallet.drongo.wallet.Wallet; |
|||
import org.jdbi.v3.sqlobject.config.RegisterRowMapper; |
|||
import org.jdbi.v3.sqlobject.statement.SqlBatch; |
|||
import org.jdbi.v3.sqlobject.statement.SqlQuery; |
|||
import org.jdbi.v3.sqlobject.statement.SqlUpdate; |
|||
|
|||
import java.util.*; |
|||
|
|||
public interface DetachedLabelDao { |
|||
@SqlQuery("select entry, label from detachedLabel") |
|||
@RegisterRowMapper(DetachedLabelMapper.class) |
|||
Map<String, String> getAll(); |
|||
|
|||
@SqlBatch("insert into detachedLabel (entry, label) values (?, ?)") |
|||
void insertDetachedLabels(List<String> entries, List<String> labels); |
|||
|
|||
@SqlUpdate("delete from detachedLabel") |
|||
void clear(); |
|||
|
|||
default void clearAndAddAll(Wallet wallet) { |
|||
clear(); |
|||
|
|||
List<String> entries = new ArrayList<>(); |
|||
List<String> labels = new ArrayList<>(); |
|||
for(Map.Entry<String, String> labelEntry : new HashSet<>(wallet.getDetachedLabels().entrySet())) { |
|||
entries.add(truncate(labelEntry.getKey(), 80)); |
|||
labels.add(truncate(labelEntry.getValue(), 255)); |
|||
} |
|||
|
|||
insertDetachedLabels(entries, labels); |
|||
} |
|||
|
|||
default String truncate(String label, int length) { |
|||
return (label != null && label.length() > length ? label.substring(0, length) : label); |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
package com.sparrowwallet.sparrow.io.db; |
|||
|
|||
import org.jdbi.v3.core.mapper.RowMapper; |
|||
import org.jdbi.v3.core.statement.StatementContext; |
|||
|
|||
import java.sql.ResultSet; |
|||
import java.sql.SQLException; |
|||
import java.util.Map; |
|||
|
|||
public class DetachedLabelMapper implements RowMapper<Map.Entry<String, String>> { |
|||
@Override |
|||
public Map.Entry<String, String> map(ResultSet rs, StatementContext ctx) throws SQLException { |
|||
String entry = rs.getString("entry"); |
|||
String label = rs.getString("label"); |
|||
|
|||
return new Map.Entry<>() { |
|||
@Override |
|||
public String getKey() { |
|||
return entry; |
|||
} |
|||
|
|||
@Override |
|||
public String getValue() { |
|||
return label; |
|||
} |
|||
|
|||
@Override |
|||
public String setValue(String value) { |
|||
return null; |
|||
} |
|||
}; |
|||
} |
|||
} |
@ -0,0 +1 @@ |
|||
create table detachedLabel (entry varchar(80) primary key not null, label varchar(255) not null); |
Loading…
Reference in new issue