Susumu Yata
null+****@clear*****
Sun Apr 10 16:51:56 JST 2016
Susumu Yata 2016-04-10 16:51:56 +0900 (Sun, 10 Apr 2016) New Revision: 7f2a9ef44368d98b7d338f35814dc2fd301345b9 https://github.com/groonga/grnci/commit/7f2a9ef44368d98b7d338f35814dc2fd301345b9 Message: Add DB.Exec (experimental). GitHub: #35 Modified files: grnci.go grnci_test.go Modified: grnci.go (+19 -10) =================================================================== --- grnci.go 2016-04-10 13:43:21 +0900 (0788a88) +++ grnci.go 2016-04-10 16:51:56 +0900 (beda337) @@ -17,6 +17,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "os" "reflect" "strconv" @@ -1703,19 +1704,13 @@ func (db *DB) PluginUnregister(name string, options *PluginUnregisterOptions) er } // -// Execute commands in a file (experimental). +// Execute commands (experimental). // -// ExecFile reads commands from a file and executes it. -func (db *DB) ExecFile(path string) ([][]byte, error) { - file, err := os.Open(path) - if err != nil { - return nil, err - } - defer file.Close() - +// Exec reads commands and executes it. +func (db *DB) Exec(reader io.Reader) ([][]byte, error) { var resps [][]byte - scanner := bufio.NewScanner(file) + scanner := bufio.NewScanner(reader) for scanner.Scan() { resp, err := db.exec([]byte(scanner.Text())) if err != nil { @@ -1730,3 +1725,17 @@ func (db *DB) ExecFile(path string) ([][]byte, error) { } return resps, nil } + +// +// Execute commands in a file (experimental). +// + +// ExecFile reads commands from a file and executes it. +func (db *DB) ExecFile(path string) ([][]byte, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + return db.Exec(file) +} Modified: grnci_test.go (+31 -0) =================================================================== --- grnci_test.go 2016-04-10 13:43:21 +0900 (83db183) +++ grnci_test.go 2016-04-10 16:51:56 +0900 (b1969ef) @@ -1,6 +1,7 @@ package grnci import ( + "bytes" "encoding/json" "io/ioutil" "os" @@ -634,6 +635,36 @@ func TestGetStructInfo(t *testing.T) { } } +func TestExec(t *testing.T) { + dirPath, _, db := createTempDB(t) + defer removeTempDB(t, dirPath, db) + + cmds := []byte(`table_create tbl TABLE_NO_KEY +column_create tbl col COLUMN_SCALAR Int32 +load --table tbl '[[\"col\"],[123],[456],[789]]' +load --table tbl +[ + ["col"], + [100], + [200], + [300], + [400], + [500] +] +`) + resps, err := db.Exec(bytes.NewReader(cmds)) + if err != nil { + t.Fatalf("DB.ExecFile failed: %v", err) + } + if len(resps) != 4 || + string(resps[0]) != "true" || + string(resps[1]) != "true" || + string(resps[2]) != "3" || + string(resps[3]) != "5" { + t.Fatalf("DB.ExecFile failed: resps = %#v", resps) + } +} + func TestExecFile(t *testing.T) { dirPath, _, db := createTempDB(t) defer removeTempDB(t, dirPath, db) -------------- next part -------------- HTML����������������������������... 下载