Compare commits

..

No commits in common. '0078a00557a7dbebe4251ba9facfd4c29f1a8203' and '84c2e9fccc54052be580308a5db166d2bb38f360' have entirely different histories.

@ -14,16 +14,13 @@ namespace ZKLT.Hadoop.API.Controllers
[ApiController] [ApiController]
public class HadoopController : ControllerBase public class HadoopController : ControllerBase
{ {
public HadoopController(IHadoopService hadoop,ITableService table) public HadoopController(IHadoopService hadoop)
{ {
_HadoopService = hadoop; _HadoopService = hadoop;
_TableService = table;
} }
private IHadoopService _HadoopService; private IHadoopService _HadoopService;
private ITableService _TableService;
[HttpGet("getid")] [HttpGet("getid")]
public ActionResult GetId([FromQuery] string? prefix, [FromQuery] int? count) { public ActionResult GetId([FromQuery] string? prefix, [FromQuery] int? count) {
if (count != null && count > 0) if (count != null && count > 0)
@ -64,7 +61,7 @@ namespace ZKLT.Hadoop.API.Controllers
{ {
try try
{ {
return Ok(_TableService.GetSource(sourceid)); return Ok(_HadoopService.GetSource(sourceid));
} }
catch (Exception e) catch (Exception e)
{ {
@ -129,7 +126,7 @@ namespace ZKLT.Hadoop.API.Controllers
{ {
try try
{ {
return Ok(_TableService.GetTable(tableid)); return Ok(_HadoopService.GetTable(tableid));
} }
catch (Exception e) catch (Exception e)
{ {

@ -13,6 +13,19 @@ namespace ZKLT.Hadoop.Interface
/// </summary> /// </summary>
public interface IHadoopService public interface IHadoopService
{ {
/// <summary>
/// 初始化云计算
/// </summary>
/// <param name="config">配置</param>
public void Init(Action<HDP_Source> config);
/// <summary>
/// 获取源
/// </summary>
/// <param name="sourceid">数据源编号</param>
/// <returns>结果</returns>
public HDP_Source? GetSource(string sourceid);
/// <summary> /// <summary>
/// 创建源 /// 创建源
/// </summary> /// </summary>
@ -41,6 +54,13 @@ namespace ZKLT.Hadoop.Interface
/// <returns>结果</returns> /// <returns>结果</returns>
public HDP_Source[] QuerySource(HDP_Command command); public HDP_Source[] QuerySource(HDP_Command command);
/// <summary>
/// 获取表
/// </summary>
/// <param name="tableid">表编号</param>
/// <returns>结果</returns>
public HDP_Table? GetTable(string tableid);
/// <summary> /// <summary>
/// 创建表 /// 创建表
/// </summary> /// </summary>

@ -4,58 +4,36 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Transactions;
using ZKLT.Hadoop.Model; using ZKLT.Hadoop.Model;
namespace ZKLT.Hadoop.Interface namespace ZKLT.Hadoop.Interface
{ {
public interface ITableService public interface ITableService
{ {
/// <summary>
/// 初始化云计算
/// </summary>
/// <param name="config">配置</param>
public void Init(Action<HDP_Source> config);
/// <summary>
/// 获取源
/// </summary>
/// <param name="sourceid">数据源编号</param>
/// <returns>结果</returns>
public HDP_Source GetSource(string sourceid);
/// <summary>
/// 获取表
/// </summary>
/// <param name="tableid">表编号</param>
/// <returns>结果</returns>
public HDP_Table GetTable(string tableid);
/// <summary> /// <summary>
/// 同步结构 /// 同步结构
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="table">数据表</param> /// <param name="table">数据表</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool InitStruct(string sourceId, HDP_Table table); public bool InitStruct(HDP_Source source, HDP_Table table);
/// <summary> /// <summary>
/// 删除结构 /// 删除结构
/// </summary> /// </summary>
/// <param name="sourceId">源</param> /// <param name="source">源</param>
/// <param name="tableId">表</param> /// <param name="tableKey">表</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool RemoveStruct(string sourceId, string tableId); public bool RemoveStruct(HDP_Source source, string tableKey);
/// <summary> /// <summary>
/// 插入数据 /// 插入数据
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Insert(string sourceId, string tableId, JToken? data); public bool Insert(HDP_Source source, HDP_Table table, JContainer? row);
/// <summary> /// <summary>
/// 更新 /// 更新
@ -63,30 +41,29 @@ namespace ZKLT.Hadoop.Interface
/// <param name="source">数据源</param> /// <param name="source">数据源</param>
/// <param name="table">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Update(string sourceId, string tableId, JToken? where, JToken? data); public bool Update(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row);
/// <summary> /// <summary>
/// 删除 /// 删除
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="row">数据</param> /// <param name="row">数据</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Delete(string sourceId, string tableId, JToken? where, JToken? data); public bool Delete(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row);
/// <summary> /// <summary>
/// 查询单个 /// 查询单个
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <returns>结果</returns> /// <returns>结果</returns>
public T? QuerySingle<T>(string sourceId, string tableId, JToken? where, JToken? data, public T? QuerySingle<T>(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row, string[]? col);
string[]? col);
/// <summary> /// <summary>
/// 查询列表 /// 查询列表
@ -95,46 +72,37 @@ namespace ZKLT.Hadoop.Interface
/// <param name="source">数据源</param> /// <param name="source">数据源</param>
/// <param name="table">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <param name="order">排序</param>
/// <param name="col">筛选返回字段</param>
/// <returns>结果集</returns> /// <returns>结果集</returns>
public T[] Query<T>(string sourceId, string tableId, JToken? where, JToken? data, public T[] Query<T>(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row,
JToken? order, string[]? col); JContainer? order, string[]? col);
public string QueryString(string sourceId, string tableId, JToken? where, JToken? data,
JToken? order, string[]? col, Dictionary<string, object> param);
/// <summary> /// <summary>
/// 查询分页列表 /// 查询列表
/// </summary> /// </summary>
/// <typeparam name="T">返回类型</typeparam> /// <typeparam name="T">返回类型</typeparam>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="pageIndex">分页下标</param>
/// <param name="pageSize">分页大小</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <param name="order">排序</param>
/// <param name="col">返回咧</param>
/// <returns>结果集</returns> /// <returns>结果集</returns>
public HDP_Page<T> QueryPage<T>(string sourceId, string tableId, int pageIndex, int pageSize, JToken? where, public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, JContainer? where, JContainer? row, JContainer? order, string[]? col);
JToken? data, JToken? order, string[]? col);
/// <summary> /// <summary>
/// 判断数据源是否存在表 /// 判断数据源是否存在表
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableName">表名</param> /// <param name="tableName">表名</param>
/// <returns>是否存在</returns> /// <returns>是否存在</returns>
public bool DbExistTable(string sourceId, string tableName); public bool DbExistTable(HDP_Source source, string tableName);
/// <summary> /// <summary>
/// 查询数据列 /// 查询数据列
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">表</param> /// <param name="tableName">表名</param>
/// <returns>列</returns> /// <returns>列</returns>
public HDP_Column[] DbGetColumns(string sourceId, string tableId); public HDP_Column[] DbGetColumns(HDP_Source source, string tableName);
} }
} }

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZKLT.Hadoop;
namespace ZKLT.Hadoop.Model namespace ZKLT.Hadoop.Model
{ {
@ -13,7 +12,6 @@ namespace ZKLT.Hadoop.Model
/// </summary> /// </summary>
public class HDP_Command public class HDP_Command
{ {
private string? _SourceId; private string? _SourceId;
private string? _TableId; private string? _TableId;
@ -26,11 +24,11 @@ namespace ZKLT.Hadoop.Model
private string[]? _Col; private string[]? _Col;
private JToken? _Where; private JContainer? _Where;
private JToken? _Data; private JContainer? _Data;
private JToken? _Order; private JContainer? _Order;
/// <summary> /// <summary>
/// 源 /// 源
@ -45,12 +43,12 @@ namespace ZKLT.Hadoop.Model
/// <summary> /// <summary>
/// 条件 /// 条件
/// </summary> /// </summary>
public JToken? Where { get => _Where; set => _Where = value; } public JContainer? Where { get => _Where; set => _Where = value; }
/// <summary> /// <summary>
/// 数据 /// 数据
/// </summary> /// </summary>
public JToken? Data { get => _Data; set => _Data = value; } public JContainer? Data { get => _Data; set => _Data = value; }
/// <summary> /// <summary>
/// 分页下标 /// 分页下标
@ -65,7 +63,7 @@ namespace ZKLT.Hadoop.Model
/// <summary> /// <summary>
/// 排序 /// 排序
/// </summary> /// </summary>
public JToken? Order { get => _Order; set => _Order = value; } public JContainer? Order { get => _Order; set => _Order = value; }
/// <summary> /// <summary>
/// 命令类型 /// 命令类型

@ -29,7 +29,7 @@ namespace ZKLT.Hadoop.Model
/// <param name="action">函数</param> /// <param name="action">函数</param>
/// <param name="param">参数</param> /// <param name="param">参数</param>
/// <returns>命令</returns> /// <returns>命令</returns>
public static object Convert(string action, JToken param) public static object Convert(string action, JContainer param)
{ {
if (action == DATENOW) if (action == DATENOW)
{ {

@ -25,7 +25,5 @@ namespace ZKLT.Hadoop.Model
public const string IN = "IN"; public const string IN = "IN";
public const string BETWEEN = "BETWEEN"; public const string BETWEEN = "BETWEEN";
public const string QUERYIN = "QUERYIN";
} }
} }

@ -38,8 +38,8 @@ namespace ZKLT.Hadoop
/// <returns></returns> /// <returns></returns>
public static IApplicationBuilder UseHadoop(this IApplicationBuilder app, Action<HDP_Source> config) public static IApplicationBuilder UseHadoop(this IApplicationBuilder app, Action<HDP_Source> config)
{ {
var _TableService = app.ApplicationServices.GetRequiredService<ITableService>(); var _HadoopService = app.ApplicationServices.GetRequiredService<IHadoopService>();
_TableService.Init(config); _HadoopService.Init(config);
return app; return app;
} }
} }
@ -52,10 +52,105 @@ namespace ZKLT.Hadoop
public HadoopService(ITableService tableService) public HadoopService(ITableService tableService)
{ {
_TableService = tableService; _TableService = tableService;
_Source = new HDP_Source();
_Tables = new List<HDP_Table>();
} }
private ITableService _TableService; private ITableService _TableService;
private HDP_Source _Source;
private List<HDP_Table> _Tables;
/// <summary>
/// 初始化云计算
/// </summary>
/// <param name="config">配置</param>
public void Init(Action<HDP_Source> config)
{
if (config == null)
{
throw new ArgumentNullException("配置无效");
}
config(_Source);
//参数校验
if (string.IsNullOrEmpty(_Source.Host))
{
throw new ArgumentException("主机无效");
}
if (string.IsNullOrEmpty(_Source.Key))
{
throw new ArgumentException("源无效");
}
if (string.IsNullOrEmpty(_Source.Account))
{
throw new ArgumentException("用户名无效");
}
if (string.IsNullOrEmpty(_Source.PassWord))
{
throw new ArgumentException("密码无效");
}
//初始化
if (_Source.Port == null)
{
_Source.Port = 3306;
}
if (string.IsNullOrEmpty(_Source.Id))
{
_Source.Id = "";
}
if (string.IsNullOrEmpty(_Source.Description))
{
_Source.Description = "云计算系统";
}
var _source = HDP_Table.Class2Table<HDP_Source>();
_Tables.Add(_source);
if (!_TableService.InitStruct(_Source, _source))
{
throw new Exception("初始化数据源失败");
}
var _table = HDP_Table.Class2Table<HDP_Table>();
_Tables.Add(_table);
if (!_TableService.InitStruct(_Source, _table))
{
throw new Exception("初始化数据表失败");
}
var _column = HDP_Table.Class2Table<HDP_Column>();
_Tables.Add(_column);
if (!_TableService.InitStruct(_Source, _column))
{
throw new Exception("初始化数据列失败");
}
}
/// <summary>
/// 获取源
/// </summary>
/// <param name="sourceid">数据源编号</param>
/// <returns>结果</returns>
public HDP_Source? GetSource(string sourceid)
{
if (string.IsNullOrEmpty(sourceid) || _Source.Id == sourceid)
{
return _Source;
}
var _result = _TableService.QuerySingle<HDP_Source>(_Source, GetTable("HDP_Source")!, new JObject
{
{ "Id","=" }
}, new JObject {
{ "Id",sourceid}
}, null);
return _result;
}
/// <summary> /// <summary>
/// 创建源 /// 创建源
/// </summary> /// </summary>
@ -88,12 +183,12 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("密码无效"); throw new ArgumentNullException("密码无效");
} }
if (_TableService.GetSource(source.Id) != null) if (GetSource(source.Id) != null)
{ {
throw new ArgumentException("编号已存在"); throw new ArgumentException("编号已存在");
} }
return _TableService.Insert("", "HDP_Source", HDP_Table.Class2JObject(source)); return _TableService.Insert(_Source, GetTable("HDP_Source")!, HDP_Table.Class2JObject(source));
} }
/// <summary> /// <summary>
@ -128,8 +223,12 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("密码无效"); throw new ArgumentNullException("密码无效");
} }
if (GetSource(source.Id) == null)
{
throw new ArgumentException("编号不存在");
}
return _TableService.Update(source.Id, "HDP_Source", new JObject { return _TableService.Update(_Source, GetTable("HDP_Source")!, new JObject {
{"Id","=" } {"Id","=" }
}, HDP_Table.Class2JObject(source)); }, HDP_Table.Class2JObject(source));
} }
@ -141,7 +240,17 @@ namespace ZKLT.Hadoop
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool DeleteSource(string sourceid) public bool DeleteSource(string sourceid)
{ {
return _TableService.Delete(sourceid, "HDP_Source", new JObject{ //校验
if (string.IsNullOrEmpty(sourceid))
{
throw new ArgumentNullException("编号无效");
}
if (GetSource(sourceid) == null)
{
throw new ArgumentException("编号不存在");
}
return _TableService.Delete(_Source, GetTable("HDP_Source")!, new JObject{
{"Id","=" } {"Id","=" }
}, new JObject{ }, new JObject{
{"Id",sourceid } {"Id",sourceid }
@ -155,9 +264,44 @@ namespace ZKLT.Hadoop
/// <returns>结果</returns> /// <returns>结果</returns>
public HDP_Source[] QuerySource(HDP_Command command) public HDP_Source[] QuerySource(HDP_Command command)
{ {
return _TableService.Query<HDP_Source>(command.SourceId!, command.TableId!, command.Where!, command.Data!, command.Order!, command.Col); return _TableService.Query<HDP_Source>(_Source, GetTable("HDP_Source")!, command.Where!, command.Data!, command.Order!, command.Col);
} }
/// <summary>
/// 获取表
/// </summary>
/// <param name="tableid">表编号</param>
/// <returns>结果</returns>
public HDP_Table? GetTable(string tableid)
{
if (string.IsNullOrEmpty(tableid))
{
throw new ArgumentNullException("数据表编号无效");
}
if (_Tables.Any(x => x.Id == tableid))
{
return _Tables.First(x => x.Id == tableid);
}
var _result = _TableService.QuerySingle<HDP_Table>(_Source, GetTable("HDP_Table")!, new JObject
{
{ "Id","=" }
}, new JObject{
{ "Id",tableid}
}, null);
if (_result != null)
{
_result.Columns = _TableService.Query<HDP_Column>(_Source, GetTable("HDP_Column")!, new JObject {
{ "TableId","="}
}, new JObject{
{"TableId",_result.Id! }
}, null, null);
}
return _result;
}
/// <summary> /// <summary>
/// 创建表 /// 创建表
@ -166,15 +310,19 @@ namespace ZKLT.Hadoop
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool InsertTable(HDP_Table table) public bool InsertTable(HDP_Table table)
{ {
if (_TableService.GetTable(table.Id) != null) if (string.IsNullOrEmpty(table.Id))
{
throw new ArgumentNullException("表编号无效");
}
if (GetTable(table.Id) != null)
{ {
throw new ArgumentNullException("表编号已存在"); throw new ArgumentNullException("表编号已存在");
} }
if (_TableService.InitStruct("", table)) if (_TableService.InitStruct(_Source, table))
{ {
using (TransactionScope _scope = new TransactionScope()) using (TransactionScope _scope = new TransactionScope())
{ {
if (!_TableService.Insert("", "HDP_Table", HDP_Table.Class2JObject(table))) if (!_TableService.Insert(_Source, GetTable("HDP_Table")!, HDP_Table.Class2JObject(table)))
{ {
return false; return false;
} }
@ -182,7 +330,7 @@ namespace ZKLT.Hadoop
{ {
var _column = table.Columns![i]; var _column = table.Columns![i];
_column.TableId = table.Id; _column.TableId = table.Id;
if (!_TableService.Insert("", "HDP_Column", HDP_Table.Class2JObject(_column))) if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2JObject(_column)))
{ {
return false; return false;
} }
@ -204,11 +352,19 @@ namespace ZKLT.Hadoop
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool UpdateTable(HDP_Table table) public bool UpdateTable(HDP_Table table)
{ {
if (_TableService.InitStruct("", table)) if (string.IsNullOrEmpty(table.Id))
{
throw new ArgumentNullException("表编号无效");
}
if (GetTable(table.Id) == null)
{
throw new ArgumentNullException("表编号不存在");
}
if (_TableService.InitStruct(_Source, table))
{ {
using (TransactionScope _scope = new TransactionScope()) using (TransactionScope _scope = new TransactionScope())
{ {
if (!_TableService.Update("", "HDP_Table", new JObject{ if (!_TableService.Update(_Source, GetTable("HDP_Table")!, new JObject{
{ "Id","="} { "Id","="}
}, HDP_Table.Class2JObject(table))) }, HDP_Table.Class2JObject(table)))
{ {
@ -219,19 +375,19 @@ namespace ZKLT.Hadoop
{ {
var _column = table.Columns![i]; var _column = table.Columns![i];
_column.TableId = table.Id; _column.TableId = table.Id;
if (_TableService.QuerySingle<HDP_Column>("", "HDP_Column", new JObject if (_TableService.QuerySingle<HDP_Column>(_Source, GetTable("HDP_Column")!, new JObject
{ {
{"Id","=" } {"Id","=" }
}, HDP_Table.Class2JObject(_column), null) == null) }, HDP_Table.Class2JObject(_column), null) == null)
{ {
if (!_TableService.Insert("", "HDP_Column", HDP_Table.Class2JObject(_column))) if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2JObject(_column)))
{ {
return false; return false;
} }
} }
else else
{ {
if (!_TableService.Update("", "HDP_Column", new JObject { if (!_TableService.Update(_Source, GetTable("HDP_Column")!, new JObject {
{"Id","=" } {"Id","=" }
}, HDP_Table.Class2JObject(_column))) }, HDP_Table.Class2JObject(_column)))
{ {
@ -262,10 +418,14 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("编号无效"); throw new ArgumentNullException("编号无效");
} }
if (GetTable(tableId) == null)
{
throw new ArgumentException("编号不存在");
}
using (TransactionScope _scope = new TransactionScope()) using (TransactionScope _scope = new TransactionScope())
{ {
if (!_TableService.Delete("", "HDP_Table", new JObject{ if (!_TableService.Delete(_Source, GetTable("HDP_Table")!, new JObject{
{"Id","=" } {"Id","=" }
}, new JObject { }, new JObject {
{"Id",tableId } {"Id",tableId }
@ -273,7 +433,7 @@ namespace ZKLT.Hadoop
{ {
return false; return false;
} }
if (!_TableService.Delete("", "HDP_Column", new JObject{ if (!_TableService.Delete(_Source, GetTable("HDP_Column")!, new JObject{
{ "TableId","="} { "TableId","="}
}, new JObject { }, new JObject {
{"TableId",tableId } {"TableId",tableId }
@ -294,7 +454,7 @@ namespace ZKLT.Hadoop
/// <returns>结果</returns> /// <returns>结果</returns>
public HDP_Table[] QueryTable(HDP_Command command) public HDP_Table[] QueryTable(HDP_Command command)
{ {
return _TableService.Query<HDP_Table>("", "HDP_Table", command.Where!, command.Data!, command.Order!, command.Col!); return _TableService.Query<HDP_Table>(_Source, GetTable("HDP_Table")!, command.Where!, command.Data!, command.Order!, command.Col!);
} }
/// <summary> /// <summary>
@ -304,7 +464,17 @@ namespace ZKLT.Hadoop
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Insert(HDP_Command command) public bool Insert(HDP_Command command)
{ {
return _TableService.Insert(command.SourceId!, command.TableId!, command.Data!); if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
var _table = GetTable(command.TableId);
if (_table == null)
{
throw new ArgumentException("表不存在");
}
var _source = GetSource(_table.SourceId!);
return _TableService.Insert(_source!, _table, command.Data!);
} }
/// <summary> /// <summary>
@ -314,7 +484,17 @@ namespace ZKLT.Hadoop
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Update(HDP_Command command) public bool Update(HDP_Command command)
{ {
return _TableService.Update(command.SourceId!, command.TableId!, command.Where!, command.Data!); if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
var _table = GetTable(command.TableId);
if (_table == null)
{
throw new ArgumentException("表不存在");
}
var _source = GetSource(_table.SourceId!);
return _TableService.Update(_source!, _table, command.Where!, command.Data!);
} }
/// <summary> /// <summary>
@ -324,7 +504,17 @@ namespace ZKLT.Hadoop
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Delete(HDP_Command command) public bool Delete(HDP_Command command)
{ {
return _TableService.Delete(command.SourceId!, command.TableId!, command.Where!, command.Data!); if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
var _table = GetTable(command.TableId);
if (_table == null)
{
throw new ArgumentException("表不存在");
}
var _source = GetSource(_table.SourceId!);
return _TableService.Delete(_source!, _table, command.Where!, command.Data!);
} }
/// <summary> /// <summary>
@ -335,7 +525,17 @@ namespace ZKLT.Hadoop
/// <returns>结果</returns> /// <returns>结果</returns>
public T? QuerySingle<T>(HDP_Command command) public T? QuerySingle<T>(HDP_Command command)
{ {
return _TableService.QuerySingle<T>(command.SourceId!, command.TableId!, command.Where!, command.Data!, command.Col); if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
var _table = GetTable(command.TableId);
if (_table == null)
{
throw new ArgumentException("表不存在");
}
var _source = GetSource(_table.SourceId!);
return _TableService.QuerySingle<T>(_source!, _table, command.Where!, command.Data!, command.Col);
} }
/// <summary> /// <summary>
@ -346,7 +546,17 @@ namespace ZKLT.Hadoop
/// <returns>结果</returns> /// <returns>结果</returns>
public T[] Query<T>(HDP_Command command) public T[] Query<T>(HDP_Command command)
{ {
return _TableService.Query<T>(command.SourceId!, command.TableId!, command.Where!, command.Data!, command.Order!, command.Col); if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
var _table = GetTable(command.TableId);
if (_table == null)
{
throw new ArgumentException("表不存在");
}
var _source = GetSource(_table.SourceId!);
return _TableService.Query<T>(_source!, _table, command.Where!, command.Data!, command.Order!, command.Col);
} }
/// <summary> /// <summary>
@ -357,6 +567,10 @@ namespace ZKLT.Hadoop
/// <returns>结果</returns> /// <returns>结果</returns>
public HDP_Page<T> Page<T>(HDP_Command command) public HDP_Page<T> Page<T>(HDP_Command command)
{ {
if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
if (command.PageIndex == null || command.PageIndex <= 0) if (command.PageIndex == null || command.PageIndex <= 0)
{ {
throw new ArgumentNullException("分页下标无效"); throw new ArgumentNullException("分页下标无效");
@ -365,7 +579,13 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("分页大小无效"); throw new ArgumentNullException("分页大小无效");
} }
return _TableService.QueryPage<T>(command.SourceId!, command.TableId!, (int)command.PageIndex, (int)command.PageSize, command.Where!, command.Data!, command.Order!, command.Col!); var _table = GetTable(command.TableId);
if (_table == null)
{
throw new ArgumentException("表不存在");
}
var _source = GetSource(_table.SourceId!);
return _TableService.QueryPage<T>(_source!, _table, (int)command.PageIndex, (int)command.PageSize, command.Where!, command.Data!, command.Order!, command.Col!);
} }
/// <summary> /// <summary>

@ -19,17 +19,6 @@ namespace ZKLT.Hadoop
{ {
public class TableService : ITableService public class TableService : ITableService
{ {
public TableService()
{
_Source = new HDP_Source();
_Tables = new List<HDP_Table>();
}
private HDP_Source _Source;
private List<HDP_Table> _Tables;
/// <summary> /// <summary>
/// 合并条件 /// 合并条件
/// </summary> /// </summary>
@ -37,7 +26,7 @@ namespace ZKLT.Hadoop
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="data">数据</param>
/// <param name="param">参数</param> /// <param name="param">参数</param>
private string MergeWhere(HDP_Table table, JToken? where, JToken? data, Dictionary<string, object> param) private string MergeWhere(HDP_Table table, JContainer? where, JContainer? data, Dictionary<string, object> param)
{ {
StringBuilder _wherestr = new StringBuilder(); StringBuilder _wherestr = new StringBuilder();
string _guid = ""; string _guid = "";
@ -53,16 +42,14 @@ namespace ZKLT.Hadoop
} }
else if (where.Type == JTokenType.Array) else if (where.Type == JTokenType.Array)
{ {
for (var i = 0; i < where.Count(); i++) for(var i = 0;i < where.Count;i++)
{ {
_wheres.Add((JObject)where.Children().ToArray()[i]); _wheres.Add((JObject)where.Children().ToArray()[i]);
_datas.Add((JObject)data.Children().ToArray()[i]); _datas.Add((JObject)data.Children().ToArray()[i]);
} }
} }
for (var j = 0; j < _wheres.Count; j++) for (var j = 0; j < _wheres.Count; j++) {
{ if (j > 0) {
if (j > 0)
{
_wherestr.AppendLine("OR"); _wherestr.AppendLine("OR");
} }
_wherestr.AppendLine("("); _wherestr.AppendLine("(");
@ -77,52 +64,31 @@ namespace ZKLT.Hadoop
var _item = (JProperty)_fileds[i]; var _item = (JProperty)_fileds[i];
if (table.Columns!.Any(x => x.Key == _item.Name) && _data.ContainsKey(_item.Name)) if (table.Columns!.Any(x => x.Key == _item.Name) && _data.ContainsKey(_item.Name))
{ {
List<string> _Itemv = new List<string>();
List<JToken> _Colv = new List<JToken>();
if (_item.Value.Type == JTokenType.String) if (_item.Value.Type == JTokenType.String)
{ {
_Itemv.Add(_item.Value.ToString()); _guid = Guid.NewGuid().ToString("N");
_Colv.Add(_data[_item.Name]!); _wherestr.AppendLine(@$"AND `{_item.Name}` {_item.Value.ToString()} @{_guid}");
if (_data[_item.Name]!.Type == JTokenType.Object || _data[_item.Name]!.Type == JTokenType.Array)
{
param.Add(_guid, _data[_item.Name]!);
}
else if (_item.Value.ToString() == HDP_WhereType.LIKE)
{
param.Add(_guid,$@"%{((JValue)_data[_item.Name]!).Value!}%" );
}
else {
param.Add(_guid, ((JValue)_data[_item.Name]!).Value!);
}
} }
else if (_item.Value.Type == JTokenType.Array) else if (_item.Value.Type == JTokenType.Array)
{ {
_Itemv.AddRange(_item.Value.ToObject<string[]>()!); string[] _itemv = _item.Value.ToObject<string[]>()!;
_Colv.AddRange(_data[_item.Name]!); object[] _colv = _data[_item.Name]!.ToObject<object[]>()!;
} for (var k = 0; k < _itemv.Length; k++)
for (var k = 0; k < _Itemv.Count; k++)
{
_guid = Guid.NewGuid().ToString("N");
switch (_Itemv[k])
{ {
case HDP_WhereType.LIKE: _guid = Guid.NewGuid().ToString("N");
_wherestr.AppendLine(@$"AND `{_item.Name}` {_Itemv[k]} @{_guid}"); _wherestr.AppendLine(@$"AND `{_item.Name}` {_itemv[k]} @{_guid}");
param.Add(_guid, $@"%{((JValue)_Colv[k]!).Value!}%"); param.Add(_guid, _colv.Length > k ? _colv[k] : _colv[_colv.Length - 1]);
break;
case HDP_WhereType.QUERYIN:
var _Command = JsonConvert.DeserializeObject<HDP_Command>(_Colv[k].ToString())!;
var _Query = QueryString(_Command.SourceId!, _Command.TableId!, _Command.Where, _Command.Data, _Command.Order, _Command.Col, param);
_wherestr.AppendLine(@$"AND `{_item.Name}` IN ({_Query})");
break;
case HDP_WhereType.EQUAL:
case HDP_WhereType.NOTEQUAL:
case HDP_WhereType.MORE:
case HDP_WhereType.LESS:
case HDP_WhereType.MORETHEN:
case HDP_WhereType.LESSTHEN:
case HDP_WhereType.IN:
case HDP_WhereType.BETWEEN:
default:
_wherestr.AppendLine(@$"AND `{_item.Name}` {_Itemv[k]} @{_guid}");
if (_Colv[k]!.Type == JTokenType.Object || _data[_item.Name]!.Type == JTokenType.Array)
{
param.Add(_guid, _Colv[k]!);
}
else
{
param.Add(_guid, ((JValue)_Colv[k]!).Value!);
}
break;
} }
} }
} }
@ -167,11 +133,11 @@ namespace ZKLT.Hadoop
/// <param name="table">表</param> /// <param name="table">表</param>
/// <param name="order">排序</param> /// <param name="order">排序</param>
/// <returns></returns> /// <returns></returns>
private string MergeOrder(HDP_Table table, JToken? order, Dictionary<string, object> param) private string MergeOrder(HDP_Table table, JContainer? order, Dictionary<string, object> param)
{ {
string _guid = ""; string _guid = "";
StringBuilder _orderstr = new StringBuilder(); StringBuilder _orderstr = new StringBuilder();
if (order != null && order.Count() > 0) if (order != null && order.Count > 0)
{ {
_orderstr.Append("ORDER BY "); _orderstr.Append("ORDER BY ");
var _fields = order.Children().ToArray(); var _fields = order.Children().ToArray();
@ -215,147 +181,14 @@ namespace ZKLT.Hadoop
return _orderstr.ToString(); return _orderstr.ToString();
} }
/// <summary>
/// 初始化云计算
/// </summary>
/// <param name="config">配置</param>
public void Init(Action<HDP_Source> config)
{
if (config == null)
{
throw new ArgumentNullException("配置无效");
}
config(_Source);
//参数校验
if (string.IsNullOrEmpty(_Source.Host))
{
throw new ArgumentException("主机无效");
}
if (string.IsNullOrEmpty(_Source.Key))
{
throw new ArgumentException("源无效");
}
if (string.IsNullOrEmpty(_Source.Account))
{
throw new ArgumentException("用户名无效");
}
if (string.IsNullOrEmpty(_Source.PassWord))
{
throw new ArgumentException("密码无效");
}
//初始化
if (_Source.Port == null)
{
_Source.Port = 3306;
}
if (string.IsNullOrEmpty(_Source.Id))
{
_Source.Id = "";
}
if (string.IsNullOrEmpty(_Source.Description))
{
_Source.Description = "云计算系统";
}
var _source = HDP_Table.Class2Table<HDP_Source>();
_Tables.Add(_source);
if (!InitStruct(_Source.Id, _source))
{
throw new Exception("初始化数据源失败");
}
var _table = HDP_Table.Class2Table<HDP_Table>();
_Tables.Add(_table);
if (!InitStruct(_Source.Id, _table))
{
throw new Exception("初始化数据表失败");
}
var _column = HDP_Table.Class2Table<HDP_Column>();
_Tables.Add(_column);
if (!InitStruct(_Source.Id, _column))
{
throw new Exception("初始化数据列失败");
}
}
/// <summary>
/// 获取源
/// </summary>
/// <param name="sourceid">数据源编号</param>
/// <returns>结果</returns>
public HDP_Source GetSource(string sourceid)
{
if (string.IsNullOrEmpty(sourceid) || _Source.Id == sourceid)
{
return _Source;
}
var _result = QuerySingle<HDP_Source>("", "HDP_Source", new JObject
{
{ "Id","=" }
}, new JObject {
{ "Id",sourceid}
}, null);
if (_result == null)
{
throw new ArgumentNullException("数据源不存在");
}
return _result;
}
/// <summary>
/// 获取表
/// </summary>
/// <param name="tableid">表编号</param>
/// <returns>结果</returns>
public HDP_Table GetTable(string tableid)
{
if (string.IsNullOrEmpty(tableid))
{
throw new ArgumentNullException("数据表编号无效");
}
if (_Tables.Any(x => x.Id == tableid))
{
return _Tables.First(x => x.Id == tableid);
}
var _result = QuerySingle<HDP_Table>("", "HDP_Table", new JObject
{
{ "Id","=" }
}, new JObject{
{ "Id",tableid}
}, null);
if (_result != null)
{
_result.Columns = Query<HDP_Column>("", "HDP_Column", new JObject {
{ "TableId","="}
}, new JObject{
{"TableId",_result.Id! }
}, null, null);
}
else
{
throw new ArgumentNullException("数据表不存在");
}
return _result;
}
/// <summary> /// <summary>
/// 同步结构 /// 同步结构
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="table">数据表</param> /// <param name="table">数据表</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool InitStruct(string sourceId, HDP_Table table) public bool InitStruct(HDP_Source source, HDP_Table table)
{ {
var source = GetSource(sourceId);
//数据校验 //数据校验
if (string.IsNullOrEmpty(table.Key)) if (string.IsNullOrEmpty(table.Key))
{ {
@ -377,9 +210,9 @@ namespace ZKLT.Hadoop
} }
StringBuilder _command = new StringBuilder(); StringBuilder _command = new StringBuilder();
if (DbExistTable(sourceId, table.Key)) if (DbExistTable(source, table.Key))
{ {
var _dbColumns = DbGetColumns(sourceId, table.Key); var _dbColumns = DbGetColumns(source, table.Key);
_command.AppendLine($@"ALTER TABLE `{table.Key}`"); _command.AppendLine($@"ALTER TABLE `{table.Key}`");
_command.AppendLine("DROP PRIMARY KEY,"); _command.AppendLine("DROP PRIMARY KEY,");
@ -525,13 +358,20 @@ namespace ZKLT.Hadoop
/// <summary> /// <summary>
/// 删除结构 /// 删除结构
/// </summary> /// </summary>
/// <param name="sourceId">源</param> /// <param name="source">源</param>
/// <param name="tableId">表</param> /// <param name="tableKey">表</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool RemoveStruct(string sourceId, string tableId) public bool RemoveStruct(HDP_Source source, string tableKey)
{ {
var source = GetSource(sourceId); //数据校验
var table = GetTable(tableId); if (string.IsNullOrEmpty(tableKey))
{
throw new ArgumentNullException("表键无效");
}
if (!DbExistTable(source, tableKey))
{
throw new ArgumentNullException("表不存在");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{ {
@ -545,24 +385,22 @@ namespace ZKLT.Hadoop
} }
StringBuilder _command = new StringBuilder(); StringBuilder _command = new StringBuilder();
_command.AppendLine(@$"DROP TABLE {table.Key}"); _command.AppendLine(@$"DROP TABLE {tableKey}");
_connection.Execute(_command.ToString()); _connection.Execute(_command.ToString());
_connection.Close(); _connection.Close();
return !DbExistTable(sourceId, table.Key!); return !DbExistTable(source, tableKey);
} }
} }
/// <summary> /// <summary>
/// 插入数据 /// 插入数据
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="data">数据</param> /// <param name="data">数据</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Insert(string sourceId, string tableId, JToken? data) public bool Insert(HDP_Source source, HDP_Table table, JContainer? data)
{ {
var source = GetSource(sourceId)!;
var table = GetTable(tableId)!;
//数据校验 //数据校验
if (string.IsNullOrEmpty(table.Key)) if (string.IsNullOrEmpty(table.Key))
{ {
@ -572,7 +410,7 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("列无效"); throw new ArgumentNullException("列无效");
} }
if (data == null || data.Count() == 0) if (data == null || data.Count == 0)
{ {
throw new ArgumentNullException("数据无效"); throw new ArgumentNullException("数据无效");
} }
@ -674,12 +512,10 @@ namespace ZKLT.Hadoop
/// <param name="source">数据源</param> /// <param name="source">数据源</param>
/// <param name="table">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Update(string sourceId, string tableId, JToken? where, JToken? data) public bool Update(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data)
{ {
var source = GetSource(sourceId);
var table = GetTable(tableId);
//数据校验 //数据校验
if (string.IsNullOrEmpty(table.Key)) if (string.IsNullOrEmpty(table.Key))
{ {
@ -702,7 +538,7 @@ namespace ZKLT.Hadoop
{ {
_where.AddRange(((JArray)where).ToObject<JObject[]>()!); _where.AddRange(((JArray)where).ToObject<JObject[]>()!);
} }
if (data == null || data.Count() == 0) if (data == null || data.Count == 0)
{ {
throw new ArgumentNullException("数据无效"); throw new ArgumentNullException("数据无效");
} }
@ -743,7 +579,7 @@ namespace ZKLT.Hadoop
var _column = table.Columns[i]; var _column = table.Columns[i];
if (_row.ContainsKey(_column.Key!) && !_where.Any(x => x.ContainsKey(_column.Key!))) if (_row.ContainsKey(_column.Key!) && !_where.Any(x => x.ContainsKey(_column.Key!)))
{ {
_colstr.Append(@$"`{_column.Key!}`={HDP_CommandAction.ColConvert(_column, _params, ((JValue)_row[_column.Key!]!).Value!)},"); _colstr.Append(@$"`{_column.Key!}`={HDP_CommandAction.ColConvert(_column,_params, ((JValue)_row[_column.Key!]!).Value!)},");
} }
else if (!string.IsNullOrEmpty(_column.UpdateDefault)) else if (!string.IsNullOrEmpty(_column.UpdateDefault))
{ {
@ -779,15 +615,13 @@ namespace ZKLT.Hadoop
/// <summary> /// <summary>
/// 删除 /// 删除
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="row">数据</param> /// <param name="row">数据</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Delete(string sourceId, string tableId, JToken? where, JToken? data) public bool Delete(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data)
{ {
var source = GetSource(sourceId);
var table = GetTable(tableId);
//数据校验 //数据校验
if (string.IsNullOrEmpty(table.Key)) if (string.IsNullOrEmpty(table.Key))
{ {
@ -801,7 +635,7 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("条件无效"); throw new ArgumentNullException("条件无效");
} }
if (data == null || data.Count() == 0) if (data == null || data.Count == 0)
{ {
throw new ArgumentNullException("数据无效"); throw new ArgumentNullException("数据无效");
} }
@ -857,17 +691,14 @@ namespace ZKLT.Hadoop
/// <summary> /// <summary>
/// 查询单个 /// 查询单个
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="data">数据</param>
/// <returns>结果</returns> /// <returns>结果</returns>
public T? QuerySingle<T>(string sourceId, string tableId, JToken? where, JToken? data, public T? QuerySingle<T>(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data,
string[]? col) string[]? col)
{ {
var source = GetSource(sourceId);
var table = GetTable(tableId);
//数据校验 //数据校验
if (string.IsNullOrEmpty(table.Key)) if (string.IsNullOrEmpty(table.Key))
{ {
@ -881,7 +712,7 @@ namespace ZKLT.Hadoop
{ {
throw new ArgumentNullException("条件无效"); throw new ArgumentNullException("条件无效");
} }
if (data == null || data.Count() == 0) if (data == null || data.Count == 0)
{ {
throw new ArgumentNullException("数据无效"); throw new ArgumentNullException("数据无效");
} }
@ -934,16 +765,19 @@ namespace ZKLT.Hadoop
/// <param name="table">数据表</param> /// <param name="table">数据表</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="data">数据</param>
/// <param name="order">排序</param>
/// <param name="col">筛选返回字段</param>
/// <returns>结果集</returns> /// <returns>结果集</returns>
public T[] Query<T>(string sourceId, string tableId, JToken? where, JToken? data, public T[] Query<T>(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data,
JToken? order, string[]? col) JContainer? order, string[]? col)
{ {
var source = GetSource(sourceId); //数据校验
var table = GetTable(tableId); if (string.IsNullOrEmpty(table.Key))
{
Dictionary<string, object> _params = new Dictionary<string, object>(); throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{ {
try try
@ -955,68 +789,44 @@ namespace ZKLT.Hadoop
throw new ArgumentException("数据源连接失败"); throw new ArgumentException("数据源连接失败");
} }
var _result = _connection.Query<T>(QueryString(sourceId, tableId, where, data, order, col, _params), _params); //查询命令
_connection.Close(); StringBuilder _command = new StringBuilder();
return _result.ToArray(); Dictionary<string, object> _params = new Dictionary<string, object>();
} if (col == null || col.Length == 0)
} {
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
public string QueryString(string sourceId, string tableId, JToken? where, JToken? data, }
JToken? order, string[]? col, Dictionary<string, object> param) else
{ {
var source = GetSource(sourceId); _command.AppendLine("SELECT");
var table = GetTable(tableId); MergeCols(table, col, _command);
//数据校验 _command.AppendLine($@"FROM `{table.Key}`");
if (string.IsNullOrEmpty(table.Key)) }
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
//查询命令
StringBuilder _command = new StringBuilder();
if (col == null || col.Length == 0)
{
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
}
else
{
_command.AppendLine("SELECT");
MergeCols(table, col, _command);
_command.AppendLine($@"FROM `{table.Key}`");
}
//执行条件 //执行条件
_command.AppendLine(MergeWhere(table, where, data, param)); _command.AppendLine(MergeWhere(table, where, data, _params));
//执行排序 //执行排序
_command.AppendLine(MergeOrder(table, order, param)); _command.AppendLine(MergeOrder(table, order, _params));
return _command.ToString(); var _result = _connection.Query<T>(_command.ToString(), _params);
_connection.Close();
return _result.ToArray();
}
} }
/// <summary> /// <summary>
/// 查询分页列表 /// 查询列表
/// </summary> /// </summary>
/// <typeparam name="T">返回类型</typeparam> /// <typeparam name="T">返回类型</typeparam>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">数据表</param> /// <param name="table">数据表</param>
/// <param name="pageIndex">分页下标</param>
/// <param name="pageSize">分页大小</param>
/// <param name="where">条件</param> /// <param name="where">条件</param>
/// <param name="data">数据</param> /// <param name="row">数据</param>
/// <param name="order">排序</param>
/// <param name="col">返回咧</param>
/// <returns>结果集</returns> /// <returns>结果集</returns>
public HDP_Page<T> QueryPage<T>(string sourceId, string tableId, int pageIndex, int pageSize, JToken? where, public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, JContainer? where,
JToken? data, JToken? order, string[]? col) JContainer? data, JContainer? order, string[]? col)
{ {
var source = GetSource(sourceId);
var table = GetTable(tableId);
//数据校验 //数据校验
if (string.IsNullOrEmpty(table.Key)) if (string.IsNullOrEmpty(table.Key))
{ {
@ -1078,12 +888,11 @@ namespace ZKLT.Hadoop
/// <summary> /// <summary>
/// 判断数据源是否存在表 /// 判断数据源是否存在表
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableName">表名</param> /// <param name="tableName">表名</param>
/// <returns>是否存在</returns> /// <returns>是否存在</returns>
public bool DbExistTable(string sourceId, string tableName) public bool DbExistTable(HDP_Source source, string tableName)
{ {
var source = GetSource(sourceId);
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{ {
try try
@ -1113,14 +922,11 @@ namespace ZKLT.Hadoop
/// <summary> /// <summary>
/// 查询数据列 /// 查询数据列
/// </summary> /// </summary>
/// <param name="sourceId">数据源</param> /// <param name="source">数据源</param>
/// <param name="tableId">表</param> /// <param name="tableName">表名</param>
/// <returns>列</returns> /// <returns>列</returns>
public HDP_Column[] DbGetColumns(string sourceId, string tableId) public HDP_Column[] DbGetColumns(HDP_Source source, string tableName)
{ {
var source = GetSource(sourceId);
var table = GetTable(tableId);
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{ {
var _result = new List<HDP_Column>(); var _result = new List<HDP_Column>();
@ -1136,7 +942,7 @@ namespace ZKLT.Hadoop
StringBuilder _command = new StringBuilder(); StringBuilder _command = new StringBuilder();
_command.AppendLine($@"SELECT `COLUMN_NAME` AS `Key`,`DATA_TYPE` AS `DataType`,`COLUMN_COMMENT` AS `Description` FROM INFORMATION_SCHEMA.COLUMNS _command.AppendLine($@"SELECT `COLUMN_NAME` AS `Key`,`DATA_TYPE` AS `DataType`,`COLUMN_COMMENT` AS `Description` FROM INFORMATION_SCHEMA.COLUMNS
WHERE `TABLE_SCHEMA`=@SourceKey AND `TABLE_NAME`=@TableKey"); WHERE `TABLE_SCHEMA`=@SourceKey AND `TABLE_NAME`=@TableKey");
_result.AddRange(_connection.Query<HDP_Column>(_command.ToString(), new { SourceKey = source.Key, TableKey = table.Key })); _result.AddRange(_connection.Query<HDP_Column>(_command.ToString(), new { SourceKey = source.Key, TableKey = tableName }));
_connection.Close(); _connection.Close();
return _result.ToArray(); return _result.ToArray();
} }

Loading…
Cancel
Save