예제 #1
0
        // Ajoute un nouvel objet a la 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 + "`");

                        val = ConvertVal(val, dateFormat);

                        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);
        }