2023-06-27 15:46:42 +08:00
|
|
|
|
using Aitex.Core.RT.Log;
|
|
|
|
|
using Npgsql;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
using System;
|
2023-06-27 15:46:42 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Data.Common;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
using System.Data;
|
2023-06-27 15:46:42 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
|
|
namespace Aitex.Core.RT.DBCore
|
|
|
|
|
{
|
2023-06-27 15:46:42 +08:00
|
|
|
|
public class PostgreSQLHelper : IDataBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据库名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string DBName;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string _connString = ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString;
|
|
|
|
|
|
|
|
|
|
public PostgreSQLHelper(string dBName)
|
|
|
|
|
{
|
|
|
|
|
DBName = dBName;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 18:21:15 +08:00
|
|
|
|
public string GetSqlByNameType(string name, Type type)
|
|
|
|
|
{
|
|
|
|
|
string sql = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (type == typeof(int) || type == typeof(ushort) || type == typeof(short))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" integer";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (type == typeof(double) || type == typeof(float))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" real";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (type == typeof(string))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" text";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (type == typeof(DateTime))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" timestamp without time zone";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (type == typeof(bool))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" boolean";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (type == typeof(byte[]))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" bytea";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (type == typeof(Int64))
|
|
|
|
|
{
|
2023-07-24 11:18:29 +08:00
|
|
|
|
sql = $"\"{name}\" bigint";
|
2023-07-10 18:21:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 15:46:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 准备Command
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cmd"></param>
|
|
|
|
|
/// <param name="conn"></param>
|
|
|
|
|
/// <param name="sql"></param>
|
2023-06-28 15:29:13 +08:00
|
|
|
|
/// <param name="isChangeDB"></param>
|
2023-06-27 15:46:42 +08:00
|
|
|
|
/// <param name="trans"></param>
|
2023-06-28 15:29:13 +08:00
|
|
|
|
/// <param name="columnsName"></param>
|
|
|
|
|
/// <param name="cmdParams"></param>
|
|
|
|
|
private void PrepareCommand(NpgsqlCommand cmd, NpgsqlConnection conn, string sql, bool isChangeDB = true,
|
|
|
|
|
NpgsqlTransaction trans = null, string[] columnsName = null, object[] cmdParams = null)
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
|
|
|
|
if (conn.State != ConnectionState.Open)
|
|
|
|
|
{
|
|
|
|
|
conn.Open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//切换数据库
|
|
|
|
|
if (isChangeDB)
|
|
|
|
|
{
|
|
|
|
|
conn.ChangeDatabase(DBName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.Connection = conn;
|
|
|
|
|
cmd.CommandText = sql;
|
|
|
|
|
cmd.CommandType = CommandType.Text;
|
|
|
|
|
|
|
|
|
|
//事务
|
|
|
|
|
if (trans != null)
|
|
|
|
|
{
|
|
|
|
|
cmd.Transaction = trans;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//参数
|
2023-06-28 15:29:13 +08:00
|
|
|
|
if (cmdParams != null && columnsName != null && cmdParams.Length != 0 && columnsName.Length == cmdParams.Length)
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
2023-06-28 15:29:13 +08:00
|
|
|
|
for (int i = 0; i < cmdParams.Length; i++)
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
2023-06-28 15:29:13 +08:00
|
|
|
|
cmd.Parameters.AddWithValue(columnsName[i], cmdParams[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (cmdParams != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (object value in cmdParams)
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.AddWithValue(string.Empty, value);
|
2023-06-27 15:46:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行SQL语句
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
2023-06-28 15:29:13 +08:00
|
|
|
|
/// <param name="isChangeDB"></param>
|
2023-06-27 15:46:42 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int ExecuteNonQuery(string sql, bool isChangeDB = true)
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand())
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-07-10 18:21:15 +08:00
|
|
|
|
PrepareCommand(cmd, conn, sql, isChangeDB);
|
2023-06-28 15:29:13 +08:00
|
|
|
|
count = cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Error("执行SQL语句出错," + sql, ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Clear();
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行带参数SQL语句
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <param name="columnsName"></param>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-07-10 18:21:15 +08:00
|
|
|
|
public int ExecuteNonQuery(string sql, string[] columnsName, params object[] args)
|
2023-06-28 15:29:13 +08:00
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand())
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-07-10 18:21:15 +08:00
|
|
|
|
PrepareCommand(cmd, conn, sql, true, null, columnsName, args);
|
2023-06-27 15:46:42 +08:00
|
|
|
|
count = cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Error("执行SQL语句出错," + sql, ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Clear();
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 返回查询结果的首行首列数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-06-28 18:26:50 +08:00
|
|
|
|
public object ExecuteScalar(string sql, bool isChangeDB = true)
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand())
|
|
|
|
|
{
|
|
|
|
|
object obj = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-06-28 18:26:50 +08:00
|
|
|
|
PrepareCommand(cmd, conn, sql, isChangeDB);
|
2023-06-27 15:46:42 +08:00
|
|
|
|
obj = cmd.ExecuteScalar();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Error("执行查询出错," + sql, ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Clear();
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询结果返回NpgsqlDataReader
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DbDataReader ExecuteReader(string sql)
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand())
|
|
|
|
|
{
|
|
|
|
|
DbDataReader reader = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
PrepareCommand(cmd, conn, sql);
|
|
|
|
|
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Error("执行查询出错," + sql, ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reader;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询结果返回DataSet
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DataSet ExecuteDataSet(string sql)
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand())
|
|
|
|
|
{
|
|
|
|
|
DataSet ds = new DataSet();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
PrepareCommand(cmd, conn, sql);
|
|
|
|
|
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
|
|
|
|
|
adapter.Fill(ds);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Error("执行查询出错," + sql, ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Clear();
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ds;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询结果返回DataTable
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DataTable ExecuteDataTable(string sql)
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand())
|
|
|
|
|
{
|
|
|
|
|
DataSet ds = new DataSet();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
PrepareCommand(cmd, conn, sql);
|
|
|
|
|
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
|
|
|
|
|
adapter.Fill(ds);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Error("执行查询出错," + sql, ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Clear();
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ds.Tables.Count > 0 ? ds.Tables[0] : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行事务
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sqlList"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool ExcuteTransAction(List<string> sqlList)
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
|
|
|
|
|
{
|
2023-07-18 14:19:03 +08:00
|
|
|
|
using(NpgsqlCommand cmd = new NpgsqlCommand())
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
2023-07-18 14:19:03 +08:00
|
|
|
|
PrepareCommand(cmd, conn, "");
|
|
|
|
|
using (NpgsqlTransaction trans = conn.BeginTransaction())
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
2023-07-18 14:19:03 +08:00
|
|
|
|
cmd.Transaction = trans;
|
|
|
|
|
try
|
2023-06-27 15:46:42 +08:00
|
|
|
|
{
|
2023-07-18 14:19:03 +08:00
|
|
|
|
//循环
|
|
|
|
|
foreach (string sql in sqlList)
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Clear();
|
|
|
|
|
cmd.CommandText = sql;
|
|
|
|
|
int val = cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
trans.Commit();
|
|
|
|
|
result = true;
|
2023-06-27 15:46:42 +08:00
|
|
|
|
}
|
2023-07-18 14:19:03 +08:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
trans.Rollback();
|
|
|
|
|
conn.Close();
|
|
|
|
|
LOG.Error("执行事务出错," + sqlList, ex);
|
2023-06-27 15:46:42 +08:00
|
|
|
|
|
2023-07-18 14:19:03 +08:00
|
|
|
|
result = false;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
conn.Close();
|
|
|
|
|
}
|
2023-06-27 15:46:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-18 14:19:03 +08:00
|
|
|
|
|
|
|
|
|
return result;
|
2023-06-27 15:46:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|