Exemplo n.º 1
0
        public EntityUpdater(EntityBase entity, UpdateAction action)
        {
            if (null == entity)
            {
                throw new ArgumentNullException("Entity");
            }

            if (!entity.Updatable)
            {
                throw new ArgumentException("The entity '" + m_Entity.EntityDbName + "' is not updatable");
            }

            m_Action = action;
            m_Entity = entity;

            m_EntityFields = m_Entity.Fields;

            if (null == m_EntityFields || m_EntityFields.Length == 0)
            {
                throw new ArgumentException("The entity '" + m_Entity.EntityDbName + "' doens''t contain any fields");
            }

            foreach (EntityDbField field in m_EntityFields)
            {
                if (field.IsAutoIncrement)
                {
                    m_AutoIncrementField = field;
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method executes stored procedures.
        /// </summary>
        /// <param name="procName">Procedure name</param>
        /// <param name="paramValues">Multi-dimensional array that describes the input parameters.
        /// Each parameter must have at least a parameter name (String) and a value (Object) in its second dimension.
        /// You can also specify "true" for an output parameter but this is not required for input parameters</param>
        /// <param name="outputParameters">This multi-dimensional array will contain the output parameters, in the format
        /// { "@Name" (String), value (Object) }. This array will also contain a return value if present, with the "@@Return" name</param>
        /// <returns>Returns any resulting rows as an array of DynamicEntity objects</returns>
        public DynamicEntity[] Execute(String procName, ExecuteCommandType commandType, Object[,] paramValues, ref Object[,] outputParameters)
        {
            List <DynamicEntity> results = new List <DynamicEntity>();

            using (SqlCommand cmd = new SqlCommand(procName, m_Connection)) {
                cmd.CommandType = (System.Data.CommandType)Enum.Parse(typeof(System.Data.CommandType), Enum.GetName(typeof(ExecuteCommandType), commandType));

                if (m_Connection.State == ConnectionState.Closed)
                {
                    m_Connection.Open();
                }

                List <SqlParameter> output = new List <SqlParameter>();

                if (null != paramValues)
                {
                    for (int i = 0; i < paramValues.GetLength(0); ++i)
                    {
                        if (null != paramValues[i, 0])
                        {
                            String name = paramValues[i, 0].ToString();

                            SqlParameter param = new SqlParameter(
                                name.StartsWith("@") ? name : "@" + name,
                                paramValues[i, 1]
                                );

                            param.Direction = ParameterDirection.Input;

                            for (int j = 2; j < paramValues.GetLength(1); ++j)
                            {
                                if ((paramValues[i, j] as Boolean?) != null)
                                {
                                    param.Direction = (bool)paramValues[i, j] ? ParameterDirection.Output : ParameterDirection.Input;
                                }
                                else if ((paramValues[i, j] as Int32?) != null)
                                {
                                    param.Size = (int)paramValues[i, j];
                                }
                            }

                            if (param.Direction == ParameterDirection.Output)
                            {
                                output.Add(param);
                            }

                            cmd.Parameters.Add(param);
                        }
                    }
                }

                using (SqlDataReader reader = cmd.ExecuteReader(m_OwnsConnection ? CommandBehavior.CloseConnection : CommandBehavior.Default)) {
                    EntityDbField[] fields = new EntityDbField[reader.FieldCount];
                    for (int i = 0; i < reader.FieldCount; ++i)
                    {
                        fields[i] = new EntityDbField(
                            reader.GetName(i),
                            SqlDbType.Variant,
                            null);
                    }

                    while (reader.Read())
                    {
                        DynamicEntity entity = new DynamicEntity(fields);
                        for (int i = 0; i < reader.FieldCount; ++i)
                        {
                            entity.SetValueForDbField(fields[i], reader.GetValue(i));
                        }
                        results.Add(entity);
                    }

                    reader.Close();
                }

                outputParameters = new Object[output.Count, 2];

                for (int j = 0; j < output.Count; ++j)
                {
                    outputParameters[j, 0] = output[j].ParameterName.Replace("@", "");
                    outputParameters[j, 1] = output[j].Value;
                }
            }

            return(results.ToArray());
        }
Exemplo n.º 3
0
 public EntityFetcher <T> AddJoinPath(EntityDbField fieldA, EntityDbField fieldB)
 {
     return(AddJoinPath(new Join(fieldA, fieldB, JoinType.Inner)));
 }
Exemplo n.º 4
0
 public EntityFetcher <T> AddJoinPath(EntityDbField fieldA, JoinType joinType, EntityDbField fieldB)
 {
     return(AddJoinPath(new Join(fieldA, fieldB, joinType)));
 }