The data construction tool class (-) generates sql based on the entity class

Generate sql based on entity class (entity class fields are consistent with the database)

entity class

public class AccountEntity:IEntity
    {
        public int? ID { get; set; }
        public string Names { get; set; }
        public string TYPEID { get; set; }
        public string PRICE { get; set; }
        public string ISOUT { get; set; }
        public string Dates { get; set; }
        public string Remark { get; set; }

        /// <summary>
        /// Set the database table name
        /// </summary>
        /// <returns></returns>
        public string TableName()
        {
            return "bus_account";
        }
    }

Generate Sql method by reflection

        /// <summary>
        /// Get sql through reflection
        /// </summary>
        /// <typeparam name="T">class name</typeparam>
        /// <param name="t">Class object</param>
        /// <param name="WhereId">Judgment basis, add or modify</param>
        /// <returns></returns>
        public static string GetSql<T>(T t,string WhereId="ID")
        {
            var type = t.GetType(); //Get the type
            var pr=type.GetProperties();
            var where = "";
            var tableMethod = type.GetMethod("TableName");
            object obj = Activator.CreateInstance(type);
            var tableName = tableMethod.Invoke(obj, null);
            //INSERT INTO `bus_account` (ID,Names,TYPEID,PRICE,ISOUT,Dates,Remark) VALUES ('0', 'morning', '1', '22.22', '0', '', 'this is A remark message, I don't know how long it is, it is very long anyway');

            var insertSqlStr = string.Format("INSERT INTO {0} ({1}) values ({2}) ", tableName, "{0}", "{1}");
            var insertColTemp = "";
            var insertValTemp = "";
            var isUpdate = false;

            //UPDATE BUS_ACCOUNT SET NAMES='123',TYPEID='2' WHERE ID='0'
            var updateSqlStr = string.Format("UPDATE {0} set {1} {2}", tableName, "{0}", "{1}");
            var updateSet = "";
            for (var i = 0; i < pr.Length; i++)
            {
                var name = pr[i].Name;
                var value = pr[i].GetValue(t, null);
                //Consistent with the judgment condition, and the value is not empty, then it is a modification statement
                if (name.Trim().ToLower() == WhereId.Trim().ToLower() && value.ToString().Trim().Length > 0)
                {
                    where = string.Format(" where {0}='{1}'", name, value);
                    isUpdate = true;
                }
                else
                {
                    insertColTemp += name + (i >= pr.Length - 1 ? "" : ",");
                    insertValTemp += string.Format("'{0}'", value) + (i >= pr.Length - 1 ? "" : ",");
                    updateSet += string.Format("{0}='{1}'", name, value) + (i >= pr.Length - 1 ? "" : ","); ;
                }
            }
            return isUpdate ? string.Format(updateSqlStr, updateSet, @where) : string.Format(insertSqlStr, insertColTemp, insertValTemp);
        }

transfer

            var account = JsonHelper.DeserializeJsonToObject<AccountEntity>(json);
            var sql = GetSql<AccountEntity>(account);



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325627450&siteId=291194637