SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
12 KiB

8 years ago
using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.Demo
{
8 years ago
public class Query:DemoBase
8 years ago
{
public static void Init()
{
Easy();
Page();
Where();
Join();
Funs();
Select();
8 years ago
Ado();
8 years ago
Group();
8 years ago
Sqlable();
8 years ago
Tran();
8 years ago
StoredProcedure();
8 years ago
}
8 years ago
private static void StoredProcedure()
8 years ago
{
var db = GetInstance();
8 years ago
//1. no result
8 years ago
db.Ado.UseStoredProcedure(() =>
8 years ago
{
string spName = "sp_help";
var getSpReslut = db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
});
//2. has result
8 years ago
var result= db.Ado.UseStoredProcedure<dynamic>(() =>
8 years ago
{
8 years ago
string spName = "sp_help";
return db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
8 years ago
});
8 years ago
//2. has output
object outPutValue;
var outputResult = db.Ado.UseStoredProcedure<dynamic>(() =>
{
string spName = "sp_school";
var p1 = new SugarParameter("@p1", "1");
var p2= new SugarParameter("@p2", null,true);//isOutput=true
var dbResult= db.Ado.SqlQueryDynamic(spName,new SugarParameter[] {p1,p2 });
outPutValue = p2.Value;
return dbResult;
});
8 years ago
}
private static void Tran()
{
var db = GetInstance();
//1. no result
8 years ago
var result = db.Ado.UseTran(() =>
8 years ago
{
var beginCount = db.Queryable<Student>().Count();
db.Ado.ExecuteCommand("delete student");
var endCount = db.Queryable<Student>().Count();
throw new Exception("error haha");
});
8 years ago
var count = db.Queryable<Student>().Count();
8 years ago
8 years ago
//2 has result
8 years ago
var result2 = db.Ado.UseTran<List<Student>>(() =>
8 years ago
{
return db.Queryable<Student>().ToList();
});
8 years ago
//3 use try
try
{
db.Ado.BeginTran();
db.Ado.CommitTran();
}
catch (Exception)
{
db.Ado.RollbackTran();
throw;
}
8 years ago
}
private static void Group()
{
var db = GetInstance();
var list = db.Queryable<Student>()
.GroupBy(it => it.Name)
.GroupBy(it => it.Id).Having(it => NBORM.AggregateAvg(it.Id) > 0)
.Select(it => new { idAvg = NBORM.AggregateAvg(it.Id), name = it.Name }).ToList();
8 years ago
//SQL:
//SELECT AVG([Id]) AS[idAvg], [Name] AS[name] FROM[Student] GROUP BY[Name],[Id] HAVING(AVG([Id]) > 0 )
8 years ago
8 years ago
//NBORM.AggregateSum(object thisValue)
//NBORM.AggregateAvg<TResult>(TResult thisValue)
//NBORM.AggregateMin(object thisValue)
//NBORM.AggregateMax(object thisValue)
//NBORM.AggregateCount(object thisValue)
8 years ago
}
private static void Ado()
{
var db = GetInstance();
8 years ago
var t1 = db.Ado.SqlQuery<string>("select 'a'");
8 years ago
var t2 = db.Ado.GetInt("select 1");
var t3 = db.Ado.GetDataTable("select 1 as id");
8 years ago
//more
//db.Ado.GetXXX...
8 years ago
}
public static void Easy()
{
var db = GetInstance();
var getAll = db.Queryable<Student>().ToList();
8 years ago
var getAllNoLock = db.Queryable<Student>().With(SqlWith.NoLock).ToList();
8 years ago
var getByPrimaryKey = db.Queryable<Student>().InSingle(2);
var getByWhere = db.Queryable<Student>().Where(it => it.Id == 1 || it.Name == "a").ToList();
var getByFuns = db.Queryable<Student>().Where(it => NBORM.IsNullOrEmpty(it.Name)).ToList();
8 years ago
var sum = db.Queryable<Student>().Sum(it => it.Id);
var isAny = db.Queryable<Student>().Where(it => it.Id == -1).Any();
8 years ago
var isAny2 = db.Queryable<Student>().Any(it => it.Id == -1);
8 years ago
var getListByRename = db.Queryable<School>().AS("Student").ToList();
8 years ago
var in1 = db.Queryable<Student>().In(it=>it.Id,new int[] { 1, 2, 3 }).ToList();
var in2 = db.Queryable<Student>().In(new int[] { 1, 2, 3 }).ToList();
int[] array = new int[] { 1, 2 };
var in3 = db.Queryable<Student>().Where(it=>NBORM.ContainsArray(array, it.Id)).ToList();
8 years ago
var group = db.Queryable<Student>().GroupBy(it => it.Id)
.Having(it => NBORM.AggregateCount(it.Id) > 10)
8 years ago
.Select(it => new { id = NBORM.AggregateCount(it.Id) }).ToList();
8 years ago
var between = db.Queryable<Student>().Where(it => NBORM.Between(it.Id, 1, 20)).ToList();
var getTodayList = db.Queryable<Student>().Where(it => NBORM.DateIsSame(it.CreateTime, DateTime.Now)).ToList();
8 years ago
}
public static void Page()
{
8 years ago
var db = GetInstance();
var pageIndex = 1;
var pageSize = 2;
var totalCount = 0;
8 years ago
//page
8 years ago
var page = db.Queryable<Student>().OrderBy(it=>it.Id).ToPageList(pageIndex, pageSize, ref totalCount);
8 years ago
8 years ago
//page join
8 years ago
var pageJoin = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).ToPageList(pageIndex, pageSize, ref totalCount);
8 years ago
//top 5
var top5 = db.Queryable<Student>().Take(5).ToList();
//skip5
var skip5 = db.Queryable<Student>().Skip(5).ToList();
8 years ago
}
public static void Where()
{
var db = GetInstance();
//join
var list = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
8 years ago
.Where((st, sc) => sc.Id == 1)
.Where((st, sc) => st.Id == 1)
8 years ago
.Where((st, sc) => st.Id == 1 && sc.Id == 2).ToList();
8 years ago
//SELECT [st].[Id],[st].[SchoolId],[st].[Name],[st].[CreateTime] FROM [Student] st
//Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] )
//WHERE ( [sc].[Id] = @Id0 ) AND ( [st].[Id] = @Id1 ) AND (( [st].[Id] = @Id2 ) AND ( [sc].[Id] = @Id3 ))
8 years ago
//Where If
string name = null;
string name2 = "sunkaixuan";
var list2 = db.Queryable<Student>()
.WhereIF(!string.IsNullOrEmpty(name), it => it.Name == name)
.WhereIF(!string.IsNullOrEmpty(name2), it => it.Name == name2).ToList();
8 years ago
}
public static void Join()
{
8 years ago
var db = GetInstance();
8 years ago
//join 2
8 years ago
var list = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
8 years ago
})
8 years ago
.Where(st => st.Name == "jack").ToList();
8 years ago
8 years ago
//join 3
8 years ago
var list2 = db.Queryable<Student, School, Student>((st, sc, st2) => new object[] {
8 years ago
JoinType.Left,st.SchoolId==sc.Id,
JoinType.Left,st.SchoolId==st2.Id
8 years ago
})
8 years ago
.Where((st, sc, st2) => st2.Id == 1 || sc.Id == 1 || st.Id == 1).ToList();
8 years ago
8 years ago
//join return List<ViewModelStudent>
8 years ago
var list3 = db.Queryable<Student, School>((st, sc) => new object[] {
8 years ago
JoinType.Left,st.SchoolId==sc.Id
8 years ago
}).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();
8 years ago
//join Order By (order by st.id desc,sc.id desc)
var list4 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
8 years ago
.OrderBy(st => st.Id, OrderByType.Desc)
.OrderBy((st, sc) => sc.Id, OrderByType.Desc)
8 years ago
.Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();
8 years ago
}
public static void Funs()
{
8 years ago
var db = GetInstance();
var t1 = db.Queryable<Student>().Where(it => NBORM.ToLower(it.Name) == NBORM.ToLower("JACK")).ToList();
//SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) )
8 years ago
8 years ago
/***More Functions***/
//NBORM.IsNullOrEmpty(object thisValue)
//NBORM.ToLower(object thisValue)
//NBORM.string ToUpper(object thisValue)
//NBORM.string Trim(object thisValue)
//NBORM.bool Contains(string thisValue, string parameterValue)
//NBORM.ContainsArray(object[] thisValue, string parameterValue)
//NBORM.StartsWith(object thisValue, string parameterValue)
//NBORM.EndsWith(object thisValue, string parameterValue)
//NBORM.Equals(object thisValue, object parameterValue)
//NBORM.DateIsSame(DateTime date1, DateTime date2)
//NBORM.DateIsSame(DateTime date1, DateTime date2, DateType dataType)
//NBORM.DateAdd(DateTime date, int addValue, DateType millisecond)
//NBORM.DateAdd(DateTime date, int addValue)
//NBORM.DateValue(DateTime date, DateType dataType)
//NBORM.Between(object value, object start, object end)
//NBORM.ToInt32(object value)
//NBORM.ToInt64(object value)
//NBORM.ToDate(object value)
//NBORM.ToString(object value)
//NBORM.ToDecimal(object value)
//NBORM.ToGuid(object value)
//NBORM.ToDouble(object value)
//NBORM.ToBool(object value)
//NBORM.Substring(object value, int index, int length)
//NBORM.Replace(object value, string oldChar, string newChar)
//NBORM.Length(object value) { throw new NotImplementedException(); }
//NBORM.AggregateSum(object thisValue)
//NBORM.AggregateAvg<TResult>(TResult thisValue)
//NBORM.AggregateMin(object thisValue)
//NBORM.AggregateMax(object thisValue)
//NBORM.AggregateCount(object thisValue)
8 years ago
}
8 years ago
public static void Select()
{
8 years ago
var db = GetInstance();
8 years ago
db.IgnoreColumns.Add("TestId", "Student");
8 years ago
var s1 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Name = it.Name, Student = it }).ToList();
var s2 = db.Queryable<Student>().Select(it => new { id = it.Id, w = new { x = it } }).ToList();
var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList();
var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList();
var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name = it.Name }).ToList();
8 years ago
var s6 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
.OrderBy(st => st.Id, OrderByType.Desc)
.OrderBy((st, sc) => sc.Id, OrderByType.Desc)
8 years ago
.Select((st, sc) => new { Name = st.Name, SchoolId = sc.Id }).ToList();
8 years ago
}
8 years ago
private static void Sqlable()
{
var db = GetInstance();
var join3 = db.Queryable("Student", "st")
.AddJoinInfo("School", "sh", "sh.id=st.schoolid")
.Where("st.id>@id")
.AddParameters(new { id = 1 })
.Select("st.*").ToList();
8 years ago
//SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id
8 years ago
}
8 years ago
}
}