using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FireBirdUtil.SqlUtils { public class SqlUtil { /// /// 判断表是否存在的SQL语句 /// 执行该sql语句后,结果为1时,表示表存在;否则不存在(小于1) /// /// 表名 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"; } /// /// 创建表的SQL语句 /// /// 表名 /// 所有字段的参数。如:uid int,uname varchar(20) public static string GetCreateTableSql(string tableName, string tableParams) { return "create table " + tableName + " (" + tableParams + ")";//tableParams如:“uid int,uname varchar(20)” } /// /// 插入一条数据的SQL语句 /// /// 表名 /// 一条数据的信息:Key为字段,Value为字段对应的值 public static string GetInsertSql(string tableName, IDictionary fieldsValues) { string insertSql = "insert into {0} ({1}) values ({2})"; insertSql = string.Format(insertSql, tableName, string.Join(", ", fieldsValues.Keys), "'" + string.Join("', '", fieldsValues.Values) + "'"); return insertSql; } /// /// 删除表 /// /// 表名 public static string GetDropSql(string tableName)//TODO:未测试 { return "drop table " + tableName; } /// /// 删除表的所有数据 /// /// 表名 public static string GetDeleteSql(string tableName) { return "delete from " + tableName; } /// /// 删除符合条件的数据 /// /// 表名 /// 以where打头的条件 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 /// /// 将符合条件的数据行的指定字段的值更新//不能处理将一个字段的值赋给另一个字段 /// /// 新值:Key为字段名,Value为新值 /// 以where打头的条件 public static string GetUpdateSql(string tableName, Dictionary newValues, string conditions) { List setValueString = new List(); 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() + "'"; } /// /// 获取自增变量名。说明:长度是有限制的,好像最长是31个字符 /// /// 表名 internal static string GetGeneratorName(string tableName) { return tableName + "_gen"; } /// /// 创建自增变量的SQL语句 /// /// 表名 public static string GetSql_AddGenerator(string tableName) { return "create generator " + GetGeneratorName(tableName); } /// /// 删除自增变量 /// /// 表名 public static string GetSql_DropGenerator(string tableName) { return "drop generator " + GetGeneratorName(tableName); } /// /// 查找指定自增变量的数量(用于判断是否存在指定自增变量) /// /// 表名 public static string GetSql_SelectGeneratorCount(string tableName) { return string.Format("select count(*) from rdb$generators where rdb$generator_name='{0}'", GetGeneratorName(tableName).ToUpper()); } /// /// 创建字段自增功能的SQL语句(通过添加触发器实现) /// 字段类型不是数字类型时,还有强制转换类型 /// /// 表名 /// 字段名 /// 强制转换后的类型。为空则不转换 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); } } }