public static string DtoToSql(SqlDto model) { var sql = "select " + model.Select + " " + model.From + " " + model.Where + " " + model.Group + " " + model.Order; return(sql.Replace(" ", " ")); }
//convert sql string to model public static SqlDto SqlToDto(string sql, bool useSquare) { if (_Str.IsEmpty(sql)) { return(null); } var sql2 = sql.ToLower(); var len = sql2.Length; int from, where, group, order; if (useSquare) { from = sql2.IndexOf("[from]"); where = sql2.IndexOf("[where]"); group = sql2.IndexOf("[group]"); order = sql2.IndexOf("[order]"); } else { from = sql2.IndexOf("from "); where = sql2.IndexOf("where "); group = sql2.IndexOf("group "); order = sql2.IndexOf("order "); } var end = len; var result = new SqlDto(); if (order < 0) { result.Order = ""; } else { result.Order = useSquare ? "Order " + sql[(order + 7)..].Trim() : sql[order..].Trim();
//convert sql string to model public static SqlDto SqlToDto(string sql, bool useSquare) { if (string.IsNullOrEmpty(sql)) { return(null); } var sql2 = sql.ToLower(); var len = sql2.Length; int from, where, group, order; if (useSquare) { from = sql2.IndexOf("[from]"); where = sql2.IndexOf("[where]"); group = sql2.IndexOf("[group]"); order = sql2.IndexOf("[order]"); } else { from = sql2.IndexOf("from "); where = sql2.IndexOf("where "); group = sql2.IndexOf("group "); order = sql2.IndexOf("order "); } var end = len; var result = new SqlDto(); if (order < 0) { result.Order = ""; } else { result.Order = useSquare ? "Order " + sql.Substring(order + 7).Trim() : sql.Substring(order).Trim(); end = order - 1; } if (group < 0) { result.Group = ""; } else { result.Group = useSquare ? "Group " + sql.Substring(group + 7, end - group - 7).Trim() : sql.Substring(group, end - group).Trim(); end = group - 1; } if (where < 0) { result.Where = ""; } else { result.Where = useSquare ? "Where " + sql.Substring(where + 7, end - where - 7).Trim() : sql.Substring(where, end - where).Trim(); end = where - 1; } if (useSquare) { result.From = "From " + sql.Substring(from + 6, end - from - 6).Trim(); result.Select = sql.Substring(0, from).Trim().Substring(7); //exclude "Select" word !! } else { result.From = sql.Substring(from, end - from).Trim(); result.Select = sql.Substring(0, from).Trim().Substring(7); //exclude "Select" word !! } //set columns[] result.Columns = result.Select.Split(','); for (var i = 0; i < result.Columns.Length; i++) { var col = result.Columns[i].Trim(); var pos = col.IndexOf(" "); result.Columns[i] = (pos > 0) ? col.Substring(0, pos) : col; } //result.Columns = result.Select.Replace(" ","").Split(','); return(result); }