Mercurial Hosting > nabble
diff src/nabble/model/TagImpl.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/model/TagImpl.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,138 @@ +package nabble.model; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.ArrayList; +import java.util.List; +import fschmidt.db.postgres.DbDatabaseImpl; + + +final class TagImpl { + + private TagImpl() {} // never + + static void addTag(Site site,Node node,User user,String label) { + try { + Connection con = site.getDb().getConnection(); + PreparedStatement stmt = con.prepareStatement( + "insert into tag (node_id,user_id,label) values (?,?,?)" + ); + if( node == null ) { + stmt.setNull( 1, Types.INTEGER ); + } else { + stmt.setLong( 1, node.getId() ); + } + if( user == null ) { + stmt.setNull( 2, Types.INTEGER ); + } else { + stmt.setLong( 2, user.getId() ); + } + stmt.setString( 3, label ); + DbDatabaseImpl.executeUpdateIgnoringDuplicateKeys(stmt); + stmt.close(); + con.close(); + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + static int countTags(SiteKey siteKey,String sqlCondition) { + try { + Connection con = siteKey.getDb().getConnection(); + try { + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery( + "select count(*) as n from tag where " + sqlCondition + ); + rs.next(); + int n = rs.getInt("n"); + rs.close(); + stmt.close(); + return n; + } finally { + con.close(); + } + } catch(SQLException e) { + throw new RuntimeException("sqlCondition = "+sqlCondition,e); + } + } + + static List<String> findTagLabels(Site site,String sqlCondition) { + try { + List<String> list = new ArrayList<String>(); + Connection con = site.getDb().getConnection(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery( + "select distinct label from tag where " + sqlCondition + ); + while( rs.next() ) { + list.add( rs.getString("label") ); + } + rs.close(); + stmt.close(); + con.close(); + return list; + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + static List<Long> findTagUserIds(Site site,String sqlCondition) { + try { + List<Long> list = new ArrayList<Long>(); + Connection con = site.getDb().getConnection(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery( + "select distinct user_id from tag where " + sqlCondition + ); + while( rs.next() ) { + list.add( rs.getLong("user_id") ); + } + rs.close(); + stmt.close(); + con.close(); + return list; + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + static List<Long> findTagNodeIds(Site site,String sqlCondition) { + try { + List<Long> list = new ArrayList<Long>(); + Connection con = site.getDb().getConnection(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery( + "select distinct node_id from tag where " + sqlCondition + ); + while( rs.next() ) { + list.add( rs.getLong("node_id") ); + } + rs.close(); + stmt.close(); + con.close(); + return list; + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + static void deleteTags(SiteKey siteKey,String sqlCondition) { + try { + Connection con = siteKey.getDb().getConnection(); + Statement stmt = con.createStatement(); + stmt.executeUpdate( + "delete from tag where " + sqlCondition + ); + stmt.close(); + con.close(); + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + +}