Example #1
0
		private Table Insert(Table table,bool ignoreAutogen)
		{
            if (_readonly)
                throw new Exception("Unable to insert into a readonly database.");
            pool.Updater.InitType(table.GetType(), this);
            sTable map = pool.Mapping[table.GetType()];
            if (!ignoreAutogen)
            {
                if (pool.Mapping.IsMappableType(table.GetType().BaseType))
                {
                    Table tblPar = (Table)table.ToType(table.GetType().BaseType, null);
                    List<SelectParameter> tmpPars = new List<SelectParameter>();
                    sTable pMap = pool.Mapping[tblPar.GetType()];
                    foreach (string str in pMap.PrimaryKeyProperties)
                        tmpPars.Add(new EqualParameter(str, tblPar.GetField(str)));
                    List<Org.Reddragonit.Dbpro.Structure.Table> tmpTbls = Select(tblPar.GetType(), tmpPars.ToArray());
                    if (tmpTbls.Count > 0)
                    {
                        Table ta = tmpTbls[0];
                        Table orig = ta.LoadCopyOfOriginal(this);
                        List<string> pProps = new List<string>(pMap.PrimaryKeyProperties);
                        foreach (string prop in pMap.Properties)
                        {
                            if (!pProps.Contains(prop))
                            {
                                ta.SetField(prop, tblPar.GetField(prop));
                            }
                        }
                        bool abort = false;
                        ConnectionPoolManager.RunTriggers(this, orig, ta, ConnectionPoolManager.TriggerTypes.PRE_UPDATE, out abort);
                        if (!abort)
                        {
                            tblPar = Update(ta);
                            orig = tblPar.LoadCopyOfOriginal(this);
                            ConnectionPoolManager.RunTriggers(this, orig, tblPar, ConnectionPoolManager.TriggerTypes.PRE_UPDATE, out abort);
                        }
                    }
                    else
                    {
                        bool abort = false;
                        ConnectionPoolManager.RunTriggers(this, null, tblPar, ConnectionPoolManager.TriggerTypes.PRE_INSERT, out abort);
                        if (!abort)
                        {
                            tblPar = Insert(tblPar, ignoreAutogen);
                            ConnectionPoolManager.RunTriggers(this, null, tblPar, ConnectionPoolManager.TriggerTypes.POST_INSERT, out abort);
                        }
                    }
                    table.CopyValuesFrom(tblPar);
                }
                foreach (string prop in map.ForeignTableProperties)
                {
                    PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
                    if (!Utility.IsEnum(pi.PropertyType))
                    {
                        if (pi.PropertyType.IsArray)
                        {
                            Table[] vals = (Table[])table.GetField(prop);
                            if (vals != null)
                            {
                                foreach (Table t in vals)
                                    this.Save(t);
                            }
                        }
                        else
                        {
                            Table ext = (Table)table.GetField(prop);
                            if (ext != null)
                            {
                                ext = Save(ext);
                                table.SetField(prop, ext);
                            }
                        }
                    }
                }
                foreach (string prop in map.ArrayProperties)
                {
                    PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
                    if (!Utility.IsEnum(pi.PropertyType))
                    {
                        if (Pool.Mapping.IsMappableType(pi.PropertyType.GetElementType()))
                        {
                            Table[] vals = (Table[])table.GetField(prop);
                            if (vals != null)
                            {
                                if (vals != null)
                                {
                                    foreach (Table t in vals)
                                        this.Save(t);
                                }
                            }
                        }
                    }
                }
            }
			string query = "";
			List<IDbDataParameter> pars = new List<IDbDataParameter>();
            if (ignoreAutogen)
                query = queryBuilder.InsertWithIdentity(table, out pars);
            else
                query = queryBuilder.Insert(table, out pars);
			ExecuteNonQuery(query,pars);
			if ((pars[pars.Count-1].Direction!=ParameterDirection.Input)&&(!ignoreAutogen))
			{
                if (pars[pars.Count - 1].Value == DBNull.Value)
                {
                    string parsError = "";
                    if (pars != null)
                    {
                        foreach (IDbDataParameter param in pars)
                        {
                            if (param.Value != null)
                                parsError += param.ParameterName + ": " + param.Value.ToString() + "\n";
                            else
                                parsError += param.ParameterName + ": NULL" + "\n";
                        }
                    }
                    throw new Exception("An error occured in executing the query: " + comm.CommandText + "\nwith the parameters: " + parsError);
                }
                table.SetField(map.AutoGenProperty, pars[pars.Count - 1].Value);
			}
			table._isSaved=true;
			table.LoadStatus= LoadStatus.Complete;
            foreach (string prop in map.ArrayProperties)
            {
                PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
                if (pool.Mapping.IsMappableType(pi.PropertyType.GetElementType()))
                {
                    Dictionary<string, List<List<IDbDataParameter>>> queries = queryBuilder.UpdateMapArray(table, prop, ignoreAutogen);
                    if (queries != null)
                    {
                        foreach (string str in queries.Keys)
                        {
                            foreach (List<IDbDataParameter> p in queries[str])
                                ExecuteNonQuery(str, p);
                        }
                    }
                }
                else
                    InsertArrayValue(table,pi,0,null, map, (Array)table.GetField(prop), prop, ignoreAutogen);
            }
			return table;
		}
Example #2
0
		private Table Update(Table table)
        {
            if (_readonly)
                throw new Exception("Unable to update to a readonly database.");
			if (table.ConnectionName!=ConnectionName)
				throw new Exception("Cannot update an entry into a table into the database connection that it was not specified for.");
			if ((table.ChangedFields==null)||(table.ChangedFields.Count==0))
				return table;
            pool.Updater.InitType(table.GetType(), this);
            sTable map = Pool.Mapping[table.GetType()];
            table._changedFields = table.ChangedFields;
			if (Pool.Mapping.IsMappableType(table.GetType().BaseType))
			{
				Table ta = Update((Table)table.ToType(table.GetType().BaseType,null));
				table.CopyValuesFrom(ta);
			}
            foreach (string prop in map.ForeignTableProperties)
            {
                PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
                if (!Utility.IsEnum(pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType))
                {
                    if (pi.PropertyType.IsArray)
                    {
                        if (table.ChangedFields.Contains(prop))
                        {
                            Table[] vals = (Table[])table.GetField(prop);
                            bool changed = false;
                            if (vals != null)
                            {
                                for (int x = 0; x < vals.Length; x++)
                                {
                                    Table t = vals[x];
                                    if (table._changedFields.Contains(prop) ||
                                        t.LoadStatus == LoadStatus.NotLoaded ||
                                        t.ChangedFields.Count > 0)
                                    {
                                        vals[x] = this.Save(t);
                                        changed = true;
                                    }
                                }
                            }
                            if (changed)
                                table.SetField(prop, vals);
                        }
                    }
                    else
                    {
                        if (table.ChangedFields.Contains(prop))
                        {
                            Table ext = (Table)table.GetField(prop);
                            if (ext != null)
                            {
                                if (ext.LoadStatus == LoadStatus.NotLoaded ||
                                        ext.ChangedFields.Count > 0)
                                {
                                    ext = Save(ext);
                                    table.SetField(prop, ext);
                                }
                            }
                        }
                    }
                }
            }
            foreach (string prop in map.ArrayProperties)
            {
                PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
                if (!Utility.IsEnum(pi.PropertyType))
                {
                    if (Pool.Mapping.IsMappableType(pi.PropertyType.GetElementType()))
                    {
                        if (table.ChangedFields.Contains(prop))
                        {
                            Table[] vals = (Table[])table.GetField(prop);
                            bool changed = false;
                            if (vals != null)
                            {
                                for (int x = 0; x < vals.Length; x++)
                                {
                                    Table t = vals[x];
                                    if (t.LoadStatus == LoadStatus.NotLoaded ||
                                        t.ChangedFields.Count > 0)
                                    {
                                        vals[x] = this.Save(t);
                                        changed = true;
                                    }
                                }
                            }
                            if (changed)
                                table.SetField(prop, vals);
                        }
                    }
                }
            }
            string query = "";
            List<IDbDataParameter> pars = new List<IDbDataParameter>();
            Org.Reddragonit.Dbpro.Structure.Attributes.Table tbl = (Org.Reddragonit.Dbpro.Structure.Attributes.Table)table.GetType().GetCustomAttributes(typeof(Org.Reddragonit.Dbpro.Structure.Attributes.Table),false)[0];
            if (tbl.AlwaysInsert)
                query = queryBuilder.Insert(table, out pars);
            else
                query = queryBuilder.Update(table, out pars);
            if (query.Length > 0)
            {
                ExecuteNonQuery(query, pars);
                if ((pars[pars.Count-1].Direction!=ParameterDirection.Input) && (tbl.AlwaysInsert))
                {
                    table.SetField(map.AutoGenProperty, pars[pars.Count-1].Value);
                    table._isSaved = true;
                    table.LoadStatus = LoadStatus.Complete;
                }
            }
            if (table.ChangedFields != null)
            {
                foreach (string prop in map.Properties)
                {
                    if (map.ArrayProperties.Contains(prop) && table.ChangedFields.Contains(prop))
                    {
                        PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
                        if (pi == null)
                            pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE);
                        if (Pool.Mapping.IsMappableType(pi.PropertyType.GetElementType()) && !Utility.IsEnum(pi.PropertyType.GetElementType()))
                        {
                            Dictionary<string, List<List<IDbDataParameter>>> queries = queryBuilder.UpdateMapArray(table, prop, false);
                            foreach (string str in queries.Keys)
                            {
                                foreach (List<IDbDataParameter> p in queries[str])
                                    ExecuteNonQuery(str, p);
                            }
                        }
                        else
                            InsertArrayValue(table,pi,table.OriginalArrayLengths[prop],(table.ReplacedArrayIndexes.ContainsKey(prop) ? table.ReplacedArrayIndexes[prop] : (List<int>)null), map, (Array)table.GetField(prop), prop, false);
                    }
                }
            }
			return table;
		}