Пример #1
0
func parseSQLFromArgs(ctx *cli.Context) *sqlcommand.SQLCommand {
	var sqlText string
	if !ctx.IsSet("sql") {
		//Trying from stdin
		fi, err := os.Stdin.Stat()
		if err != nil {
			panic(err)
		}
		if fi.Mode()&os.ModeNamedPipe == 0 {
			return nil
		}
		bio := bufio.NewReader(os.Stdin)
		sqlText, err = bio.ReadString(0)
		if err != nil && err != io.EOF {
			panic(err)
		}
	} else {
		sqlText = ctx.String("sql")
	}
	//Prepare parameters
	colParams, e := parameters.GetInstance().All()
	if e != nil {
		panic(e)
	}
	params := colParams.Get()
	paramsArgs := ctx.StringSlice("param")
	for i := 0; i < len(paramsArgs); i++ {

		newparam, e := paramsreplace.Replace(paramsArgs[i], params)
		if e != nil {
			logger.Error.Println(e)
		} else {
			paramsArgs[i] = newparam
		}
	}
	return sqlcommand.New(sqlText, paramsArgs)
}
Пример #2
0
Файл: run.go Проект: arteev/dsql
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
		}

	}()
	return nil
}