Skip to content

Commit

Permalink
refs Mach5#63 changing table "user" to "users" (Keyword unusable in PG)
Browse files Browse the repository at this point in the history
Removing id=null on insert statement
Adding primary key when needed on some create table statement (foreign
key not usable)
  • Loading branch information
Adi3000 committed Apr 7, 2013
1 parent 5497d7d commit 3c3cccb
Show file tree
Hide file tree
Showing 50 changed files with 427 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author Sindre Mehus
*/
public class PlayerSettingsCommand {
private String playerId;
private Integer playerId;
private String name;
private String description;
private String type;
Expand All @@ -55,11 +55,11 @@ public class PlayerSettingsCommand {
private boolean isAdmin;
private boolean isReloadNeeded;

public String getPlayerId() {
public Integer getPlayerId() {
return playerId;
}

public void setPlayerId(String playerId) {
public void setPlayerId(Integer playerId) {
this.playerId = playerId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1578,13 +1578,13 @@ private HttpServletRequest wrapRequest(HttpServletRequest request) {
}

private HttpServletRequest wrapRequest(final HttpServletRequest request, boolean jukebox) {
final String playerId = createPlayerIfNecessary(request, jukebox);
final Integer playerId = createPlayerIfNecessary(request, jukebox);
return new HttpServletRequestWrapper(request) {
@Override
public String getParameter(String name) {
// Returns the correct player to be used in PlayerService.getPlayer()
if ("player".equals(name)) {
return playerId;
return playerId == null ? null : playerId.toString();
}

// Support old style ID parameters.
Expand Down Expand Up @@ -1655,7 +1655,7 @@ private XMLBuilder createXMLBuilder(HttpServletRequest request, HttpServletRespo
return builder;
}

private String createPlayerIfNecessary(HttpServletRequest request, boolean jukebox) {
private Integer createPlayerIfNecessary(HttpServletRequest request, boolean jukebox) {
String username = request.getRemoteUser();
String clientId = request.getParameter("c");
if (jukebox) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/sourceforge/subsonic/dao/AbstractDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,12 @@ protected <T> T queryOne(String sql, RowMapper rowMapper, Object... args) {
public void setDaoHelper(DaoHelper daoHelper) {
this.daoHelper = daoHelper;
}

public String getColumnForInsertByDO(AbstractDataObject dataObject, String listWithId, String listWithoutId){
if(dataObject.getId() == null){
return listWithoutId;
}else{
return listWithId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.sourceforge.subsonic.dao;

public abstract class AbstractDataObject {

public abstract Integer getId();

}
8 changes: 4 additions & 4 deletions src/main/java/net/sourceforge/subsonic/dao/AlbumDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
public class AlbumDao extends AbstractDao {

private static final Logger LOG = Logger.getLogger(AlbumDao.class);
private static final String COLUMNS = "id, name, artist, song_count, duration_seconds, cover_art_path, " +
"play_count, last_played, comment, created, last_scanned, present";
private static final String COLUMNS_FOR_INSERT = "name, artist, song_count, duration_seconds, cover_art_path, " +
"play_count, last_played, comment, created, last_scanned, present";
private static final String COLUMNS = "id, "+ COLUMNS_FOR_INSERT;

private final RowMapper rowMapper = new AlbumMapper();

Expand Down Expand Up @@ -82,8 +83,7 @@ public synchronized void createOrUpdateAlbum(Album album) {
album.getComment(), album.getCreated(), album.getLastScanned(), album.isPresent(), album.getArtist(), album.getName());

if (n == 0) {

update("insert into album (" + COLUMNS + ") values (" + questionMarks(COLUMNS) + ")", null, album.getName(), album.getArtist(),
update("insert into album (" + COLUMNS_FOR_INSERT + ") values (" + questionMarks(COLUMNS_FOR_INSERT) + ")", album.getName(), album.getArtist(),
album.getSongCount(), album.getDurationSeconds(), album.getCoverArtPath(), album.getPlayCount(), album.getLastPlayed(),
album.getComment(), album.getCreated(), album.getLastScanned(), album.isPresent());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/sourceforge/subsonic/dao/ArtistDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
public class ArtistDao extends AbstractDao {

private static final Logger LOG = Logger.getLogger(ArtistDao.class);
private static final String COLUMNS = "id, name, cover_art_path, album_count, last_scanned, present";
private static final String COLUMNS_FOR_INSERT = "name, cover_art_path, album_count, last_scanned, present";
private static final String COLUMNS = "id, "+COLUMNS_FOR_INSERT;

private final RowMapper rowMapper = new ArtistMapper();

Expand Down Expand Up @@ -76,8 +77,7 @@ public synchronized void createOrUpdateArtist(Artist artist) {
int n = update(sql, artist.getCoverArtPath(), artist.getAlbumCount(), artist.getLastScanned(), artist.isPresent(), artist.getName());

if (n == 0) {

update("insert into artist (" + COLUMNS + ") values (" + questionMarks(COLUMNS) + ")", null,
update("insert into artist (" + COLUMNS_FOR_INSERT + ") values (" + questionMarks(COLUMNS_FOR_INSERT) + ")",
artist.getName(), artist.getCoverArtPath(), artist.getAlbumCount(), artist.getLastScanned(), artist.isPresent());
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sourceforge/subsonic/dao/AvatarDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*/
public class AvatarDao extends AbstractDao {

private static final String COLUMNS = "id, name, created_date, mime_type, width, height, data";
private static final String COLUMNS_FOR_INSERT = "name, created_date, mime_type, width, height, data";
private static final String COLUMNS = "id, "+COLUMNS_FOR_INSERT;
private final AvatarRowMapper rowMapper = new AvatarRowMapper();

/**
Expand Down Expand Up @@ -78,8 +79,8 @@ public void setCustomAvatar(Avatar avatar, String username) {
update(sql, username);

if (avatar != null) {
update("insert into custom_avatar(" + COLUMNS + ", username) values(" + questionMarks(COLUMNS) + ", ?)",
null, avatar.getName(), avatar.getCreatedDate(), avatar.getMimeType(),
update("insert into custom_avatar(" + COLUMNS_FOR_INSERT + ", username) values(" + questionMarks(COLUMNS_FOR_INSERT) + ", ?)",
avatar.getName(), avatar.getCreatedDate(), avatar.getMimeType(),
avatar.getWidth(), avatar.getHeight(), avatar.getData(), username);
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/net/sourceforge/subsonic/dao/DaoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@
import net.sourceforge.subsonic.dao.schema.Schema46;
import net.sourceforge.subsonic.dao.schema.Schema47;
import net.sourceforge.subsonic.service.SettingsService;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jndi.JndiObjectFactoryBean;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.File;

Expand Down Expand Up @@ -94,12 +101,17 @@ public JdbcTemplate getJdbcTemplate() {

private DataSource createDataSource() {
File subsonicHome = SettingsService.getSubsonicHome();
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:file:" + subsonicHome.getPath() + "/db/subsonic");
ds.setUsername("sa");
ds.setPassword("");

Context ctx = null;
DataSource ds = null;
try {
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("jdbc/supersonic");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataSourceUtils.getConnection((DataSource)ds);

return ds;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
public class InternetRadioDao extends AbstractDao {

private static final Logger LOG = Logger.getLogger(InternetRadioDao.class);
private static final String COLUMNS = "id, name, stream_url, homepage_url, enabled, changed";
private static final String COLUMNS_FOR_INSERT = "name, stream_url, homepage_url, enabled, changed";
private static final String COLUMNS = "id, "+COLUMNS_FOR_INSERT;
private final InternetRadioRowMapper rowMapper = new InternetRadioRowMapper();

/**
Expand All @@ -54,7 +55,7 @@ public List<InternetRadio> getAllInternetRadios() {
* @param radio The internet radio station to create.
*/
public void createInternetRadio(InternetRadio radio) {
String sql = "insert into internet_radio (" + COLUMNS + ") values (null, ?, ?, ?, ?, ?)";
String sql = "insert into internet_radio (" + COLUMNS_FOR_INSERT + ") values (" + questionMarks(COLUMNS_FOR_INSERT) + ")";
update(sql, radio.getName(), radio.getStreamUrl(), radio.getHomepageUrl(), radio.isEnabled(), radio.getChanged());
LOG.info("Created internet radio station " + radio.getName());
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/sourceforge/subsonic/dao/MediaFileDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
public class MediaFileDao extends AbstractDao {

private static final Logger LOG = Logger.getLogger(MediaFileDao.class);
private static final String COLUMNS = "id, path, folder, type, format, title, album, artist, disc_number, " +
private static final String COLUMNS_FOR_INSERT = "path, folder, type, format, title, album, artist, disc_number, " +
"track_number, year, genre, bit_rate, variable_bit_rate, duration_seconds, file_size, width, height, cover_art_path, " +
"parent_path, play_count, last_played, comment, created, last_modified, last_scanned, children_last_updated, present, version";

private static final String COLUMNS = "id, "+COLUMNS_FOR_INSERT;
private static final int VERSION = 1;

private final RowMapper rowMapper = new MediaFileMapper();
Expand Down Expand Up @@ -140,8 +140,7 @@ public synchronized void createOrUpdateMediaFile(MediaFile file) {
file.setLastPlayed(musicFileInfo.getLastPlayed());
file.setPlayCount(musicFileInfo.getPlayCount());
}

update("insert into media_file (" + COLUMNS + ") values (" + questionMarks(COLUMNS) + ")", null,
update("insert into media_file (" + COLUMNS_FOR_INSERT + ") values (" + questionMarks(COLUMNS_FOR_INSERT) + ")",
file.getPath(), file.getFolder(), file.getMediaType().name(), file.getFormat(), file.getTitle(), file.getAlbumName(), file.getArtist(),
file.getDiscNumber(), file.getTrackNumber(), file.getYear(), file.getGenre(), file.getBitRate(),
file.isVariableBitRate(), file.getDurationSeconds(), file.getFileSize(), file.getWidth(), file.getHeight(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
public class MusicFolderDao extends AbstractDao {

private static final Logger LOG = Logger.getLogger(MusicFolderDao.class);
private static final String COLUMNS = "id, path, name, enabled, changed";
private static final String COLUMNS_FOR_INSERT = "path, name, enabled, changed";
private static final String COLUMNS = "id, "+COLUMNS_FOR_INSERT;
private final MusicFolderRowMapper rowMapper = new MusicFolderRowMapper();

/**
Expand All @@ -55,7 +56,7 @@ public List<MusicFolder> getAllMusicFolders() {
* @param musicFolder The music folder to create.
*/
public void createMusicFolder(MusicFolder musicFolder) {
String sql = "insert into music_folder (" + COLUMNS + ") values (null, ?, ?, ?, ?)";
String sql = "insert into music_folder (" + COLUMNS_FOR_INSERT + ") values (" + questionMarks(COLUMNS_FOR_INSERT) + ")";
update(sql, musicFolder.getPath(), musicFolder.getName(), musicFolder.isEnabled(), musicFolder.getChanged());
LOG.info("Created music folder " + musicFolder.getPath());
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/net/sourceforge/subsonic/dao/PlayerDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
public class PlayerDao extends AbstractDao {

private static final Logger LOG = Logger.getLogger(PlayerDao.class);
private static final String COLUMNS = "id, name, type, username, ip_address, auto_control_enabled, " +
private static final String COLUMNS_FOR_INSERT = "name, type, username, ip_address, auto_control_enabled, " +
"last_seen, cover_art_scheme, transcode_scheme, dynamic_ip, technology, client_id";

private static final String COLUMNS = "id, "+COLUMNS_FOR_INSERT;
private PlayerRowMapper rowMapper = new PlayerRowMapper();
private Map<String, Playlist> playlists = Collections.synchronizedMap(new HashMap<String, Playlist>());
private Map<Integer, Playlist> playlists = Collections.synchronizedMap(new HashMap<Integer, Playlist>());

/**
* Returns all players.
Expand Down Expand Up @@ -83,7 +83,7 @@ public List<Player> getPlayersForUserAndClientId(String username, String clientI
* @param id The unique player ID.
* @return The player with the given ID, or <code>null</code> if no such player exists.
*/
public Player getPlayerById(String id) {
public Player getPlayerById(Integer id) {
String sql = "select " + COLUMNS + " from player where id=?";
return queryOne(sql, rowMapper, id);
}
Expand All @@ -95,7 +95,7 @@ public Player getPlayerById(String id) {
*/
public synchronized void createPlayer(Player player) {
int id = getJdbcTemplate().queryForInt("select max(id) from player") + 1;
player.setId(String.valueOf(id));
player.setId(id);
String sql = "insert into player (" + COLUMNS + ") values (" + questionMarks(COLUMNS) + ")";
update(sql, player.getId(), player.getName(), player.getType(), player.getUsername(),
player.getIpAddress(), player.isAutoControlEnabled(),
Expand All @@ -113,9 +113,17 @@ public synchronized void createPlayer(Player player) {
* @param id The player ID.
*/
public void deletePlayer(String id) {
String sql = "delete from player where id=?";
update(sql, id);
playlists.remove(id);
deletePlayer(Integer.valueOf(id));
}
/**
* Deletes the player with the given ID.
*
* @param id The player ID.
*/
public void deletePlayer(Integer id) {
String sql = "delete from player where id=?";
update(sql, id);
playlists.remove(id);
}


Expand Down Expand Up @@ -158,7 +166,7 @@ public void updatePlayer(Player player) {
player.getIpAddress(), player.isAutoControlEnabled(),
player.getLastSeen(), player.getCoverArtScheme().name(),
player.getTranscodeScheme().name(), player.isDynamicIp(),
player.getTechnology(), player.getClientId(), player.getId());
player.getTechnology().name(), player.getClientId(), player.getId());
}

private void addPlaylist(Player player) {
Expand All @@ -174,7 +182,7 @@ private class PlayerRowMapper implements ParameterizedRowMapper<Player> {
public Player mapRow(ResultSet rs, int rowNum) throws SQLException {
Player player = new Player();
int col = 1;
player.setId(rs.getString(col++));
player.setId(rs.getInt(col++));
player.setName(rs.getString(col++));
player.setType(rs.getString(col++));
player.setUsername(rs.getString(col++));
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/sourceforge/subsonic/dao/PodcastDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
*/
public class PodcastDao extends AbstractDao {

private static final String CHANNEL_COLUMNS = "id, url, title, description, status, error_message";
private static final String EPISODE_COLUMNS = "id, channel_id, url, path, title, description, publish_date, " +
private static final String CHANNEL_COLUMNS_FOR_INSERT = "url, title, description, status, error_message";
private static final String CHANNEL_COLUMNS = "id, "+CHANNEL_COLUMNS_FOR_INSERT;
private static final String EPISODE_COLUMNS_FOR_INSERT = "channel_id, url, path, title, description, publish_date, " +
"duration, bytes_total, bytes_downloaded, status, error_message";
private static final String EPISODE_COLUMNS = "id, "+EPISODE_COLUMNS_FOR_INSERT;

private PodcastChannelRowMapper channelRowMapper = new PodcastChannelRowMapper();
private PodcastEpisodeRowMapper episodeRowMapper = new PodcastEpisodeRowMapper();
Expand All @@ -50,8 +52,8 @@ public class PodcastDao extends AbstractDao {
* @return The ID of the newly created channel.
*/
public synchronized int createChannel(PodcastChannel channel) {
String sql = "insert into podcast_channel (" + CHANNEL_COLUMNS + ") values (" + questionMarks(CHANNEL_COLUMNS) + ")";
update(sql, null, channel.getUrl(), channel.getTitle(), channel.getDescription(),
String sql = "insert into podcast_channel (" + CHANNEL_COLUMNS_FOR_INSERT + ") values (" + questionMarks(CHANNEL_COLUMNS_FOR_INSERT) + ")";
update(sql, channel.getUrl(), channel.getTitle(), channel.getDescription(),
channel.getStatus().name(), channel.getErrorMessage());

return getJdbcTemplate().queryForInt("select max(id) from podcast_channel");
Expand Down Expand Up @@ -94,8 +96,8 @@ public void deleteChannel(int id) {
* @param episode The Podcast episode to create.
*/
public void createEpisode(PodcastEpisode episode) {
String sql = "insert into podcast_episode (" + EPISODE_COLUMNS + ") values (" + questionMarks(EPISODE_COLUMNS) + ")";
update(sql, null, episode.getChannelId(), episode.getUrl(), episode.getPath(),
String sql = "insert into podcast_episode (" + EPISODE_COLUMNS_FOR_INSERT + ") values (" + questionMarks(EPISODE_COLUMNS_FOR_INSERT) + ")";
update(sql, episode.getChannelId(), episode.getUrl(), episode.getPath(),
episode.getTitle(), episode.getDescription(), episode.getPublishDate(),
episode.getDuration(), episode.getBytesTotal(), episode.getBytesDownloaded(),
episode.getStatus().name(), episode.getErrorMessage());
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/sourceforge/subsonic/dao/RatingDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public List<String> getHighestRated(int offset, int count) {
if (count < 1) {
return new ArrayList<String>();
}
String sql = "select path from user_rating " +
"where exists (select 1 from media_file where user_rating.path = media_file.path and present) " +
String sql = "select path from users_rating " +
"where exists (select 1 from media_file where users_rating.path = media_file.path and present) " +
"group by path " +
"order by avg(rating) desc " +
" limit " + count + " offset " + offset;
Expand All @@ -62,9 +62,9 @@ public void setRatingForUser(String username, MediaFile mediaFile, Integer ratin
return;
}

update("delete from user_rating where username=? and path=?", username, mediaFile.getPath());
update("delete from users_rating where username=? and path=?", username, mediaFile.getPath());
if (rating != null) {
update("insert into user_rating values(?, ?, ?)", username, mediaFile.getPath(), rating);
update("insert into users_rating values(?, ?, ?)", username, mediaFile.getPath(), rating);
}
}

Expand All @@ -76,7 +76,7 @@ public void setRatingForUser(String username, MediaFile mediaFile, Integer ratin
*/
public Double getAverageRating(MediaFile mediaFile) {
try {
return (Double) getJdbcTemplate().queryForObject("select avg(rating) from user_rating where path=?", new Object[]{mediaFile.getPath()}, Double.class);
return (Double) getJdbcTemplate().queryForObject("select avg(rating) from users_rating where path=?", new Object[]{mediaFile.getPath()}, Double.class);
} catch (EmptyResultDataAccessException x) {
return null;
}
Expand All @@ -91,7 +91,7 @@ public Double getAverageRating(MediaFile mediaFile) {
*/
public Integer getRatingForUser(String username, MediaFile mediaFile) {
try {
return getJdbcTemplate().queryForInt("select rating from user_rating where username=? and path=?", new Object[]{username, mediaFile.getPath()});
return getJdbcTemplate().queryForInt("select rating from users_rating where username=? and path=?", new Object[]{username, mediaFile.getPath()});
} catch (EmptyResultDataAccessException x) {
return null;
}
Expand Down
Loading

0 comments on commit 3c3cccb

Please sign in to comment.