forked from noborus/trdsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slice read / writer (noborus#47)
* Add slice read / writer * Add go slice to import/export. * Add test and example
- Loading branch information
Showing
15 changed files
with
700 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/noborus/trdsql" | ||
) | ||
|
||
type ArrayTable struct { | ||
table [][]string | ||
} | ||
|
||
func (a *ArrayTable) PreWrite(columns []string, types []string) error { | ||
a.table = make([][]string, 0) | ||
fmt.Println(columns, types) | ||
return nil | ||
} | ||
func (a *ArrayTable) WriteRow(values []interface{}, columns []string) error { | ||
row := make([]string, len(values)) | ||
for i, v := range values { | ||
row[i] = trdsql.ValString(v) | ||
} | ||
a.table = append(a.table, row) | ||
return nil | ||
} | ||
func (a *ArrayTable) PostWrite() error { | ||
return nil | ||
} | ||
|
||
func exec(query string) [][]string { | ||
writer := &ArrayTable{} | ||
trd := trdsql.NewTRDSQL(trdsql.NewImporter(), trdsql.NewExporter(writer)) | ||
trd.Driver = "postgres" | ||
trd.Dsn = "" | ||
err := trd.Exec(query) | ||
func main() { | ||
trd := trdsql.NewTRDSQL( | ||
trdsql.NewImporter(trdsql.InDelimiter(":")), | ||
trdsql.NewExporter(trdsql.NewWriter()), | ||
) | ||
err := trd.Exec("SELECT c1 FROM /etc/passwd") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return writer.table | ||
} | ||
|
||
func main() { | ||
trdsql.EnableDebug() | ||
table := exec("SELECT * FROM test") | ||
fmt.Println(table) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/noborus/trdsql" | ||
) | ||
|
||
func sliceQuery(slice interface{}, tableName string, query string) ([][]interface{}, error) { | ||
// trdsql.EnableDebug() | ||
importer := trdsql.NewSliceImporter(tableName, slice) | ||
writer := trdsql.NewSliceWriter() | ||
trd := trdsql.NewTRDSQL(importer, trdsql.NewExporter(writer)) | ||
err := trd.Exec(query) | ||
return writer.Table, err | ||
} | ||
|
||
func main() { | ||
data := []struct { | ||
id int | ||
name string | ||
}{ | ||
{id: 1, name: "Bod"}, | ||
{id: 2, name: "Alice"}, | ||
{id: 3, name: "Henry"}, | ||
} | ||
table, err := sliceQuery(data, "slice", "SELECT name,id FROM slice ORDER BY id DESC") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(table) | ||
// Output: | ||
// [[Henry 3] [Alice 2] [Bod 1]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package trdsql_test | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"github.com/noborus/trdsql" | ||
) | ||
|
||
func Example() { | ||
in := []byte(`"Rob","Pike",rob | ||
Ken,Thompson,ken | ||
"Robert","Griesemer","gri" | ||
`) | ||
tmpfile, err := ioutil.TempFile("/tmp", "xxx") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer func() { | ||
defer os.Remove(tmpfile.Name()) | ||
}() | ||
_, err = tmpfile.Write(in) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
trd := trdsql.NewTRDSQL( | ||
trdsql.NewImporter(), | ||
trdsql.NewExporter(trdsql.NewWriter()), | ||
) | ||
query := fmt.Sprintf("SELECT c1 FROM %s ORDER BY c1", tmpfile.Name()) | ||
err = trd.Exec(query) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// Output: | ||
// Ken | ||
// Rob | ||
// Robert | ||
} | ||
|
||
func ExampleSliceReader() { | ||
data := []struct { | ||
id int | ||
name string | ||
}{ | ||
{id: 1, name: "Bod"}, | ||
{id: 2, name: "Alice"}, | ||
{id: 3, name: "Henry"}, | ||
} | ||
tableName := "slice" | ||
importer := trdsql.NewSliceImporter(tableName, data) | ||
writer := trdsql.NewSliceWriter() | ||
trd := trdsql.NewTRDSQL(importer, trdsql.NewExporter(writer)) | ||
|
||
err := trd.Exec("SELECT name,id FROM slice ORDER BY id DESC") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
table := writer.Table | ||
fmt.Print(table) | ||
// Output: | ||
// [[Henry 3] [Alice 2] [Bod 1]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package trdsql | ||
|
||
type SliceImporter struct { | ||
*SliceReader | ||
} | ||
|
||
func NewSliceImporter(tableName string, data interface{}) *SliceImporter { | ||
return &SliceImporter{ | ||
SliceReader: NewSliceReader(tableName, data), | ||
} | ||
} | ||
|
||
func (i *SliceImporter) Import(db *DB, query string) (string, error) { | ||
names, err := i.Names() | ||
if err != nil { | ||
return query, err | ||
} | ||
types, err := i.Types() | ||
if err != nil { | ||
return query, err | ||
} | ||
err = db.CreateTable(i.tableName, names, types, true) | ||
if err != nil { | ||
return query, err | ||
} | ||
err = db.Import(i.tableName, names, i.SliceReader) | ||
return query, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.