// DO NOT EDIT MANUALLY
// 
// generated by sqlgg 0.2.3-43-g8cd5b20 on 2009-11-07T13:43Z

import java.sql.*;

public class demo_java_gen
{
  Connection db;

  public demo_java_gen(Connection aDb)
  {
    db = aDb;
  }

  PreparedStatement pstmt_create_person;

  public int create_person() throws SQLException
  {
    if (null == pstmt_create_person)
      pstmt_create_person = db.prepareStatement("CREATE TABLE IF NOT EXISTS person (id INTEGER PRIMARY KEY AUTO_INCREMENT,name TEXT,surname TEXT)");
    return pstmt_create_person.executeUpdate();
  }

  PreparedStatement pstmt_add_person;

  public int add_person(String name, String surname) throws SQLException
  {
    if (null == pstmt_add_person)
      pstmt_add_person = db.prepareStatement("INSERT INTO person (name,surname) VALUES (?,?)");
    pstmt_add_person.setString(1, name);
    pstmt_add_person.setString(2, surname);
    return pstmt_add_person.executeUpdate();
  }

  PreparedStatement pstmt_create_money;

  public int create_money() throws SQLException
  {
    if (null == pstmt_create_money)
      pstmt_create_money = db.prepareStatement("CREATE TABLE IF NOT EXISTS money (src INTEGER, dst INTEGER, amount INTEGER)");
    return pstmt_create_money.executeUpdate();
  }

  PreparedStatement pstmt_add_money;

  public int add_money(int src, int dst, int amount) throws SQLException
  {
    if (null == pstmt_add_money)
      pstmt_add_money = db.prepareStatement("INSERT INTO money VALUES (?,?,?)");
    pstmt_add_money.setInt(1, src);
    pstmt_add_money.setInt(2, dst);
    pstmt_add_money.setInt(3, amount);
    return pstmt_add_money.executeUpdate();
  }

  PreparedStatement pstmt_calc_total;

  public static interface calc_total_callback
  {
    public void callback(String fullname, int total);
  } // public static interface calc_total_callback

  public int calc_total(calc_total_callback result) throws SQLException
  {
    if (null == pstmt_calc_total)
      pstmt_calc_total = db.prepareStatement("SELECT CONCAT(name,' ',surname) AS fullname, SUM(amount) as total FROM person JOIN money ON src = id GROUP BY id");
    ResultSet res = pstmt_calc_total.executeQuery();
    int count = 0;
    while (res.next())
    {
      result.callback(res.getString(1),res.getInt(2));
      count++;
    }
    return count;
  }

  PreparedStatement pstmt_list_donors;

  public static interface list_donors_callback
  {
    public void callback(String surname);
  } // public static interface list_donors_callback

  public int list_donors(String _0, int limit, list_donors_callback result) throws SQLException
  {
    if (null == pstmt_list_donors)
      pstmt_list_donors = db.prepareStatement("SELECT DISTINCT surname FROM person JOIN money ON src = id AND dst = (SELECT id FROM person WHERE surname LIKE ?) LIMIT ?");
    pstmt_list_donors.setString(1, _0);
    pstmt_list_donors.setInt(2, limit);
    ResultSet res = pstmt_list_donors.executeQuery();
    int count = 0;
    while (res.next())
    {
      result.callback(res.getString(1));
      count++;
    }
    return count;
  }

  PreparedStatement pstmt_drop_person;

  public int drop_person() throws SQLException
  {
    if (null == pstmt_drop_person)
      pstmt_drop_person = db.prepareStatement("DROP TABLE IF EXISTS person");
    return pstmt_drop_person.executeUpdate();
  }

  PreparedStatement pstmt_drop_money;

  public int drop_money() throws SQLException
  {
    if (null == pstmt_drop_money)
      pstmt_drop_money = db.prepareStatement("DROP TABLE IF EXISTS money");
    return pstmt_drop_money.executeUpdate();
  }

} // public class demo_java_gen

