Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions app/controllers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"github.com/revel/revel"
"sasuke/app/service/sql"
"strings"
"fmt"
)
Expand All @@ -11,7 +12,33 @@ type App struct {
}

func (c App) Index() revel.Result {
return c.Render()
s := &sql.Handler{}
table_name_slice, column_name_slice := s.FetchTableSchema()
// tableのダブり値を削除
m := make(map[string]bool)
uniq := [] string{}

for _, ele := range table_name_slice {
if !m[ele] {
m[ele] = true
uniq = append(uniq, ele)
}
}

// var table_schema [ len(uniq) ][len(column_name_slice)]string

//for i:=0 ;i < len(column_name_slice) ;i++ {
//
// fmt.Printf(table_name_slice[i])
// fmt.Printf(column_name_slice[i])
// before_index := i -1
// // テーブル名変更された時
// if table_name_slice[i] != table_name_slice[before_index] {
// table_schema[i][0] = table_name_slice[i]
// table_schema[i] = append(table_schema[i], column_name_slice[i])
// }
//}
return c.Render(table_name_slice, column_name_slice)
}

func (c App) Execute() revel.Result {
Expand Down Expand Up @@ -63,14 +90,26 @@ func (c App) Execute() revel.Result {
// 外部キーを取得
sc := []rune(table)
foreign_key := string(sc[:(len(sc) - 1)]) + "_id"
query = "select " + select_columns + " from " + table + " left join " + relation + " on " + table + ".id " + "= " + relation + "." + foreign_key
query = "select " + select_columns +
" from " + table +
" left join " + relation +
" on " + table + ".id " + "= " + relation + "." + foreign_key
}
// todo sqlを投げて、その結果を渡す
return c.Render(query, columns)
// return c.Redirect(App.Result, query)

// Query実行
s := &sql.Handler{}
hcolumns, records := s.ExecuteQuery(query)

return c.Redirect(App.Result, hcolumns, records)
}


func (c App) Result() revel.Result {
return c.Render()
// (画面確認用)結果画面の確認のため:Query実行
query := "select * from articles;"
s := &sql.Handler{}
hcolumns, records := s.ExecuteQuery(query)
return c.Render(hcolumns, records)

return c.Render(hcolumns, records)
}
130 changes: 130 additions & 0 deletions app/service/sql/sql.go
Original file line number Diff line number Diff line change
@@ -1 +1,131 @@
package sql

import (
_ "github.com/go-sql-driver/mysql"
"log"
"database/sql"
"sasuke/app/service/file"
"os"
)

type Handler struct {
}


func (h *Handler)FetchTableSchema ()([]string,[]string){

// Todo: Connect()
f := &file.Handler{}
f.LoadEnv();

db := os.Getenv("db")
host := os.Getenv("host")
port := os.Getenv("port")
dbuser := os.Getenv("dbuser")
dbname := os.Getenv("dbname")
password := os.Getenv("password")

mysql, error := sql.Open(db,dbuser+":"+password+"@tcp("+host+":"+port+")/"+"information_schema")
if error != nil {
log.Fatal("open erro: %v", error)
}
defer mysql.Close()


main_query :=`select table_name,column_name from columns where table_schema="`+dbname+ `";`
rows, err := mysql.Query(main_query)
defer rows.Close()

if err != nil {
panic(err.Error())
}

type TableSchema struct {
table_name string
column_name string
}

var table_name_slice []string
var column_name_slice []string

for rows.Next() {
var table_name string
var column_name string
if err := rows.Scan(&table_name, &column_name); err != nil {
log.Fatal(err)
}

table_name_slice = append(table_name_slice,table_name)
column_name_slice = append(column_name_slice,column_name)
}

return table_name_slice,column_name_slice
}



// SQL Queryを実行し、カラム名・結果レコードを返す
func(h *Handler) ExecuteQuery(query string)([]string, [][]interface{}){

// Todo: Connect()
f := &file.Handler{}
f.LoadEnv();

db := os.Getenv("db")
host := os.Getenv("host")
port := os.Getenv("port")
dbuser := os.Getenv("dbuser")
dbname := os.Getenv("dbname")
password := os.Getenv("password")

mysql, err := sql.Open(db,dbuser+":"+password+"@tcp("+host+":"+port+")/"+dbname)
if err != nil {
log.Fatal("open erro: %v", err)
}
defer mysql.Close()


// query実行
rows, err := mysql.Query(query)
if err != nil{
panic(err.Error())
}
defer rows.Close()

// オブジェクトから値を取り出す
// rows.Scanで値を取り出すために[]interface{}を定義
columns, err := rows.Columns()
if err != nil {
panic(err.Error())
}
count := len(columns)
valuePtrs := make([]interface{}, count)

records := make([][]interface{}, 0)


for rows.Next() {
values := make([]interface{}, count)

for i,_ := range columns{
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)

for i,_ := range columns{
var v interface{}
val := values[i]
b, ok := val.([]byte)
if(ok){
v = string(b)
}else{
v = val
}
values[i] = v
}
records = append(records, values)
}

return columns, records;
}

8 changes: 7 additions & 1 deletion app/views/app/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
</div>
</div>
</div>

</body>
<script>

const table_schema = [
{table_name: 'users', columns: ['id','first_name','last_name','created_at','updated_at']},
{table_name: 'article', columns: ['id','user_id','content','created_at','updated_at']}
]
</script>
<script type="text/javascript" src="/public/js/vue.min.js"></script>
<script src="/public/js/app_index.js"></script>
{{template "footer.html" .}}
22 changes: 22 additions & 0 deletions app/views/app/Result.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
<div class="span6">
{{template "flash.html" .}}
</div>
<div class="col-12">
<table class="table">
<thead class="thead-inverse">
<tr>
<th>#</th>
{{ range $index, $column := .hcolumns }}
<th>{{ $column }}</th>
{{ end }}
</tr>
</thead>
<tbody>
{{ range $index, $records := .records }}
<tr>
<th scope="row">{{ $index }}</th>
{{ range $index, $value := $records }}
<th>{{ $value }}</th>
{{ end }}
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
</div>
</body>
Expand Down
3 changes: 3 additions & 0 deletions main.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CREATE DATABASE SASUKE_TEST
update user set authentication_string=PASSWORD("password") where User='root';
create user 'test_user'@'localhost' identified by 'password';
grant all privileges on *.* to test_user@"%" identified by 'password';
CREATE TABLE SASUKE_TEST.articles (id INT , content VARCHAR(255) ,user_id INTEGER , created_at DATETIME,updated_at DATETIME);
CREATE TABLE SASUKE_TEST.users (id INT ,first_naem VARCHAR(20) ,last_name VARCHAR(20) ,created_at DATETIME,updated_at DATETIME);
10 changes: 6 additions & 4 deletions tests/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ func (t *ConfigTest) TestConfigPageWorks(){

func (t *ConfigTest) TestConfigPostFormSuccessData(){

// 自分の開発環境の接続情報
testdb = "mysql"
testhost = "192.168.192.168"
testhost = "0.0.0.0"
testport = "3306"
testuser = "testuser"
testname = "testdb"
testpass = "P@ssw0rd"
testuser = "root"
testname = "SASUKE_TEST"
testpass = "mysql"

configData := url.Values{}
configData.Add("db", testdb)
Expand All @@ -54,6 +55,7 @@ func (t *ConfigTest) TestConfigPostFormSuccessData(){

f := &file.Handler{}
f.LoadEnv()

t.AssertEqual(testdb, os.Getenv("db"))
t.AssertEqual(testhost, os.Getenv("host"))
t.AssertEqual(testport, os.Getenv("port"))
Expand Down