// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLiteDemo implements Demo {
    public void findAbbreviation(Connection con) throws SQLException {
        String s = "IN";
        System.out.println("String (SQLite):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,term FROM abbr " +
                                                       "WHERE SUBSTR(term, 1, 1) || UPPER(SUBSTR(term, LENGTH(term), 1)) = ?");
        pstmt.setString(1, s);
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            String term = rs.getString(2);
            System.out.printf("%d : %s\n", id, term);
        }
        rs.close();
        pstmt.close();
    }
    public void later(Connection con) throws SQLException {
        System.out.println("Later (SQLite):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE dt > DATETIME()");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            String dt = rs.getString(2);
            System.out.printf("%d %s\n", id, dt);
        }
        rs.close();
        pstmt.close();
    }
    public void thisMonth(Connection con) throws SQLException {
        System.out.println("This month (SQLite):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE STRFTIME('%m',dt) = STRFTIME('%m',DATE()) AND STRFTIME('%Y',dt) = STRFTIME('%Y', DATE())");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            String dt = rs.getString(2);
            System.out.printf("%d %s\n", id, dt);
        }
        rs.close();
        pstmt.close();
    }
    public void getAutoIds(Connection con) throws SQLException {
        System.out.println("Id (SQLite):");
        Statement stmt = con.createStatement();
        PreparedStatement pstmt = con.prepareStatement("INSERT INTO aid (val) VALUES(?)");
        for(int i = 0; i < 3; i++) {
            pstmt.setString(1, "Bla bla bla");
            pstmt.executeUpdate();
            ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ROWID()");
            if(rs.next()) {
                System.out.println(rs.getInt(1));
            } else {
                System.out.println("?");
            }
            rs.close();
        }
        pstmt.close();
        stmt.close();
    }
    public void limit(Connection con) throws SQLException {
        System.out.println("Limit (SQLite):");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id FROM customer LIMIT 3");
        while(rs.next()) {
            int id = rs.getInt(1);
            System.out.printf("%d\n", id);
        }
        rs.close();
        stmt.close();
    }
}
