Пример #1
0
func init() {
	flag.Parse()
	if *VerboseTests {
		u.SetupLogging("debug")
		u.SetColorOutput()
	}

	expr.FuncAdd("eq", Eq)
	expr.FuncAdd("toint", ToInt)
	expr.FuncAdd("yy", Yy)
}
Пример #2
0
func main() {

	if sqlText == "" {
		u.Errorf("You must provide a valid select query in argument:    --sql=\"select ...\"")
		return
	}

	// load all of our built-in functions
	builtins.LoadAllBuiltins()

	// Add a custom function to the VM to make available to SQL language
	expr.FuncAdd("email_is_valid", EmailIsValid)

	// Our file source of csv's is stdin
	stdIn, err := os.Open("/dev/stdin")
	if err != nil {
		u.Errorf("could not open stdin? %v", err)
		return
	}

	// We are registering the "csv" datasource, to show that
	// the backend/sources can be easily created/added.  This csv
	// reader is an example datasource that is very, very simple.
	exit := make(chan bool)
	src, _ := datasource.NewCsvSource("stdin", 0, stdIn, exit)
	datasource.Register("csv", src)

	db, err := sql.Open("qlbridge", "csv:///dev/stdin")
	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

	rows, err := db.Query(sqlText)
	if err != nil {
		u.Errorf("could not execute query: %v", err)
		return
	}
	defer rows.Close()
	cols, _ := rows.Columns()

	// this is just stupid hijinx for getting pointers for unknown len columns
	readCols := make([]interface{}, len(cols))
	writeCols := make([]string, len(cols))
	for i, _ := range writeCols {
		readCols[i] = &writeCols[i]
	}
	fmt.Printf("\n\nScanning through CSV: (%v)\n\n", strings.Join(cols, ","))
	for rows.Next() {
		rows.Scan(readCols...)
		fmt.Println(strings.Join(writeCols, ", "))
	}
	fmt.Println("")
}
Пример #3
0
func LoadAllBuiltins() {
	expr.FuncAdd("gt", Gt)
	expr.FuncAdd("ge", Ge)
	expr.FuncAdd("ne", Ne)
	expr.FuncAdd("le", LeFunc)
	expr.FuncAdd("lt", LtFunc)
	expr.FuncAdd("not", NotFunc)
	expr.FuncAdd("eq", Eq)
	expr.FuncAdd("exists", Exists)
	expr.FuncAdd("now", Now)
	expr.FuncAdd("yy", Yy)
	expr.FuncAdd("yymm", YyMm)
	expr.FuncAdd("mm", Mm)
	expr.FuncAdd("monthofyear", Mm)
	expr.FuncAdd("dayofweek", DayOfWeek)
	//expr.FuncAdd("hod", HourOfDay)
	expr.FuncAdd("hourofday", HourOfDay)
	expr.FuncAdd("hourofweek", HourOfWeek)
	expr.FuncAdd("totimestamp", ToTimestamp)
	expr.FuncAdd("todate", ToDate)

	expr.FuncAdd("contains", ContainsFunc)
	expr.FuncAdd("tolower", Lower)
	expr.FuncAdd("toint", ToInt)
	expr.FuncAdd("split", SplitFunc)
	expr.FuncAdd("join", JoinFunc)
	expr.FuncAdd("oneof", OneOfFunc)
	expr.FuncAdd("any", AnyFunc)
	expr.FuncAdd("email", EmailFunc)
	expr.FuncAdd("emaildomain", EmailDomainFunc)
	expr.FuncAdd("emailname", EmailNameFunc)
	expr.FuncAdd("host", HostFunc)
	expr.FuncAdd("path", UrlPath)
	expr.FuncAdd("qs", Qs)
}
Пример #4
0
func LoadAllBuiltins() {
	expr.FuncAdd("gt", Gt)
	expr.FuncAdd("ge", Ge)
	expr.FuncAdd("ne", Ne)
	expr.FuncAdd("le", LeFunc)
	expr.FuncAdd("lt", LtFunc)
	expr.FuncAdd("not", NotFunc)
	expr.FuncAdd("eq", Eq)
	expr.FuncAdd("exists", Exists)
	expr.FuncAdd("map", MapFunc)
	expr.FuncAdd("now", Now)
	expr.FuncAdd("yy", Yy)
	expr.FuncAdd("yymm", YyMm)
	expr.FuncAdd("mm", Mm)
	expr.FuncAdd("monthofyear", Mm)
	expr.FuncAdd("dayofweek", DayOfWeek)
	//expr.FuncAdd("hod", HourOfDay)
	expr.FuncAdd("hourofday", HourOfDay)
	expr.FuncAdd("hourofweek", HourOfWeek)
	expr.FuncAdd("totimestamp", ToTimestamp)
	expr.FuncAdd("todate", ToDate)
	expr.FuncAdd("seconds", TimeSeconds)
	expr.FuncAdd("uuid", UuidGenerate)
	expr.FuncAdd("contains", ContainsFunc)
	expr.FuncAdd("tolower", Lower)
	expr.FuncAdd("toint", ToInt)
	expr.FuncAdd("tonumber", ToNumber)
	expr.FuncAdd("split", SplitFunc)
	expr.FuncAdd("replace", Replace)
	expr.FuncAdd("join", JoinFunc)
	expr.FuncAdd("oneof", OneOfFunc)
	expr.FuncAdd("match", Match)
	expr.FuncAdd("any", AnyFunc)
	expr.FuncAdd("all", AllFunc)
	expr.FuncAdd("email", EmailFunc)
	expr.FuncAdd("emaildomain", EmailDomainFunc)
	expr.FuncAdd("emailname", EmailNameFunc)
	expr.FuncAdd("host", HostFunc)
	expr.FuncAdd("path", UrlPath)
	expr.FuncAdd("qs", Qs)
	expr.FuncAdd("urlmain", UrlMain)
	expr.FuncAdd("urlminusqs", UrlMinusQs)
	expr.FuncAdd("urldecode", UrlDecode)
	expr.FuncAdd("extract", TimeExtractFunc)
}
Пример #5
0
func LoadAllBuiltins() {
	loadOnce.Do(func() {

		// math
		expr.FuncAdd("sqrt", SqrtFunc)
		expr.FuncAdd("pow", PowFunc)

		// agregate ops
		expr.AggFuncAdd("count", CountFunc)
		expr.AggFuncAdd("avg", AvgFunc)
		expr.AggFuncAdd("sum", SumFunc)

		// logical
		expr.FuncAdd("gt", Gt)
		expr.FuncAdd("ge", Ge)
		expr.FuncAdd("ne", Ne)
		expr.FuncAdd("le", LeFunc)
		expr.FuncAdd("lt", LtFunc)
		expr.FuncAdd("not", NotFunc)
		expr.FuncAdd("eq", Eq)
		expr.FuncAdd("exists", Exists)
		expr.FuncAdd("map", MapFunc)

		// Date/Time functions
		expr.FuncAdd("now", Now)
		expr.FuncAdd("yy", Yy)
		expr.FuncAdd("yymm", YyMm)
		expr.FuncAdd("mm", Mm)
		expr.FuncAdd("monthofyear", Mm)
		expr.FuncAdd("dayofweek", DayOfWeek)
		expr.FuncAdd("hourofday", HourOfDay)
		expr.FuncAdd("hourofweek", HourOfWeek)
		expr.FuncAdd("totimestamp", ToTimestamp)
		expr.FuncAdd("todate", ToDate)
		expr.FuncAdd("seconds", TimeSeconds)

		// String Functions
		expr.FuncAdd("contains", ContainsFunc)
		expr.FuncAdd("tolower", Lower)
		expr.FuncAdd("toint", ToInt)
		expr.FuncAdd("tonumber", ToNumber)
		expr.FuncAdd("uuid", UuidGenerate)
		expr.FuncAdd("split", SplitFunc)
		expr.FuncAdd("replace", Replace)
		expr.FuncAdd("join", JoinFunc)

		// array, string
		expr.FuncAdd("len", LengthFunc)

		// selection
		expr.FuncAdd("oneof", OneOfFunc)
		expr.FuncAdd("match", Match)
		expr.FuncAdd("any", AnyFunc)
		expr.FuncAdd("all", AllFunc)
		expr.FuncAdd("filter", FilterFunc)

		// special items
		expr.FuncAdd("email", EmailFunc)
		expr.FuncAdd("emaildomain", EmailDomainFunc)
		expr.FuncAdd("emailname", EmailNameFunc)
		expr.FuncAdd("domain", DomainFunc)
		expr.FuncAdd("domains", DomainsFunc)
		expr.FuncAdd("host", HostFunc)
		expr.FuncAdd("hosts", HostsFunc)
		expr.FuncAdd("path", UrlPath)
		expr.FuncAdd("qs", Qs)
		expr.FuncAdd("urlmain", UrlMain)
		expr.FuncAdd("urlminusqs", UrlMinusQs)
		expr.FuncAdd("urldecode", UrlDecode)
		expr.FuncAdd("extract", TimeExtractFunc)

		// Hashing functions
		expr.FuncAdd("hash.md5", HashMd5Func)
		expr.FuncAdd("hash.sha1", HashSha1Func)
		expr.FuncAdd("hash.sha256", HashSha256Func)

		// MySQL Builtins
		expr.FuncAdd("cast", CastFunc)
		expr.FuncAdd("char_length", LengthFunc)
	})
}