Esempio n. 1
0
        //[ActionKey("View")]
        /// <summary>
        /// 获取下拉框Json结果集(统一处理)
        /// </summary>
        /// <returns></returns>
        public void GetCombobox()
        {
            string itemJson = Query <string>("sys_json");

            if (!string.IsNullOrEmpty(itemJson))
            {
                List <ComboboxItem> boxes = JsonHelper.ToList <ComboboxItem>(itemJson);
                JsonHelper          json  = new JsonHelper();
                if (boxes.Count > 0)
                {
                    List <MDataTable> dtList = new List <MDataTable>();
                    StringBuilder     sb     = new StringBuilder();
                    string            value  = null;
                    for (int i = 0; i < boxes.Count; i++)
                    {
                        ComboboxItem item = boxes[i];
                        string       code = SqlCode.GetCode(item.ObjName);
                        if (code != item.ObjName)
                        {
                            #region 核心处理
                            var sql = SqlCode.GetCode(item.ObjName).ToLower();//已经处理过系统参数和Post参数
                            //格式化请求参数
                            string key   = "@text";
                            int    index = sql.IndexOf(key);
                            if (index > -1)
                            {
                                value = Query <string>("q", item.Para);                                 //easyui远程传递参数
                                if (string.IsNullOrEmpty(value) && sql.IndexOf('=', index - 5, 5) > -1) //处理成1=1,同时有=号
                                {
                                    int    end   = index + key.Length;
                                    string temp  = sql.Substring(0, index - 5);
                                    int    start = temp.LastIndexOf(' ');
                                    sql = sql.Replace(sql.Substring(start + 1, end - start - 1), "1=1");
                                }
                                else
                                {
                                    sql = sql.Replace(key, value);
                                }
                            }
                            sb.Append(sql + ";");
                            #endregion
                        }
                        else
                        {
                            #region 找不到,则移除。
                            boxes.RemoveAt(i);
                            //从程序里找。
                            MDataTable dt = Combobox.Get(item.ObjName, Query <string>("q", item.Para));
                            if (dt != null)
                            {
                                dtList.Insert(0, dt);
                                boxes.Insert(0, item);
                            }
                            else
                            {
                                i--;
                            }
                            #endregion
                        }
                    }
                    if (sb.Length > 0)
                    {
                        string sql = sb.ToString().TrimEnd(';');
                        using (MProc proc = new MProc(null, CrossDb.GetConn(sql)))
                        {
                            if (proc.DalType == DalType.MsSql)
                            {
                                proc.ResetProc(sql);
                                dtList.AddRange(proc.ExeMDataTableList());
                            }
                            else
                            {
                                string[] items = sql.Split(';');
                                foreach (string item in items)
                                {
                                    proc.ResetProc(item);
                                    dtList.Add(proc.ExeMDataTable());
                                }
                            }
                        }
                    }
                    if (dtList.Count == 1 && Query <string>("q", null) != null)
                    {
                        jsonResult = dtList[0].ToJson(false, false, true);
                        return;
                    }
                    for (int i = 0; i < dtList.Count; i++)
                    {
                        json.Add(boxes[i].ObjName, dtList[i].Rows.Count > 0 ? dtList[i].ToJson(false, false, true) : "[]", true);
                        if (!string.IsNullOrEmpty(boxes[i].Parent))
                        {
                            json.Add("Target", boxes[i].Parent);
                        }
                        json.AddBr();
                    }
                }
                jsonResult = json.ToString(true);
            }
        }
Esempio n. 2
0
        public virtual void Delete()
        {
            string ids = GetID;

            string[] values = null;
            string where = string.Empty;
            string     parentField = Query <string>("parentField");
            string     idField     = Query <string>("idField");
            MDataTable dt          = null;

            using (MAction action = new MAction(CrossTableName))
            {
                action.BeginTransation();

delChild:       //删除子节点循环处。

                //dg.foreignKeys="TableA.ColumnNameA,TableB.ColumnNameB";
                string foreignKeys = Query <string>("foreignKeys");
                bool result = true;
                if (!string.IsNullOrEmpty(foreignKeys))//增加了外键删除
                {
                    string[] items = foreignKeys.Split(',');
                    foreach (string item in items)
                    {
                        string[] kv = item.Split('.');
                        if (kv.Length == 2)
                        {
                            action.ResetTable(CrossDb.GetEnum(kv[0]));
                            result = action.Delete(GetWhereIn(kv[1], null, values));
                            if (!result)
                            {
                                break;
                            }
                        }
                    }
                    if (result)
                    {
                        action.ResetTable(CrossTableName);
                    }
                }
                if (result)
                {
                    //第一次,需要将ID转为idField指定的上级关联字段。
                    string pkName = action.Data.Columns.FirstPrimary.ColumnName;
                    if (string.IsNullOrEmpty(idField))
                    {
                        idField = pkName;
                    }
                    where = values == null ? ids : GetWhereIn(idField, null, values);

                    if (values == null && !string.IsNullOrEmpty(idField) && pkName.ToLower() != idField.ToLower())
                    {
                        action.SetSelectColumns(idField);
                        dt = action.Select(where);
                        if (dt.Rows.Count > 0)
                        {
                            values = dt.GetColumnItems <string>(0).ToArray();
                        }
                    }

                    result = action.Delete(where);
                }
                if (!result)
                {
                    action.RollBack();
                }
                else if (!string.IsNullOrEmpty(parentField) && !string.IsNullOrEmpty(idField))
                {
                    action.SetSelectColumns(idField);
                    dt = action.Select(GetWhereIn(parentField, null, values));
                    if (dt.Rows.Count > 0)
                    {
                        values = dt.GetColumnItems <string>(0).ToArray();
                        goto delChild;
                    }
                }
                action.EndTransation();
                if (result)
                {
                    SetSuccess(LangConst.DeleteSuccess);
                }
                else
                {
                    if (AppConfig.Debug.OpenDebugInfo)
                    {
                        Log.WriteLogToTxt("Delete(): " + action.DebugInfo);
                    }
                    SetError(LangConst.DeleteError, action.DebugInfo);
                }
            }
        }
Esempio n. 3
0
        //[ActionKey("View")]
        /// <summary>
        /// 获取下拉框Json结果集(统一处理)
        /// </summary>
        /// <returns></returns>
        public void GetCombobox()
        {
            string itemJson = Query <string>("sys_json");

            if (!string.IsNullOrEmpty(itemJson))
            {
                List <ComboItem> boxes = JsonHelper.ToList <ComboItem>(itemJson);
                JsonHelper       json  = new JsonHelper();
                if (boxes.Count > 0)
                {
                    List <MDataTable> dtList = new List <MDataTable>();
                    StringBuilder     sb     = new StringBuilder();
                    for (int i = 0; i < boxes.Count; i++)
                    {
                        ComboItem item = boxes[i];
                        string    code = SqlCode.GetCode(item.ObjName);
                        if (code != item.ObjName)
                        {
                            #region 核心处理
                            var sql = SqlCode.GetCode(item.ObjName);//已经处理过系统参数和Post参数
                            sql = ReplacePara(sql, "@para", item.Para);
                            sql = ReplacePara(sql, "@parent", item.Parent);

                            sb.Append(sql + ";");
                            #endregion
                        }
                        else
                        {
                            #region 找不到,则移除。
                            boxes.RemoveAt(i);
                            //从程序里找。
                            MDataTable dt = Combobox.Get(item.ObjName, Query <string>("q", item.Para));
                            if (dt != null)
                            {
                                dtList.Insert(0, dt);
                                boxes.Insert(0, item);
                            }
                            else
                            {
                                i--;
                            }
                            #endregion
                        }
                    }
                    if (sb.Length > 0)
                    {
                        string sql = sb.ToString().TrimEnd(';');
                        using (MProc proc = new MProc(null, CrossDb.GetConn(sql)))
                        {
                            if (proc.DalType == DalType.MsSql)
                            {
                                proc.ResetProc(sql);
                                dtList.AddRange(proc.ExeMDataTableList());
                            }
                            else
                            {
                                string[] items = sql.Split(';');
                                foreach (string item in items)
                                {
                                    proc.ResetProc(item);
                                    dtList.Add(proc.ExeMDataTable());
                                }
                            }
                        }
                    }
                    if (dtList.Count == 1 && Query <string>("q", null) != null)
                    {
                        jsonResult = dtList[0].ToJson(false, false, RowOp.None, true);
                        return;
                    }
                    for (int i = 0; i < dtList.Count; i++)
                    {
                        json.Add(boxes[i].ObjName, dtList[i].Rows.Count > 0 ? dtList[i].ToJson(false, false, RowOp.None, true) : "[]", true);
                    }
                    json.AddBr();
                }
                jsonResult = json.ToString();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 获取SQl脚本
        /// </summary>
        /// <param name="row"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static string GetSQLScript(MDataRow row, params string[] keys)
        {
            DalType dalType   = CrossDb.GetDalType(row.TableName);
            string  tableName = DBTool.Keyword(row.TableName, dalType);

            string where = string.Empty;
            StringBuilder sb = new StringBuilder();

            //sb.AppendFormat("if not EXISTS (select 1 from {0} where ", tableName);
            //foreach (string key in keys)
            //{
            //    if (where != string.Empty)
            //    {
            //        where += " and ";
            //    }
            //    where +=DBTool.Keyword(key,dalType)+"='" + row[key].ToString() + "'";
            //}
            //sb.Append(where);
            //sb.AppendLine(")\r\nbegin");//Insert

            sb.AppendFormat("insert into {0}(", tableName);//Insert
            string columns = string.Empty;

            foreach (var ct in row.Columns)
            {
                columns += DBTool.Keyword(ct.ColumnName, dalType) + ",";
            }
            sb.Append(columns.TrimEnd(',') + ") \r\n values(");
            columns = string.Empty;
            foreach (var ct in row.Columns)
            {
                if (row[ct.ColumnName].IsNull)
                {
                    columns += "null,";
                }
                else
                {
                    columns += "'" + row[ct.ColumnName].ToString() + "',";
                }
            }
            sb.Append(columns.TrimEnd(',') + ")\r\n");
            //sb.AppendLine("end");
            //sb.AppendLine("else");
            //sb.AppendLine("begin");//Update
            //sb.AppendFormat("update {0} set ", tableName);//Insert
            //columns = string.Empty;
            //List<string> items = new List<string>();
            //items.AddRange(keys);
            //foreach (var ct in row.Columns)
            //{
            //    if (items.Contains(ct.ColumnName))
            //    {
            //        continue;
            //    }
            //    if (row[ct.ColumnName].IsNull)
            //    {
            //        columns += DBTool.Keyword(ct.ColumnName,dalType)+ "=null,";
            //    }
            //    else
            //    {
            //        columns += DBTool.Keyword(ct.ColumnName,dalType) + "='" + row[ct.ColumnName].ToString() + "',";
            //    }
            //}
            //sb.AppendFormat(columns.TrimEnd(',') + " where {0}\r\n", where);
            //sb.AppendLine("end \r\n");
            return(sb.ToString());
        }