#!/usr/bin/env ascript
# -*- coding: shift_jis -*-
import(csv)
import(sqlite3)

sys.stdout.setencoding("shift_jis")

lambda {
	fs.remove("50records-en.sqlite3")
	sqlite3.open("50records-en.sqlite3") {|db|
		db.exec("""\
			create table people (
				name     text,
				email    text,
				gender   text,
				age      integer,
				birthday text,
				mobile   text
			)
		""")
		Person = struct(name:string, email:string,
				gender:string, age:number, birthday:string, mobile:string)
		people = Person * csv.read(open("50records-en.csv")):iter
		db.transaction {
			for (person in people) {
				db.exec("insert into people values ('%s', '%s', '%s', %d, '%s', '%s')" % \
					person.tolist())
			}
		}
	}
}()
lambda {
	Person = struct(name:string, email:string,
				gender:string, age:number, birthday:string, mobile:string)
	db = sqlite3.open("50records-en.sqlite3")
	println()
	println("column names: ", db.getcolnames("select * from people").join(", "))
	println(Person * db.query("select * from people"))
	db.close()
}()
lambda {
	Person = struct(name:string, email:string,
				gender:string, age:number, birthday:string, mobile:string)
	sqlite3.open("50records-en.sqlite3") {|db|
		println()
		println("column names: ", db.getcolnames("select * from people").join(", "))
		println(Person * db.query("select * from people"))
	}
}()
lambda {
	fs.remove("50records-jp.sqlite3")
	sqlite3.open("50records-jp.sqlite3") {|db|
		db.exec("""\
			create table people (
				名前       text,
				ふりがな   text,
				電子メール text,
				性別       text,
				年齢       integer,
				誕生日     text,
				結婚       text,
				出身県     text,
				携帯電話   text,
				携帯会社   text,
				くせ       text
			)
		""")
		Person = struct(name:string, name_ruby:string, email:string,
				gender:string, age:number, birthday:string,
				married:string, prefecture:string, mobile:string,
				mobile_career:string, habit:string)
		people = Person * csv.read(open("50records-jp.csv", "r", "shift_jis")):iter
		db.transaction {
			for (person in people) {
				db.exec("insert into people values (" \
					"'%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s')" % \
					person.tolist())
			}
		}
	}
}()
lambda {
	Person = struct(name:string, name_ruby:string, email:string,
			gender:string, age:number, birthday:string,
			married:string, prefecture:string, mobile:string,
			mobile_career:string, habit:string)
	sqlite3.open("50records-jp.sqlite3") {|db|
		println()
		println("column names: ", db.getcolnames("select * from people").join(", "))
		println(Person * \
			db.query("select 名前, ふりがな, 電子メール, " \
					"性別, 年齢, 誕生日, 結婚, 出身県, " \
					"携帯電話, 携帯会社, くせ from people"))
	}
}()
