1
0
mirror of https://github.com/Eyre-S/sekai-scores.git synced 2024-11-22 03:04:55 +08:00

add DDL and getSong for database api. fix /database no param bug

This commit is contained in:
A.C.Sukazyo Eyre 2023-01-22 16:08:27 +08:00
parent 86bc530f14
commit a0e62de2c4
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
7 changed files with 125 additions and 6 deletions

View File

@ -1,7 +1,9 @@
## Project Configurations ## Project Configurations
##
## project sekia-scores
# Proj Metadata # Proj Metadata
projVersion = 0.8.1 projVersion = 0.9
# Dependencies # Dependencies

View File

@ -1,7 +1,10 @@
## Project Configurations ## Project Configurations
#
# for subproject sekai-cli
# belongs to sekai-scores
# Proj Metadata # Proj Metadata
moduleVersion = 0.5.4 moduleVersion = 0.5.5
# dependencies # dependencies
lib_postgres_driver = 42.5.1 lib_postgres_driver = 42.5.1

View File

@ -29,6 +29,12 @@ public class Database {
final List<String> $args = new ArrayList<>(List.of(args)); final List<String> $args = new ArrayList<>(List.of(args));
if ($args.size() < 1) {
System.out.println("""
usage of database:
import - import song/difficulty data to database
""");
}
final String subcommand = $args.remove(0); final String subcommand = $args.remove(0);
if (subcommand.equals("import")) { if (subcommand.equals("import")) {

View File

@ -0,0 +1,14 @@
package cc.sukazyo.sekai_db;
public class Util {
public static int[] arraysShortToInt (final Short[] source) {
if (source == null) return null;
final int[] result = new int[source.length];
for (int i = 0; i < source.length; i++) {
result[i] = source[i];
}
return result;
}
}

View File

@ -13,6 +13,25 @@ import java.sql.Types;
public class SekaiSongDifficulties { public class SekaiSongDifficulties {
public static final String TABLE_DDL = """
create table sekai_song_difficulties
(
song_id integer not null,
difficulty sekai.sekai_difficulties not null,
level smallint not null,
notes smallint not null,
"lvl+" smallint,
"flvl+" smallint,
"plvl+" smallint
);
create index sekai_songs_difficulties_song_id_difficulty_name_index
on sekai_song_difficulties (song_id, difficulty);
create index sekai_songs_difficulties_notes_index
on sekai_song_difficulties (notes);
""";
public record DatabaseStruct ( public record DatabaseStruct (
int songId, int songId,
@Nonnull SekaiDifficulties difficulty, @Nonnull SekaiDifficulties difficulty,
@ -28,6 +47,10 @@ public class SekaiSongDifficulties {
private SekaiSongDifficulties (PostgresSession session) { this.session = session; } private SekaiSongDifficulties (PostgresSession session) { this.session = session; }
public static SekaiSongDifficulties as (PostgresSession session) { return new SekaiSongDifficulties(session); } public static SekaiSongDifficulties as (PostgresSession session) { return new SekaiSongDifficulties(session); }
public int create () throws SQLException {
return session.session.prepareStatement(TABLE_DDL).executeUpdate();
}
public boolean contains (int songId) throws SQLException { public boolean contains (int songId) throws SQLException {
final PreparedStatement check = session.session.prepareStatement(""" final PreparedStatement check = session.session.prepareStatement("""
select song_id, difficulty from sekai_song_difficulties select song_id, difficulty from sekai_song_difficulties

View File

@ -1,17 +1,54 @@
package cc.sukazyo.sekai_db.table; package cc.sukazyo.sekai_db.table;
import cc.sukazyo.sekai_db.PostgresSession; import cc.sukazyo.sekai_db.PostgresSession;
import cc.sukazyo.sekai_scores.DifficultiesEmpty;
import cc.sukazyo.sekai_scores.Difficulty; import cc.sukazyo.sekai_scores.Difficulty;
import cc.sukazyo.sekai_scores.Song; import cc.sukazyo.sekai_scores.Song;
import cc.sukazyo.sekai_scores.SongUnit;
import java.sql.PreparedStatement; import java.sql.*;
import java.sql.SQLException; import java.time.Duration;
import java.sql.Timestamp; import java.time.ZoneOffset;
import java.sql.Types; import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.Arrays;
import static cc.sukazyo.sekai_db.Util.arraysShortToInt;
public class SekaiSongs { public class SekaiSongs {
public static final String TABLE_DDL = """
create table sekai_songs
(
id integer not null
constraint sekai_songs_pk
primary key,
name text not null
constraint sekai_songs_pk_name
unique,
producer text,
arranger text,
composer text,
bpm smallint[],
duration interval(1),
release_date timestamp(3) with time zone,
name_alias text[],
lyricist text,
unit_seq smallint
);
comment on column sekai_songs.name_alias is
'the alias/tags for this song,
it usually is a unofficial name of this song.
used to search songs.';
create index sekai_songs_name_alias_index
on sekai_songs (name_alias);
create index sekai_songs_producer_index
on sekai_songs (producer);
""";
private final PostgresSession session; private final PostgresSession session;
private SekaiSongs(PostgresSession session) { private SekaiSongs(PostgresSession session) {
@ -21,6 +58,10 @@ public class SekaiSongs {
return new SekaiSongs(session); return new SekaiSongs(session);
} }
public int create () throws SQLException {
return session.session.prepareStatement(TABLE_DDL).executeUpdate();
}
public boolean hasSong(int songId) throws SQLException { public boolean hasSong(int songId) throws SQLException {
final PreparedStatement statement = session.session.prepareStatement(""" final PreparedStatement statement = session.session.prepareStatement("""
select id from sekai_songs select id from sekai_songs
@ -30,6 +71,33 @@ public class SekaiSongs {
return statement.executeQuery().next(); return statement.executeQuery().next();
} }
public Song getSong (int songId) throws SQLException {
final PreparedStatement getById = session.session.prepareStatement("""
select
id, unit_seq, name, producer, arranger, composer, lyricist, bpm, duration, release_date, name_alias
from sekai_songs
where id = ?
""");
getById.setInt(1, songId);
final ResultSet data = getById.executeQuery();
if (!data.next()) return null;
final Timestamp _release_date = data.getTimestamp("release_date");
return new Song(
data.getInt("id"),
data.getString("name"),
SongUnit.getBySeq(data.getInt("unit_seq")),
new DifficultiesEmpty(),
data.getString("producer"),
data.getString("arranger"),
data.getString("composer"),
data.getString("lyricist"),
arraysShortToInt((Short[])data.getArray("bpm").getArray()),
Duration.parse("PT0s"), // todo
_release_date == null ? null : ZonedDateTime.ofInstant(_release_date.toInstant(), ZoneOffset.UTC),
(String[])data.getArray("name_alias").getArray()
);
}
public int insert (Song song) throws SQLException { public int insert (Song song) throws SQLException {
int updated = 0; int updated = 0;

View File

@ -1,4 +1,7 @@
## Project Configurations ## Project Configurations
#
# for subproject sekai-meta
# belongs to sekai-scores
# Proj Metadata # Proj Metadata
moduleVersion = 0.3 moduleVersion = 0.3