Ejemplo n.º 1
0
 internal EntityList(EntityBase Parent, string RelatedPropertyName, Type RelatedPropertyType)
 {
     Init(Parent, RelatedPropertyName, RelatedPropertyType);
 }
Ejemplo n.º 2
0
 internal EntityList(EntityBase Parent, string RelatedPropertyName)
 {
     Init(Parent, RelatedPropertyName, null);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts a datatable into a set of instances of baseClass.
        /// </summary>
        /// <param name="table">A System.Data.DataTable with raw database data.</param>
        /// <param name="lazyLoading">Indicates whether to enable the lazyLoading on created instances.</param>
        /// <param name="baseClass">Defines which type to load.</param>
        /// <exception cref="System.ArgumentNullException">Occurs when table or baseClass parameters are null.</exception>
        /// <returns>An array of entities.</returns>
        internal static EntityBase[] BindRows(DataTable table, SearchOptions searchOptions, string baseAlias)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (searchOptions == null)
            {
                throw new ArgumentNullException("searchOptions");
            }
            if (searchOptions.eagerLoading.Count == 0)
            {
                //TODO: Check if this is necessary by performance needs.
                EntityBase[] instances = (EntityBase[])(Array.CreateInstance(searchOptions.baseType, table.Rows.Count));
                for (int i = 0; i <= instances.Length - 1; i++)
                {
                    instances[i] = (EntityBase)(Activator.CreateInstance(searchOptions.baseType));
                    instances[i].Bind(searchOptions.LazyLoading, baseAlias, null, table.Rows[i]);
                }
                return(instances);
            }
            else
            {
                Type  listof    = typeof(List <>).MakeGenericType(searchOptions.baseType);
                IList instances = (IList)Activator.CreateInstance(listof);
                //lets get metadata for baseClass and other things requested by the user.

                List <FieldInfo[]> meta = new List <FieldInfo[]>();
                meta.Add(EntityBase.GetPrimaryKeys(searchOptions.baseType));

                List <ForeignKeyInfo> keys = new List <ForeignKeyInfo>(); //i need this to get fks by index
                foreach (ForeignKeyInfo fkInfo in searchOptions.eagerLoading.Keys)
                {
                    keys.Add(fkInfo);
                    meta.Add(EntityBase.GetPrimaryKeys(fkInfo.ElementType)); //pks of each eagerloading
                }

                List <string>[] lastPk = new List <string> [meta.Count];
                for (int i = 0; i < meta.Count; i++)
                {
                    lastPk[i] = new List <string>();
                }

                foreach (DataRow dr in table.Rows)
                {
                    string basePk = string.Empty;
                    foreach (FieldInfo fi in meta[0])
                    {
                        string alias = fi.DataFieldName;
                        basePk += "||" + dr[alias].ToString();
                    }

                    for (int i = 0; i < meta.Count; i++)
                    {
                        ForeignKeyInfo key = (i == 0 ? null : keys[i - 1]);

                        string currentPKs = basePk;
                        if (i > 0)
                        {
                            foreach (FieldInfo fi in meta[i])
                            {
                                string alias = (i == 0 ? fi.DataFieldName : searchOptions.eagerLoading[key] + fi.DataFieldName);
                                currentPKs += "||" + dr[alias].ToString();
                            }
                        }

                        if (!string.IsNullOrEmpty(currentPKs) && !lastPk[i].Contains(currentPKs))
                        {
                            lastPk[i].Add(currentPKs);
                            EntityBase instance = (EntityBase)Activator.CreateInstance((i == 0 ? searchOptions.baseType : key.ElementType));
                            instance.Bind(searchOptions.LazyLoading, (i == 0 ? null : searchOptions.eagerLoading[key]), null, dr);

                            if (key == null)
                            {
                                instances.Add(instance);
                            }
                            else
                            {
                                int        index        = lastPk[0].IndexOf(basePk);
                                EntityBase baseInstance = (EntityBase)instances[index];
                                if (key.IsArray)
                                {
                                    baseInstance.lazyEnabled = false;
                                    IList value = (IList)baseInstance.GetPropertyValue(key.RelatedProperty.Name, false);
                                    baseInstance.lazyEnabled = true;
                                    value.Add(instance);
                                }
                                else
                                {
                                    baseInstance.SetPropertyValue(key.RelatedProperty.Name, instance);
                                }
                            }
                        }
                    }
                }

                return((EntityBase[])instances.GetType().GetMethod("ToArray").Invoke(instances, null));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks if the entity already exists based on the properties passed and inserts if it doesn't. In case of canUpdate is true and the entity already exists, updates it to the current values.
        /// </summary>
        /// <param name="conditionalProperties">Properties to be considered when checking existence of the entity.</param>
        /// <param name="canUpdate">If true, specifies that if a record exists, it will be updated.</param>
        /// <param name="connection">The connection.</param>
        public virtual bool SaveConditional(string[] conditionalProperties, bool canUpdate, ConnectionStringSettings connection)
        {
            if (Validate())
            {
                if (conditionalProperties == null || conditionalProperties.Length == 0)
                {
                    throw new ArgumentNullException("conditionalProperties");
                }

                TableInfo table = TableInfo.CreateTableInfo(this.GetType());
                if (connection == null)
                {
                    connection = table.GetConnection();
                }


                FieldInfo[] fields = EntityBase.GetFields(this.GetType(), null, conditionalProperties);
                if (fields.Length != conditionalProperties.Length)
                {
                    throw new MissingFieldsException(this.GetType(), true);

                    /*
                     * throw (new ArgumentException("Cannot find one or more ConditionalFields", "conditionalProperties"));
                     */
                }


                FieldInfo[] fieldsPrimary = EntityBase.GetFields(this.GetType(), true);


                FieldInfo autoKeyField = null;

                TenorParameter[] parameters = null;

                GeneralDialect dialect = null;

                string insertQuery = GetSaveSql(false, connection, null, out autoKeyField, out parameters, out dialect);

                //updateQuery doesn't need parameters cause it's already set.\
                TenorParameter[] parameters2 = null;
                string           updateQuery = GetSaveSql(true, connection, null, out autoKeyField, out parameters2, out dialect);
                parameters2 = null;

                string query = dialect.CreateConditionalSaveSql(insertQuery, updateQuery, conditionalProperties, fieldsPrimary);

                DataTable result = Helper.QueryData(connection, query.ToString(), parameters);

                if (!canUpdate && System.Convert.ToInt32(result.Rows[0][0]) == -1)
                {
                    return(false);
                }
                else if (canUpdate && System.Convert.ToInt32(result.Rows[0][0]) == -1)
                {
                    return(false);
                }
                else
                {
                    if (autoKeyField != null)
                    {
                        autoKeyField.SetPropertyValue(this, Convert.ChangeType(result.Rows[0][0], autoKeyField.FieldType));
                    }
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Executes the query defined on this instance respecting skip and take arguments.
 /// </summary>
 /// <param name="skip">Number of rows to skip</param>
 /// <param name="take">Number of rows to take</param>
 public EntityBase[] ExecuteSkipTake(int skip, int take, ConnectionStringSettings connection)
 {
     Tenor.Data.DataTable rs = SearchWithDataTable(connection, false, skip, take);
     return(EntityBase.BindRows(rs, this));
 }