Beispiel #1
0
        internal string GetValue(DirectModelPropertySignature signature)
        {
            var val = Model.GetType().GetProperty(signature.PropertyName).GetValue(this.Model, null);

            // override nullable strings
            if (val != null && Model.GetType().GetProperty(signature.PropertyName).PropertyType == typeof(string) && string.IsNullOrEmpty(val.ToString()) && signature.Nullable)
            {
                return("NULL");
            }

            return(val == null ? "NULL" : val.ToString());
        }
        public static void UpdateValue(this DirectModelPropertySignature snap, DirectModel model, JToken value)
        {
            if (snap.PropertyInfo.PropertyType == typeof(string))
            {
                snap.PropertyInfo.SetValue(model, value.ToString());
            }

            else if (snap.PropertyInfo.PropertyType == typeof(int) ||
                     snap.PropertyInfo.PropertyType == typeof(int?))
            {
                snap.PropertyInfo.SetValue(model, (int)value);
            }

            else if (snap.PropertyInfo.PropertyType == typeof(uint) ||
                     snap.PropertyInfo.PropertyType == typeof(uint?))
            {
                snap.PropertyInfo.SetValue(model, (uint)value);
            }

            else if (snap.PropertyInfo.PropertyType == typeof(DateTime) ||
                     snap.PropertyInfo.PropertyType == typeof(DateTime?))
            {
                snap.PropertyInfo.SetValue(model, (DateTime)value);
            }

            else if (snap.PropertyInfo.PropertyType == typeof(double) ||
                     snap.PropertyInfo.PropertyType == typeof(double?))
            {
                snap.PropertyInfo.SetValue(model, (double)value);
            }

            else if (snap.PropertyInfo.PropertyType == typeof(bool) ||
                     snap.PropertyInfo.PropertyType == typeof(bool?))
            {
                snap.PropertyInfo.SetValue(model, (bool)value);
            }
        }
        internal static string GetObjectQueryValue(this DirectDatabaseBase db, PropertyInfo obj, DirectModelPropertySignature signature, object parentObject)
        {
            var model = parentObject as DirectModel;

            if (obj == null)
            {
                return("NULL");
            }

            if (signature.UpdateDateTime)
            {
                if (db != null)
                {
                    return(db.CurrentDateQueryString);
                }
                else
                {
                    return("CURRENT_TIMESTAMP");
                }
            }


            object value = obj.GetValue(parentObject);
            var    type  = obj.PropertyType;

            if (type == typeof(DirectTime))
            {
                return(db.CurrentDateQueryString);
            }
            else if (type == typeof(DirectScopeID))
            {
                return(db.QueryScopeID);
            }
            else if (type == typeof(bool))
            {
                return((bool)value ? "1" : "0");
            }
            else if (type == typeof(int) || type == typeof(double) || type == typeof(long) ||
                     type == typeof(uint) || type == typeof(ulong) || type == typeof(short) ||
                     type == typeof(int?) || type == typeof(double?) || type == typeof(long?) ||
                     type == typeof(uint?) || type == typeof(ulong?) || type == typeof(short?))
            {
                return(value.ToString());
            }
            else if (type == typeof(string) || type == typeof(Guid) || type == typeof(String) || type == typeof(char))
            {
                string extra = model.GetDatabase().DatabaseType == DirectDatabaseType.SQLServer ? "N" : ""; // for adding utf8 data
                return(string.Format(extra + "'{0}'", value.ToString().EscapeString()));
            }
            else if (type == typeof(byte[]))
            {
                byte[] data         = (byte[])value;
                bool   hasAllZeroes = data.All(singleByte => singleByte == 0);
                if (hasAllZeroes)
                {
                    return("NULL");
                }
                string hex = StringToHex.ToHexString(data, false);
                //data.ToList().ForEach(b => hex += b.ToString("x2"));
                return(string.Format("X'{0}'", hex));
            }
            else if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                if (value == null)
                {
                    return("NULL");
                }

                DateTime?dt = value as DateTime?;
                if (dt != null)
                {
                    if (db != null)
                    {
                        return(db.ConstructDateTimeParam(dt.Value));
                    }
                    else
                    {
                        return(string.Format("'{0}'", dt.Value.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                }
                else
                {
                    return("NULL");
                }
            }

            return("NULL");
        }
        internal static string GetObjectQueryValue(this DirectDatabaseBase db, PropertyInfo obj, DirectModelPropertySignature signature, object parentObject)
        {
            if (obj == null)
            {
                return("NULL");
            }

            if (signature.UpdateDateTime)
            {
                return(db.CurrentDateQueryString);
            }

            object value = obj.GetValue(parentObject);
            var    type  = obj.PropertyType;

            if (type == typeof(DirectTime))
            {
                return(db.CurrentDateQueryString);
            }
            else if (type == typeof(DirectScopeID))
            {
                return(db.QueryScopeID);
            }
            else if (type == typeof(bool))
            {
                return((bool)value ? "1" : "0");
            }
            else if (type == typeof(int) || type == typeof(double) || type == typeof(long) ||
                     type == typeof(uint) || type == typeof(ulong) || type == typeof(short) ||
                     type == typeof(int?) || type == typeof(double?) || type == typeof(long?) ||
                     type == typeof(uint?) || type == typeof(ulong?) || type == typeof(short?))
            {
                return(value.ToString());
            }
            else if (type == typeof(string) || type == typeof(String) || type == typeof(char))
            {
                return(string.Format("'{0}'", value.ToString()));
            }
            else if (type == typeof(DateTime))
            {
                DateTime?dt = value as DateTime?;
                if (dt != null)
                {
                    return(db.ConstructDateTimeParam(dt.Value));
                }
                else
                {
                    return("NULL");
                }
            }

            return("NULL");
        }