adrianheras / lacassa
Lumen 5.7.x 数据库查询构建器的 Cassandra 支持
Requires
- datastax/php-driver: ^1.2
README
Lumen 5.7.x 数据库查询构建器的 Cassandra 支持
目录
-
先决条件
-
安装
-
查询构建器
-
模式
-
扩展
-
示例
先决条件
- Datastax PHP Driver for Apache Cassandra (https://github.com/datastax/php-driver)
- PHP 7.*
安装
使用 composer
composer require adrianheras/lumencassandra
在 bootstrap/app.php 文件的 "注册服务提供者" 中添加以下行
$app->register(Adrianheras\Lumencassandra\CassandraServiceProvider::class);
在 .env 文件中包含以下填写的数据
DB_CONNECTION=cassandra
DB_HOST=
DB_PORT=9042
DB_KEYSPACE=mykeyspace
DB_USERNAME=
DB_PASSWORD=
身份验证
您可以使用 Laravel 的原生身份验证功能对 Cassandra 进行操作,请确保您的 config/auth.php 文件如下所示
'providers' => [
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
'users' => [
'driver' => 'database',
'table' => 'users',
],
],
模式
在 Adrianheras\Lumencassandra\PrepareSchema 中准备模式
支持的 CQL 数据类型
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(['a', 'b'])
查询构建器
数据库驱动程序直接连接到原始查询构建器。当使用 Cassandra 连接时,您将能够构建流畅的查询以执行数据库操作。
$emp = DB::table('emp')->get();
$emp = DB::table('emp')->where('emp_name', 'Christy')->first();
如果您没有更改默认数据库连接,则在查询时需要指定它。
$emp = DB::connection('cassandra')->table('emp')->get();
示例
基本用法
检索所有记录
$emp = DB::table('emp')->all();
索引列
CREATE INDEX 在给定的表上为命名列创建一个新的索引。
DB::table('users')->index(['name']);
选择列
$emp = DB::table('emp')->where('emp_no', '>', 50)->select('emp_name', 'emp_no')->get();
$emp = DB::table('emp')->where('emp_no', '>', 50)->get(['emp_name', 'emp_no']);
WHERE 子句
WHERE 子句指定要查询的行。在 WHERE 子句中,使用实际名称引用列,而不是别名。WHERE 子句中的列需要满足以下要求之一
-
分区键定义包括该列。
-
使用 CREATE INDEX 索引的列。
$emp = DB::table('emp')->where('emp_no', '>', 50)->take(10)->get();
AND 语句
$emp = DB::table('emp')->where('emp_no', '>', 50)->where('emp_name', '=', 'Christy')->get();
使用数组与 WHERE IN 结合使用
$emp = DB::table('emp')->whereIn('emp_no', [12, 17, 21])->get();
ORDER BY
ORDER BY 子句只能选择一个列。排序可以是升序或降序,默认为升序,并使用 ASC 或 DESC 关键字指定。在 ORDER BY 子句中,使用实际名称引用列,而不是别名。
$emp = DB::table('emp')->where('emp_name','Christy')->orderBy('emp_no', 'desc')->get();
限制
我们可以使用 limit() 和 take() 限制查询。
$emp = DB::table('emp')->where('emp_no', '>', 50)->take(10)->get();
$emp = DB::table('emp')->where('emp_no', '>', 50)->limit(10)->get();
Distinct
Distinct 需要一个字段来返回不同的值。
$emp = DB::table('emp')->distinct()->get(['emp_id']);
Distinct 可以与 where 结合使用
$emp = DB::table('emp')->where('emp_sal', 45000)->distinct()->get(['emp_name']);
Count
$number = DB::table('emp')->count();
Count 可以与 where 结合使用
$sal = DB::table('emp')->where('emp_sal', 45000)->count();
Truncate
$sal = DB::table('emp')->truncate();
过滤集合集、列表或映射
您可以索引集合列,然后在 WHERE 子句中使用 CONTAINS 条件来过滤特定值的数据。
$emp = DB::table('emp')->where('emp_name','contains', 'Christy')->get();
在 索引 venues 映射中的集合键 之后,您可以过滤映射键。
$emp = DB::table(emp')->where('todo','contains key', '2014-10-02 06:30:00+0000')->get();
原始查询
可以将 CQL 表达式直接注入到查询中。
$emp = DB::raw('select * from emp');
插入、更新和删除
插入、更新和删除记录的工作方式与原始 QB 相同。
插入
DB::table('emp')->insert(['emp_id' => 11, 'emp_city' => '{"kochi", "tvm", "kollam"}', 'emp_name' => 'Christy', 'emp_phone' => 12345676890, 'emp_sal' => 500]);
更新
要更新模型,您可以检索它,更改一个属性,然后使用更新方法。
DB::table('emp')->where('emp_id', 11)->update(['emp_city' => 'kochi', 'emp_name' => 'Christy jos', 'emp_phone' => 1234567890]);
更新集合集、列表和映射
在单行中更新集合。该方法如下
updateCollection(collection_type, column_name, operator, value);
Collection_type 是 set、list 或 map 中的任何一种。
Column_name 是要更新的列的名称。
操作符是+或-,+用于将值添加到集合中,-用于从集合中移除值。
值可以是关联数组(map类型),也可以是字符串/数字数组(list和set类型)。
DB::table('users')->where('id', 1)->updateCollection('set', 'phn', '+', [123, 1234,12345])->update();
DB::table('users')->where('id', 1)->updateCollection('set', 'phn', '-', [123])->update();
DB::table('users')->where('id', 1)->updateCollection('list', 'hobbies', '+', ['reading', 'cooking', 'cycling'])->update();
DB::table('users')->where('id', 1)->updateCollection('list', 'hobbies', '-', ['cooking'])->update();
DB::table('users')->where('id', 1)->updateCollection('map', 'friends', '+', ['John' => 'Male', 'Rex' => 'Male'])->update();
DB::table('users')->where('id', 1)->updateCollection('map', 'friends', '-', ['John'])->update();
删除
要删除一个模型,只需在实例上调用delete方法。我们可以使用deleteRow方法来删除表中的行。
$emp = DB::table('emp')->where('emp_city', 'Kochi')->deleteRow();
我们还可以使用deleteColumn方法通过列来执行删除操作。
$emp = DB::table('emp')->where('emp_id', 3)->deleteColumn();