初版接口

main
潘建东 7 months ago
commit 3e18879140

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

63
.gitattributes vendored

@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

363
.gitignore vendored

@ -0,0 +1,363 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd

@ -0,0 +1,228 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI.Relational;
using Newtonsoft.Json.Linq;
using ZKLT.Hadoop.Interface;
using ZKLT.Hadoop.Model;
namespace ZKLT.Hadoop.API.Controllers
{
/// <summary>
/// 云计算接口
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class HadoopController : ControllerBase
{
public HadoopController(IHadoopService hadoop)
{
_HadoopService = hadoop;
}
private IHadoopService _HadoopService;
[HttpGet("getsource")]
public ActionResult GetSource([FromQuery] string sourceid)
{
try
{
return Ok(_HadoopService.GetSource(sourceid));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("insertsource")]
public ActionResult InsertSource(HDP_Source source)
{
try
{
return Ok(_HadoopService.InsertSource(source));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("updatesource")]
public ActionResult UpdateSource(HDP_Source source)
{
try
{
return Ok(_HadoopService.UpdateSource(source));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("deletesource")]
public ActionResult DeleteSource([FromQuery] string sourceid)
{
try
{
return Ok(_HadoopService.DeleteSource(sourceid));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("querysource")]
public ActionResult QuerySource(HDP_Command command)
{
try
{
return Ok(_HadoopService.QuerySource(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("gettable")]
public ActionResult GetTable([FromQuery] string tableid)
{
try
{
return Ok(_HadoopService.GetTable(tableid));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("inserttable")]
public ActionResult InsertTable(HDP_Table table)
{
try
{
return Ok(_HadoopService.InsertTable(table));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("updatetable")]
public ActionResult UpdateTable(HDP_Table table)
{
try
{
return Ok(_HadoopService.UpdateTable(table));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("deletetable")]
public ActionResult DeleteTable([FromQuery] string tableId) {
try
{
return Ok(_HadoopService.DeleteTable(tableId));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("querytable")]
public ActionResult QueryTable(HDP_Command command) {
try
{
return Ok(_HadoopService.QueryTable(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("insert")]
public ActionResult Insert(HDP_Command command) {
try
{
return Ok(_HadoopService.Insert(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("update")]
public ActionResult Update(HDP_Command command)
{
try
{
return Ok(_HadoopService.Update(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("delete")]
public ActionResult Delete(HDP_Command command)
{
try
{
return Ok(_HadoopService.Delete(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("querysingle")]
public ActionResult QuerySingle(HDP_Command command)
{
try
{
return Ok(_HadoopService.QuerySingle<dynamic>(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("query")]
public ActionResult Query(HDP_Command command)
{
try
{
return Ok(_HadoopService.Query<dynamic>(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPost("page")]
public ActionResult Page(HDP_Command command) {
try
{
return Ok(_HadoopService.Page<dynamic>(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}
}

@ -0,0 +1,27 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Hadoop/ZKLT.Hadoop.API/ZKLT.Hadoop.API.csproj", "Hadoop/ZKLT.Hadoop.API/"]
COPY ["Hadoop/ZKLT.Hadoop/ZKLT.Hadoop.csproj", "Hadoop/ZKLT.Hadoop/"]
COPY ["Hadoop/ZKLT.Hadoop.Interface/ZKLT.Hadoop.Interface.csproj", "Hadoop/ZKLT.Hadoop.Interface/"]
COPY ["Hadoop/ZKLT.Hadoop.Model/ZKLT.Hadoop.Model.csproj", "Hadoop/ZKLT.Hadoop.Model/"]
RUN dotnet restore "./Hadoop/ZKLT.Hadoop.API/./ZKLT.Hadoop.API.csproj"
COPY . .
WORKDIR "/src/Hadoop/ZKLT.Hadoop.API"
RUN dotnet build "./ZKLT.Hadoop.API.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./ZKLT.Hadoop.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ZKLT.Hadoop.API.dll"]

@ -0,0 +1,49 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ZKLT.Hadoop;
namespace ZKLT.Hadoop.API
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHadoop();
var app = builder.Build();
app.UseHadoop((c) => {
c.Host = "127.0.0.1";
c.Account = "root";
c.PassWord = "root";
c.Key = "devdb";
c.Port = 3306;
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}

@ -0,0 +1,40 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5171"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"environmentVariables": {
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59844",
"sslPort": 0
}
}
}

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZKLT.Hadoop\ZKLT.Hadoop.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using ZKLT.Hadoop.Model;
namespace ZKLT.Hadoop.Interface
{
/// <summary>
/// 云计算接口
/// </summary>
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>
/// <param name="source">源</param>
/// <returns>是否成功</returns>
public bool InsertSource(HDP_Source source);
/// <summary>
/// 更新源
/// </summary>
/// <param name="source">源</param>
/// <returns>是否成功</returns>
public bool UpdateSource(HDP_Source source);
/// <summary>
/// 删除源
/// </summary>
/// <param name="sourceid">源</param>
/// <returns>是否成功</returns>
public bool DeleteSource(string sourceid);
/// <summary>
/// 查询源
/// </summary>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public HDP_Source[] QuerySource(HDP_Command command);
/// <summary>
/// 获取表
/// </summary>
/// <param name="tableid">表编号</param>
/// <returns>结果</returns>
public HDP_Table? GetTable(string tableid);
/// <summary>
/// 创建表
/// </summary>
/// <param name="table">表</param>
/// <returns>是否成功</returns>
public bool InsertTable(HDP_Table table);
/// <summary>
/// 更新表
/// </summary>
/// <param name="table"></param>
/// <returns>是否成功</returns>
public bool UpdateTable(HDP_Table table);
/// <summary>
/// 删除表
/// </summary>
/// <param name="tableId"></param>
/// <returns>是否成功</returns>
public bool DeleteTable(string tableId);
/// <summary>
/// 查询表
/// </summary>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public HDP_Table[] QueryTable(HDP_Command command);
/// <summary>
/// 插入数据
/// </summary>
/// <param name="command">命令</param>
/// <returns>是否成功</returns>
public bool Insert(HDP_Command command);
/// <summary>
/// 更新数据
/// </summary>
/// <param name="command"></param>
/// <returns>是否成功</returns>
public bool Update(HDP_Command command);
/// <summary>
/// 删除数据
/// </summary>
/// <param name="command">命令</param>
/// <returns>是否成功</returns>
public bool Delete(HDP_Command command);
/// <summary>
/// 查询单条
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public T? QuerySingle<T>(HDP_Command command);
/// <summary>
/// 查询列表
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public T[] Query<T>(HDP_Command command);
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public HDP_Page<T> Page<T>(HDP_Command command);
}
}

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZKLT.Hadoop.Model;
namespace ZKLT.Hadoop.Interface
{
public interface ITableService
{
/// <summary>
/// 同步结构
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <returns>是否成功</returns>
public bool InitStruct(HDP_Source source, HDP_Table table);
/// <summary>
/// 删除结构
/// </summary>
/// <param name="source">源</param>
/// <param name="tableKey">表</param>
/// <returns>是否成功</returns>
public bool RemoveStruct(HDP_Source source, string tableKey);
/// <summary>
/// 插入数据
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="row">数据</param>
/// <returns>是否成功</returns>
public bool Insert(HDP_Source source, HDP_Table table, Dictionary<string, object> row);
/// <summary>
/// 更新
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>是否成功</returns>
public bool Update(HDP_Source source, HDP_Table table, Dictionary<string, string> where, Dictionary<string, object> row);
/// <summary>
/// 删除
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>是否成功</returns>
public bool Delete(HDP_Source source, HDP_Table table, Dictionary<string, string> where, Dictionary<string, object> row);
/// <summary>
/// 查询单个
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果</returns>
public T? QuerySingle<T>(HDP_Source source, HDP_Table table, Dictionary<string, string> where, Dictionary<string, object> row);
/// <summary>
/// 查询列表
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public T[] Query<T>(HDP_Source source, HDP_Table table, Dictionary<string, string>? where = null, Dictionary<string, object>? row = null, Dictionary<string, string>? order = null);
/// <summary>
/// 查询列表
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary<string, string>? where = null, Dictionary<string, object>? row = null, Dictionary<string, string>? order = null);
/// <summary>
/// 判断数据源是否存在表
/// </summary>
/// <param name="source">数据源</param>
/// <param name="tableName">表名</param>
/// <returns>是否存在</returns>
public bool DbExistTable(HDP_Source source, string tableName);
/// <summary>
/// 查询数据列
/// </summary>
/// <param name="source">数据源</param>
/// <param name="tableName">表名</param>
/// <returns>列</returns>
public HDP_Column[] DbGetColumns(HDP_Source source, string tableName);
}
}

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ZKLT.Hadoop.Model\ZKLT.Hadoop.Model.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 数据列
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
[HDP_Table(Key = "HDP_Column", Description = "列")]
public class HDP_Column : Attribute
{
private string? _Id;
private string? _TableId;
private string? _Key;
private bool _IsPrimary;
private string? _Description;
private string? _DataType;
private int _Length;
private int _DecimalLength;
/// <summary>
/// 编号
/// </summary>
[HDP_Column(Key = "Id",Description = "编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR, IsPrimary = true)]
public string? Id { get => _Id; set => _Id = value; }
/// <summary>
/// 表编号
/// </summary>
[HDP_Column(Key = "TableId",Description = "表编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? TableId { get => _TableId; set => _TableId = value; }
/// <summary>
/// 列名
/// </summary>
[HDP_Column(Key = "Key", Description = "列名", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Key { get => _Key; set => _Key = value; }
/// <summary>
/// 是否主键
/// </summary>
[HDP_Column(Key = "IsPrimary", Description = "是否主键", DataType = HDP_ColumnDataType.BOOL)]
public bool IsPrimary { get => _IsPrimary; set => _IsPrimary = value; }
/// <summary>
/// 描述
/// </summary>
[HDP_Column(Key = "Description", Description = "描述",Length = 200, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Description { get => _Description; set => _Description = value; }
/// <summary>
/// 数据类型
/// </summary>
[HDP_Column(Key = "DataType", Description = "数据类型", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? DataType { get => _DataType; set => _DataType = value; }
/// <summary>
/// 长度
/// </summary>
[HDP_Column(Key = "Length", Description = "长度", DataType = HDP_ColumnDataType.INT)]
public int Length { get => _Length; set => _Length = value; }
/// <summary>
/// 小数长度
/// </summary>
[HDP_Column(Key = "DecimalLength", Description = "小数长度", DataType = HDP_ColumnDataType.INT)]
public int DecimalLength { get => _DecimalLength; set => _DecimalLength = value; }
}
}

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 列数据类型
/// </summary>
public class HDP_ColumnDataType
{
/// <summary>
/// 整数
/// </summary>
public const string INT = "INT";
/// <summary>
/// 大整数
/// </summary>
public const string BIGINT = "BIGINT";
/// <summary>
/// 浮点数
/// </summary>
public const string DECIMAL = "DECIMAL";
/// <summary>
/// 字符串
/// </summary>
public const string VARCHAR = "VARCHAR";
/// <summary>
/// 日期
/// </summary>
public const string DATETIME = "DATETIME";
/// <summary>
/// 布尔值
/// </summary>
public const string BOOL = "BOOL";
}
}

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 命令
/// </summary>
public class HDP_Command
{
private string? _SourceId;
private string? _TableId;
private int? _PageIndex;
private int? _PageSize;
private Dictionary<string, string>? _Where;
private Dictionary<string, object>? _Data;
private Dictionary<string, string>? _Order;
/// <summary>
/// 源
/// </summary>
public string? SourceId { get => _SourceId; set => _SourceId = value; }
/// <summary>
/// 表
/// </summary>
public string? TableId { get => _TableId; set => _TableId = value; }
/// <summary>
/// 条件
/// </summary>
public Dictionary<string, string>? Where { get => _Where; set => _Where = value; }
/// <summary>
/// 数据
/// </summary>
public Dictionary<string, object>? Data { get => _Data; set => _Data = value; }
/// <summary>
/// 分页下标
/// </summary>
public int? PageIndex { get => _PageIndex; set => _PageIndex = value; }
/// <summary>
/// 分页大小
/// </summary>
public int? PageSize { get => _PageSize; set => _PageSize = value; }
/// <summary>
/// 排序
/// </summary>
public Dictionary<string, string>? Order { get => _Order; set => _Order = value; }
}
}

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 分页数据
/// </summary>
public class HDP_Page<T>
{
private int _PageIndex;
private int _PageSize;
private int _Total;
private T?[]? _Data;
/// <summary>
/// 分页下标
/// </summary>
public int PageIndex { get => _PageIndex; set => _PageIndex = value; }
/// <summary>
/// 分页大小
/// </summary>
public int PageSize { get => _PageSize; set => _PageSize = value; }
/// <summary>
/// 总条数
/// </summary>
public int Total { get => _Total; set => _Total = value; }
/// <summary>
/// 数据
/// </summary>
public T?[]? Data { get => _Data; set => _Data = value; }
}
}

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 源
/// </summary>
[HDP_Table(Key = "HDP_Source",Description = "源")]
public class HDP_Source
{
private string? _Id;
private string? _Key;
private string? _Host;
private int? _Port;
private string? _Account;
private string? _PassWord;
private string? _Description;
/// <summary>
/// 编号
/// </summary>
[HDP_Column(Key = "Id", Description = "编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR, IsPrimary = true)]
public string? Id { get => _Id; set => _Id = value; }
/// <summary>
/// 键
/// </summary>
[HDP_Column(Key = "Key", Description = "键", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Key { get => _Key; set => _Key = value; }
/// <summary>
/// 主机
/// </summary>
[HDP_Column(Key = "Host", Description = "主机", Length = 200, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Host { get => _Host; set => _Host = value; }
/// <summary>
/// 端口
/// </summary>
[HDP_Column(Key = "Port", Description = "端口", DataType = HDP_ColumnDataType.INT)]
public int? Port { get => _Port; set => _Port = value; }
/// <summary>
/// 用户名
/// </summary>
[HDP_Column(Key = "Account", Description = "用户名",Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Account { get => _Account; set => _Account = value; }
/// <summary>
/// 密码
/// </summary>
[HDP_Column(Key = "PassWord", Description = "密码", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? PassWord { get => _PassWord; set => _PassWord = value; }
/// <summary>
/// 描述
/// </summary>
[HDP_Column(Key = "Description", Description = "描述", Length = 200, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Description { get => _Description; set => _Description = value; }
/// <summary>
/// 获取连接字符串
/// </summary>
/// <returns>连接字符串</returns>
public string GetConnectString() {
return @$"Data Source={Host};Port={Port};Database={Key};User Id={Account};
Pwd={PassWord};Charset=utf8;Pooling=true;";
}
}
}

@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 表
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
[HDP_Table(Key = "HDP_Table", Description = "表")]
public class HDP_Table : Attribute
{
private string? _Id;
private string? _SourceId;
private string? _Key;
private string? _Description;
private HDP_Column[]? _Columns;
/// <summary>
/// 编号
/// </summary>
[HDP_Column(Key = "Id", Description = "编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR, IsPrimary = true)]
public string? Id { get => _Id; set => _Id = value; }
/// <summary>
/// 源编号
/// </summary>
[HDP_Column(Key = "SourceId", Description = "源编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? SourceId { get => _SourceId; set => _SourceId = value; }
/// <summary>
/// 键
/// </summary>
[HDP_Column(Key = "Key", Description = "键", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Key { get => _Key; set => _Key = value; }
/// <summary>
/// 描述
/// </summary>
[HDP_Column(Key = "Description", Description = "描述", Length = 200, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Description { get => _Description; set => _Description = value; }
/// <summary>
/// 列
/// </summary>
public HDP_Column[]? Columns { get => _Columns; set => _Columns = value; }
/// <summary>
/// 类转换Table
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
public static HDP_Table Class2Table<T>()
{
Type _type = typeof(T);
var _tableAttribute = _type.GetCustomAttribute<HDP_Table>();
if (_tableAttribute == null)
{
throw new ArgumentNullException("Table标记不存在");
}
if (string.IsNullOrEmpty(_tableAttribute.Key))
{
_tableAttribute.Key = _type.Name;
}
if (string.IsNullOrEmpty(_tableAttribute.Id))
{
_tableAttribute.Id = _tableAttribute.Key;
}
var _properties = _type.GetProperties();
var _columns = new List<HDP_Column>();
foreach (var _property in _properties)
{
var _column = _property.GetCustomAttribute<HDP_Column>();
if (_column == null)
{
continue;
}
if (string.IsNullOrEmpty(_column.TableId))
{
_column.TableId = _tableAttribute.Id;
}
if (string.IsNullOrEmpty(_column.Key))
{
_column.Key = _property.Name;
}
if (string.IsNullOrEmpty(_column.Id))
{
_column.Id = _column.Key;
}
_columns.Add(_column);
}
_tableAttribute.Columns = _columns.ToArray();
return _tableAttribute;
}
/// <summary>
/// 类
/// </summary>
/// <param name="data">数据</param>
/// <returns>字典</returns>
public static Dictionary<string, object> Class2Dictionary(object data)
{
var _result = new Dictionary<string, object>();
Type _type = data.GetType();
var _properties = _type.GetProperties();
foreach (var _property in _properties)
{
_result.Add(_property.Name, _property.GetValue(data)!);
}
return _result;
}
}
}

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
public class HDP_WhereType
{
public const string EQUAL = "=";
public const string NOTEQUAL = "!=";
public const string LIKE = "LIKE";
public const string MORE = ">";
public const string LESS = "<";
public const string MORETHEN = ">=";
public const string LESSTHEN = "<=";
}
}

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="HDP_Table.cs~RF4db01d6c.TMP" />
</ItemGroup>
</Project>

@ -0,0 +1,589 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using MySqlX.XDevAPI.Relational;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using ZKLT.Hadoop.Interface;
using ZKLT.Hadoop.Model;
namespace ZKLT.Hadoop
{
/// <summary>
/// 云计算扩展
/// </summary>
public static class HadoopServiceExtend
{
/// <summary>
/// 注入云计算服务
/// </summary>
/// <param name="services">服务集合</param>
/// <returns></returns>
public static IServiceCollection AddHadoop(this IServiceCollection services)
{
services.AddSingleton<IHadoopService, HadoopService>();
services.AddSingleton<ITableService, TableService>();
return services;
}
/// <summary>
/// 应用云计算
/// </summary>
/// <param name="app">应用</param>
/// <param name="config">源配置</param>
/// <returns></returns>
public static IApplicationBuilder UseHadoop(this IApplicationBuilder app, Action<HDP_Source> config)
{
var _HadoopService = app.ApplicationServices.GetRequiredService<IHadoopService>();
_HadoopService.Init(config);
return app;
}
}
/// <summary>
/// 云计算服务
/// </summary>
public class HadoopService : IHadoopService
{
public HadoopService(ITableService tableService)
{
_TableService = tableService;
_Source = new HDP_Source();
_Tables = new List<HDP_Table>();
}
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 Dictionary<string, string>
{
{ "Id","=" }
}, new Dictionary<string, object> {
{ "Id",sourceid}
});
return _result;
}
/// <summary>
/// 创建源
/// </summary>
/// <param name="source">源</param>
/// <returns>是否成功</returns>
public bool InsertSource(HDP_Source source)
{
//校验
if (string.IsNullOrEmpty(source.Id))
{
throw new ArgumentNullException("编号无效");
}
if (string.IsNullOrEmpty(source.Host))
{
throw new ArgumentNullException("主机无效");
}
if (source.Port == null)
{
throw new ArgumentNullException("端口无效");
}
if (string.IsNullOrEmpty(source.Key))
{
throw new ArgumentNullException("数据源键无效");
}
if (string.IsNullOrEmpty(source.Account))
{
throw new ArgumentNullException("用户名无效");
}
if (string.IsNullOrEmpty(source.PassWord))
{
throw new ArgumentNullException("密码无效");
}
if (GetSource(source.Id) != null)
{
throw new ArgumentException("编号已存在");
}
return _TableService.Insert(_Source, GetTable("HDP_Source")!, HDP_Table.Class2Dictionary(source));
}
/// <summary>
/// 更新源
/// </summary>
/// <param name="source">源</param>
/// <returns>是否成功</returns>
public bool UpdateSource(HDP_Source source)
{
//校验
if (string.IsNullOrEmpty(source.Id))
{
throw new ArgumentNullException("编号无效");
}
if (string.IsNullOrEmpty(source.Host))
{
throw new ArgumentNullException("主机无效");
}
if (source.Port == null)
{
throw new ArgumentNullException("端口无效");
}
if (string.IsNullOrEmpty(source.Key))
{
throw new ArgumentNullException("数据源键无效");
}
if (string.IsNullOrEmpty(source.Account))
{
throw new ArgumentNullException("用户名无效");
}
if (string.IsNullOrEmpty(source.PassWord))
{
throw new ArgumentNullException("密码无效");
}
if (GetSource(source.Id) == null)
{
throw new ArgumentException("编号不存在");
}
return _TableService.Update(_Source, GetTable("HDP_Source")!, new Dictionary<string, string> {
{"Id","=" }
}, HDP_Table.Class2Dictionary(source));
}
/// <summary>
/// 删除源
/// </summary>
/// <param name="sourceid">源</param>
/// <returns>是否成功</returns>
public bool DeleteSource(string sourceid)
{
//校验
if (string.IsNullOrEmpty(sourceid))
{
throw new ArgumentNullException("编号无效");
}
if (GetSource(sourceid) == null)
{
throw new ArgumentException("编号不存在");
}
return _TableService.Delete(_Source, GetTable("HDP_Source")!, new Dictionary<string, string> {
{"Id","=" }
}, new Dictionary<string, object> {
{"Id",sourceid }
});
}
/// <summary>
/// 查询源
/// </summary>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public HDP_Source[] QuerySource(HDP_Command command)
{
return _TableService.Query<HDP_Source>(_Source, GetTable("HDP_Source")!, command.Where!, command.Data!, command.Order!);
}
/// <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 Dictionary<string, string>
{
{ "Id","=" }
}, new Dictionary<string, object> {
{ "Id",tableid}
});
if (_result != null)
{
_result.Columns = _TableService.Query<HDP_Column>(_Source, GetTable("HDP_Column")!, new Dictionary<string, string> {
{ "TableId","="}
}, new Dictionary<string, object> {
{"TableId",_result.Id! }
});
}
return _result;
}
/// <summary>
/// 创建表
/// </summary>
/// <param name="table">表</param>
/// <returns>是否成功</returns>
public bool InsertTable(HDP_Table 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())
{
if (!_TableService.Insert(_Source, GetTable("HDP_Table")!, HDP_Table.Class2Dictionary(table)))
{
return false;
}
for (var i = 0; i < table.Columns!.Length; i++)
{
var _column = table.Columns![i];
_column.TableId = table.Id;
if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2Dictionary(_column)))
{
return false;
}
}
_scope.Complete();
return true;
}
}
else
{
return false;
}
}
/// <summary>
/// 更新表
/// </summary>
/// <param name="table">表</param>
/// <returns>是否成功</returns>
public bool UpdateTable(HDP_Table 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())
{
if (!_TableService.Update(_Source, GetTable("HDP_Table")!, new Dictionary<string, string> {
{ "Id","="}
}, HDP_Table.Class2Dictionary(table)))
{
return false;
}
for (var i = 0; i < table.Columns!.Length; i++)
{
var _column = table.Columns![i];
_column.TableId = table.Id;
if (_TableService.QuerySingle<HDP_Column>(_Source, GetTable("HDP_Column")!, new Dictionary<string, string>
{
{"Id","=" }
}, HDP_Table.Class2Dictionary(_column)) == null)
{
if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2Dictionary(_column)))
{
return false;
}
}
else
{
if (!_TableService.Update(_Source, GetTable("HDP_Column")!, new Dictionary<string, string> {
{"Id","=" }
}, HDP_Table.Class2Dictionary(_column)))
{
return false;
}
}
}
_scope.Complete();
return true;
}
}
else
{
return false;
}
}
/// <summary>
/// 删除表
/// </summary>
/// <param name="tableId">表编号</param>
/// <returns>是否成功</returns>
public bool DeleteTable(string tableId)
{
//校验
if (string.IsNullOrEmpty(tableId))
{
throw new ArgumentNullException("编号无效");
}
if (GetTable(tableId) == null)
{
throw new ArgumentException("编号不存在");
}
using (TransactionScope _scope = new TransactionScope())
{
if (!_TableService.Delete(_Source, GetTable("HDP_Table")!, new Dictionary<string, string> {
{"Id","=" }
}, new Dictionary<string, object> {
{"Id",tableId }
}))
{
return false;
}
if (!_TableService.Delete(_Source, GetTable("HDP_Column")!, new Dictionary<string, string> {
{ "TableId","="}
}, new Dictionary<string, object> {
{"TableId",tableId }
}))
{
return false;
}
_scope.Complete();
return true;
}
}
/// <summary>
/// 查询表
/// </summary>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public HDP_Table[] QueryTable(HDP_Command command)
{
return _TableService.Query<HDP_Table>(_Source, GetTable("HDP_Table")!, command.Where!, command.Data!, command.Order!);
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="command">命令</param>
/// <returns>是否成功</returns>
public bool Insert(HDP_Command command)
{
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>
/// <param name="command"></param>
/// <returns>是否成功</returns>
public bool Update(HDP_Command command)
{
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>
/// <param name="command">命令</param>
/// <returns>是否成功</returns>
public bool Delete(HDP_Command command)
{
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>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public T? QuerySingle<T>(HDP_Command command)
{
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!);
}
/// <summary>
/// 查询列表
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public T[] Query<T>(HDP_Command command)
{
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!);
}
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="command">命令</param>
/// <returns>结果</returns>
public HDP_Page<T> Page<T>(HDP_Command command)
{
if (string.IsNullOrEmpty(command.TableId))
{
throw new ArgumentNullException("表无效");
}
if (command.PageIndex == null || command.PageIndex <= 0)
{
throw new ArgumentNullException("分页下标无效");
}
if (command.PageSize == null || command.PageSize <= 0)
{
throw new ArgumentNullException("分页大小无效");
}
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!);
}
}
}

@ -0,0 +1,828 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Dapper;
using ZKLT.Hadoop.Interface;
using ZKLT.Hadoop.Model;
using MySqlX.XDevAPI.Relational;
using Mysqlx.Crud;
namespace ZKLT.Hadoop
{
public class TableService : ITableService
{
/// <summary>
/// 同步结构
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <returns>是否成功</returns>
public bool InitStruct(HDP_Source source, HDP_Table table)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
StringBuilder _command = new StringBuilder();
if (DbExistTable(source, table.Key))
{
var _dbColumns = DbGetColumns(source, table.Key);
_command.AppendLine($@"ALTER TABLE `{table.Key}`");
_command.AppendLine("DROP PRIMARY KEY,");
//加载列
for (var i = 0; i < table.Columns.Length; i++)
{
StringBuilder _colstr = new StringBuilder();
var _column = table.Columns[i];
if (_dbColumns.Any(x => x.Key == _column.Key))
{
_colstr.Append($@"MODIFY `{_column.Key}`");
}
else
{
_colstr.Append($@"ADD `{_column.Key}`");
}
switch (_column.DataType)
{
case HDP_ColumnDataType.VARCHAR:
if (_column.Length == 0)
{
throw new ArgumentNullException(@$"列{_column.Key}长度无效");
}
_colstr.Append($@" {_column.DataType}({_column.Length})");
break;
case HDP_ColumnDataType.DECIMAL:
if (_column.Length == 0 || _column.DecimalLength == 0)
{
throw new ArgumentNullException(@$"列{_column.Key}长度无效");
}
_colstr.Append($@" {_column.DataType}({_column.Length},{_column.DecimalLength})");
break;
default:
_colstr.Append(@$" {_column.DataType}");
break;
}
if (!string.IsNullOrEmpty(_column.Description))
{
_colstr.Append($@" COMMENT '{_column.Description}'");
}
_colstr.Append(",");
_command.AppendLine(_colstr.ToString());
}
//加载主键
var _primarys = table.Columns.Where(x => x.IsPrimary == true).ToArray();
if (_primarys.Length > 0)
{
StringBuilder _primaryStr = new StringBuilder();
_primaryStr.Append("ADD PRIMARY KEY (");
for (var i = 0; i < _primarys.Length; i++)
{
var _primary = _primarys[i];
_primaryStr.Append($@"`{_primary.Key}`");
if (i < _primarys.Length - 1)
{
_primaryStr.Append(",");
}
}
_primaryStr.Append(")");
_command.AppendLine(_primaryStr.ToString());
}
if (!string.IsNullOrEmpty(table.Description))
{
_command.AppendLine(@$"COMMENT '{table.Description}'");
}
_connection.Execute(_command.ToString());
return true;
}
else
{
_command.AppendLine($@"CREATE TABLE `{table.Key}` (");
//加载列
for (int i = 0; i < table.Columns.Length; i++)
{
StringBuilder _colstr = new StringBuilder();
var _column = table.Columns[i];
_colstr.Append($@"`{_column.Key}`");
switch (_column.DataType)
{
case HDP_ColumnDataType.VARCHAR:
if (_column.Length == 0)
{
throw new ArgumentNullException(@$"列{_column.Key}长度无效");
}
_colstr.Append($@" {_column.DataType}({_column.Length})");
break;
case HDP_ColumnDataType.DECIMAL:
if (_column.Length == 0 || _column.DecimalLength == 0)
{
throw new ArgumentNullException(@$"列{_column.Key}长度无效");
}
_colstr.Append($@" {_column.DataType}({_column.Length},{_column.DecimalLength})");
break;
default:
_colstr.Append(@$" {_column.DataType}");
break;
}
if (!string.IsNullOrEmpty(_column.Description))
{
_colstr.Append($@" COMMENT '{_column.Description}'");
}
if (i < table.Columns.Length - 1)
{
_colstr.Append(",");
}
_command.AppendLine(_colstr.ToString());
}
//加载主键
var _primarys = table.Columns.Where(x => x.IsPrimary == true).ToArray();
if (_primarys.Length > 0)
{
_command.Append(",");
StringBuilder _primaryStr = new StringBuilder();
_primaryStr.Append("PRIMARY KEY (");
for (var i = 0; i < _primarys.Length; i++)
{
var _primary = _primarys[i];
_primaryStr.Append($@"`{_primary.Key}`");
if (i < _primarys.Length - 1)
{
_primaryStr.Append(",");
}
}
_primaryStr.Append(")");
_command.AppendLine(_primaryStr.ToString());
}
_command.AppendLine(")");
if (!string.IsNullOrEmpty(table.Description))
{
_command.AppendLine(@$"COMMENT '{table.Description}'");
}
_connection.Execute(_command.ToString());
}
_connection.Close();
return true;
}
}
/// <summary>
/// 删除结构
/// </summary>
/// <param name="source">源</param>
/// <param name="tableKey">表</param>
/// <returns>是否成功</returns>
public bool RemoveStruct(HDP_Source source, string tableKey)
{
//数据校验
if (string.IsNullOrEmpty(tableKey))
{
throw new ArgumentNullException("表键无效");
}
if (!DbExistTable(source, tableKey))
{
throw new ArgumentNullException("表不存在");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
StringBuilder _command = new StringBuilder();
_command.AppendLine(@$"DROP TABLE {tableKey}");
_connection.Execute(_command.ToString());
_connection.Close();
return !DbExistTable(source, tableKey);
}
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="row">数据</param>
/// <returns>是否成功</returns>
public bool Insert(HDP_Source source, HDP_Table table, Dictionary<string, object> row)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
if (row == null || row.Count == 0)
{
throw new ArgumentNullException("数据无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
StringBuilder _command = new StringBuilder();
//主键检查
var _primarys = table.Columns.Where(x => x.IsPrimary == true).ToArray();
for (var i = 0; i < _primarys.Length; i++)
{
var _primary = _primarys[i];
if (row[_primary.Key!] == null || string.IsNullOrEmpty(row[_primary.Key!].ToString()))
{
throw new ArgumentException($@"主键{_primary.Key}值无效");
}
}
//插入命令
_command.AppendLine($@"INSERT INTO `{table.Key}` (");
StringBuilder _colstr = new StringBuilder();
StringBuilder _parmstr = new StringBuilder();
Dictionary<string, object> _params = new Dictionary<string, object>();
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (row.ContainsKey(_column.Key!))
{
_colstr.Append($@"`{_column.Key!}`,");
_parmstr.Append($@"@{_column.Key},");
_params.Add(_column.Key!, row[_column.Key!]);
}
}
if (_colstr[_colstr.Length - 1] == ',')
{
_colstr.Remove(_colstr.Length - 1, 1);
}
if (_parmstr[_parmstr.Length - 1] == ',')
{
_parmstr.Remove(_parmstr.Length - 1, 1);
}
_command.AppendLine(_colstr.ToString());
_command.AppendLine(") VALUES (");
_command.AppendLine(_parmstr.ToString());
_command.AppendLine(")");
var _result = _connection.Execute(_command.ToString(), _params);
_connection.Close();
if (_result > 0)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 更新
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>是否成功</returns>
public bool Update(HDP_Source source, HDP_Table table, Dictionary<string, string> where, Dictionary<string, object> row)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
if (where == null || where.Count == 0)
{
throw new ArgumentNullException("条件无效");
}
if (row == null || row.Count == 0)
{
throw new ArgumentNullException("数据无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
//更新命令
StringBuilder _command = new StringBuilder();
Dictionary<string, object> _params = new Dictionary<string, object>();
_command.AppendLine(@$"UPDATE `{table.Key}` SET ");
//更新列
StringBuilder _colstr = new StringBuilder();
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (row.ContainsKey(_column.Key!) && !where.ContainsKey(_column.Key!))
{
_colstr.Append($@"`{_column.Key!}`=@{_column.Key!},");
_params.Add(_column.Key!, row[_column.Key!]);
}
}
if (_colstr[_colstr.Length - 1] == ',')
{
_colstr.Remove(_colstr.Length - 1, 1);
}
_command.AppendLine(_colstr.ToString());
//执行条件
StringBuilder _wherestr = new StringBuilder();
_wherestr.Append("WHERE 1 = 1");
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (where.ContainsKey(_column.Key!))
{
switch (where[_column.Key!])
{
case HDP_WhereType.LIKE:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
break;
default:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
break;
}
_params.Add(_column.Key!, row[_column.Key!]);
}
}
_command.AppendLine(_wherestr.ToString());
var _result = _connection.Execute(_command.ToString(), _params);
_connection.Close();
if (_result > 0)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>是否成功</returns>
public bool Delete(HDP_Source source, HDP_Table table, Dictionary<string, string> where, Dictionary<string, object> row)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
if (where == null || where.Count == 0)
{
throw new ArgumentNullException("条件无效");
}
if (row == null || row.Count == 0)
{
throw new ArgumentNullException("数据无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
//更新命令
StringBuilder _command = new StringBuilder();
Dictionary<string, object> _params = new Dictionary<string, object>();
_command.AppendLine(@$"DELETE FROM `{table.Key}`");
//执行条件
StringBuilder _wherestr = new StringBuilder();
_wherestr.Append("WHERE 1 = 1");
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (where.ContainsKey(_column.Key!))
{
switch (where[_column.Key!])
{
case HDP_WhereType.LIKE:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
break;
default:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
break;
}
_params.Add(_column.Key!, row[_column.Key!]);
}
}
_command.AppendLine(_wherestr.ToString());
var _result = _connection.Execute(_command.ToString(), _params);
_connection.Close();
if (_result > 0)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 查询单个
/// </summary>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果</returns>
public T? QuerySingle<T>(HDP_Source source, HDP_Table table, Dictionary<string, string> where, Dictionary<string, object> row)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
if (where == null || where.Count == 0)
{
throw new ArgumentNullException("条件无效");
}
if (row == null || row.Count == 0)
{
throw new ArgumentNullException("数据无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
//查询命令
StringBuilder _command = new StringBuilder();
Dictionary<string, object> _params = new Dictionary<string, object>();
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
//执行条件
StringBuilder _wherestr = new StringBuilder();
_wherestr.Append("WHERE 1 = 1");
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (where.ContainsKey(_column.Key!))
{
switch (where[_column.Key!])
{
case HDP_WhereType.LIKE:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
break;
default:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
break;
}
_params.Add(_column.Key!, row[_column.Key!]);
}
}
_command.AppendLine(_wherestr.ToString());
var _result = _connection.Query<T>(_command.ToString(), _params).ToArray();
_connection.Close();
if (_result.Length > 0)
{
return _result[0];
}
else
{
return default(T);
}
}
}
/// <summary>
/// 查询列表
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public T[] Query<T>(HDP_Source source, HDP_Table table, Dictionary<string, string>? where = null, Dictionary<string, object>? row = null, Dictionary<string, string>? order = null)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
//查询命令
StringBuilder _command = new StringBuilder();
Dictionary<string, object> _params = new Dictionary<string, object>();
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
//执行条件
StringBuilder _wherestr = new StringBuilder();
_wherestr.Append("WHERE 1 = 1");
if (where != null && where.Count > 0)
{
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (where.ContainsKey(_column.Key!))
{
switch (where[_column.Key!])
{
case HDP_WhereType.LIKE:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
break;
default:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
break;
}
_params.Add(_column.Key!, row[_column.Key!]);
}
}
_command.AppendLine(_wherestr.ToString());
}
//执行排序
StringBuilder _orderstr = new StringBuilder();
_orderstr.Append("ORDER BY ");
if (order != null && order.Count > 0)
{
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (order.ContainsKey(_column.Key!))
{
switch (order[_column.Key!])
{
case "DESC":
_orderstr.Append($@"`{_column.Key!}` DESC,");
break;
default:
_orderstr.Append($@"`{_column.Key!}` ASC,");
break;
}
}
}
if (_orderstr[_orderstr.Length - 1] == ',')
{
_orderstr.Remove(_orderstr.Length - 1, 1);
}
_command.AppendLine(_orderstr.ToString());
}
var _result = _connection.Query<T>(_command.ToString(), _params);
_connection.Close();
return _result.ToArray();
}
}
/// <summary>
/// 查询列表
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="table">数据表</param>
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary<string, string>? where = null, Dictionary<string, object>? row = null, Dictionary<string, string>? order = null)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
{
throw new ArgumentNullException("表键无效");
}
if (table.Columns == null || table.Columns.Length == 0)
{
throw new ArgumentNullException("列无效");
}
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
//查询命令
StringBuilder _command = new StringBuilder();
Dictionary<string, object> _params = new Dictionary<string, object>();
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
//执行条件
StringBuilder _wherestr = new StringBuilder();
_wherestr.Append("WHERE 1 = 1");
if (where != null && where.Count > 0)
{
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (where.ContainsKey(_column.Key!))
{
switch (where[_column.Key!])
{
case HDP_WhereType.LIKE:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
break;
default:
_wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
break;
}
_params.Add(_column.Key!, row[_column.Key!]);
}
}
_command.AppendLine(_wherestr.ToString());
}
//执行排序
StringBuilder _orderstr = new StringBuilder();
_orderstr.Append("ORDER BY ");
if (order != null && order.Count > 0)
{
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (order.ContainsKey(_column.Key!))
{
switch (order[_column.Key!])
{
case "DESC":
_orderstr.Append($@"`{_column.Key!}` DESC,");
break;
default:
_orderstr.Append($@"`{_column.Key!}` ASC,");
break;
}
}
}
if (_orderstr[_orderstr.Length - 1] == ',')
{
_orderstr.Remove(_orderstr.Length - 1, 1);
}
_command.AppendLine(_orderstr.ToString());
}
var _result = new HDP_Page<T>();
_result.PageIndex = pageIndex;
_result.PageSize = pageSize;
StringBuilder _totalstr = new StringBuilder();
_totalstr.AppendLine($@"SELECT COUNT(0) FROM (");
_totalstr.AppendLine(_command.ToString());
_totalstr.AppendLine(") AS Temp");
_result.Total = _connection.QuerySingle<int>(_totalstr.ToString(), _params);
StringBuilder _pagestr = new StringBuilder();
_pagestr.AppendLine($@"SELECT * FROM (");
_pagestr.AppendLine(_command.ToString());
_pagestr.AppendLine(") AS Temp");
_pagestr.AppendLine(@$"LIMIT {(_result.PageIndex - 1) * _result.PageSize},{_result.PageSize}");
_result.Data = _connection.Query<T>(_pagestr.ToString(), _params).ToArray();
_connection.Close();
return _result;
}
}
/// <summary>
/// 判断数据源是否存在表
/// </summary>
/// <param name="source">数据源</param>
/// <param name="tableName">表名</param>
/// <returns>是否存在</returns>
public bool DbExistTable(HDP_Source source, string tableName)
{
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
StringBuilder _command = new StringBuilder();
_command.AppendLine($@"SELECT COUNT(0) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_TYPE`='BASE TABLE' AND `TABLE_SCHEMA`=@SourceKey AND `TABLE_NAME` =@TableKey");
int _result = _connection.QuerySingle<int>(_command.ToString(), new { SourceKey = source.Key, TableKey = tableName });
_connection.Close();
if (_result > 0)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 查询数据列
/// </summary>
/// <param name="source">数据源</param>
/// <param name="tableName">表名</param>
/// <returns>列</returns>
public HDP_Column[] DbGetColumns(HDP_Source source, string tableName)
{
using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString()))
{
var _result = new List<HDP_Column>();
try
{
_connection.Open();
}
catch
{
throw new ArgumentException("数据源连接失败");
}
StringBuilder _command = new StringBuilder();
_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");
_result.AddRange(_connection.Query<HDP_Column>(_command.ToString(), new { SourceKey = source.Key, TableKey = tableName }));
_connection.Close();
return _result.ToArray();
}
}
}
}

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="MySql.Data" Version="8.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZKLT.Hadoop.Interface\ZKLT.Hadoop.Interface.csproj" />
<ProjectReference Include="..\ZKLT.Hadoop.Model\ZKLT.Hadoop.Model.csproj" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>

@ -0,0 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZKLT.Hadoop", "Hadoop\ZKLT.Hadoop\ZKLT.Hadoop.csproj", "{CD7387DB-B80A-412E-89B9-830EFD28C0F4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Hadoop", "Hadoop", "{14EA48C3-3F3A-4789-BB0B-5485771D3840}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZKLT.Hadoop.Model", "Hadoop\ZKLT.Hadoop.Model\ZKLT.Hadoop.Model.csproj", "{070B12FF-0B5A-4D0B-B444-70D6F91FB338}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZKLT.Hadoop.Interface", "Hadoop\ZKLT.Hadoop.Interface\ZKLT.Hadoop.Interface.csproj", "{893FE0B2-8D14-42BB-B19E-0FD09EEA2433}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZKLT.Hadoop.API", "Hadoop\ZKLT.Hadoop.API\ZKLT.Hadoop.API.csproj", "{398DF3E5-B94B-4B2E-9DC6-B6741D7CF4DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CD7387DB-B80A-412E-89B9-830EFD28C0F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD7387DB-B80A-412E-89B9-830EFD28C0F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD7387DB-B80A-412E-89B9-830EFD28C0F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD7387DB-B80A-412E-89B9-830EFD28C0F4}.Release|Any CPU.Build.0 = Release|Any CPU
{070B12FF-0B5A-4D0B-B444-70D6F91FB338}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{070B12FF-0B5A-4D0B-B444-70D6F91FB338}.Debug|Any CPU.Build.0 = Debug|Any CPU
{070B12FF-0B5A-4D0B-B444-70D6F91FB338}.Release|Any CPU.ActiveCfg = Release|Any CPU
{070B12FF-0B5A-4D0B-B444-70D6F91FB338}.Release|Any CPU.Build.0 = Release|Any CPU
{893FE0B2-8D14-42BB-B19E-0FD09EEA2433}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{893FE0B2-8D14-42BB-B19E-0FD09EEA2433}.Debug|Any CPU.Build.0 = Debug|Any CPU
{893FE0B2-8D14-42BB-B19E-0FD09EEA2433}.Release|Any CPU.ActiveCfg = Release|Any CPU
{893FE0B2-8D14-42BB-B19E-0FD09EEA2433}.Release|Any CPU.Build.0 = Release|Any CPU
{398DF3E5-B94B-4B2E-9DC6-B6741D7CF4DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{398DF3E5-B94B-4B2E-9DC6-B6741D7CF4DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{398DF3E5-B94B-4B2E-9DC6-B6741D7CF4DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{398DF3E5-B94B-4B2E-9DC6-B6741D7CF4DB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CD7387DB-B80A-412E-89B9-830EFD28C0F4} = {14EA48C3-3F3A-4789-BB0B-5485771D3840}
{070B12FF-0B5A-4D0B-B444-70D6F91FB338} = {14EA48C3-3F3A-4789-BB0B-5485771D3840}
{893FE0B2-8D14-42BB-B19E-0FD09EEA2433} = {14EA48C3-3F3A-4789-BB0B-5485771D3840}
{398DF3E5-B94B-4B2E-9DC6-B6741D7CF4DB} = {14EA48C3-3F3A-4789-BB0B-5485771D3840}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {87BED7C0-F181-4EA3-85CD-6D146DA33FF3}
EndGlobalSection
EndGlobal
Loading…
Cancel
Save