Example #1
0
        /// <summary>
        /// Generates a select for a single object
        /// </summary>
        /// <param name="item">The item to load (primary key needs to be set)</param>
        /// <returns></returns>
        public virtual IDbCommand GetSelectCommand(object item)
        {
            Type             t   = item.GetType();
            DatabaseTypeInfo ti  = TypeParser.GetTypeInfo(t);
            IDbCommand       cmd = _connection.GetCommand();

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("SELECT {0} FROM {1} WHERE ", GetSelectList(t), ResolveTableName(ti));

            for (int i = 0; i < ti.PrimaryKeys.Count; i++)
            {
                DataFieldInfo dfi   = ti.PrimaryKeys[i];
                object        value = dfi.Getter(item);

                if (value != null)
                {
                    if (i > 0)
                    {
                        sb.Append(" AND ");
                    }
                    string pName = GetParameterName(cmd);
                    sb.Append(string.Concat(dfi.EscapedFieldName, "=", pName));
                    cmd.Parameters.Add(_connection.GetParameter(pName, value));
                }
            }

            cmd.CommandText = sb.ToString();
            return(cmd);
        }
Example #2
0
        /// <summary>
        /// Returns a command for performing an update on an object
        /// </summary>
        /// <param name="item">The object to update</param>
        /// <returns></returns>
        public virtual IDbCommand GetUpdateCommand(object item)
        {
            DatabaseTypeInfo data      = TypeParser.GetTypeInfo(item.GetType());
            IDbCommand       toReturn  = _connection.GetCommand();
            StringBuilder    fieldList = new StringBuilder("UPDATE ");

            fieldList.Append(ResolveTableName(data));
            fieldList.Append(" SET ");
            bool addComma = false;

            foreach (DataFieldInfo dfi in data.DataFields)
            {
                if (!dfi.PrimaryKey && dfi.SetOnInsert)
                {
                    if (addComma)
                    {
                        fieldList.Append(",");
                    }
                    object value = dfi.Getter(item);

                    if (value != null)
                    {
                        string fName = GetParameterName(toReturn);
                        fieldList.Append(string.Concat(dfi.EscapedFieldName, "=", fName));
                        toReturn.Parameters.Add(_connection.GetParameter(fName, value));
                    }
                    else
                    {
                        fieldList.Append(string.Format(@"{0}=null", dfi.EscapedFieldName));
                    }
                    addComma = true;
                }
            }

            fieldList.Append(" WHERE ");
            for (int i = 0; i < data.PrimaryKeys.Count(); i++)
            {
                DataFieldInfo dField = data.PrimaryKeys.ElementAt(i);
                if (i > 0)
                {
                    fieldList.Append(" AND ");
                }

                string fName = GetParameterName(toReturn);
                fieldList.Append(string.Concat(dField.EscapedFieldName, "=", fName));
                toReturn.Parameters.Add(_connection.GetParameter(fName, dField.Getter(item)));
            }

            toReturn.CommandText = fieldList.ToString();
            return(toReturn);
        }