LACASSA

Cassandra based query builder for laravel

Laravel Cassandra

A Query builder with support for Cassandra, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.

Table of contents

Installation
Configuration
Auth
Schema
Query Builder
Examples

Installation

Make sure you have the DataStax PHP Driver for Apache Cassandra installed. You can find installation instructions at https://github.com/datastax/php-driver or https://github.com/datastax/php-driver/blob/master/ext/README.md

datastax php-driver requires php version 5.6+
Installation using composer:

And add the service provider in config/app.php:

Configuration

Change your default database connection name in config/database.php:

And add a new cassandra connection:

Auth

You can use Laravel’s native Auth functionality for cassandra, make sure your config/auth.php looks like

 

Schema

The database driver also has (limited) schema builder support. You can easily create/delete tables.

CREATE table

DROP table

CQL data types supported

text(‘a’)
bigint(‘b’)
blob(‘c’)
boolean(‘d’)
counter(‘e’)
decimal(‘f’)
double(‘g’)
float(‘h’)
frozen(‘i’)
inet(‘j’)
nt(‘k’)
listCollection(‘l’, ‘text’)
mapCollection(‘m’, ‘timestamp’, ‘text’)
setCollection(‘n’, ‘int’)
timestamp(‘o’)
timeuuid(‘p’)
tuple(‘q’, ‘int’, ‘text’, ‘timestamp’)
uuid(‘r’)
varchar(‘s’)
varint(‘t’)
ascii(‘u’)

Primary Key

primary([‘a’, ‘b’])

Query Builder

The database driver plugs right into the original query builder. When using cassandra connections, you will be able to build fluent queries to perform database operations.

If you did not change your default database connection, you will need to specify it when querying.

Examples

Retrieving All Records

 

Indexing columns

CREATE INDEX creates a new index on the given table for the named column.

Selecting columns

Wheres
The WHERE clause specifies which rows to query. In the WHERE clause, refer to a column using the actual name, not an alias. Columns in the WHERE clause need to meet one of these requirements:

  • The partition key definition includes the column.
  • A column that is indexed using CREATE INDEX.

If the primary key is ‘id’, then

And Statements

Using Where In With An Array

Order By

ORDER BY clauses can select a single column only. Ordering can be done in ascending or descending order, default ascending, and specified with the ASC or DESC keywords. In the ORDER BY clause, refer to a column using the actual name, not the aliases.

Limit

We can use limit() and take() for limiting the query.

Distinct

Distinct requires a field for which to return the distinct values.

Distinct can be combined with where:

Count

Count can be combined with where:

Truncate

Filtering a collection set, list, or map

You can index the collection column, and then use the CONTAINS condition in the WHERE clause to filter the data for a particular value in the collection.

After indexing the collection keys in the venues map, you can filter on map keys.

Raw Query

The CQL expressions can be injected directly into the query.

Inserts, updates and deletes

Inserting, updating and deleting records works just like the original QB.
Insert

Inserting a collection set, list, and map

Insert collections in a row. The method will be like

Collection_type is any of set, list or map.

Column_name is the name of column to be inserted.

Value can be associative array for map type and array of string/number for list and set types.

Updating

To update a model, you may retrieve it, change an attribute, and use the update method.

Updating a collection set, list, and map

Update collections in a row. The method will be like

Collection_type is any of set, list or map.

Column_name is the name of column to be updated.

Operator is + or -, + for adding the values to collection and – to remove the value from collection.

Value can be associative array for map type and array of string/number for list and set types.

Deleting

To delete a model, simply call the delete method on the instance. We can delete the rows in a table by using deleteRow method:

We can also perform delete by the column in a table using deleteColumn method:

 

Let us help you tell your story

Start a Project