using System; using System.Collections.Generic; using System.Linq; using System.Text; using DataLibrary; using CoreWebServiceLibrary; namespace CoreSavingLibrary { public enum ExecuteType { Update = 1, Insert = 2, Delete = 3 } public class ExecuteDataSource { private PageWeb page; public bool ExeOnAppServer { get; set; } public List SQL { get; set; } public int CountSQL { get { return SQL.Count; } } public ExecuteDataSource(PageWeb page) { SQL = new List(); this.ExeOnAppServer = false; this.page = page; } public void AddFormView(DataSourceFormView dataSourctFormView, ExecuteType type) { if (type == ExecuteType.Update) { SQL.Add(dataSourctFormView.CreateSyntaxUpdate()); } else if (type == ExecuteType.Insert) { SQL.Add(dataSourctFormView.CreateSyntaxInsert()); } else if (type == ExecuteType.Delete) { SQL.Add(dataSourctFormView.CreateSyntaxDelete()); } } public void AddRepeater(DataSourceRepeater dataSourceRepeater) { List syn = dataSourceRepeater.CreateSyntax(); for (int i = 0; i < syn.Count; i++) { SQL.Add(syn[i]); } } public string replaceOracle2Mysql(String sql) { if (Sta.IS_MYSQL_MODE) { sql = sql.Replace("sysdate", "NOW()"); sql = sql.Replace("SYSDATE", "CURDATE()"); sql = sql.Replace("to_date(", "str_to_date("); sql = sql.Replace("TO_DATE(", "STR_TO_DATE("); sql = sql.Replace("yyyy-mm-dd hh24:mi:ss", "%Y-%m-%d %T"); sql = sql.Replace("yyyy-MM-dd hh24:mi:ss", "%Y-%m-%d %T"); sql = sql.Replace("yyyy/mm/dd hh24:mi:ss", "%Y/%m/%d %T"); sql = sql.Replace("yyyy/MM/dd hh24:mi:ss", "%Y/%m/%d %T"); } return sql; } public int ExeDataSourceTools(String wsPass, String[] list) { int result = 0; Security sec = new Security(wsPass); Sta ta = new Sta(sec.ConnectionString); ta.Transection(); try { for (int i = 0; i < list.Length; i++) { //list[i] = replaceOracle2Mysql(list[i]); int re = ta.Exe(list[i]); result += re; } ta.Commit(); ta.Close(); } catch (Exception ex) { ta.RollBack(); ta.Close(); throw ex; } return result; } /// /// สั้ง DataSourceTool ทั้งหมดสร้างคำสั่ง sql และทำการ execute คำสั่ง sql ทั้งหมด โดยจะ return ค่่าเป็นจำนวนแถวที่ได้ทำงาน /// /// public int Execute() { if (ExeOnAppServer) { return ExeDataSourceTools(page.state.SsWsPass, SQL.ToArray()); } else { int result = 0; Sta ta = new Sta(page.state.SsConnectionString); ta.Transection(); try { for (int i = 0; i < SQL.Count; i++) { int re = ta.Exe(SQL[i]); result += re; } ta.Commit(); ta.Close(); } catch (Exception ex) { ta.RollBack(); ta.Close(); throw ex; } return result; } } } }