Exemplo n.º 1
0
Arquivo: cass.go Projeto: araddon/cass
/**
 * Executes a CQL (Cassandra Query Language) statement and returns a * CqlResult containing the results.
 *
 * Parameters:
 *  - Query
 *  - Compression  ["Compression_GZIP", "GZIP", "Compression_NONE", "NONE"]
 *  - ConsistencyLevel
 *
 * conn.QueryArgs(`Create table user (...)`, "NONE", false, cassandra.ConsistencyLevel_QUORUM)
 */
func (c *CassandraConnection) QueryArgs(cql, compression string, requireKeyspace bool, consistency cassandra.ConsistencyLevel) (rows [][]*cassandra.Column, err error) {

	if c.Client == nil {
		Logf(ERROR, "Nil Client")
		return nil, errors.New("No Client for cass conn")
	}
	if requireKeyspace {
		if err := c.RequireKeyspaceSet(); err != nil {
			return nil, err
		}
	}
	Debug(cql)
	ret, ire, ue, te, sde, err := c.Client.ExecuteCql3Query([]byte(cql), cassandra.FromCompressionString(compression), consistency)
	if ire != nil || ue != nil || te != nil || sde != nil || err != nil {
		if err != nil && strings.Contains(err.Error(), "Remote side has closed") {
			// Cannot read. Remote side has closed. Tried to read 4 bytes, but only got 0 bytes.
			Log(ERROR, "Trying to reopen for add/update ")
			c.Close()
			if err := c.Open(c.Keyspace); err != nil {
				return nil, fmt.Errorf("Failed to reopen: %s", err.Error())
			}
			ret, ire, ue, te, sde, err = c.Client.ExecuteCql3Query([]byte(cql), cassandra.FromCompressionString(compression), consistency)
			if ire != nil || ue != nil || te != nil || sde != nil || err != nil {
				err = CassandraError(fmt.Sprint("Error on Query ", ire, ue, te, sde, err))
				Log(ERROR, err.Error())
				return rows, err
			}
		} else {
			err = CassandraError(fmt.Sprint("Error on Query ", ire, ue, te, sde, err, cql))
			Log(ERROR, err.Error())
			return rows, err
		}
	}
	if ret != nil && ret.Rows != nil {
		rowCt := ret.Rows.Len()
		//Debug("RowCt = ", rowCt)
		rows = make([][]*cassandra.Column, rowCt)
		var cqlRow cassandra.CqlRow
		//cols := make([]cassandra.Column, rowCt)
		for i := 0; i < rowCt; i++ {
			cr := (ret.Rows.At(i)).(*cassandra.CqlRow)
			cqlRow = *cr
			//Debug(*cr)
			//Debug(cr.Key)
			cols := make([]*cassandra.Column, cqlRow.Columns.Len())
			for x := 0; x < cqlRow.Columns.Len(); x++ {
				cols[x] = cqlRow.Columns.At(x).(*cassandra.Column)
				//cols[x] = cqlRow.Columns.At(x).(cassandra.Column)
			}
			rows[i] = cols
			//*(ret.At(i).(*cassandra.ColumnOrSuperColumn)).Column
			//cols[i] = (ret.Rows.At(i)).(cassandra.Column)
		}
		return rows, nil
	}
	return rows, err
}
Exemplo n.º 2
0
/**
 * Executes a CQL (Cassandra Query Language) statement and returns a * CqlResult containing the results.
 *
 * Parameters:
 *  - Query
 *  - Compression
 */
func (c *CassandraConnection) Query(cql, compression string) (rows [][]*cassandra.Column, err error) {

	//ExecuteCqlQuery(query string, compression Compression)
	//  (retval474 *CqlResult, ire *InvalidRequestException, ue *UnavailableException, te *TimedOutException, sde *SchemaDisagreementException, err error)

	ret, ire, ue, te, sde, err := c.Client.ExecuteCqlQuery(cql, cassandra.FromCompressionString(compression))
	if ire != nil || ue != nil || te != nil || sde != nil || err != nil {
		err = CassandraError(fmt.Sprint("Error on Query ", ire, ue, te, sde, err))
		Log(ERROR, err.Error())
		return rows, err
	}

	if ret != nil && ret.Rows != nil {
		rowCt := ret.Rows.Len()
		//Debug("RowCt = ", rowCt)
		rows = make([][]*cassandra.Column, rowCt)
		var cqlRow cassandra.CqlRow
		//cols := make([]cassandra.Column, rowCt)
		for i := 0; i < rowCt; i++ {
			cr := (ret.Rows.At(i)).(*cassandra.CqlRow)
			cqlRow = *cr
			//Debug(*cr)
			//Debug(cr.Key)
			cols := make([]*cassandra.Column, cqlRow.Columns.Len())
			for x := 0; x < cqlRow.Columns.Len(); x++ {
				cols[x] = cqlRow.Columns.At(x).(*cassandra.Column)
				//cols[x] = cqlRow.Columns.At(x).(cassandra.Column)
			}
			rows[i] = cols
			//*(ret.At(i).(*cassandra.ColumnOrSuperColumn)).Column
			//cols[i] = (ret.Rows.At(i)).(cassandra.Column)
		}
		return rows, nil
	}
	return rows, err
}