当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.GetDef方法代码示例

本文整理汇总了Golang中github.com/arteev/dsql/rdb/action.Context.GetDef方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.GetDef方法的具体用法?Golang Context.GetDef怎么用?Golang Context.GetDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/arteev/dsql/rdb/action.Context的用法示例。


在下文中一共展示了Context.GetDef方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: SelectError

//SelectError trigger error for select action
func SelectError(dbs []db.Database, ctx *action.Context) error {

	logger.Trace.Println("Failed execute")
	if !ctx.GetDef("silent", false).(bool) {
		fmt.Println("All requests will fail.")
	}
	switch ctx.Get("format") {
	case "json", "xml":
		return SelectAfter(dbs, ctx)
	}

	done := ctx.Get("chandone")
	if done != nil {
		done.(chan bool) <- true
	}
	return nil
}
开发者ID:arteev,项目名称:dsql,代码行数:18,代码来源:select.go

示例2: Exec

//Exec - execute sql for the databases
func Exec(dbContext db.Database, dbHandle *sql.DB, cmd *sqlcommand.SQLCommand, ctx *action.Context) error {
	logger.Trace.Println("run exec", dbContext.Code)
	defer logger.Trace.Println("done exec", dbContext.Code)

	localCtx := ctx.Get("context" + dbContext.Code).(*action.Context)
	commit := ctx.GetDef("commit", false).(bool)
	var pint []interface{}
	for _, p := range cmd.Params {
		pint = append(pint, p)
	}

	tx, err := dbHandle.Begin()
	if err != nil {
		return err
	}
	sqmt, err := tx.Prepare(cmd.Script)
	if err != nil {
		return err
	}
	defer sqmt.Close()
	res, err := sqmt.Exec(pint...)
	if err != nil {
		return err
	}
	defer sqmt.Close()

	if commit {
		logger.Debug.Printf("%s Transaction commited", dbContext.Code)
		if err := tx.Commit(); err != nil {
			return err
		}
	} else {
		if err := tx.Rollback(); err != nil {
			return err
		}
		logger.Warn.Printf("%s: Transaction rollback.Use a special flag for commit the transaction\n", dbContext.Code)
	}
	ra, err := res.RowsAffected()
	if err != nil {
		return err
	}

	localCtx.IncInt64("rowsaffected", ra)
	ctx.IncInt64("rowsaffected", ra)
	return nil
}
开发者ID:arteev,项目名称:dsql,代码行数:47,代码来源:exec.go

示例3: doOutputXML

func doOutputXML(dbs []db.Database, ctx *action.Context) error {
	datasets := ctx.Get("datasets").(*dataset.CollectionDataset)
	if err := fillDatasetsByErrors(datasets, dbs, ctx); err != nil {
		return err
	}
	subformat := ctx.GetDef("subformat", "").(string)
	if subformat == "" {
		datasets.WriteXML(os.Stdout)
		return nil
	}
	f, err := os.Create(subformat)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = datasets.WriteXML(f)
	return err
}
开发者ID:arteev,项目名称:dsql,代码行数:18,代码来源:select.go

示例4: SelectBefore

//SelectBefore trigger before for select action
func SelectBefore(dbs []db.Database, ctx *action.Context) error {
	// Prepare data in ctx.datasets
	logger.Trace.Println("SelectBefore")
	format := ctx.Get("format")
	subformat := ctx.GetDef("subformat", "").(string)

	datasets := dataset.NewColllection()
	chanHdr := make(chanHeader)
	chandata := make(chanData)
	chanDone := make(chan bool)

	ctx.Set("chanheader", chanHdr)
	ctx.Set("chandata", chandata)
	ctx.Set("chandone", chanDone)
	ctx.Set("datasets", datasets)

	line := 0
	go func() {
		for {
			select {
			case hdr := <-chanHdr:
				ds := datasets.GetOrCreateDataset(hdr.Code)
				ds.AddColumns(hdr.Columns...)
			case cudata := <-chandata:
				line++
				ds := datasets.GetOrCreateDataset(cudata.Code)
				ds.Append(cudata.Data)
				if format == "raw" {
					fmt.Println(formatRaw(subformat, ds.RowsCount(), line, ds.GetColumnsNames(), cudata.Data))
				}
			case <-chanDone:
				logger.Trace.Println("SelectBefore do done")
				return
			}
		}
	}()

	return nil
}
开发者ID:arteev,项目名称:dsql,代码行数:40,代码来源:select.go

示例5: PrintStatistic

//PrintStatistic - print common statistic after execute action
func PrintStatistic(dbs []db.Database, ctx *action.Context) error {
	var buf bytes.Buffer
	pget := ctx.Get("params").(parametergetter.ParameterGetter)
	if pget.GetDef(parametergetter.Statistic, false).(bool) {
		mSec := ctx.Snap.Finished().Sub(ctx.Snap.Started()).Nanoseconds() / 1000 / 1000
		exec := ctx.GetDef("exec", 0).(int)
		success := ctx.GetDef("success", 0).(int)
		failed := ctx.GetDef("failed", 0).(int)
		buf.WriteString(fmt.Sprintf("Executed: %-4d Success:%-3d (%3.2f%%) Failed:%-3d \n", exec, success, float64(success)/float64(exec)*100, failed))
		buf.WriteString(fmt.Sprintf("Completed: %v msec", mSec))
		fmt.Println(buf.String())
	}
	return nil
}
开发者ID:arteev,项目名称:dsql,代码行数:15,代码来源:outstatistic.go

示例6: runItem

func runItem(d db.Database, s *sqlcommand.SQLCommand, doaction action.Actioner, ctx *action.Context, pget parametergetter.ParameterGetter) error {
	logger.Trace.Println("runItem")
	defer logger.Trace.Println(d.Code, "runItem done")
	if s != nil {
		logger.Trace.Println(d.Code, s.Script)
	}
	wg.Add(1)
	ctx.IncInt("exec", 1)
	params := ctx.Get("Params").([]parameters.Parameter)

	go func() {

		timeout := ctx.GetDef("timeout", 0).(int)
		defer wg.Done()

		var (
			ctxExec context.Context
			cancel  context.CancelFunc
		)
		ch := make(chan bool)
		if timeout > 0 {
			ctxExec, cancel = context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
		} else {
			ctxExec, cancel = context.WithCancel(context.Background())
		}

		defer cancel()
		localCtx := action.NewContext()

		go func() {

			defer func() {
				ch <- true
				close(ch)
			}()

			ctx.Set("context"+d.Code, localCtx)
			ctx.Set("iscancel", ch)
			localCtx.Snap.Start()
			localCtx.Set("success", false)

			connectionString, e := paramsreplace.Replace(d.ConnectionString, params)
			if e != nil {
				ctx.IncInt("failed", 1)
				logger.Error.Println(e)

				return
			}
			logger.Debug.Println(d.Code, "Connection string:", connectionString)
			connection, err := rdb.Open(d.Engine, connectionString)
			if err != nil {
				ctx.IncInt("failed", 1)
				logger.Error.Println(d.Code, err)
				return
			}
			defer func() {
				if err := connection.Close(); err != nil {
					panic(err)
				} else {
					logger.Trace.Printf("%s disconnected", d.Code)
				}
			}()

			err = doaction(d, connection, s, ctx)
			if err != nil {
				if err.Error() != "cancel" {
					ctx.IncInt("failed", 1)
					localCtx.Snap.Done(err)
					logger.Error.Println(d.Code, err)
					if !ctx.GetDef("silent", false).(bool) {
						fmt.Fprintf(os.Stdout, "%s: %s\n", d.Code, strings.Replace(err.Error(), "\n", " ", -1))
					}
				}

				return
			}

			localCtx.Set("success", true)
			ctx.IncInt("success", 1)
			localCtx.Snap.Done(nil)
			runtime.Gosched()
		}()

		select {
		case <-ch:
			logger.Trace.Println("operation done w/o timeout")
			return
		case <-ctxExec.Done():
			err := ctxExec.Err()
			logger.Trace.Printf("operation done: %s\n", err)

			ctx.IncInt("failed", 1)
			localCtx.Snap.Done(err)
			logger.Error.Println(d.Code, err)

			//	ch <- true

			return
		}

//.........这里部分代码省略.........
开发者ID:arteev,项目名称:dsql,代码行数:101,代码来源:run.go

示例7: Select

//Select - it action for select command
func Select(dbs1 db.Database, dsrc *sql.DB, cmd *sqlcommand.SQLCommand, ctx *action.Context) error {
	logger.Trace.Println("run select", dbs1.Code)

	timeout := ctx.GetDef("timeout", 0).(int)
	logger.Debug.Printf("run select timeout %d sec", timeout)

	var pint []interface{}

	chanCancel := ctx.Get("iscancel").(chan bool)
	for _, p := range cmd.Params {
		pint = append(pint, p)
	}

	/*tx,err:=dsrc.Begin()
	if err!=nil {
		return err
	}
	defer tx.Rollback()
	rw, err := tx.Query(cmd.Script, pint...)*/
	tx, err := dsrc.Begin()
	if err != nil {
		return err
	}
	defer tx.Rollback()
	stmt, err := tx.Prepare(cmd.Script)
	if err != nil {
		return err
	}
	defer stmt.Close()

	rw, err := tx.Query(cmd.Script, pint...)
	if err != nil {
		return err
	}

	defer func() {
		if err := rw.Close(); err != nil {
			panic(err)
		}
	}()

	cols, _ := rw.Columns()

	localCtx := ctx.Get("context" + dbs1.Code).(*action.Context)
	chanHdr := ctx.Get("chanheader")
	chandata := ctx.Get("chandata")
	if chanHdr != nil {
		chanHdr.(chanHeader) <- &headerTable{
			Code:    dbs1.Code,
			Columns: cols,
		}
	}

	rg := rowgetter.MustRowGetter(rw)
	for {
		select {
		case <-chanCancel:
			logger.Info.Println("run select canceled", dbs1.Code)
			return fmt.Errorf("cancel")
		default:
		}
		row, ok := rg.Next()
		if !ok {
			break
		}

		localCtx.IncInt("rowcount", 1)

		if /*format=="table" &&*/ chandata != nil {
			data := make(map[string]interface{})
			for i, r := range row {
				data[cols[i]] = r
			}
			data["_CODE_"] = dbs1.Code

			chandata.(chanData) <- &dataTable{
				Code: dbs1.Code,
				Data: data,
			}
		}

	}

	return nil
}
开发者ID:arteev,项目名称:dsql,代码行数:86,代码来源:select.go

示例8: doOutputTable

func doOutputTable(dbs []db.Database, ctx *action.Context) error {
	datasets := ctx.Get("datasets").(*dataset.CollectionDataset)
	table := fmttab.New("", fmttab.BorderThin, nil)
	table.AddColumn("_CODE_", 10, fmttab.AlignLeft)
	ctx.Set("table", table)

	for _, col := range datasets.GetUniqueColumnsNames() {
		table.AddColumn(col, 15, fmttab.AlignLeft)
	}
	for _, ds := range datasets.GetDatasets() {
		for _, row := range ds.Rows {
			table.AppendData(row.GetDataMap())
		}
	}

	pget := ctx.Get("params").(parametergetter.ParameterGetter)

	if pget.GetDef(parametergetter.AutoFitWidthColumns, true).(bool) {
		//todo: move into fmttab
		cols := table.Columns.ColumnsVisible()
		for c, col := range cols {
			max := utf8.RuneCountInString(col.Name)
			for i := 0; i < len(table.Data); i++ {
				val, ok := table.Data[i][col.Name]

				if ok && val != nil {
					fval := fmt.Sprintf("%v", val)
					l := utf8.RuneCountInString(fval)
					if l > max {
						max = l
					}
				}
			}
			if max != 0 {
				cols[c].Width = max
			}
		}
	}

	switch pget.GetDef(parametergetter.BorderTable, "").(string) {
	case "Thin":
		table.SetBorder(fmttab.BorderThin)
	case "Double":
		table.SetBorder(fmttab.BorderDouble)
	case "None":
		table.SetBorder(fmttab.BorderNone)
	case "Simple":
		table.SetBorder(fmttab.BorderSimple)
	}

	tabex.SetTableSubFormat(table, ctx.GetDef("subformat", "").(string))

	if pget.GetDef(parametergetter.Fit, true).(bool) {

		if e := termbox.Init(); e != nil {
			return e
		}
		tw, _ := termbox.Size()
		table.AutoSize(true, tw)
		termbox.Close()
	}

	if _, err := table.WriteTo(os.Stdout); err != nil {
		return err
	}
	return nil
}
开发者ID:arteev,项目名称:dsql,代码行数:67,代码来源:select.go


注:本文中的github.com/arteev/dsql/rdb/action.Context.GetDef方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。