/*
 * ZipBuild.cpp
 * 梄曋斣崋CSV傪撉傒弌偟丄SQLite僥乕僽儖偵奿擺偡傞
 */

int main() {
  Oblaat::SQLite3::Database^ db = gcnew Oblaat::SQLite3::Database();
  db->open("zip.db");
  System::String^ errmsg = nullptr;
  db->exec(
    "CREATE TABLE zip_table ("
     " id TEXT, zip_old TEXT, zip TEXT, "
     " pref_kana TEXT, city_kana TEXT, town_kana TEXT,"
     " pref TEXT, city TEXT, town TEXT)",
          nullptr, nullptr, errmsg);
  db->exec("BEGIN TRANSACTION", nullptr, nullptr, errmsg);
  Oblaat::SQLite3::Statement^ statement;
  db->prepare("INSERT INTO zip_table VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )", statement);
  Wankuma::IO::CSVReaderEnumerable^ target =
    gcnew Wankuma::IO::CSVReaderEnumerable(15, "KEN_ALL.CSV", 
             System::Text::Encoding::GetEncoding("shift_jis"));
  int n = 0;
  for each ( System::Collections::Generic::List<System::String^>^ row in target->RowList() ) {
    statement->reset();
    statement->bind_text( 1, row[0]); // 抧曽岞嫟抍懱僐乕僪
    statement->bind_text( 2, row[1]); // 媽梄曋斣崋(5寘)
    statement->bind_text( 3, row[2]); // 梄曋斣崋(7寘)
    statement->bind_text( 4, row[3]); // 搒摴晎導柤(敿妏僇僞僇僫)
    statement->bind_text( 5, row[4]); // 巗挰懞柤(敿妏僇僞僇僫)
    statement->bind_text( 6, row[5]); // 挰堟柤(敿妏僇僞僇僫)
    statement->bind_text( 7, row[6]); // 搒摴晎導柤
    statement->bind_text( 8, row[7]); // 巗挰懞柤
    statement->bind_text( 9, row[8]); // 挰堟柤
    statement->step();
    ++n;
  }
  delete target;
  statement->finalize();
  db->exec("COMMIT TRANSACTION", nullptr, nullptr, errmsg);
  db->close();
  System::Console::WriteLine("{0} records inserted.", n);
}
