public bool deleteProperty(DataProperty dataProperty) { foreach (DataProperty property in dataProperties) { if (dataProperty == property) { dataProperties.Remove(dataProperty); break; } } foreach (KeyProperty keyProperty in keyProperties) { if (keyProperty.keyPropertyName.ToLower() == dataProperty.propertyName.ToLower()) { keyProperties.Remove(keyProperty); break; } } return(true); }
public static string FormWhereClause(DataObject objectType, IList <string> identifiers) { try { StringBuilder clauseBuilder = new StringBuilder(); string[] delim = new string[] { objectType.keyDelimeter }; foreach (string id in identifiers) { StringBuilder exprBuilder = new StringBuilder(); string[] idParts = id.Split(delim, StringSplitOptions.None); if (objectType.keyProperties.Count > 1) { idParts = id.Split(delim, StringSplitOptions.None); } else { idParts[0] = id; } for (int i = 0; i < objectType.keyProperties.Count; i++) { string key = objectType.keyProperties[i].keyPropertyName; DataProperty prop = objectType.dataProperties.Find(x => x.propertyName.ToLower() == key.ToLower()); string expr = string.Empty; if (IsNumeric(prop.dataType)) { expr = string.Format("{0} = {1}", key, idParts[i]); } else if (string.IsNullOrEmpty(idParts[i])) { expr = string.Format("({0} = '' OR {0} IS NULL)", key); } else { expr = string.Format("{0} = '{1}'", key, idParts[i]); } if (exprBuilder.Length > 0) { exprBuilder.Append(" AND "); } exprBuilder.Append(expr); } if (clauseBuilder.Length > 0) { clauseBuilder.Append(" OR "); } clauseBuilder.Append("(" + exprBuilder.ToString() + ")"); } if (clauseBuilder.Length > 0) { clauseBuilder.Insert(0, " WHERE "); } return(clauseBuilder.ToString()); } catch (Exception ex) { string error = "Error forming WHERE clause: " + ex; _logger.Error(error); throw new Exception(error); } }
public static string CreateUpdateSQL(string tableName, DataObject objectType, SerializableDataObject dataObject) { switch (dataObject.State) { case ObjectState.Create: { StringBuilder colsBuilder = new StringBuilder(); StringBuilder valsBuilder = new StringBuilder(); foreach (var prop in dataObject.Dictionary) { DataProperty dataProp = objectType.dataProperties.Find(x => x.propertyName == prop.Key); colsBuilder.Append("," + prop.Key); if (BaseLightweightDataLayer.IsNumeric(dataProp.dataType)) { if (prop.Value != null && prop.Value != "") { valsBuilder.Append("," + prop.Value); } else { valsBuilder.Append("," + "null"); } } else if (dataProp.dataType == DataType.Date || dataProp.dataType == DataType.DateTime) { if (prop.Value != "") { DateTime dateTime = Utility.FromXsdDateTime(prop.Value.ToString()); valsBuilder.Append("," + prop.Key + "='" + dateTime.ToString() + "'"); } else { valsBuilder.Append("," + prop.Key + "= null"); } } else { valsBuilder.Append("," + "'" + prop.Value + "'"); } } if (dataObject.HasContent) { colsBuilder.Append("," + HAS_CONTENT); valsBuilder.Append("," + "'" + dataObject.HasContent + "'"); } string insertSQL = string.Format(INSERT_SQL_TPL, tableName, colsBuilder.Remove(0, 1), valsBuilder.Remove(0, 1)); return(insertSQL); } case ObjectState.Update: { StringBuilder builder = new StringBuilder(); string whereClause = BaseLightweightDataLayer.FormWhereClause(objectType, dataObject.Id); foreach (var prop in dataObject.Dictionary) { DataProperty dataProp = objectType.dataProperties.Find(x => x.propertyName == prop.Key); if (BaseLightweightDataLayer.IsNumeric(dataProp.dataType)) { if (prop.Value != null && prop.Value != "") { builder.Append("," + prop.Key + "=" + prop.Value); } else { builder.Append("," + prop.Key + "=" + "null"); } } else if (dataProp.dataType == DataType.Date || dataProp.dataType == DataType.DateTime) { if (prop.Value != "") { DateTime dateTime = Utility.FromXsdDateTime(prop.Value.ToString()); builder.Append("," + prop.Key + "='" + dateTime.ToString() + "'"); } else { builder.Append("," + prop.Key + "= null"); } } else { builder.Append("," + prop.Key + "='" + prop.Value + "'"); } } if (dataObject.HasContent) { builder.Append("," + HAS_CONTENT + "='" + dataObject.HasContent + "'"); } string updateSQL = string.Format(UPDATE_SQL_TPL, tableName, builder.Remove(0, 1), whereClause); return(updateSQL); } case ObjectState.Delete: { string whereClause = BaseLightweightDataLayer.FormWhereClause(objectType, dataObject.Id); string deleteSQL = string.Format(DELETE_SQL_TPL, tableName, whereClause); return(deleteSQL); } } return(string.Empty); }