コード例 #1
0
ファイル: a_d_c_f.go プロジェクト: suifengRock/clown
func (f searchADCFFilter) GetQuery(tx *xorm.Session) *xorm.Session {
	f.CheckUnscoped(tx)

	if f.HasField("AAA") && len(f.AAA) > 0 {
		name := fmt.Sprintf("%%%s%%", f.AAA)
		tx.And("a_a_a like ?", f.AAA)
	}

	if f.HasField("BBB") && len(f.BBB) > 0 {
		name := fmt.Sprintf("%%%s%%", f.BBB)
		tx.And("b_b_b like ?", f.BBB)
	}

	if f.HasField("CCC") {
		tx.And("c_c_c = ?", f.CCC)
	}
	if f.HasField("CCC_In") && len(f.CCC) > 0 {
		tx.In("c_c_c", f.CCC_In)
	}

	if f.HasField("UpdateTime") && f.UpdateTime > 0 {
		tx.And("update_at > ?", utils.UnixtimeToDate(f.UpdateTime, ""))
	}
	return tx
}
コード例 #2
0
ファイル: ssh_key.go プロジェクト: andreynering/gogs
// deletePublicKeys does the actual key deletion but does not update authorized_keys file.
func deletePublicKeys(e *xorm.Session, keyIDs ...int64) error {
	if len(keyIDs) == 0 {
		return nil
	}

	_, err := e.In("id", strings.Join(base.Int64sToStrings(keyIDs), ",")).Delete(new(PublicKey))
	return err
}
コード例 #3
0
ファイル: issue.go プロジェクト: yweber/gogs
// It's caller's responsibility to create action.
func newIssue(e *xorm.Session, repo *Repository, issue *Issue, labelIDs []int64, uuids []string, isPull bool) (err error) {
	issue.Name = strings.TrimSpace(issue.Name)
	issue.Index = repo.NextIssueIndex()

	if issue.AssigneeID > 0 {
		// Silently drop invalid assignee
		valid, err := hasAccess(e, &User{ID: issue.AssigneeID}, repo, ACCESS_MODE_WRITE)
		if err != nil {
			return fmt.Errorf("hasAccess: %v", err)
		} else if !valid {
			issue.AssigneeID = 0
		}
	}

	if _, err = e.Insert(issue); err != nil {
		return err
	}

	if isPull {
		_, err = e.Exec("UPDATE `repository` SET num_pulls=num_pulls+1 WHERE id=?", issue.RepoID)
	} else {
		_, err = e.Exec("UPDATE `repository` SET num_issues=num_issues+1 WHERE id=?", issue.RepoID)
	}
	if err != nil {
		return err
	}

	if len(labelIDs) > 0 {
		// During the session, SQLite3 dirver cannot handle retrieve objects after update something.
		// So we have to get all needed labels first.
		labels := make([]*Label, 0, len(labelIDs))
		if err = e.In("id", labelIDs).Find(&labels); err != nil {
			return fmt.Errorf("find all labels: %v", err)
		}

		for _, label := range labels {
			if label.RepoID != repo.ID {
				continue
			}

			if err = issue.addLabel(e, label); err != nil {
				return fmt.Errorf("addLabel: %v", err)
			}
		}
	}

	if issue.MilestoneID > 0 {
		if err = changeMilestoneAssign(e, 0, issue); err != nil {
			return err
		}
	}

	if err = newIssueUsers(e, repo, issue); err != nil {
		return err
	}

	// Check attachments.
	attachments := make([]*Attachment, 0, len(uuids))
	for _, uuid := range uuids {
		attach, err := getAttachmentByUUID(e, uuid)
		if err != nil {
			if IsErrAttachmentNotExist(err) {
				continue
			}
			return fmt.Errorf("getAttachmentByUUID[%s]: %v", uuid, err)
		}
		attachments = append(attachments, attach)
	}

	for i := range attachments {
		attachments[i].IssueID = issue.ID
		// No assign value could be 0, so ignore AllCols().
		if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
			return fmt.Errorf("update attachment[%d]: %v", attachments[i].ID, err)
		}
	}

	return issue.loadAttributes(e)
}
コード例 #4
0
ファイル: issue.go プロジェクト: VoyTechnology/gogs
func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
	opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
	opts.Issue.Index = opts.Repo.NextIssueIndex()

	if opts.Issue.MilestoneID > 0 {
		milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
		if err != nil && !IsErrMilestoneNotExist(err) {
			return fmt.Errorf("getMilestoneByID: %v", err)
		}

		// Assume milestone is invalid and drop silently.
		opts.Issue.MilestoneID = 0
		if milestone != nil {
			opts.Issue.MilestoneID = milestone.ID
			opts.Issue.Milestone = milestone
			if err = changeMilestoneAssign(e, opts.Issue, -1); err != nil {
				return err
			}
		}
	}

	if opts.Issue.AssigneeID > 0 {
		assignee, err := getUserByID(e, opts.Issue.AssigneeID)
		if err != nil && !IsErrUserNotExist(err) {
			return fmt.Errorf("getUserByID: %v", err)
		}

		// Assume assignee is invalid and drop silently.
		opts.Issue.AssigneeID = 0
		if assignee != nil {
			valid, err := hasAccess(e, assignee, opts.Repo, ACCESS_MODE_WRITE)
			if err != nil {
				return fmt.Errorf("hasAccess [user_id: %d, repo_id: %d]: %v", assignee.ID, opts.Repo.ID, err)
			}
			if valid {
				opts.Issue.AssigneeID = assignee.ID
				opts.Issue.Assignee = assignee
			}
		}
	}

	// Milestone and assignee validation should happen before insert actual object.
	if _, err = e.Insert(opts.Issue); err != nil {
		return err
	}

	if opts.IsPull {
		_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
	} else {
		_, err = e.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", opts.Issue.RepoID)
	}
	if err != nil {
		return err
	}

	if len(opts.LableIDs) > 0 {
		// During the session, SQLite3 dirver cannot handle retrieve objects after update something.
		// So we have to get all needed labels first.
		labels := make([]*Label, 0, len(opts.LableIDs))
		if err = e.In("id", opts.LableIDs).Find(&labels); err != nil {
			return fmt.Errorf("find all labels [label_ids: %v]: %v", opts.LableIDs, err)
		}

		for _, label := range labels {
			// Silently drop invalid labels.
			if label.RepoID != opts.Repo.ID {
				continue
			}

			if err = opts.Issue.addLabel(e, label); err != nil {
				return fmt.Errorf("addLabel [id: %d]: %v", label.ID, err)
			}
		}
	}

	if err = newIssueUsers(e, opts.Repo, opts.Issue); err != nil {
		return err
	}

	if len(opts.Attachments) > 0 {
		attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
		if err != nil {
			return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
		}

		for i := 0; i < len(attachments); i++ {
			attachments[i].IssueID = opts.Issue.ID
			if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
				return fmt.Errorf("update attachment [id: %d]: %v", attachments[i].ID, err)
			}
		}
	}

	return opts.Issue.loadAttributes(e)
}