diff --git a/app/controllers/app.go b/app/controllers/app.go index ef7e48c..6dfc42b 100644 --- a/app/controllers/app.go +++ b/app/controllers/app.go @@ -2,6 +2,7 @@ package controllers import ( "github.com/revel/revel" + "sasuke/app/service/sql" "strings" "fmt" ) @@ -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 { @@ -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) } diff --git a/app/service/sql/sql.go b/app/service/sql/sql.go index e4b317b..836aeb8 100644 --- a/app/service/sql/sql.go +++ b/app/service/sql/sql.go @@ -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; +} + diff --git a/app/views/app/Index.html b/app/views/app/Index.html index 2edbf13..7d38e2b 100644 --- a/app/views/app/Index.html +++ b/app/views/app/Index.html @@ -12,8 +12,14 @@ - + {{template "footer.html" .}} diff --git a/app/views/app/Result.html b/app/views/app/Result.html index 8a932a5..5a51832 100644 --- a/app/views/app/Result.html +++ b/app/views/app/Result.html @@ -8,6 +8,28 @@
{{template "flash.html" .}}
+
+ + + + + {{ range $index, $column := .hcolumns }} + + {{ end }} + + + + {{ range $index, $records := .records }} + + + {{ range $index, $value := $records }} + + {{ end }} + + {{ end }} + +
#{{ $column }}
{{ $index }}{{ $value }}
+
diff --git a/main.sql b/main.sql index 73774fc..c307720 100644 --- a/main.sql +++ b/main.sql @@ -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); diff --git a/tests/configtest.go b/tests/configtest.go index a4f2927..7a76670 100644 --- a/tests/configtest.go +++ b/tests/configtest.go @@ -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) @@ -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"))