using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FirebirdSql.Data.FirebirdClient;
namespace FireBirdUtil.SqlUtils
{
public static class SQLExecuteUtil_Transaction
{//TODO:是否增加方法:用FbCommand参数,替代FbConnection参数的方法
///
/// 在事务中:插入数据
///
/// 连接.在调用该方法之前,应该已打开
/// 事务
/// 表名
/// 一条数据的信息
public static void Insert(FbConnection conn, FbTransaction tr, string tableName, IDictionary fieldsValues)
{
var insertSql = SqlUtil.GetInsertSql(tableName, fieldsValues);
SqlExecuteBaseUtil.ExecuteNonQuery(conn, tr, insertSql);
}
///
/// 在事务中:删除符合条件的数据
///
/// 连接.在调用该方法之前,应该已打开
/// 事务
/// 表名
/// 以where打头的条件
public static void Delete(FbConnection conn, FbTransaction tr, string tableName, string conditions)
{
var deleteSql = SqlUtil.GetDeleteSql(tableName, conditions);
SqlExecuteBaseUtil.ExecuteNonQuery(conn, tr, deleteSql);
}
///
/// 在事务中:更新符合条件的数据
///
/// 连接.在调用该方法之前,应该已打开
/// 事务
/// 表名
/// 新值:Key为字段名,Value为新值
/// 以where打头的条件
public static void Update(FbConnection conn, FbTransaction tr, string tableName, Dictionary newValues, string conditions)
{
var updateSql = SqlUtil.GetUpdateSql(tableName, newValues, conditions);
SqlExecuteBaseUtil.ExecuteNonQuery(conn, tr, updateSql);
}
///
/// 在事务中:如果指定数据库中,不存在指定的表,则根据创建表的参数,创建之
///
/// 连接.在调用该方法之前,应该已打开
/// 事务
/// 表名
/// 创建表的字段的相关参数信息
public static void CreateTableIfNotExist(FbConnection conn, FbTransaction tr, string tableName, string tableParams)
{
if (IsTableExist(conn, tr, tableName))
return;
CreateTable(conn, tr, tableName, tableParams);
}
///
/// 在事务中:在指定数据库中,判断表是否存在
///
/// 连接.在调用该方法之前,应该已打开
/// 事务
/// 表名
public static bool IsTableExist(FbConnection conn, FbTransaction tr, string tableName)
{
return SqlExecuteBaseUtil.ExecuteScalar(conn, tr, SqlUtil.GetJudgeTableExistSql(tableName), obj => {
int count = 0;
if (obj.Equals(null) || obj.Equals(System.DBNull.Value)) {
count = -1;
} else {
count = (int)obj;
}
return count > 0;
});
}
///
/// 在事务中:在指定数据库中,创建数据表
///
/// 连接.在调用该方法之前,应该已打开
/// 事务
/// 表名
/// 创建表的字段的相关参数信息
public static void CreateTable(FbConnection conn, FbTransaction tr, string tableName, string tableParams)
{
string strSQL = SqlUtil.GetCreateTableSql(tableName, tableParams);
SqlExecuteBaseUtil.ExecuteNonQuery(conn, tr, strSQL);
}
}
}