如何从带有squeryl的表中删除所有记录?(How can I delete all records from a table with squeryl?)

编程入门 行业动态 更新时间:2024-10-27 17:12:11
如何从带有squeryl的表中删除所有记录?(How can I delete all records from a table with squeryl?)

我已经研究了如何使用squeryl删除表中的所有记录。 我唯一能想到的就是

myTable.deleteWhere(r => r.id.isNotNull) //id is the primary key

这看起来很奇怪,也可能效率低下。

使用squeryl时从表中删除所有记录的最简洁方法是什么?

I've looked at ways to use squeryl to delete all records from a table. The only thing I could come up with is

myTable.deleteWhere(r => r.id.isNotNull) //id is the primary key

This seems strange and possibly inefficient.

What is the cleanest way to delete all records from a table when using squeryl?

最满意答案

deleteWhere子句采用任何逻辑布尔值,因此您可以简单地说:

myTable.deleteWhere(r => 1 === 1)

哪个应输出声明:

DELETE FROM mytable WHERE 1 = 1

如果要自动消除where子句,可以尝试:

myTable.deleteWhere(r => 1 === 1.inhibitWhen(true))

哪个应该完全压制where子句。

如果您正在寻找一种更有效的方法,并且您的数据库支持TRUNCATE或其他等效函数,您可以从org.squeryl.Session获取java.sql.Connection并直接通过JDBC发出查询。 不幸的是,这将失去一些Squeryl提供的类型安全性。

The deleteWhere clause takes any logical boolean, so you could simply say:

myTable.deleteWhere(r => 1 === 1)

Which should output the statement:

DELETE FROM mytable WHERE 1 = 1

If you want to eliminate the where clause automatically, you could try:

myTable.deleteWhere(r => 1 === 1.inhibitWhen(true))

Which should suppress the where clause altogether.

If you are looking for an even more efficient method and your database supports TRUNCATE or another equivalent function, you can get a java.sql.Connection from org.squeryl.Session and issue the query directly through JDBC. Unfortunately, this would lose some of the type safety Squeryl offers.

更多推荐

本文发布于:2023-08-06 03:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:delete   squeryl   table   records

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!