#!/usr/bin/env ruby22
# -*- coding: utf-8 -*-
require 'cgi'
require 'sqlite3'
require 'kconv'
c = CGI.new(:tag_maker => "html5", :accept_charest => "UTF-8")
db = SQLite3::Database.new("../sql/main.sq3")

getAC1 = ARGV[0]
pass = "hogehogeboy"

hsh = c["SQ1"]
hsh += c["SQ2"]

pts = "<table><tr><th>id</th><td>name</td><td>cal</td><td>age</td><td>sex</td><td>aria</td><td>exp</td><td>pt</td><td>lv</td></tr>"


if c["SQ2"] == "toi"
pts = "<table><tr><th>id</th><td>q1</td><td>q2</td><td>q3</td><td>q4</td><td>q5</td><td>q6</td><td>q7</td><td>q8</td><td>q9</td><td>secret</td></tr>"
elsif c["SQ2"] == "time"
pts = "<table><tr><th>cd</th><td>now</td><td>id</td></tr>"
elsif c["SQ2"] == "goods"
pts = "<table><tr><th>id</th><td>おちゃ</td><td>しょうひん2</td><td>しょうひん3</td><td>しょうひん4</td><td>しょうひん5</td><td>しょうひん6</td></tr>"
elsif c["SQ2"] == "pass"
pts = "<table><tr><th>id</th><td>pass</td><td>name</td></tr>"

end


######################################
################SQ3###################
if c["SQ3"] == ""
else
upd = c["SQ3"]
db.execute(upd)
end

################SQ3###################
######################################




################################
################################
if hsh == ""
#  pts = "hoge"
  hsh = "select * from test"
else
end

hsh2 = db.execute(hsh)
i = 0
while i < hsh2.length
  data = hsh2[i]
  if c["SQ2"] == "toi"
    pts += "<tr><th>#{data[0]}</th><td>#{data[1]}</td><td>#{data[2]}</td><td>#{data[3]}</td><td>#{data[4]}</td><td>#{data[5]}</td><td>#{data[6]}</td><td>#{data[7]}</td><td>#{data[8]}</td><td>#{data[9]}</td><td>#{data[10]}</td></tr>" 
  elsif c["SQ2"] == "time"
    pts += "<tr><th>#{data[0]}</th><td>#{data[1]}</td><td>#{data[2]}</td></tr>"
  elsif c["SQ2"] == "goods"
    pts += "<tr><th>#{data[0]}</th><td>#{data[1]}</td><td>#{data[2]}</td><td>#{data[3]}</td><td>#{data[4]}</td><td>#{data[5]}</td><td>#{data[6]}</td></tr>"
  else
    pts += "<tr><th>#{data[0]}</th><td>#{data[1]}</td><td>#{data[2]}</td><td>#{data[3]}</td><td>#{data[4]}</td><td>#{data[5]}</td><td>#{data[6]}</td><td>#{data[7]}</td><td>#{data[8]}</td><td>#{data[9]}</td></tr>" 
  end
  i += 1
end
#end
pts += "</table>"


print"Content-type: text/html; charset=UTF-8\n\n"


######################################
################if ~html##############

#if getAC1 == pass

printf(<<_EOS_, pts)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no">
<link media="only screen and (max-device-width:480px)"
href="../smart.css" type="text/css" rel="stylesheet" />
<link media="screen and (min-device-width:481px)" href="../profeel.css"
type="text/css" rel="stylesheet" />

<link rel="shortcut icon" href="../img/tamago.png"/>

<title>control</title>
<style type="text/css">
<!--
-->
</style>
</head>

<body>
<header><p>test,toi,goods,time,num,pass<p></header>



<h1>control</h1>
<form method="POST" action="check.rb?">
<input type="text" name="SQ1" value="select * from ">
<input type="text" name="SQ2" placeholder='test JOIN pass ON test.id==pass.id;' value="test">
<input type="submit">
</form>

<form method="POST" action="check.rb?#{pass}">
<input type="text" name="SQ3" size="42" placeholder='insert into test values(501, "foo", "inu", 40, "G", "sakata", 0,0,1)' value="update test set pt = 0 where id = 0">
<input type="submit">
</form>
<p>%s<br></p>

<footer></footer>
</body>
</html>

_EOS_
#############################################
#############################################
#else
