예제 #1
0
        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);
            }
        }
예제 #2
0
        public static IDataObject ToDataObject(DataObject objectType, DataRow row)
        {
            GenericDataObject dataObject = null;

            if (row != null)
            {
                try
                {
                    dataObject = new GenericDataObject()
                    {
                        ObjectType = objectType.objectName
                    };
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Error instantiating data object: {0}", ex));
                    throw ex;
                }

                if (dataObject != null && objectType.dataProperties != null)
                {
                    foreach (DataProperty objectProperty in objectType.dataProperties)
                    {
                        try
                        {
                            if (objectProperty.propertyName != null)
                            {
                                if (row.Table.Columns.Contains(objectProperty.propertyName))
                                {
                                    object value = row[objectProperty.propertyName];

                                    if (value.GetType() == typeof(System.DBNull))
                                    {
                                        value = null;
                                    }

                                    if (objectProperty.dataType.ToString().ToUpper() == "DECIMAL" ||
                                        objectProperty.dataType.ToString().ToUpper() == "DOUBLE")
                                    {
                                        dataObject.SetPropertyNumericValue(objectProperty.propertyName, value);
                                    }
                                    else
                                    {
                                        dataObject.SetPropertyValue(objectProperty.propertyName, value);
                                    }
                                }
                                else
                                {
                                    _logger.Warn(String.Format("Value for column [{0}] not found in data row of table [{1}]",
                                                               objectProperty.columnName, objectType.tableName));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(string.Format("Error getting data row value: {0}", ex));
                            throw ex;
                        }
                    }
                }

                //Extension Properties____Starts
                if (dataObject != null && objectType.extensionProperties != null)
                {
                    foreach (ExtensionProperty extentionProperty in objectType.extensionProperties)
                    {
                        try
                        {
                            if (extentionProperty.propertyName != null)
                            {
                                if (row.Table.Columns.Contains(extentionProperty.propertyName))
                                {
                                    object value = row[extentionProperty.propertyName];

                                    if (value.GetType() == typeof(System.DBNull))
                                    {
                                        value = null;
                                    }

                                    dataObject.SetPropertyValue(extentionProperty.propertyName, value);
                                }
                                else
                                {
                                    _logger.Warn(String.Format("Value for column [{0}] not found in data row of table [{1}]",
                                                               extentionProperty.columnName, objectType.tableName));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(string.Format("Error getting data row value: {0}", ex));
                            throw ex;
                        }
                    }
                }
                //Extension Properties____Ends
            }
            else
            {
                dataObject = new GenericDataObject()
                {
                    ObjectType = objectType.objectName
                };

                foreach (DataProperty objectProperty in objectType.dataProperties)
                {
                    dataObject.SetPropertyValue(objectProperty.propertyName, null);
                }
            }

            if (row[HAS_CONTENT] != DBNull.Value && Convert.ToBoolean(row[HAS_CONTENT]))
            {
                dataObject.HasContent = true;
            }

            return(dataObject);
        }
예제 #3
0
 public static string FormWhereClause(DataObject objectType, string identifier)
 {
     return(FormWhereClause(objectType, new List <string> {
         identifier
     }));
 }
예제 #4
0
        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);
        }
예제 #5
0
 public abstract List <IContentObject> GetContents(DataObject objectType, Dictionary <string, string> idFormats);
예제 #6
0
 public abstract Response Update(DataObject objectType, List <SerializableDataObject> dataObjects);
예제 #7
0
 public abstract List <SerializableDataObject> Get(DataObject objectType);