/// <summary> /// Generating set to table sql command from provided source data. /// </summary> /// <param name="tableType">Type that has defined Table attribute. /// Would be used as table descriptor during queri building.</param> /// <param name="data">Object that contain's fields that would be writed to database. /// Affected only fields and properties with defined Column attribute.</param> /// <param name="error">Error faces during operation.</param> /// <returns>Generated command or null if failed.</returns> public DbCommand GenerateSetToTableCommand(Type tableType, object data, out string error) { #region Validate entry data // Check is SQL operator exist. if (Active == null) { throw new NullReferenceException("Active 'ISQLOperator' not exist. Select it before managing of database."); } // Loking for table descriptor. if (!TableAttribute.TryToGetTableAttribute(tableType, out TableAttribute tableDescriptor, out error)) { return(null); } #endregion #region Members detection // Detect memebers on objects that contain columns definition. List <MemberInfo> members = MembersHandler.FindMembersWithAttribute <ColumnAttribute>(data.GetType()).ToList(); // Drop set ignore columns. members = MembersHandler.FindMembersWithoutAttribute <SetQueryIgnoreAttribute>(members).ToList(); // Drop virtual generated columns. bool NotVirtual(MemberInfo member) { return(!(member.GetCustomAttribute <IsGeneratedAttribute>() is IsGeneratedAttribute isGenerated) || isGenerated.mode != IsGeneratedAttribute.Mode.Virual); }; members = MembersHandler.FindMembersWithoutAttribute <IsGeneratedAttribute>(members, NotVirtual).ToList(); // Trying to detect member with defined isAutoIncrement attribute that has default value. MemberInfo autoIncrementMember = null; try { autoIncrementMember = IsAutoIncrementAttribute.GetIgnorable(ref data, members); } catch (Exception ex) { error = ex.Message; return(null); } // Remove ignorable. if (autoIncrementMember != null) { members.Remove(autoIncrementMember); } // Find our not key elements. IEnumerable <MemberInfo> membersNK = MembersHandler.FindMembersWithoutAttribute <IsPrimaryKeyAttribute>(members); #endregion #region Generating command // Command that can be executed on the server. DbCommand command = Active.NewCommand(); // Set values as local params. ColumnAttribute.MembersDataToCommand(ref data, ref command, members); // Getting metas. ColumnAttribute.MembersToMetaLists(members, out List <ColumnAttribute> membersColumns, out List <string> membersVars); ColumnAttribute.MembersToMetaLists(membersNK, out List <ColumnAttribute> membersNKColumns, out List <string> membersNKVars); string commadText = ""; commadText += "INSERT INTO `" + tableDescriptor.schema + "`.`" + tableDescriptor.table + "`\n"; commadText += "\t\t(" + SqlOperatorHandler.CollectionToString(membersColumns) + ")\n"; commadText += "\tVALUES\n"; commadText += "\t\t(" + SqlOperatorHandler.CollectionToString(membersVars) + ")\n"; commadText += "\tON DUPLICATE KEY UPDATE\n"; commadText += "\t\t" + SqlOperatorHandler.ConcatFormatedCollections(membersNKColumns, membersNKVars, '\0') + ";\n"; command.CommandText = commadText; #endregion error = null; return(command); }