|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace FireBirdUtil.SqlUtils
|
|
|
{
|
|
|
public class SqlUtil
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 判断表是否存在的SQL语句
|
|
|
/// <para>执行该sql语句后,结果为1时,表示表存在;否则不存在(小于1)</para>
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
public static string GetJudgeTableExistSql(string tableName)//查询结果为1时,表示表存在;否则不存在(小于1)
|
|
|
{
|
|
|
return "select count(*) from rdb$relations where rdb$relation_name='" + tableName.ToUpper() + "' AND RDB$VIEW_SOURCE IS NULL";
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 创建表的SQL语句
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
/// <param name="tableParams">所有字段的参数。如:uid int,uname varchar(20)</param>
|
|
|
public static string GetCreateTableSql(string tableName, string tableParams)
|
|
|
{
|
|
|
return "create table " + tableName + " (" + tableParams + ")";//tableParams如:“uid int,uname varchar(20)”
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 插入一条数据的SQL语句
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
/// <param name="fieldsValues">一条数据的信息:Key为字段,Value为字段对应的值</param>
|
|
|
public static string GetInsertSql(string tableName, IDictionary<string, object> fieldsValues)
|
|
|
{
|
|
|
string insertSql = "insert into {0} ({1}) values ({2})";
|
|
|
insertSql = string.Format(insertSql, tableName, string.Join(", ", fieldsValues.Keys), "'" + string.Join("', '", fieldsValues.Values) + "'");
|
|
|
return insertSql;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除表
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
public static string GetDropSql(string tableName)//TODO:未测试
|
|
|
{
|
|
|
return "drop table " + tableName;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除表的所有数据
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
public static string GetDeleteSql(string tableName)
|
|
|
{
|
|
|
return "delete from " + tableName;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除符合条件的数据
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
/// <param name="conditions">以where打头的条件</param>
|
|
|
public static string GetDeleteSql(string tableName, string conditions)
|
|
|
{
|
|
|
return "delete from " + tableName + " " + conditions;
|
|
|
}
|
|
|
//delete from table1 where id=1 or id=2
|
|
|
//delete from table1 where id in (3,4,5)
|
|
|
//delete from table1 where age is null
|
|
|
//delete from table1 where age is null and id<10
|
|
|
|
|
|
/// <summary>
|
|
|
/// 将符合条件的数据行的指定字段的值更新//不能处理将一个字段的值赋给另一个字段
|
|
|
/// </summary>
|
|
|
/// <param name="newValues">新值:Key为字段名,Value为新值</param>
|
|
|
/// <param name="conditions">以where打头的条件</param>
|
|
|
public static string GetUpdateSql(string tableName, Dictionary<string, object> newValues, string conditions)
|
|
|
{
|
|
|
List<string> setValueString = new List<string>();
|
|
|
foreach (var item in newValues) {
|
|
|
setValueString.Add(item.Key + "=" + UpdateValueToString(item.Value));
|
|
|
}
|
|
|
return "update " + tableName + " set " + string.Join(", ", setValueString) + " " + conditions;
|
|
|
}
|
|
|
|
|
|
private static string UpdateValueToString(object obj)
|
|
|
{
|
|
|
return "'" + obj.ToString() + "'";
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取自增变量名。说明:长度是有限制的,好像最长是31个字符
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
internal static string GetGeneratorName(string tableName)
|
|
|
{
|
|
|
return tableName + "_gen";
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 创建自增变量的SQL语句
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
public static string GetSql_AddGenerator(string tableName)
|
|
|
{
|
|
|
return "create generator " + GetGeneratorName(tableName);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除自增变量
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
public static string GetSql_DropGenerator(string tableName)
|
|
|
{
|
|
|
return "drop generator " + GetGeneratorName(tableName);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查找指定自增变量的数量(用于判断是否存在指定自增变量)
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
public static string GetSql_SelectGeneratorCount(string tableName)
|
|
|
{
|
|
|
return string.Format("select count(*) from rdb$generators where rdb$generator_name='{0}'", GetGeneratorName(tableName).ToUpper());
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 创建字段自增功能的SQL语句(通过添加触发器实现)
|
|
|
/// <para>字段类型不是数字类型时,还有强制转换类型</para>
|
|
|
/// </summary>
|
|
|
/// <param name="tableName">表名</param>
|
|
|
/// <param name="fieldName">字段名</param>
|
|
|
/// <param name="AutoIncreasedFieldTypeIfNotNum">强制转换后的类型。为空则不转换</param>
|
|
|
public static string GetSql_AddAutoIncreaseTrigger(string tableName, string fieldName, string AutoIncreasedFieldTypeIfNotNum)
|
|
|
{//TODO:未测试AutoIncreasedFieldTypeIfNotNum不为空的情况
|
|
|
string generatorName = GetGeneratorName(tableName);
|
|
|
string triggerName = tableName + "_increaseID";
|
|
|
string newValue = string.Format("gen_id({0}, 1)", generatorName);
|
|
|
if (!string.IsNullOrEmpty(AutoIncreasedFieldTypeIfNotNum))
|
|
|
newValue = string.Format("cast({0} as {1})", newValue, AutoIncreasedFieldTypeIfNotNum);
|
|
|
string sql = @"
|
|
|
create trigger {1} for {2} active before insert
|
|
|
as
|
|
|
begin
|
|
|
if ((new.{3} is null) or (new.{3} = 0)) then
|
|
|
begin
|
|
|
new.{3} = {4};
|
|
|
end
|
|
|
end
|
|
|
";//TODO:需不需要判读是否为0?
|
|
|
return string.Format(sql, generatorName, triggerName, tableName, fieldName, newValue);
|
|
|
}
|
|
|
}
|
|
|
}
|