我一般会推荐他们使用 List list = fsql.Ado.Query("select * from t1"); 等类似的操作,IAdo下有大量的ADO.NET基础的调用操作。但开发者还想使用类似Page,Skip,OrderBy等方法。鉴于fsql可以生成SQL,可以将不同的SQL组合,实现更加复杂的功能。
public void ConfigureServices(IServiceCollection services)
{
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
.Build();
services.AddSingleton(fsql);
}
可使用以下方式实现
WithSql 自定义SQL
定义实体类
public class TestClass
{
[Column(Name = "ID", IsPrimary = true)]
public string No { get; set; }
public int? Age { get; set; }
public string Name { get; set; }
[Column(Name = "BIRTH_DAY")]
public DateTime? Birthday { get; set; }
public decimal Point { get; set; }
public Sex? Sex { get; set; }
}
public class TestClssDto
{
public string ID { get; set; }
public int? Age { get; set; }
}
不同的查询方式。
返回DataTable
返回List 即List<(string,string)>元组
返回List 且能支持分页
返回List且能支持分页
1.返回DataTable
DataTable dt1 = _fsql.Select
SELECT ID,Age
FROM(select * from TestClass ) a
2.返回DataTable
DataTable dt2 = _fsql.Select
SELECT *
FROM ( select * from TestClass ) a
3.返回List 即List<(string,string)> 元组
List<(string,string)> list1 = _fsql
.Select
SELECT ID, Age
FROM(select * from TestClass ) a
4.返回List
var list2 = _fsql.Select
SELECT *
FROM(select * from TestClass ) a
5.返回List 且能支持分页
var list3 = _fsql.Select()
.WithSql("select * from TestClass ")
.WhereIf(true, "1=1")
.Page(1, 10).OrderBy("ID DESC")
.ToList("ID,Age");
SELECT ID, Age
FROM(select * from TestClass ) a
WHERE(1 = 1)
ORDER BY ID DESC
limit 0,10
6.返回List且能支持分页
var list4 = _fsql.Select()
.WithSql("select * from TestClass ")
.WhereIf(true, "1=1")
.Page(1, 10)
.OrderBy("ID DESC")
.ToList("ID,Age");
SELECT ID, Age
FROM(select * from TestClass ) a
WHERE(1 = 1)
ORDER BY ID DESC
limit 0,10
通过 WithSql+ ToSQL实现 Union ALL 查询方法
1、二次 ISelect 查询:WithSql 使用多次,等于 UNION ALL 查询
WithSql 使用多次为 UNION ALL 查询,所以我们可以利用 ISelect.ToSql(FieldAliasOptions.AsProperty) 得到生成的 SQL,如下:
var sql1 = fsql.Select()
.Where(a => a.Title.Contains("xxx"))
.ToSql();
var sql2 = fsql.Select()
.Where(a => a.Title.Contains("yyy"))
.ToSql();
fsql.Select()
.WithSql(sql1)
.WithSql(sql2)
.ToList();
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%xxx%') ) a) ftb
UNION ALL
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%yyy%') ) a) ftb
select * from (SELECT a."Id" as1 FROM "table_1" a) ftb
UNION ALL
select * from (SELECT a."Id" as1 FROM "table_2" a) ftb
3、利用 ToSql 拼接新的 SQL,使用 IAdo 执行
var sql1 = fsql.Select()
.Where(a => a.Id > 100 && a.Id < 200)
.ToSql(a => new { a.Id, a.Title }, FieldAliasOptions.AsProperty);
var sql2 = fsql.Select()
.Where(a => a.Id > 1001 && a.Id < 1200)
.ToSql(a => new { a.Id, a.Title }, FieldAliasOptions.AsProperty);
fsql.Ado.CommandFluent($"{sql1} UNION ALL {sql2}")
.ExecuteDataTable();
分页问题
Union All 之后 如果直接 分页会有一个问题。请看具体示例
多次WithSql+Page存在问题:每个WithSql内都有一个Page分页
var sql1 = fsql.Select()
.Where(a => a.Title.Contains("xxx"))
.ToSql();
var sql2 = fsql.Select()
.Where(a => a.Title.Contains("yyy"))
.ToSql();
fsql.Select().WithSql(sql1).WithSql(sql2).Page(1, 20).ToList();
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%xxx%') ) a
limit 0,20) ftb
UNION ALL
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%yyy%') ) a
limit 0,20) ftb
多个sql union all使用withsql,直接Page分页,会导致每个子表都生效,子表都生成分页。
WithSql 可以和 AsTable 实现分表的功能。
分表跨表查询的时候,分页是要向每个子表(即每个WithSql中的SQL分页)都生效。
解决方案
多次withsql,如需分页,需要按下面的二步操作
第一步:通过witsql,将二个sql组成一个sql。
var sql = fsql.Select()
.WithSql("SELECT * FROM tb_topic where id > 11")
.WithSql("SELECT * FROM tb_topic where id < 10")
.ToSql("*")
如上生成的UOION ALL的sql
SELECT * from (SELECT *
FROM ( SELECT * FROM tb_topic where id > 11 ) a) ftb
UNION ALL
SELECT * from (SELECT *
FROM ( SELECT * FROM tb_topic where id < 10 ) a) ftb
第二步:之后 调用Page则是通过Union ALL后的结果上分页
var sql2 = g.mysql.Select()
.WithSql(sql)
.Page(2, 10)
.ToSql();
SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT * from (SELECT *
FROM ( SELECT * FROM tb_topic where id > 11 ) a) ftb
UNION ALL
SELECT * from (SELECT *
FROM ( SELECT * FROM tb_topic where id < 10 ) a) ftb ) a
limit 10,10
什么是Hessian
The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary p
在Spark Shell上,通过创建HiveContext可以直接进行Hive操作
1. 操作Hive中已存在的表
[hadoop@hadoop bin]$ ./spark-shell
Spark assembly has been built with Hive, including Datanucleus jars on classpath
Welcom
JMS Message Delivery Reliability and Acknowledgement Patterns
http://wso2.com/library/articles/2013/01/jms-message-delivery-reliability-acknowledgement-patterns/
Transaction and redelivery in
转载请出自出处:http://eksliang.iteye.com/blog/2177567 一、游标
数据库使用游标返回find的执行结果。客户端对游标的实现通常能够对最终结果进行有效控制,从shell中定义一个游标非常简单,就是将查询结果分配给一个变量(用var声明的变量就是局部变量),便创建了一个游标,如下所示:
> var