2023-07-31 18:55:12 +08:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel.DataAnnotations ;
using System.ComponentModel.DataAnnotations.Schema ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using SqlSugar ;
2023-08-02 08:32:38 +08:00
using TDengineDriver ;
2023-07-31 23:59:42 +08:00
2023-07-31 18:55:12 +08:00
namespace OrmTest
{
2023-08-02 08:32:38 +08:00
public class ORMTest
2023-07-31 18:55:12 +08:00
{
public static void Init ( )
{
2023-07-31 23:59:42 +08:00
var db = new SqlSugarClient ( new ConnectionConfig ( )
2023-07-31 18:55:12 +08:00
{
2023-07-31 23:59:42 +08:00
DbType = SqlSugar . DbType . TDengine ,
2023-07-31 18:55:12 +08:00
ConnectionString = Config . ConnectionString ,
IsAutoCloseConnection = true ,
AopEvents = new AopEvents
{
OnLogExecuting = ( sql , p ) = >
{
2023-08-08 19:17:54 +08:00
Console . WriteLine ( UtilMethods . GetNativeSql ( sql , p ) ) ;
2023-07-31 18:55:12 +08:00
}
}
} ) ;
2023-08-08 19:03:31 +08:00
2023-07-31 23:59:42 +08:00
//建库
2023-08-03 12:50:08 +08:00
db . DbMaintenance . CreateDatabase ( ) ;
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//建超级表
2023-08-02 08:32:38 +08:00
db . Ado . ExecuteCommand ( "CREATE STABLE IF NOT EXISTS St01 (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT, isdelete BOOL, name BINARY(64)) TAGS (location BINARY(64), groupId INT)" ) ;
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//创建子表
2023-08-02 08:32:38 +08:00
db . Ado . ExecuteCommand ( @"create table IF NOT EXISTS MyTable02 using St01 tags('California.SanFrancisco',1)" ) ;
2023-08-08 19:03:31 +08:00
2023-07-31 18:55:12 +08:00
2023-07-31 23:59:42 +08:00
//查询子表
2023-08-08 18:57:59 +08:00
var dt = db . Ado . GetDataTable ( "select * from MyTable02 " ) ;
2023-07-31 18:55:12 +08:00
2023-08-08 19:03:31 +08:00
//插入单条子表
2023-08-02 08:32:38 +08:00
db . Insertable ( new MyTable02 ( )
2023-07-31 18:55:12 +08:00
{
2023-07-31 23:59:42 +08:00
ts = DateTime . Now ,
current = Convert . ToSingle ( 1.1 ) ,
groupId = 1 ,
2023-08-08 19:03:31 +08:00
isdelete = true ,
name = "haha" ,
2023-07-31 23:59:42 +08:00
location = "aa" ,
2023-08-08 19:03:31 +08:00
phase = Convert . ToSingle ( 1.2 ) ,
2023-07-31 23:59:42 +08:00
voltage = 11
} ) . ExecuteCommand ( ) ;
2023-08-08 19:03:31 +08:00
//批量插入子表
db . Insertable ( GetInsertDatas ( ) ) . ExecuteCommand ( ) ;
//查询子表(主表字段也能查出来)
var list = db . Queryable < MyTable02 > ( ) . OrderBy ( it = > it . ts ) . ToList ( ) ;
//条件查询
var list2 = db . Queryable < MyTable02 > ( ) . Where ( it = > it . name = = "测试2" ) . ToList ( ) ;
var list22 = db . Queryable < MyTable02 > ( ) . Where ( it = > it . voltage = = 222 ) . ToList ( ) ;
var list222 = db . Queryable < MyTable02 > ( ) . Where ( it = > it . phase = = 1.2 ) . ToList ( ) ;
var list2222 = db . Queryable < MyTable02 > ( ) . Where ( it = > it . isdelete = = true ) . ToList ( ) ;
//模糊查询
var list3 = db . Queryable < MyTable02 > ( ) . Where ( it = > it . name . Contains ( "a" ) ) . ToList ( ) ;
//分页
var Count = 0 ;
var list4 = db . Queryable < MyTable02 > ( ) . Where ( it = > it . voltage = = 111 )
. ToPageList ( 1 , 2 , ref Count ) ;
//删除子表
var ts = list . First ( ) . ts ;
var de = DateTime . Now . AddYears ( - 1 ) ;
var count = db . Deleteable < MyTable02 > ( ) . Where ( it = > it . ts > de ) . ExecuteCommand ( ) ;
}
private static List < MyTable02 > GetInsertDatas ( )
{
return new List < MyTable02 > ( ) {
2023-08-08 18:14:40 +08:00
new MyTable02 ( )
{
2023-08-08 18:57:59 +08:00
ts = DateTime . Now . AddDays ( - 1 ) ,
2023-08-08 18:14:40 +08:00
current = Convert . ToSingle ( 1.1 ) ,
groupId = 1 ,
2023-08-08 18:57:59 +08:00
isdelete = false ,
name = "测试1" ,
location = "false" ,
2023-08-08 18:14:40 +08:00
phase = Convert . ToSingle ( 1.1 ) ,
2023-08-08 18:57:59 +08:00
voltage = 222
} ,
new MyTable02 ( )
{
ts = DateTime . Now . AddDays ( - 2 ) ,
current = Convert . ToSingle ( 1.1 ) ,
groupId = 1 ,
isdelete = false ,
name = "测试2" ,
location = "false" ,
phase = Convert . ToSingle ( 1.1 ) ,
voltage = 222
2023-08-08 18:14:40 +08:00
} ,
new MyTable02 ( )
{
ts = DateTime . Now ,
current = Convert . ToSingle ( 1.1 ) ,
groupId = 1 ,
isdelete = true ,
2023-08-08 18:57:59 +08:00
name = "测试3" ,
location = "true" ,
2023-08-08 18:14:40 +08:00
phase = Convert . ToSingle ( 1.1 ) ,
2023-08-08 18:57:59 +08:00
voltage = 111
2023-08-08 18:14:40 +08:00
}
2023-08-08 19:03:31 +08:00
} ;
2023-07-31 18:55:12 +08:00
}
2023-08-02 08:32:38 +08:00
public class MyTable02
2023-07-31 18:55:12 +08:00
{
2023-08-01 00:28:46 +08:00
[SugarColumn(IsPrimaryKey =true)]
2023-07-31 23:59:42 +08:00
public DateTime ts { get ; set ; }
public float current { get ; set ; }
2023-08-02 08:32:38 +08:00
public bool isdelete { get ; set ; }
public string name { get ; set ; }
2023-07-31 23:59:42 +08:00
public int voltage { get ; set ; }
public float phase { get ; set ; }
[SugarColumn(IsOnlyIgnoreInsert =true,IsOnlyIgnoreUpdate =true)]
public string location { get ; set ; }
[SugarColumn(IsOnlyIgnoreInsert = true, IsOnlyIgnoreUpdate = true)]
public int groupId { get ; set ; }
2023-08-01 00:28:46 +08:00
}
2023-07-31 18:55:12 +08:00
}
}