From 6d07dcf15d2991480525d4972c5f2a4bc911eb32 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 21 Jun 2022 00:03:18 +0200 Subject: [PATCH] simplify interface BDD... --- .../src/org/kar/karusic/api/DataResource.java | 6 +- .../org/kar/karusic/api/NodeInterface.java | 176 ------------------ back/src/org/kar/karusic/model/Album.java | 23 ++- back/src/org/kar/karusic/model/Artist.java | 24 ++- back/src/org/kar/karusic/model/Gender.java | 11 +- .../org/kar/karusic/model/GenericTable.java | 115 +++++++++++- back/src/org/kar/karusic/model/NodeSmall.java | 150 ++++++++++++++- back/src/org/kar/karusic/model/Playlist.java | 36 ++-- back/src/org/kar/karusic/model/Track.java | 39 ++-- 9 files changed, 331 insertions(+), 249 deletions(-) diff --git a/back/src/org/kar/karusic/api/DataResource.java b/back/src/org/kar/karusic/api/DataResource.java index 801e522..c3d6ac8 100644 --- a/back/src/org/kar/karusic/api/DataResource.java +++ b/back/src/org/kar/karusic/api/DataResource.java @@ -210,11 +210,11 @@ public class DataResource { entry.disconnect(); } - static String saveTemporaryFile(InputStream uploadedInputStream, long idData) { + public static String saveTemporaryFile(InputStream uploadedInputStream, long idData) { return saveFile(uploadedInputStream, DataResource.getTmpFileInData(idData)); } - static void removeTemporaryFile(long idData) { + public static void removeTemporaryFile(long idData) { String filepath = DataResource.getTmpFileInData(idData); if (Files.exists(Paths.get(filepath))) { try { @@ -227,7 +227,7 @@ public class DataResource { } // save uploaded file to a defined location on the server - static String saveFile(InputStream uploadedInputStream, String serverLocation) { + public static String saveFile(InputStream uploadedInputStream, String serverLocation) { String out = ""; try { OutputStream outpuStream = new FileOutputStream(new File( diff --git a/back/src/org/kar/karusic/api/NodeInterface.java b/back/src/org/kar/karusic/api/NodeInterface.java index b524a60..8514946 100644 --- a/back/src/org/kar/karusic/api/NodeInterface.java +++ b/back/src/org/kar/karusic/api/NodeInterface.java @@ -76,39 +76,6 @@ public class NodeInterface { ORDER BY node.name */ - public static List get(String typeInNode) { - System.out.println(typeInNode + " get"); - DBEntry entry = new DBEntry(WebLauncher.dbConfig); - List out = new ArrayList<>(); - String query = "SELECT node.id," + - " node.name," + - " node.description," + - " node.parent_id," + - " (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" + - " FROM cover_link_node tmp" + - " WHERE tmp.deleted = false" + - " AND node.id = tmp.node_id" + - " GROUP BY tmp.node_id) AS covers" + - " FROM node" + - " WHERE node.deleted = false " + - " AND node.type = ?" + - " ORDER BY node.name"; - try { - PreparedStatement ps = entry.connection.prepareStatement(query); - int iii = 1; - ps.setString(iii++, typeInNode); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - out.add(new NodeSmall(rs)); - } - } catch (SQLException throwables) { - throwables.printStackTrace(); - } - entry.disconnect(); - entry = null; - System.out.println("retrieve " + out.size() + " " + typeInNode); - return out; - } public static NodeSmall getWithId(String typeInNode, long id) { DBEntry entry = new DBEntry(WebLauncher.dbConfig); @@ -276,131 +243,6 @@ public class NodeInterface { return createNode(typeInNode, name, null, parentId); } - static private String multipartCorrection(String data) { - if (data == null) { - return null; - } - if (data.isEmpty()) { - return null; - } - if (data.contentEquals("null")) { - return null; - } - return data; - } - - static public Response uploadCover(String typeInNode, - Long nodeId, - String file_name, - InputStream fileInputStream, - FormDataContentDisposition fileMetaData - ) { - try { - // correct input string stream : - file_name = multipartCorrection(file_name); - - //public NodeSmall uploadFile(final FormDataMultiPart form) { - System.out.println("Upload media file: " + fileMetaData); - System.out.println(" - id: " + nodeId); - System.out.println(" - file_name: " + file_name); - System.out.println(" - fileInputStream: " + fileInputStream); - System.out.println(" - fileMetaData: " + fileMetaData); - System.out.flush(); - NodeSmall media = getWithId(typeInNode, nodeId); - if (media == null) { - return Response.notModified("Media Id does not exist or removed...").build(); - } - - long tmpUID = DataResource.getTmpDataId(); - String sha512 = DataResource.saveTemporaryFile(fileInputStream, tmpUID); - Data data = DataResource.getWithSha512(sha512); - if (data == null) { - System.out.println("Need to add the data in the BDD ... "); - System.out.flush(); - try { - data = DataResource.createNewData(tmpUID, file_name, sha512); - } catch (IOException ex) { - DataResource.removeTemporaryFile(tmpUID); - ex.printStackTrace(); - return Response.notModified("can not create input media").build(); - } catch (SQLException ex) { - ex.printStackTrace(); - DataResource.removeTemporaryFile(tmpUID); - return Response.notModified("Error in SQL insertion ...").build(); - } - } else if (data.deleted == true) { - System.out.println("Data already exist but deleted"); - System.out.flush(); - DataResource.undelete(data.id); - data.deleted = false; - } else { - System.out.println("Data already exist ... all good"); - System.out.flush(); - } - // Fist step: retrieve all the Id of each parents:... - System.out.println("Find typeNode"); - - DBEntry entry = new DBEntry(WebLauncher.dbConfig); - long uniqueSQLID = -1; - // real add in the BDD: - try { - // prepare the request: - String query = "INSERT INTO cover_link_node (create_date, modify_date, node_id, data_id)" + - " VALUES (now(3), now(3), ?, ?)"; - PreparedStatement ps = entry.connection.prepareStatement(query, - Statement.RETURN_GENERATED_KEYS); - int iii = 1; - ps.setLong(iii++, media.id); - ps.setLong(iii++, data.id); - // execute the request - int affectedRows = ps.executeUpdate(); - if (affectedRows == 0) { - throw new SQLException("Creating data failed, no rows affected."); - } - // retreive uid inserted - try (ResultSet generatedKeys = ps.getGeneratedKeys()) { - if (generatedKeys.next()) { - uniqueSQLID = generatedKeys.getLong(1); - } else { - throw new SQLException("Creating user failed, no ID obtained (1)."); - } - } catch (Exception ex) { - System.out.println("Can not get the UID key inserted ... "); - ex.printStackTrace(); - throw new SQLException("Creating user failed, no ID obtained (2)."); - } - } catch (SQLException ex) { - ex.printStackTrace(); - entry.disconnect(); - return Response.serverError().build(); - } - // if we do not une the file .. remove it ... otherwise this is meamory leak... - DataResource.removeTemporaryFile(tmpUID); - System.out.println("uploaded .... compleate: " + uniqueSQLID); - return Response.ok(getWithId(typeInNode, nodeId)).build(); - } catch (Exception ex) { - System.out.println("Cat ann unexpected error ... "); - ex.printStackTrace(); - } - return Response.serverError().build(); - } - static public Response removeCover(String typeInNode, Long nodeId, Long coverId) { - DBEntry entry = new DBEntry(WebLauncher.dbConfig); - String query = "UPDATE `cover_link_node` SET `modify_date`=now(3), `deleted`=true WHERE `node_id` = ? AND `data_id` = ?"; - try { - PreparedStatement ps = entry.connection.prepareStatement(query); - int iii = 1; - ps.setLong(iii++, nodeId); - ps.setLong(iii++, coverId); - ps.executeUpdate(); - } catch (SQLException throwables) { - throwables.printStackTrace(); - entry.disconnect(); - return Response.serverError().build(); - } - entry.disconnect(); - return Response.ok(getWithId(typeInNode, nodeId)).build(); - } static public Response put(String typeInNode, Long id, String jsonRequest) { ObjectMapper mapper = new ObjectMapper(); @@ -461,22 +303,4 @@ public class NodeInterface { } - static public Response delete(String typeInNode, Long nodeId) { - DBEntry entry = new DBEntry(WebLauncher.dbConfig); - String query = "UPDATE `node` SET `modify_date`=now(3), `deleted`=true WHERE `id` = ? AND `type` = ?"; - try { - PreparedStatement ps = entry.connection.prepareStatement(query); - int iii = 1; - ps.setLong(iii++, nodeId); - ps.setString(iii++, typeInNode); - ps.executeUpdate(); - } catch (SQLException throwables) { - throwables.printStackTrace(); - entry.disconnect(); - return Response.serverError().build(); - } - entry.disconnect(); - return Response.ok().build(); - } - } diff --git a/back/src/org/kar/karusic/model/Album.java b/back/src/org/kar/karusic/model/Album.java index 17ebe56..93da0d9 100644 --- a/back/src/org/kar/karusic/model/Album.java +++ b/back/src/org/kar/karusic/model/Album.java @@ -25,8 +25,8 @@ public class Album extends Playlist { public Album() { } - public Album(ResultSet rs) { - super(rs); + protected Album(String tableName, ResultSet rs) { + super(tableName, rs); int iii = super.getRsCount();; try { this.publication = rs.getDate(iii++); @@ -38,6 +38,10 @@ public class Album extends Playlist { } } + public Album(ResultSet rs) { + this("album", rs); + } + @Override public int getRsCount() { return super.getRsCount() + 1; @@ -50,14 +54,17 @@ public class Album extends Playlist { } @Override - public String getTableSql(String tableName) { - return super.getTableSql(tableName).replaceAll("<<>>", """ + public String getTableSqlModel() { + return super.getTableSqlModel().replaceAll("<<>>", """ `publication` timestamp(3) NULL COMMENT 'death date of the artist', <<>> """); } - @Override - public String getTableSql() { - return getTableSql("album").replaceAll("<<<.*>>>", ""); - } + protected String getQueryString() { + return super.getQueryString().replaceAll("<<>>", ", <<>>.publication <<>>"); + } + + Object createObject(ResultSet rs) { + return new Album(rs); + } } diff --git a/back/src/org/kar/karusic/model/Artist.java b/back/src/org/kar/karusic/model/Artist.java index a6e15d9..358645f 100644 --- a/back/src/org/kar/karusic/model/Artist.java +++ b/back/src/org/kar/karusic/model/Artist.java @@ -26,10 +26,10 @@ public class Artist extends NodeSmall { Date death = null; public Artist() { - + super("artist"); } - public Artist(ResultSet rs) { - super(rs); + public Artist(String tableName, ResultSet rs) { + super("artist", rs); int iii = super.getRsCount();; try { @@ -53,6 +53,9 @@ public class Artist extends NodeSmall { ex.printStackTrace(); } } + public Artist(ResultSet rs) { + this("artist", rs); + } @Override public int getRsCount() { return super.getRsCount() + 4; @@ -63,8 +66,8 @@ public class Artist extends NodeSmall { ", firstName=" + firstName + ", surname=" + surname + ", birth=" + birth + ", death=" + death + "]"; } @Override - public String getTableSql(String tableName) { - return super.getTableSql(tableName).replaceAll("<<>>", """ + public String getTableSqlModel() { + return super.getTableSqlModel().replaceAll("<<>>", """ `firstName` varchar(256) NOT NULL, `surname` varchar(256) NOT NULL, `birth` timestamp(3) NULL COMMENT 'birth date of the artist', @@ -72,8 +75,11 @@ public class Artist extends NodeSmall { <<>> """); } - @Override - public String getTableSql() { - return getTableSql("artist").replaceAll("<<<.*>>>", ""); - } + protected String getQueryString() { + return super.getQueryString().replaceAll("<<>>", ", <<>>.firstName, <<>>.surname, <<>>.birth, <<>>.death <<>>"); + } + + Object createObject(ResultSet rs) { + return new Album(rs); + } } diff --git a/back/src/org/kar/karusic/model/Gender.java b/back/src/org/kar/karusic/model/Gender.java index afb32fd..3cdb5a2 100644 --- a/back/src/org/kar/karusic/model/Gender.java +++ b/back/src/org/kar/karusic/model/Gender.java @@ -20,10 +20,15 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; public class Gender extends NodeSmall { public Gender() { + super("gender"); + } + + protected Gender(String tableName, ResultSet rs) { + super(tableName, rs); } public Gender(ResultSet rs) { - super(rs); + this("gender", rs); // nothing to do... } @Override @@ -35,8 +40,4 @@ public class Gender extends NodeSmall { return "Gender [id=" + id + ", name=" + name + ", description=" + description + ", covers=" + covers + "]"; } - @Override - public String getTableSql() { - return getTableSql("gender").replaceAll("<<<.*>>>", ""); - } } diff --git a/back/src/org/kar/karusic/model/GenericTable.java b/back/src/org/kar/karusic/model/GenericTable.java index 70ff77e..70675e6 100644 --- a/back/src/org/kar/karusic/model/GenericTable.java +++ b/back/src/org/kar/karusic/model/GenericTable.java @@ -1,11 +1,17 @@ package org.kar.karusic.model; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -public abstract class GenericTable { +import javax.ws.rs.core.Response; + +import org.kar.karusic.WebLauncher; +import org.kar.karusic.db.DBEntry; + +public class GenericTable { protected String tableName = "plopppppp"; public Long id = null; GenericTable(String tableName) { @@ -20,6 +26,15 @@ public abstract class GenericTable { ex.printStackTrace(); } } + protected GenericTable(ResultSet rs) { + this.tableName = "genericTable"; + int iii = 1; + try { + this.id = rs.getLong(iii++); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } protected List getListOfIds(ResultSet rs, int iii) throws SQLException { String trackString = rs.getString(iii); @@ -43,6 +58,15 @@ public abstract class GenericTable { return 1 + 1; } + public String getQueryLinkString(String from, String to, String as) { + String out = " (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" + + " FROM <<>>_link_<<>> tmp" + + " WHERE tmp.deleted = false" + + " AND <<>>.id = <<>>.<<>>_id" + + " GROUP BY tmp.<<>>_id) AS <<>>"; + return out.replaceAll("<<>>", from).replaceAll("<<>>", to).replaceAll("<<>>", as); + } + public String getTableLinkSql(String from, String to) { return """ DROP TABLE IF EXISTS `<<>>_link_<<>>`; @@ -71,10 +95,97 @@ public abstract class GenericTable { ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; <<>> - """.replace("<<>>", getTableLinkSql("<<>>", "data")); + """; } public final String getTableSql() { return getTableSqlModel().replace("<<>>", tableName).replaceAll("<<<.*>>>", ""); } + + public Response dbDelete(Long nodeId) { + DBEntry entry = new DBEntry(WebLauncher.dbConfig); + String query = "UPDATE `" + tableName + "` SET `modify_date`=now(3), `deleted`=true WHERE `id` = ?"; + try { + PreparedStatement ps = entry.connection.prepareStatement(query); + int iii = 1; + ps.setLong(iii++, nodeId); + ps.executeUpdate(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + entry.disconnect(); + return Response.serverError().build(); + } + entry.disconnect(); + return Response.ok().build(); + } + + public GenericTable dbGetWithId(String typeInNode, long id) { + DBEntry entry = new DBEntry(WebLauncher.dbConfig); + String query = "SELECT " + tableName + ".id," + + " FROM " + tableName + + " WHERE " + tableName + ".deleted = false " + + " AND " + tableName + ".id = ?" + + " ORDER BY node.name"; + try { + PreparedStatement ps = entry.connection.prepareStatement(query); + int iii = 1; + ps.setString(iii++, typeInNode); + ps.setLong(iii++, id); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + GenericTable out = new GenericTable(this.tableName, rs); + entry.disconnect(); + entry = null; + return out; + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + entry.disconnect(); + entry = null; + return null; + } + + Object createObject(ResultSet rs) { + return null; + } + protected String getQueryString() { + return "SELECT <<>>.id" + + " <<>>" + + " FROM <<>>" + + " WHERE <<>>.deleted = false "; + } + public List get() { + System.out.println("'" + tableName + "' get ..."); + DBEntry entry = new DBEntry(WebLauncher.dbConfig); + List out = new ArrayList<>(); + String query = getQueryString(); + /*"SELECT <<>>.id" + + " <<>>" + + " node.name," + + " node.description," + + " node.parent_id," + + " (SELECT GROUP_CONCAT(tmp.data_id SEPARATOR '-')" + + " FROM cover_link_node tmp" + + " WHERE tmp.deleted = false" + + " AND node.id = tmp.node_id" + + " GROUP BY tmp.node_id) AS covers" + + " FROM <<>>" + + " WHERE <<>>.deleted = false "; + */ + try { + PreparedStatement ps = entry.connection.prepareStatement(query); + int iii = 1; + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + out.add(this.createObject(rs)); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + entry.disconnect(); + entry = null; + System.out.println("retrieve " + out.size() + " '" + tableName + "'"); + return out; + } } diff --git a/back/src/org/kar/karusic/model/NodeSmall.java b/back/src/org/kar/karusic/model/NodeSmall.java index 7791efd..244e98e 100644 --- a/back/src/org/kar/karusic/model/NodeSmall.java +++ b/back/src/org/kar/karusic/model/NodeSmall.java @@ -12,10 +12,21 @@ CREATE TABLE `node` ( ) AUTO_INCREMENT=10; */ +import java.io.IOException; +import java.io.InputStream; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.List; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.kar.karusic.WebLauncher; +import org.kar.karusic.api.DataResource; +import org.kar.karusic.db.DBEntry; + import com.fasterxml.jackson.databind.annotation.JsonSerialize; @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) @@ -53,11 +64,144 @@ public class NodeSmall extends GenericTable { return super.getRsCount() + 3; } @Override - public String getTableSql(String tableName) { - return super.getTableSql(tableName).replaceAll("<<>>", """ + public String getTableSqlModel() { + return super.getTableSqlModel().replaceAll("<<>>", """ `name` text CHARACTER SET utf8mb3 COLLATE utf8_general_ci NOT NULL, `description` text CHARACTER SET utf8mb3 COLLATE utf8_general_ci, <<>> - """).replaceAll("<<>>", getTableLinkSql(tableName, "data")); + """).replaceAll("<<>>", getTableLinkSql("<<>>", "cover")); } + protected String getQueryString() { + return super.getQueryString().replaceAll("<<>>", ", <<>>.name, <<>>.description, " + getQueryLinkString("<<>>", "cover", "covers") + " <<>>"); + } + + Object createObject(ResultSet rs) { + return null;//new NodeSmall(rs); + } + + static private String multipartCorrection(String data) { + if (data == null) { + return null; + } + if (data.isEmpty()) { + return null; + } + if (data.contentEquals("null")) { + return null; + } + return data; + } + + public Response dbUploadCover(String typeInNode, + Long nodeId, + String file_name, + InputStream fileInputStream, + FormDataContentDisposition fileMetaData + ) { + try { + // correct input string stream : + file_name = multipartCorrection(file_name); + + //public NodeSmall uploadFile(final FormDataMultiPart form) { + System.out.println("Upload media file: " + fileMetaData); + System.out.println(" - id: " + nodeId); + System.out.println(" - file_name: " + file_name); + System.out.println(" - fileInputStream: " + fileInputStream); + System.out.println(" - fileMetaData: " + fileMetaData); + System.out.flush(); + GenericTable media = dbGetWithId(typeInNode, nodeId); + if (media == null) { + return Response.notModified("Media Id does not exist or removed...").build(); + } + + long tmpUID = DataResource.getTmpDataId(); + String sha512 = DataResource.saveTemporaryFile(fileInputStream, tmpUID); + Data data = DataResource.getWithSha512(sha512); + if (data == null) { + System.out.println("Need to add the data in the BDD ... "); + System.out.flush(); + try { + data = DataResource.createNewData(tmpUID, file_name, sha512); + } catch (IOException ex) { + DataResource.removeTemporaryFile(tmpUID); + ex.printStackTrace(); + return Response.notModified("can not create input media").build(); + } catch (SQLException ex) { + ex.printStackTrace(); + DataResource.removeTemporaryFile(tmpUID); + return Response.notModified("Error in SQL insertion ...").build(); + } + } else if (data.deleted == true) { + System.out.println("Data already exist but deleted"); + System.out.flush(); + DataResource.undelete(data.id); + data.deleted = false; + } else { + System.out.println("Data already exist ... all good"); + System.out.flush(); + } + // Fist step: retrieve all the Id of each parents:... + System.out.println("Find typeNode"); + + DBEntry entry = new DBEntry(WebLauncher.dbConfig); + long uniqueSQLID = -1; + // real add in the BDD: + try { + // prepare the request: + String query = "INSERT INTO " + this.tableName + "_link_cover (create_date, modify_date, " + this.tableName + "_id, cover_id)" + + " VALUES (now(3), now(3), ?, ?)"; + PreparedStatement ps = entry.connection.prepareStatement(query, + Statement.RETURN_GENERATED_KEYS); + int iii = 1; + ps.setLong(iii++, media.id); + ps.setLong(iii++, data.id); + // execute the request + int affectedRows = ps.executeUpdate(); + if (affectedRows == 0) { + throw new SQLException("Creating data failed, no rows affected."); + } + // retreive uid inserted + try (ResultSet generatedKeys = ps.getGeneratedKeys()) { + if (generatedKeys.next()) { + uniqueSQLID = generatedKeys.getLong(1); + } else { + throw new SQLException("Creating user failed, no ID obtained (1)."); + } + } catch (Exception ex) { + System.out.println("Can not get the UID key inserted ... "); + ex.printStackTrace(); + throw new SQLException("Creating user failed, no ID obtained (2)."); + } + } catch (SQLException ex) { + ex.printStackTrace(); + entry.disconnect(); + return Response.serverError().build(); + } + // if we do not une the file .. remove it ... otherwise this is meamory leak... + DataResource.removeTemporaryFile(tmpUID); + System.out.println("uploaded .... compleate: " + uniqueSQLID); + return Response.ok(dbGetWithId(typeInNode, nodeId)).build(); + } catch (Exception ex) { + System.out.println("Cat ann unexpected error ... "); + ex.printStackTrace(); + } + return Response.serverError().build(); + } + public Response dbRemoveCover(String typeInNode, Long nodeId, Long coverId) { + DBEntry entry = new DBEntry(WebLauncher.dbConfig); + String query = "UPDATE `" + this.tableName + "_link_cover` SET `modify_date`=now(3), `deleted`=true WHERE `" + this.tableName + "_id` = ? AND `cover_id` = ?"; + try { + PreparedStatement ps = entry.connection.prepareStatement(query); + int iii = 1; + ps.setLong(iii++, nodeId); + ps.setLong(iii++, coverId); + ps.executeUpdate(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + entry.disconnect(); + return Response.serverError().build(); + } + entry.disconnect(); + return Response.ok(dbGetWithId(typeInNode, nodeId)).build(); + } } diff --git a/back/src/org/kar/karusic/model/Playlist.java b/back/src/org/kar/karusic/model/Playlist.java index 8aa43c7..08e162e 100644 --- a/back/src/org/kar/karusic/model/Playlist.java +++ b/back/src/org/kar/karusic/model/Playlist.java @@ -24,9 +24,10 @@ public class Playlist extends NodeSmall { List tracks = null; public Playlist() { + super("playlist"); } - public Playlist(ResultSet rs) { - super(rs); + protected Playlist(String tableName, ResultSet rs) { + super(tableName, rs); int iii = super.getRsCount();; try { @@ -43,6 +44,9 @@ public class Playlist extends NodeSmall { ex.printStackTrace(); } } + public Playlist(ResultSet rs) { + this("playlist", rs); + } @Override public int getRsCount() { @@ -55,24 +59,14 @@ public class Playlist extends NodeSmall { } @Override - public String getTableSql(String tableName) { - return super.getTableSql(tableName).replaceAll("<<>>", """ - DROP TABLE IF EXISTS `<<>>_link_track`; - CREATE TABLE `<<>>_link_track` ( - `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'table ID', - `deleted` tinyint(1) NOT NULL DEFAULT '0', - `create_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `modify_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `<<>>_id` bigint DEFAULT NULL, - `track_id` bigint NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - - <<>> - """).replaceAll("<<>>", tableName); - } - @Override - public String getTableSql() { - return getTableSql("playlist").replaceAll("<<<.*>>>", ""); + public String getTableSqlModel() { + return super.getTableSqlModel().replaceAll("<<>>", getTableLinkSql("<<>>", "track")); } + protected String getQueryString() { + return super.getQueryString().replaceAll("<<>>", ", " + getQueryLinkString("<<>>", "track", "tracks") + " <<>>"); + } + + Object createObject(ResultSet rs) { + return new Playlist(rs); + } } diff --git a/back/src/org/kar/karusic/model/Track.java b/back/src/org/kar/karusic/model/Track.java index 7832472..6580674 100644 --- a/back/src/org/kar/karusic/model/Track.java +++ b/back/src/org/kar/karusic/model/Track.java @@ -25,10 +25,11 @@ public class Track extends NodeSmall { List artistId = null; public Track() { + super("track"); } - public Track(ResultSet rs) { - super(rs); + protected Track(String tableName, ResultSet rs) { + super(tableName, rs); int iii = super.getRsCount();; try { this.genderId = rs.getLong(iii++); @@ -44,6 +45,9 @@ public class Track extends NodeSmall { ex.printStackTrace(); } } + public Track(ResultSet rs) { + this("track", rs); + } @Override public int getRsCount() { return super.getRsCount() + 4; @@ -55,28 +59,19 @@ public class Track extends NodeSmall { } @Override - public String getTableSql(String tableName) { - return super.getTableSql(tableName).replaceAll("<<>>", """ + public String getTableSqlModel() { + return super.getTableSqlModel().replaceAll("<<>>", """ `genderId` bigint NULL, `dataId` bigint NULL, <<>> - """).replaceAll("<<>>", """ - DROP TABLE IF EXISTS `<<>>_link_artist`; - CREATE TABLE `<<>>_link_artist` ( - `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'table ID', - `deleted` tinyint(1) NOT NULL DEFAULT '0', - `create_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `modify_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `<<>>_id` bigint DEFAULT NULL, - `artist_id` bigint NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - - <<>> - """).replaceAll("<<>>", tableName); - } - @Override - public String getTableSql() { - return getTableSql("track").replaceAll("<<<.*>>>", ""); + """).replaceAll("<<>>", getTableLinkSql("<<>>", "artist")); } + protected String getQueryString() { + return super.getQueryString().replaceAll("<<>>", ", <<>>.genderId, <<>>.dataId, " + getQueryLinkString("<<>>", "artist", "artists") + " <<>>"); + } + + Object createObject(ResultSet rs) { + return new Track(rs); + } + }