Example #1
0
File: server.go Project: tmc/adt
func (s *Server) srvDBIndex(rw http.ResponseWriter, r *http.Request) {
	start := time.Now()
	log.Println("start", r.URL.String())
	defer func() {
		log.Println("end", r.URL.String(), time.Now().Sub(start))
	}()
	name := r.URL.Path[len("/dbs/"):]
	parts := strings.Split(name, "/")
	table, err := adt.TableFromPath(filepath.Join(s.path, parts[0]))
	if renderErr(rw, err) {
		return
	}
	if query := r.URL.Query().Get("q"); query != "" {
		rw.Header().Add("Content-Type", "application/json")
		field := r.URL.Query().Get("field")
		for i := int(table.RecordCount) - 1; i >= 0; i-- {
			data, err := table.Get(i)
			if renderErr(rw, err) {
				return
			}
			if fmt.Sprint(data[field]) == query {
				data, err = s.decorateRecord(parts[0], data)
				if renderErr(rw, err) {
					return
				}
				json.NewEncoder(rw).Encode(data)
				return
			}
		}
		rw.WriteHeader(http.StatusNotFound)
	}
	render(rw, "db.tmpl", table)
}
Example #2
0
File: server.go Project: tmc/adt
func (s *Server) srvDBRecord(rw http.ResponseWriter, r *http.Request) {
	start := time.Now()
	log.Println("start", r.URL.String())
	defer func() {
		log.Println("end", r.URL.String(), time.Now().Sub(start))
	}()
	name := r.URL.Path[len("/dbs/"):]
	parts := strings.Split(name, "/")
	table, err := adt.TableFromPath(filepath.Join(s.path, parts[0]))
	if renderErr(rw, err) {
		return
	}
	index, err := strconv.Atoi(parts[1])
	if renderErr(rw, err) {
		return
	}
	data, err := table.Get(index - 1)
	if renderErr(rw, err) {
		return
	}
	data, err = s.decorateRecord(parts[0], data)
	if renderErr(rw, err) {
		return
	}
	rw.Header().Add("Content-Type", "application/json")
	json.NewEncoder(rw).Encode(data)
}
Example #3
0
File: main.go Project: tmc/adt
func main() {
	flag.Parse()
	table, err := adt.TableFromPath(*flagFile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	until := int(table.RecordCount)
	if *flagNum != -1 {
		until = *flagIndex + *flagNum
	}
	for i := *flagIndex; i < until; i++ {
		r, err := table.Get(i)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		var buf []byte
		if *flagIndent {
			buf, _ = json.MarshalIndent(r, "", "  ")
		} else {
			buf, _ = json.Marshal(r)
		}
		os.Stdout.Write(buf)
		os.Stdout.WriteString("\n")
	}

}
Example #4
0
File: main.go Project: tmc/adt
func main() {
	flag.Parse()
	table, err := adt.TableFromPath(*flagFile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	spew.Dump(table)
	r, err := table.Get(int(table.RecordCount - 2))
	fmt.Printf("%+v\n", r)

	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
Example #5
0
func TestTableRead(t *testing.T) {
	db := os.Getenv("ADT_TEST_FILE")
	if db == "" {
		t.Skip("ADT_TEST_FILE not set")
	}

	table, err := adt.TableFromPath(db)
	if err != nil {
		t.Fatal(err)
	}
	for i := 0; i < int(table.RecordCount); i++ {
		_, err := table.Get(i)
		//fmt.Printf("%+v\n", r)
		if err != nil {
			t.Error("row", i, err)
		}
	}
}
Example #6
0
File: server.go Project: tmc/adt
func (s *Server) lookupRecord(tableName Table, pk interface{}) (map[string]interface{}, error) {
	table, err := adt.TableFromPath(filepath.Join(s.path, string(tableName)))
	if err != nil {
		return nil, err
	}
	pkCol, err := table.GetPK()
	if err != nil {
		return nil, err
	}
	for i := int(table.RecordCount) - 1; i >= 0; i-- {
		record, err := table.Get(i)
		if err != nil {
			return nil, err
		}
		if fmt.Sprint(record[pkCol.Name]) == fmt.Sprint(pk) {
			return record, nil
		}
	}
	return nil, nil
}
Example #7
0
File: main.go Project: tmc/adt
func main() {
	flag.Parse()
	table, err := adt.TableFromPath(*flagFile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	until := int(table.RecordCount)
	if *flagNum != -1 {
		until = *flagIndex + *flagNum
	}

	w := csv.NewWriter(os.Stdout)
	// header
	headers := []string{}
	for _, c := range table.Columns {
		headers = append(headers, c.Name)
	}
	if err := w.Write(headers); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	for i := *flagIndex; i < until; i++ {
		fields := make([]string, 0, len(table.Columns))
		r, err := table.Get(i)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		for _, c := range table.Columns {
			fields = append(fields, fmt.Sprint(r[c.Name]))
		}
		if err := w.Write(fields); err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}
	w.Flush()
}
Example #8
0
File: main.go Project: tmc/adt
func migrate() error {
	dbURL := os.Getenv("DATABASE_URL")
	if dbURL == "" {
		return fmt.Errorf("No value present in DATABASE_URL environment variable.")
	}
	db, err := newDBFromURL(dbURL)
	if err != nil {
		return err
	}
	if *flagTableName == "" {
		*flagTableName = strings.TrimSuffix(*flagFile, ".ADT")
	}

	table, err := adt.TableFromPath(*flagFile)
	if err != nil {
		return err
	}
	if int(table.RecordCount) < *flagMinRecords {
		return fmt.Errorf("too few records (%d)", table.RecordCount)
	}

	ddl, err := table.SQLDDL(*flagTableName)
	if err != nil {
		return err
	}

	if *flagVerbose {
		fmt.Println(ddl)
	}
	if _, err = db.Exec(ddl); err != nil {
		return err
	}

	prepped, err := db.Preparex(table.InsertSQL(*flagTableName))
	if err != nil {
		return err
	}

	for i := 0; i < int(table.RecordCount); i++ {
		r, err := table.Get(i)
		if err != nil {
			return err
		}

		values := make([]interface{}, 0, len(table.Columns))
		for _, column := range table.Columns {
			var value interface{} = r[column.Name]
			if !reflect.ValueOf(value).IsValid() {
				value = nil
			}
			if dur, ok := value.(time.Duration); ok {
				value = durToSQL(dur)
			}
			values = append(values, value)
		}
		if _, err = prepped.Exec(values...); err != nil {
			fmt.Println("insert error")
			spew.Dump(r)
			return err
		}
	}
	log.Println(*flagTableName, table.RecordCount, "rows inserted")

	return nil
}