using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.CodeGenerator;
namespace ZR.Repository.System
{
[AppService(ServiceLifetime = LifeTime.Transient)]
public class CodeGeneratorRepository : BaseRepository
{
///
/// 获取数据库
///
///
public List GetAllDb()
{
//return Db.Ado.SqlQuery("select name as DbName from master..sysdatabases ");
var list = Db.DbMaintenance.GetDataBaseList(Db);
List dataBases = new List();
list.ForEach(item =>
{
dataBases.Add(new DataBaseInfo() { DbName = item });
});
return dataBases;
}
///
/// 根据数据库名获取所有的表
///
///
///
///
///
public List GetAllTables(string dbName, string tableName, PagerInfo pager)
{
var tableList = GetSugarDbContext(dbName).DbMaintenance.GetTableInfoList(true);
if (!string.IsNullOrEmpty(tableName))
{
tableList = tableList.Where(f => f.Name.Contains(tableName)).ToList();
}
pager.TotalNum = tableList.Count;
return tableList.Skip(pager.PageSize * (pager.PageNum - 1)).Take(pager.PageSize).OrderBy(f => f.Name).ToList();
}
///
/// 获取表格列信息
///
///
///
///
public List GetColumnInfo(string dbName, string tableName)
{
return GetSugarDbContext(dbName).DbMaintenance.GetColumnInfosByTableName(tableName, true);
}
///
/// 获取当前数据库表名
///
///
///
public List GetAllTables(string[] tabList)
{
string sql = @"SELECT tbs.name as TableName ,ds.value as Description FROM sys.tables tbs
left join sys.extended_properties ds on ds.major_id=tbs.object_id and ds.minor_id=0";
return Db.SqlQueryable(sql).WhereIF(tabList.Length > 0, x => tabList.Contains(x.TableName)).ToList();
}
}
}