Ejemplo n.º 1
0
func Truncate(session *gocql.Session, table cqlc.Table) {
	stmt := fmt.Sprintf("truncate %s", table.TableName())
	err := session.Query(stmt).Exec()

	if err != nil {
		log.Fatalf("Could not %s: %v", stmt, err)
		os.Exit(1)
	}
}
Ejemplo n.º 2
0
Archivo: cqlc.go Proyecto: qtabot/cqlc
func (c *Context) Exec(s *gocql.Session) error {
	stmt, placeHolders, err := BuildStatement(c)

	if err != nil {
		return err
	}

	if c.Debug {
		fmt.Printf("%+v bound to stmt: %s\n", placeHolders, stmt)
	}

	return s.Query(stmt, placeHolders...).Exec()
}
Ejemplo n.º 3
0
Archivo: cqlc.go Proyecto: qtabot/cqlc
// Returns true if the CAS operation was applied, false otherwise.
// If the operation was applied, then the result bindings will not be popluated.
func (c *Context) Swap(s *gocql.Session) (bool, error) {

	if len(c.CASBindings) == 0 {
		return false, ErrCASBindings
	}

	casPlaceHolders := make([]interface{}, len(c.CASBindings))
	for i, binding := range c.CASBindings {
		casPlaceHolders[i] = binding.Value
	}

	stmt, queryPlaceholders, err := BuildStatement(c)
	if err != nil {
		return false, err
	}

	return s.Query(stmt, queryPlaceholders...).ScanCAS(casPlaceHolders...)
}
Ejemplo n.º 4
0
Archivo: cqlc.go Proyecto: qtabot/cqlc
func (c *Context) Fetch(s *gocql.Session) (*gocql.Iter, error) {

	stmt, err := c.RenderCQL()
	if err != nil {
		return nil, err
	}

	// The reason why this is so dynamic is because of WHERE foo IN (?,?,?) clauses
	// The factoring for an IN clause is bad, since we are storing an array into the value
	// and using reflection to dig it out again
	// This should be more strongly typed

	placeHolders := make([]interface{}, 0)

	for _, cond := range c.Conditions {
		v := cond.Binding.Value

		switch reflect.TypeOf(v).Kind() {
		case reflect.Slice:
			{
				s := reflect.ValueOf(v)
				for i := 0; i < s.Len(); i++ {
					placeHolders = append(placeHolders, s.Index(i).Interface())
				}
			}
		case reflect.Array:
			{

				// Not really happy about having to special case UUIDs
				// but this works for now

				if val, ok := v.(gocql.UUID); ok {
					placeHolders = append(placeHolders, val.Bytes())
				} else {
					return nil, bindingErrorf("Cannot bind component: %+v (type: %s)", v, reflect.TypeOf(v))
				}
			}
		default:
			{
				placeHolders = append(placeHolders, &v)
			}
		}
	}

	c.Dispose()

	if c.Debug {
		fmt.Printf("%+v bound to stmt: %s\n", placeHolders, stmt)
	}

	iter := s.Query(stmt, placeHolders...).Iter()
	return iter, nil
}