Esempio n. 1
0
 public void SaveObject(DataObject Char)
 {
     Char.Dirty = true;
     CharacterDB.SaveObject(Char);
 }
Esempio n. 2
0
 public void AddObject(DataObject Char)
 {
     CharacterDB.AddObject(Char);
 }
Esempio n. 3
0
 public void RemoveObject(DataObject Char)
 {
     CharacterDB.DeleteObject(Char);
 }
Esempio n. 4
0
 public void SetPreCachedObject(object key, DataObject obj)
 {
     _precache[key] = obj;
 }
Esempio n. 5
0
 public void SetCacheObject(object key, DataObject obj)
 {
     _cache[key] = obj;
 }
        // Ajoute un nouvel objet a la DB
        // Adds a new object DB
        protected override bool AddObjectImpl(DataObject dataObject)
        {
            try
            {
                string tableName = dataObject.TableName;

                if (dataObject.ObjectId == null)
                {
                    dataObject.ObjectId = IDGenerator.GenerateID();
                }

                var columns = new StringBuilder();
                var values  = new StringBuilder();

                MemberInfo[] objMembers   = dataObject.GetType().GetMembers();
                bool         hasRelations = false;
                string       dateFormat   = Connection.GetDBDateFormat();

                columns.Append("`" + tableName + "_ID`");
                values.Append("'" + Escape(dataObject.ObjectId) + "'");

                for (int i = 0; i < objMembers.Length; i++)
                {
                    if (!hasRelations)
                    {
                        object[] relAttrib = GetRelationAttributes(objMembers[i]);
                        hasRelations = relAttrib.Length > 0;
                    }
                    object[] keyAttrib = objMembers[i].GetCustomAttributes(typeof(PrimaryKey), true);
                    object[] attrib    = objMembers[i].GetCustomAttributes(typeof(DataElement), true);
                    if (attrib.Length > 0 || keyAttrib.Length > 0)
                    {
                        object val = null;
                        if (objMembers[i] is PropertyInfo)
                        {
                            val = ((PropertyInfo)objMembers[i]).GetValue(dataObject, null);
                        }
                        else if (objMembers[i] is FieldInfo)
                        {
                            val = ((FieldInfo)objMembers[i]).GetValue(dataObject);
                        }

                        columns.Append(", ");
                        values.Append(", ");
                        columns.Append("`" + objMembers[i].Name + "`");

                        if (val is bool)
                        {
                            val = ((bool)val) ? (byte)1 : (byte)0;
                        }
                        else if (val is DateTime)
                        {
                            val = ((DateTime)val).ToString(dateFormat);
                        }
                        else if (val is float)
                        {
                            val = ((float)val).ToString(Nfi);
                        }
                        else if (val is double)
                        {
                            val = ((double)val).ToString(Nfi);
                        }
                        else if (val is string)
                        {
                            val = Escape(val.ToString());
                        }
                        else if (val is List <byte> )
                        {
                            List <byte> bytes  = val as List <byte>;
                            string      Result = "";
                            foreach (byte b in bytes)
                            {
                                Result += b.ToString("X2") + " ";
                            }
                            val = Result;
                        }
                        else if (val is List <UInt32> )
                        {
                            List <UInt32> bytes  = val as List <UInt32>;
                            string        Result = "";
                            foreach (UInt32 b in bytes)
                            {
                                Result += b.ToString("X4") + " ";
                            }
                            val = Result;
                        }
                        else if (val is List <long> )
                        {
                            List <long> bytes  = val as List <long>;
                            string      Result = "";
                            foreach (long b in bytes)
                            {
                                Result += b.ToString("X8") + " ";
                            }
                            val = Result;
                        }

                        values.Append('\'');
                        values.Append(val);
                        values.Append('\'');
                    }
                }

                string sql = "INSERT INTO `" + tableName + "` (" + columns + ") VALUES (" + values + ")";

                Log.Debug("MysqlObject", sql);

                int res = Connection.ExecuteNonQuery(sql);
                if (res == 0)
                {
                    Log.Error("MysqlObject", "Add Error : " + dataObject.TableName + " ID=" + dataObject.ObjectId + "Query = " + sql);
                    return(false);
                }

                if (hasRelations)
                {
                    SaveObjectRelations(dataObject);
                }

                dataObject.Dirty     = false;
                dataObject.IsValid   = true;
                dataObject.IsDeleted = false;

                return(true);
            }
            catch (Exception e)
            {
                Log.Error("MysqlObject", "Add Error : " + dataObject.TableName + " " + dataObject.ObjectId + e.ToString());
            }

            return(false);
        }
        // Persiste l'objet dans la DB
        // Persists the object in the DB
        protected override void SaveObjectImpl(DataObject dataObject)
        {
            try
            {
                string tableName = dataObject.TableName;

                var sb = new StringBuilder("UPDATE `" + tableName + "` SET ");

                BindingInfo[] bindingInfo  = GetBindingInfo(dataObject.GetType());
                bool          hasRelations = false;
                bool          first        = true;
                string        dateFormat   = Connection.GetDBDateFormat();

                for (int i = 0; i < bindingInfo.Length; i++)
                {
                    BindingInfo bind = bindingInfo[i];

                    if (bind.ReadOnly)
                    {
                        continue;
                    }

                    if (!hasRelations)
                    {
                        hasRelations = bind.HasRelation;
                    }
                    if (!bind.HasRelation)
                    {
                        object val = null;
                        if (bind.Member is PropertyInfo)
                        {
                            val = ((PropertyInfo)bind.Member).GetValue(dataObject, null);
                        }
                        else if (bind.Member is FieldInfo)
                        {
                            val = ((FieldInfo)bind.Member).GetValue(dataObject);
                        }
                        else
                        {
                            continue;
                        }

                        if (!first)
                        {
                            sb.Append(", ");
                        }
                        else
                        {
                            first = false;
                        }

                        if (val is bool)
                        {
                            val = ((bool)val) ? (byte)1 : (byte)0;
                        }
                        else if (val is DateTime)
                        {
                            val = ((DateTime)val).ToString(dateFormat);
                        }
                        else if (val is float)
                        {
                            val = ((float)val).ToString(Nfi);
                        }
                        else if (val is double)
                        {
                            val = ((double)val).ToString(Nfi);
                        }
                        else if (val is string)
                        {
                            val = Escape(val.ToString());
                        }
                        else if (val is List <byte> )
                        {
                            List <byte> bytes  = val as List <byte>;
                            string      Result = "";
                            foreach (byte b in bytes)
                            {
                                Result += b.ToString("X2") + " ";
                            }
                            val = Result;
                        }
                        else if (val is List <UInt32> )
                        {
                            List <UInt32> bytes  = val as List <UInt32>;
                            string        Result = "";
                            foreach (UInt32 b in bytes)
                            {
                                Result += b.ToString("X4") + " ";
                            }
                            val = Result;
                        }
                        else if (val is List <long> )
                        {
                            List <long> bytes  = val as List <long>;
                            string      Result = "";
                            foreach (long b in bytes)
                            {
                                Result += b.ToString("X8") + " ";
                            }
                            val = Result;
                        }

                        sb.Append("`" + bind.Member.Name + "` = ");
                        sb.Append('\'');
                        sb.Append(val);
                        sb.Append('\'');
                    }
                }

                sb.Append(" WHERE `" + tableName + "_ID` = '" + Escape(dataObject.ObjectId) + "'");

                string sql = sb.ToString();
                Log.Debug("MysqlObject", sql);

                int res = Connection.ExecuteNonQuery(sql);
                if (res == 0)
                {
                    Log.Error("MysqlObject", "Modify error : " + dataObject.TableName + " ID=" + dataObject.ObjectId + " --- keyvalue changed? " + sql + " " + Environment.StackTrace);
                    return;
                }

                if (hasRelations)
                {
                    SaveObjectRelations(dataObject);
                }

                dataObject.Dirty   = false;
                dataObject.IsValid = true;
            }
            catch (Exception e)
            {
                Log.Error("MysqlObject", "Modify error : " + dataObject.TableName + " " + dataObject.ObjectId + e.ToString());
            }
        }