예제 #1
0
파일: crud.go 프로젝트: apaoww/gocqltable
func (t CRUD) Update(row interface{}) error {

	rowKeys := t.RowKeys()
	rangeKeys := t.RangeKeys()

	where := []string{}
	for _, key := range append(rowKeys, rangeKeys...) {
		where = append(where, strings.ToLower(fmt.Sprintf("%q", key))+" = ?")
	}

	m, ok := r.StructToMap(row)
	if !ok {
		panic("Unable to get map from struct during update")
	}

	ids := []interface{}{}
	set := []string{}
	vals := []interface{}{}

	for _, rowKey := range append(rowKeys, rangeKeys...) {
		for key, value := range m {
			if strings.ToLower(key) == strings.ToLower(rowKey) {
				ids = append(ids, value)
				break
			}
		}
	}

	for key, value := range m {
		isAKey := false
		for _, rowKey := range append(rowKeys, rangeKeys...) {
			if strings.ToLower(key) == strings.ToLower(rowKey) {
				isAKey = true
				break
			}
		}
		if !isAKey {
			set = append(set, strings.ToLower(fmt.Sprintf("%q", key))+" = ?")
			vals = append(vals, value)
		}
	}

	if len(ids) < len(rowKeys)+len(rangeKeys) {
		return errors.New(fmt.Sprintf("To few key-values to update row (%d of the required minimum of %d)", len(ids), len(rowKeys)+len(rangeKeys)))
	}

	err := t.Query(fmt.Sprintf(`UPDATE %q.%q SET %s WHERE %s`, t.Keyspace().Name(), t.Name(), strings.Join(set, ", "), strings.Join(where, " AND ")), append(vals, ids...)...).Exec()
	if err != nil {
		return err
	}
	return nil

}
예제 #2
0
파일: crud.go 프로젝트: apaoww/gocqltable
func (t CRUD) insert(row interface{}, ttl *time.Time) error {

	rowKeys := t.RowKeys()
	rangeKeys := t.RangeKeys()

	// TODO: Test and remove
	// where := []string{}
	// for _, key := range append(rowKeys, rangeKeys...) {
	// 	where = append(where, key+" = ?")
	// }

	m, ok := r.StructToMap(row)
	if !ok {
		panic("Unable to get map from struct during update")
	}

	fields := []string{}
	placeholders := []string{}
	vals := []interface{}{}
	for key, value := range m {
		// Check for empty row- or range keys
		for _, rowKey := range append(rowKeys, rangeKeys...) {
			if strings.ToLower(key) == strings.ToLower(rowKey) {
				if value == nil {
					return errors.New(fmt.Sprintf("Inserting row failed due to missing key value (for key %q)", rowKey))
				}
				break
			}
		}
		// Append to insertion slices
		fields = append(fields, strings.ToLower(fmt.Sprintf("%q", key)))
		placeholders = append(placeholders, "?")
		vals = append(vals, value)
	}

	options := ""
	if ttl != nil {
		options = "USING TTL ?"
		vals = append(vals, int(ttl.Sub(time.Now().UTC()).Seconds()+.5))
	}

	err := t.Query(fmt.Sprintf(`INSERT INTO %q.%q (%s) VALUES (%s) %s`, t.Keyspace().Name(), t.Name(), strings.Join(fields, ", "), strings.Join(placeholders, ", "), options), vals...).Exec()
	if err != nil {
		for _, v := range vals {
			log.Printf("%T %v", v, v)
		}
		return err
	}

	return nil

}
예제 #3
0
파일: table.go 프로젝트: apaoww/gocqltable
func (t Table) create(props ...string) error {

	if t.session == nil {
		t.session = defaultSession
	}

	rowKeys := t.RowKeys()
	rangeKeys := t.RangeKeys()

	pkString := "PRIMARY KEY ((" + strings.Join(rowKeys, ", ") + ")"
	if len(rangeKeys) > 0 {
		pkString = pkString + ", " + strings.Join(rangeKeys, ", ")
	}
	pkString = pkString + ")"

	fields := []string{}

	m, ok := r.StructToMap(t.Row())
	if !ok {
		panic("Unable to get map from struct during create table")
	}

	for key, value := range m {
		key = strings.ToLower(key)
		typ, err := stringTypeOf(value)
		if err != nil {
			return err
		}
		fields = append(fields, fmt.Sprintf(`%q %v`, key, typ))
	}

	// Add primary key value to fields list
	fields = append(fields, pkString)

	propertiesString := ""
	if len(props) > 0 {
		propertiesString = "WITH " + strings.Join(props, " AND ")
	}

	return t.session.Query(fmt.Sprintf(`CREATE TABLE %q.%q (%s) %s`, t.Keyspace().Name(), t.Name(), strings.Join(fields, ", "), propertiesString)).Exec()

}
예제 #4
0
파일: crud.go 프로젝트: apaoww/gocqltable
func (t CRUD) Delete(row interface{}) error {

	rowKeys := t.RowKeys()
	rangeKeys := t.RangeKeys()

	where := []string{}
	for _, key := range append(rowKeys, rangeKeys...) {
		where = append(where, strings.ToLower(fmt.Sprintf("%q", key))+" = ?")
	}

	m, ok := r.StructToMap(row)
	if !ok {
		panic("Unable to get map from struct during update")
	}

	ids := []interface{}{}
	for _, rowKey := range append(rowKeys, rangeKeys...) {
		for key, value := range m {
			if strings.ToLower(key) == strings.ToLower(rowKey) {
				ids = append(ids, value)
				break
			}
		}
	}

	if len(ids) < len(rowKeys)+len(rangeKeys) {
		return errors.New(fmt.Sprintf("To few key-values to delete row (%d of the required %d)", len(ids), len(rowKeys)+len(rangeKeys)))
	}

	err := t.Query(fmt.Sprintf(`DELETE FROM %q.%q WHERE %s`, t.Keyspace().Name(), t.Name(), strings.Join(where, " AND ")), ids...).Exec()
	if err != nil {
		return err
	}
	return nil

}