Advertisement
janosoto

Query

Nov 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 KB | None | 0 0
  1. package util;
  2.  
  3. import entities.Correlation;
  4. import entities.Rating;
  5. import entities.User;
  6. import entities.Venue;
  7. import it.unimi.dsi.fastutil.longs.Long2IntArrayMap;
  8. import it.unimi.dsi.fastutil.longs.LongArraySet;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. public class Query {
  16.    
  17.     private final Statement stmt;
  18.    
  19.     public Query(Statement stmt){
  20.         this.stmt = stmt;
  21.     }
  22.    
  23.     public List<Rating> getRatingsByUser(long user_id) throws SQLException{
  24.         List<Rating> ratings = new ArrayList();
  25.         ResultSet rs = stmt.executeQuery("SELECT venue_id, rating FROM rating WHERE user_id = "+user_id);
  26.         while(rs.next()){
  27.             ratings.add(new Rating(user_id, rs.getInt(1), rs.getInt(2)));
  28.         }
  29.         return ratings;
  30.     }
  31.  
  32.     public Long2IntArrayMap getRatingsByUserId(long user_id) throws SQLException{
  33.         Long2IntArrayMap ratings = new Long2IntArrayMap();
  34.         ResultSet rs = stmt.executeQuery("SELECT venue_id, rating FROM rating WHERE user_id = "+user_id);
  35.         while(rs.next()){
  36.             ratings.put(rs.getLong(1), rs.getInt(2));
  37.         }
  38.         return ratings;
  39.     }    
  40.    
  41.     public List<User> getAllUsers() throws SQLException{
  42.         List<User> users = new ArrayList();
  43.         ResultSet rs = stmt.executeQuery("SELECT * FROM user LIMIT 10000");
  44.         while(rs.next()){
  45.             users.add(new User(rs.getLong(1), rs.getDouble(2), rs.getDouble(3)));
  46.         }
  47.         return users;
  48.     }
  49.    
  50.     public List<User> findUsersByFrontier(String [] frontier) throws SQLException{
  51.         String polygon = "\'POLYGON((";
  52.         for(String gp : frontier){
  53.             polygon = polygon.concat(gp+", ");
  54.         }
  55.         polygon = polygon.concat(frontier[0]+"))\'");
  56.        
  57.         List<User> users = new ArrayList();
  58.        
  59.         ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE ST_CONTAINS(ST_GEOMFROMTEXT("+polygon+"), POINT(user.latitude, user.longitude))");
  60.         while(rs.next()){
  61.             users.add(new User(rs.getLong(1), rs.getDouble(2), rs.getDouble(3)));
  62.         }
  63.         return users;
  64.     }
  65.    
  66.     public List<Venue> findVenuesByFrontier(String [] frontier) throws SQLException {
  67.         String polygon = "\'POLYGON((";
  68.         for(String gp : frontier){
  69.             polygon = polygon.concat(gp+", ");
  70.         }
  71.         polygon = polygon.concat(frontier[0]+"))\'");
  72.        
  73.         List<Venue> venues = new ArrayList();
  74.        
  75.         ResultSet rs = stmt.executeQuery("SELECT * FROM venue WHERE ST_CONTAINS(ST_GEOMFROMTEXT("+polygon+"), POINT(venue.latitude, venue.longitude))");
  76.         while(rs.next()){
  77.             venues.add(new Venue(rs.getLong(1), rs.getDouble(2), rs.getDouble(3)));
  78.         }
  79.         return venues;
  80.     }
  81.    
  82.     public List<Long> getAllUsersIds() throws SQLException{
  83.         List<Long> usersId = new ArrayList();
  84.         ResultSet rs = stmt.executeQuery("SELECT id FROM user");
  85.         while(rs.next()){
  86.             usersId.add(rs.getLong(1));
  87.         }
  88.         return usersId;
  89.     }
  90.    
  91.     public LongArraySet getUsersIds() throws SQLException{
  92.         LongArraySet usersId = new LongArraySet();
  93.         ResultSet rs = stmt.executeQuery("SELECT id FROM user LIMIT 10000");
  94.         while(rs.next()){
  95.             usersId.add(rs.getLong(1));
  96.         }
  97.         return usersId;
  98.     }
  99.    
  100.     public void saveCorrelation(long user1_id, long user2_id, double corr) throws SQLException{
  101.         stmt.execute("INSERT INTO correlation (user1_id, user2_id, correlation_value)"
  102.                 + " VALUES ("+user1_id+","+user2_id+","+corr+")");
  103.     }
  104.    
  105.     public boolean existsCorrelation(long user1_id, long user2_id) throws SQLException{
  106.         ResultSet rs = stmt.executeQuery("SELECT * FROM correlation WHERE user1_id = "+user1_id+" AND user2_id = "+user2_id);
  107.         return rs.next();
  108.     }
  109.    
  110.     public int countVenues() throws SQLException{
  111.         ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM venue");
  112.         if(rs.next()){
  113.             return rs.getInt(1);
  114.         }
  115.         else {
  116.             return 0;
  117.         }
  118.        
  119.     }
  120.    
  121.     public double getAverageRatingByUser(long user_id) throws SQLException{
  122.         ResultSet rs = stmt.executeQuery("SELECT AVG(rating) FROM `rating` WHERE user_id = "+user_id);
  123.         if(rs.next()){
  124.             return rs.getDouble(1);
  125.         }
  126.         else {
  127.             return 0.0;
  128.         }
  129.     }
  130.    
  131.     public List<Correlation> getNeighborhoodByUser(long user_id, int k) {
  132.         List<Correlation> neighborhood = new ArrayList();
  133.         ResultSet rs;
  134.         try {
  135.             rs = stmt.executeQuery("SELECT * FROM correlation WHERE user1_id = "+user_id+" OR user2_id = "+user_id+" ORDER BY correlation_value DESC LIMIT "+k);            
  136.             while(rs.next()){
  137.                 neighborhood.add(new Correlation(rs.getLong(1), rs.getLong(2), rs.getDouble(3)));
  138.             }
  139.         } catch (SQLException ex) {
  140.             ex.printStackTrace(System.out);
  141.         }
  142.         return neighborhood;
  143.     }
  144.            
  145.     public List<Venue> getAllVenues(){
  146.         List<Venue> venues = new ArrayList();
  147.         try {
  148.             ResultSet rs = stmt.executeQuery("SELECT * FROM venue");
  149.             while(rs.next()){
  150.                 venues.add(new Venue(rs.getLong(1), rs.getDouble(2), rs.getDouble(3)));
  151.             }
  152.         } catch (SQLException ex) {
  153.             ex.printStackTrace(System.out);
  154.         }
  155.         return venues;
  156.     }
  157.    
  158.     public List<Venue> getNVenues(int n){
  159.         List<Venue> venues = new ArrayList();
  160.         try {
  161.             ResultSet rs = stmt.executeQuery("SELECT * FROM venue LIMIT "+n);
  162.             while(rs.next()){
  163.                 venues.add(new Venue(rs.getLong(1), rs.getDouble(2), rs.getDouble(3)));
  164.             }
  165.         } catch (SQLException ex) {
  166.             ex.printStackTrace(System.out);
  167.         }
  168.         return venues;
  169.     }
  170.    
  171.     public double getCorrelationByUsers(long user1_id, long user2_id){
  172.         try {
  173.             ResultSet rs = stmt.executeQuery("SELECT correlation_value FROM correlation WHERE user1_id = "+user1_id+" AND user2_id = "+user2_id);
  174.             if(rs.next()){
  175.                 return rs.getDouble(1);
  176.             }
  177.         } catch (SQLException ex) {
  178.             ex.printStackTrace(System.out);
  179.         }
  180.         return 2;
  181.     }
  182.    
  183.     public int getRatingByUserAndVenue(long user_id, long venue_id){
  184.         try {
  185.             ResultSet rs = stmt.executeQuery("SELECT rating FROM rating WHERE user_id = "+user_id+" AND venue_id = "+venue_id);
  186.             if(rs.next()){
  187.                 return rs.getInt(1);
  188.             }
  189.         } catch (SQLException ex) {
  190.             ex.printStackTrace(System.out);
  191.         }
  192.         return 0;
  193.     }
  194.    
  195.     public List<Rating> getAllRatings() throws SQLException{
  196.         List<Rating> ratings = new ArrayList();
  197.         ResultSet rs = stmt.executeQuery("SELECT * FROM rating");
  198.         while(rs.next()){
  199.             ratings.add(new Rating(rs.getLong(2), rs.getLong(3), rs.getInt(4)));
  200.         }
  201.         return ratings;
  202.     }
  203.    
  204.     public User getUserById(long user_id) throws SQLException {
  205.         ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE id = "+user_id);
  206.         if(rs.next()){
  207.             return new User(rs.getLong(1), rs.getDouble(2), rs.getDouble(3));
  208.         }
  209.         else {
  210.             return null;
  211.         }
  212.     }
  213.    
  214.     public List<User> getNHigherRatingsUsers(int n) throws SQLException{
  215.         List<User> users = new ArrayList<>();
  216.         ResultSet rs = stmt.executeQuery("SELECT event.user_id, user.user_latitude, user.user_longitude, COUNT(*) as rated FROM event, user WHERE event.user_id = user.user_id GROUP BY event.user_id HAVING rated >= "+n+" ORDER BY rated DESC");
  217.         while(rs.next()){
  218.             users.add(new User(rs.getLong(1), rs.getDouble(2), rs.getDouble(3)));
  219.         }
  220.         return users;
  221.     }
  222.    
  223.     public List<Rating> getRatingPerUserFrontier(String frontier) throws SQLException{
  224.         List<Rating> events = new ArrayList<>();
  225.         ResultSet rs = stmt.executeQuery("SELECT geo_event.user_id, geo_event.item_id, geo_event.event_value "
  226.                                         +"FROM geo_event WHERE ST_CONTAINS("
  227.                                                         + "ST_GEOMFROMTEXT('POLYGON(("+frontier+"))'), "
  228.                                                         + "POINT(geo_event.item_latitude, geo_event.item_longitude))");
  229.         while(rs.next()){
  230.             events.add(new Rating(rs.getLong(1), rs.getLong(2), rs.getInt(3)));
  231.         }
  232.         return events;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement