# -*- coding: emacs-mule -*-

=begin
http://www.sqlite.org/sqlite.html <-

http://whytheluckystiff.net/articles/aQuickGuideToSQLite.html  <-ruby, de hazudik

http://sqlite-ruby.rubyforge.org/sqlite3/faq.html
=end


=begin

#db letrehozasa

sqlite3 /tmp/proba.db

create table hallgatok (
kod char(6) primary key,
nev varchar(100),
szak varchar(60));

create table telefon (
 hallgato char(6) references hallgatok(kod),
 tipus char(7),
 szam varchar(20)
);

=end


#db feltoltese

require 'sqlite3'

db = SQLite3::Database.new('/tmp/db.db')
# ha van ilyen, azt hasznalja, ha nincs, letrehozza


# "Kovacs Bela" -> "KB"
def monogram (nev)
  nev.split(" ").collect {|x| x[0,1]}.join
end

#a file-ban nev, szak %-al elvalasztva
db.execute("delete from hallgatok")
db.execute("delete from telefon")

File.open("/home/simon/wp/2010/ea5/hallgat.txt","r") do |f|
  while line = f.gets
    a = line.split('%')
    db.execute("insert into hallgatok (kod, nev, szak) values ('#{monogram(a[0])+a[0][1,3]}','#{a[0].rstrip}','#{a[1].rstrip}')")
    #monogram nem egyert., azert adunk hozza meg kicsit a nevbol
  end
end

db.execute("select kod,nev from hallgatok") do |row| 
  (1+rand(2)).times do 
    db.execute("insert into telefon(hallgato,tipus,szam) values ('#{row[0]}','#{rand(2)== 0 ? 'vezetek' : 'mobil'}','#{monogram(row[1])+(rand(999999).to_s)}') ")
  end
end

#db feltoltes vege


db.execute("select nev, kod from hallgatok") 
# -> az eredmeny soraibol allo tomb (kiveve, ha... ld. kesobb); minden
# sor maga is tomb, aminek k. eleme a k+1. mezo execute2 ugyanez, csak
# az elso sor a mezok neveibol fog allni

db.execute("select nev, szak from hallgatok") do |row| 
  puts "Név: " + row[0] + " Szak: " + row[1]
end

db.execute("select nev, szak from hallgatok where szak like 'matematika (%'") do |row| 
  puts "Név: " + row[0] + " Szak: " + row[1]
end

db.execute("select nev, szak from hallgatok where szak not like '%bsc%'") do |row| 
  puts "Név: " + row[0] + " Szak: " + row[1]
end


db.execute("select nev, szam from hallgatok,telefon where hallgatok.kod = telefon.hallgato") do
  |row|
  puts "Nev: " + row[0] + " Telefon: " + row[1]
end

db.get_first_row("select nev, szam from hallgatok,telefon where hallgatok.kod = telefon.hallgato")

db.get_first_value("select count(*) from hallgatok,telefon where hallgatok.kod = telefon.hallgato")


db.results_as_hash = true
# -> az eredmeny sorai hash-ek

db.execute("select nev, szam from hallgatok,telefon where hallgatok.kod = telefon.hallgato") do
  |row|
  puts "Nev: " + row['nev'] + " Telefon: " + row['szam']
end


#de db.execute2-ben az elso sor tovabbara is tomb.

db.results_as_hash = false
# -> vissza az egesz

# parameterek
db.execute("select nev, szam from hallgatok,telefon where hallgatok.kod = telefon.hallgato and hallgatok.kod='HEava'") 
#helyett:
db.execute("select nev, szam from hallgatok,telefon where hallgatok.kod = telefon.hallgato and hallgatok.kod=?", "HEava")

db.execute("insert into hallgatok (kod, nev, szak) values (?,?,?)", 'KBA132','Kiss Sarolta',"sporthorgász")

#transaction - nem mukodik.

db.execute("delete from hallgatok where kod = ?", "KBA132")

db.transaction do |d|
  d.execute("insert into hallgatok (kod, nev, szak) values (?,?,?)", 'KBA132','Kiss Sarolta',"sporthorgász")
  d.execute("insert into hallgatok (kod, nev, szak) values (?,?,?)", 'KBA133','Kisss Sarolta',"sporthorgásznő")
raise
end

