Пример #1
0
        protected override object Load(IDataReader dr)
        {
            Object res = Activator.CreateInstance(type);

            foreach (PropertyInfo x in colsProperties)
            {
                if (!x.PropertyType.FullName.StartsWith("System."))
                {
                    Type t = x.PropertyType;
                    if (!dictionary.TryGetValue(t, out ReflectDataMapper dm))
                    {
                        dm = new ReflectDataMapper(t, connStr);
                    }
                    Object o = dm.GetById(dr[dm.pk.Name]);
                    x.SetValue(res, o, null);
                }
                else
                {
                    var aux = dr[x.Name];
                    if (aux.GetType() != typeof(DBNull))
                    {
                        x.SetValue(res, dr[x.Name], null);
                    }
                }
            }
            return(res);
        }
Пример #2
0
        protected override object Load(SqlDataReader dr)
        {
            object item = Activator.CreateInstance(klass);

            foreach (var p in klass.GetProperties())
            {
                Type   propertyType = p.PropertyType;
                object setParam;

                if (!propertyType.IsPrimitive && propertyType != typeof(string))
                {
                    string connStr = this.connStr;
                    //should have checked dictionary
                    ReflectDataMapper reflectDataMapper = new ReflectDataMapper(propertyType, connStr);
                    setParam = reflectDataMapper.GetById(dr[reflectDataMapper.idField].ToString());
                }
                else
                {
                    setParam = dr[p.Name];
                }
                if (setParam.GetType() == typeof(DBNull))
                {
                    if (propertyType.IsPrimitive)
                    {
                        setParam = 0;
                    }
                    else
                    {
                        setParam = null;
                    }
                }
                p.SetValue(item, setParam);
            }
            return(item);
        }
        public ReflectDataMapper(Type klass, string connStr) : base(connStr)
        {
            DomainObjectType = klass;
            ConnStr          = connStr;
            PropertyInfo[] DomainObjectProperties = klass.GetProperties();

            //Get TableAtribute from the custom atributes
            TableAttribute tb        = (TableAttribute)klass.GetCustomAttribute(typeof(TableAttribute), false);
            string         tableName = tb.Name;

            StringBuilder columnsSB = new StringBuilder();
            StringBuilder updateSB  = new StringBuilder();
            String        pkName    = "";
            int           i         = 0;

            //-1 because the primary key is not here
            ColumnsOfDomain = new AbstractColumn[DomainObjectProperties.Length - 1];


            //Iterate all proprieties to build the string for columns and for the update query
            foreach (PropertyInfo property in DomainObjectProperties)
            {
                //AbstractColumn currentColumn;

                Type propType = property.PropertyType;
                if (property.IsDefined(typeof(PKAttribute), false))
                {
                    PrimaryKey = new PrimitiveColumn(property);
                    pkName     = PrimaryKey.GetName();
                }
                else if (propType.IsPrimitive || propType.Equals(typeof(string)))
                {
                    PrimitiveColumn currentColumn = new PrimitiveColumn(property);
                    ColumnsOfDomain[i] = currentColumn;
                    ConstructSQLQuery(currentColumn.GetName(), ++i, columnsSB, updateSB);
                }
                else
                {
                    ReflectDataMapper  rdm           = new ReflectDataMapper(propType, connStr);
                    NotPrimitiveColumn currentColumn = new NotPrimitiveColumn(property, rdm);
                    ColumnsOfDomain[i] = currentColumn;
                    ConstructSQLQuery(currentColumn.GetFKName(), ++i, columnsSB, updateSB);
                }
            }

            //remove last unnecessary comma ','
            if (columnsSB[columnsSB.Length - 1] == ',')
            {
                columnsSB.Remove(columnsSB.Length - 1, 1);
                updateSB.Remove(updateSB.Length - 1, 1);
            }

            //Build all SQL queries Strings
            SQL_GET_ALL   = "SELECT " + pkName + "," + columnsSB.ToString() + " FROM " + tableName;
            SQL_GET_BY_ID = SQL_GET_ALL + " WHERE " + pkName + " = ";
            SQL_INSERT    = "INSERT INTO " + tableName + "(" + columnsSB.ToString() + ")" + " OUTPUT INSERTED." + pkName + " VALUES ";
            SQL_DELETE    = "DELETE FROM " + tableName + " WHERE " + pkName + " = ";
            SQL_UPDATE    = "UPDATE " + tableName + " SET " + updateSB.ToString() + " WHERE " + pkName + "={0}";
        }
Пример #4
0
        protected override string SqlInsert(object target)
        {
            StringBuilder str             = new StringBuilder("INSERT INTO " + tableName + " (");
            bool          isAutoIncrement = ((PKAttribute)pk.GetCustomAttribute(typeof(PKAttribute))).AutoIncrement;

            str.Append(columns);
            if (!isAutoIncrement)
            {
                str.Append(", ").Append(pk.Name);
            }
            str.Append(") OUTPUT INSERTED.").Append(pk.Name).Append(" VALUES (");

            int aux = 0;

            foreach (PropertyInfo p in target.GetType().GetProperties())
            {
                if (!p.IsDefined(typeof(PKAttribute)))
                {
                    if (aux++ != 0)
                    {
                        str.Append(", ");
                    }
                    Object curr;

                    if (!p.PropertyType.FullName.StartsWith("System."))
                    {
                        if (!dictionary.TryGetValue(p.PropertyType, out ReflectDataMapper dm))
                        {
                            dm = new ReflectDataMapper(p.PropertyType, connStr);
                        }
                        Object buff = p.GetValue(target);
                        curr = buff.GetType().GetProperty(dm.pk.Name).GetValue(buff);
                    }
                    else
                    {
                        curr = p.GetValue(target);
                    }

                    if (curr == null)
                    {
                        str.Append("NULL");
                    }
                    else if (curr.GetType() == typeof(String))
                    {
                        str.Append('\'').Append(curr).Append('\'');
                    }
                    else
                    {
                        str.Append(curr);
                    }
                }
            }
            if (!isAutoIncrement)
            {
                if (aux == 1)
                {
                    str.Append(pk.GetType() == typeof(String) ? ("'" + pk.GetValue(target) + "'") : pk.GetValue(target));
                }
                else
                {
                    str.Append(", ").Append(pk.PropertyType == typeof(String) ? ("'" + pk.GetValue(target) + "'") : pk.GetValue(target));
                }
            }

            return(str.Append(" )").ToString());
        }
 public NotPrimitiveColumn(PropertyInfo Pi, ReflectDataMapper rdm) : base(Pi)
 {
     notPrimitiveValue = rdm;
     this.Name         = Pi.Name;
 }