//Generates SQL for an AliasedExpression (e.g. I("a").As("b") -> "a" AS "b")
358664
func (me *DefaultAdapter) AliasedExpressionSql(buf *SqlBuilder, aliased AliasedExpression) error {
	if err := me.Literal(buf, aliased.Aliased()); err != nil {
		return err
	}
	buf.Write(me.AsFragment)
	return me.Literal(buf, aliased.GetAs())
}
1144112
func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
	return engine.SQL(querystring, args...)
}
885619
func alias(a, b *decimal.Big) *decimal.Big {
	if a != b {
		return a
	}
	return decimal.WithContext(a.Context)
}
481450
func (me *Dataset) As(alias string) *Dataset {
	ret := me.copy()
	ret.clauses.Alias = I(alias)
	return ret
}
603314
func (session *Session) Sql(query string, args ...interface{}) *Session {
	return session.SQL(query, args...)
}
559709
func (ar AliasesResult) IndicesByAlias(aliasName string) []string {
	var indices []string
	for indexName, indexInfo := range ar.Indices {
		for _, aliasInfo := range indexInfo.Aliases {
			if aliasInfo.AliasName == aliasName {
				indices = append(indices, indexName)
			}
		}
	}
	return indices
}
193860
func (s *IndicesRolloverService) Alias(alias string) *IndicesRolloverService {
	s.alias = alias
	return s
}
937244
func parseExpression(expr string) (node ast.ExprNode, err error) {
	expr = fmt.Sprintf("select %s", expr)
	charset, collation := charset.GetDefaultCharsetAndCollate()
	stmts, _, err := parser.New().Parse(expr, charset, collation)
	if err == nil {
		node = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
	}
	return node, util.SyntaxError(err)
}
842444
func (d *Dataset) sql(table, dbType string) *Exportable {
	b := newBuffer()

	tableSQL, columnTypes, columnValues := d.createTable(table, dbType)
	b.WriteString(tableSQL)

	reg, _ := regexp.Compile("[']")
	// inserts
	for i := range d.data {
		b.WriteString("INSERT INTO " + table + " VALUES(" + strconv.Itoa(i+1) + ", ")
		for j, col := range d.headers {
			asStr := d.asString(columnValues[col][i])
			if isStringColumn(columnTypes[col]) {
				b.WriteString("'" + reg.ReplaceAllString(asStr, "''") + "'")
			} else if strings.HasPrefix(columnTypes[col], "TIMESTAMP") {
				if dbType == typeMySQL {
					b.WriteString("CONVERT_TZ('" + asStr[:10] + " " + asStr[11:19] + "', '" + asStr[len(asStr)-6:] + "', 'SYSTEM')")
				} else {
					b.WriteString("'" + asStr + "'") // simpler with Postgres
				}
			} else {
				b.WriteString(asStr)
			}
			if j < len(d.headers)-1 {
				b.WriteString(", ")
			}
		}
		b.WriteString(");\n")
	}
	b.WriteString("\nCOMMIT;\n")

	return newExportable(b)
}
131106
func (m *Sqlbridge) parseDescribe() (SqlStatement, error) {

	req := &SqlDescribe{Raw: m.l.RawInput()}
	req.Tok = m.Cur()
	m.Next() // Consume Describe

	//u.Debugf("token:  %v", m.Cur())
	switch nextWord := strings.ToLower(m.Cur().V); nextWord {
	case "select":
		// TODO:  make the lexer handle this
		sqlText := strings.Replace(m.l.RawInput(), req.Tok.V, "", 1)
		sqlSel, err := ParseSql(sqlText)
		if err != nil {
			return nil, err
		}
		req.Stmt = sqlSel
		return req, nil
	case "extended":
		sqlText := strings.Replace(m.l.RawInput(), req.Tok.V, "", 1)
		sqlText = strings.Replace(sqlText, m.Cur().V, "", 1)
		sqlSel, err := ParseSql(sqlText)
		if err != nil {
			return nil, err
		}
		req.Stmt = sqlSel
		return req, nil
	default:
		if lex.TokenIdentity == m.Cur().T {
			req.Identity = m.Cur().V
		} else {
			return nil, m.ErrMsg("expected idenity")
		}

	}

	return req, nil
}
454145
func (me *DefaultAdapter) OrderedExpressionSql(buf *SqlBuilder, order OrderedExpression) error {
	if err := me.Literal(buf, order.SortExpression()); err != nil {
		return err
	}
	if order.Direction() == SORT_DESC {
		buf.Write(me.DescFragment)
	} else {
		buf.Write(me.AscFragment)
	}
	switch order.NullSortType() {
	case NULLS_FIRST:
		buf.Write(me.NullsFirstFragment)
	case NULLS_LAST:
		buf.Write(me.NullsLastFragment)
	}
	return nil
}
45110
func (qb *TiDBQueryBuilder) Subquery(sub string, alias string) string {
	return fmt.Sprintf("(%s) AS %s", sub, alias)
}
466198
func (pq *ParsedQuery) GenerateQuery(bindVariables map[string]*querypb.BindVariable, extras map[string]Encodable) (string, error) {
	if len(pq.bindLocations) == 0 {
		return pq.Query, nil
	}
	var buf strings.Builder
	buf.Grow(len(pq.Query))
	if err := pq.Append(&buf, bindVariables, extras); err != nil {
		return "", err
	}
	return buf.String(), nil
}
261850
func Alias(d string) {
	if r, ok := relationalModelDefinition(false); ok {
		r.Alias = d
	} else if f, ok := relationalFieldDefinition(false); ok {
		f.DatabaseFieldName = d
	}
}
1301577
func (engine *Engine) Alias(alias string) *Session {
	session := engine.NewSession()
	session.IsAutoClose = true
	return session.Alias(alias)
}
1153532
func (me *DefaultAdapter) CastExpressionSql(buf *SqlBuilder, cast CastExpression) error {
	buf.WriteString("CAST")
	buf.WriteRune(left_paren_rune)
	if err := me.Literal(buf, cast.Casted()); err != nil {
		return err
	}
	buf.Write(me.AsFragment)
	if err := me.Literal(buf, cast.Type()); err != nil {
		return err
	}
	buf.WriteRune(right_paren_rune)
	return nil
}
79467
func (me *DefaultAdapter) BooleanExpressionSql(buf *SqlBuilder, operator BooleanExpression) error {
	buf.WriteRune(left_paren_rune)
	if err := me.Literal(buf, operator.Lhs()); err != nil {
		return err
	}
	buf.WriteRune(space_rune)
	operatorOp := operator.Op()
	if operator.Rhs() == nil {
		switch operatorOp {
		case EQ_OP:
			operatorOp = IS_OP
		case NEQ_OP:
			operatorOp = IS_NOT_OP
		}
	}
	if val, ok := me.BooleanOperatorLookup[operatorOp]; ok {
		buf.Write(val)
	} else {
		return NewGoquError("Boolean operator %+v not supported", operatorOp)
	}
	rhs := operator.Rhs()
	if (operatorOp == IS_OP || operatorOp == IS_NOT_OP) && me.UseLiteralIsBools {
		if rhs == true {
			rhs = L("TRUE")
		} else if rhs == false {
			rhs = L("FALSE")
		}
	}
	buf.WriteRune(space_rune)
	if err := me.Literal(buf, rhs); err != nil {
		return err
	}
	buf.WriteRune(right_paren_rune)
	return nil
}