public override string MergeStr <T>(T model, bool IsPosibleNullValue) { StringBuilder str = new StringBuilder(""); string tableName = helper.GetTableName(model.GetType()); var props = model.GetType().GetProperties(); long count = 0; long pk_Count = 0; str.AppendFormat("INSERT INTO {0} ( ", tableName); foreach (var p in props) { if (!helper.CheckIgnore(p)) { var columnName = helper.ColumnName(p); if (p.GetValue(model) != null || helper.CheckCreatedDate(p)) { if (helper.CheckKey(p)) { pk_Count++; } if (count >= 1) { str.Append(", "); } str.Append(columnName); count++; } } } if (pk_Count < 1) { throw new PkNotFoundException("Insert를 할때에는 Primary Key가 한개 이상 존재해야 합니다."); } str.Append(" ) VALUES ( "); count = 0; foreach (var p in props) { if (!helper.CheckIgnore(p)) { var columnName = helper.ColumnName(p); if (p.GetValue(model) != null || helper.CheckCreatedDate(p)) { if (count >= 1) { str.Append(", "); } if (helper.CheckCreatedDate(p)) { str.Append("NOW()"); } else { str.Append("@" + p.Name); } count++; } } } str.Append(" ) ON DUPLICATE KEY UPDATE "); count = 0; foreach (var p in props) { var columnName = helper.ColumnName(p); if (IsPosibleNullValue || p.GetValue(model) != null || helper.CheckLastModifiedDate(p)) { if (helper.CheckCreatedDate(p) || helper.CheckKey(p)) { continue; } if (!helper.CheckIgnore(p)) { if (count >= 1) { str.Append(", "); } if (helper.CheckLastModifiedDate(p)) { str.AppendFormat("{0} = {1}", columnName, "NOW()"); } else if (p.GetValue(model) == null) { str.AppendFormat("{0} = NULL", columnName); } else if (helper.CheckCreatedDate(p) == false) { str.AppendFormat("{0} = {1}{2}", columnName, "@", p.Name); } count++; } } } return(str.ToString().TrimEnd()); }
public override string MergeStr <T>(T model, bool IsPosibleNullValue) { StringBuilder str = new StringBuilder(""); string tableName = helper.GetTableName(model.GetType()); var props = model.GetType().GetProperties(); long count = 0; long pk_Count = 0; str.AppendFormat("INSERT OR REPLACE INTO {0} (", tableName); foreach (var p in props) { if (!helper.CheckIgnore(p)) { var columnName = helper.ColumnName(p); if (p.GetValue(model) != null || helper.CheckCreatedDate(p)) { if (helper.CheckKey(p)) { pk_Count++; } if (count >= 1) { str.Append(", "); } str.Append(columnName); count++; } } } if (pk_Count < 1) { throw new PkNotFoundException("Merge 할때에는 Primary Key가 한개 이상 존재해야 합니다."); } str.Append(" ) VALUES ( "); count = 0; foreach (var p in props) { if (!helper.CheckIgnore(p)) { var columnName = helper.ColumnName(p); if (p.GetValue(model) != null || helper.CheckCreatedDate(p)) { if (count >= 1) { str.Append(", "); } if (helper.CheckCreatedDate(p)) { str.Append("DATETIME('NOW','LOCALTIME')"); } else { str.Append(":" + p.Name); } count++; } } } str.Append(" )"); return(str.ToString().TrimEnd()); }
public string InsertStr <T>(T model) { StringBuilder str = new StringBuilder(""); string tableName = helper.GetTableName(model.GetType()); var props = model.GetType().GetProperties(); long count = 0; long pk_Count = 0; str.AppendFormat("INSERT INTO {0} ( ", tableName); foreach (var p in props) { if (!helper.CheckIgnore(p)) { var columnName = helper.ColumnName(p); if (p.GetValue(model) != null || helper.CheckCreatedDate(p)) { if (helper.CheckKey(p)) { pk_Count++; } if (count >= 1) { str.Append(", "); } str.Append(columnName); count++; } // AutoCreate이면서 PK인 경우는 pk_Count 자동 증가해줌 if (p.GetValue(model) == null && helper.CheckKey(p) && helper.CheckAutoCreate(p)) { pk_Count++; } // 필수 입력인데 값이 안들어가 있는 경우 RequiredValueNotFoundException if (p.GetValue(model) == null && helper.CheckRequiredColumn(p)) { throw new RequiredValueNotFoundException(p.GetValue(model) + "에 반드시 값이 들어가야 할 컬럼입니다."); } } } if (pk_Count < 1) { throw new PkNotFoundException("Insert를 할때에는 Primary Key가 한개 이상 존재해야 합니다."); } str.Append(" ) VALUES ( "); count = 0; foreach (var p in props) { if (!helper.CheckIgnore(p)) { var columnName = helper.ColumnName(p); if (p.GetValue(model) != null || helper.CheckCreatedDate(p)) { if (count >= 1) { str.Append(", "); } if (helper.CheckCreatedDate(p)) { str.Append(DBNowDatefunction); } else { str.Append(ParamMark + p.Name); } count++; } // 필수 입력인데 값이 안들어가 있는 경우 RequiredValueNotFoundException if (p.GetValue(model) == null && helper.CheckRequiredColumn(p)) { throw new RequiredValueNotFoundException(p.GetValue(model) + "에 반드시 값이 들어가야 할 컬럼입니다."); } } } str.Append(" )"); return(str.ToString().TrimEnd()); }