main

mattermost/focalboard

Last updated at: 29/12/2023 09:47

sharing.go

TLDR

The file sharing.go contains two methods for interacting with the sharing table in the SQLStore package.

Methods

upsertSharing

This method is responsible for upserting a sharing record into the database. It takes a db parameter of type sq.BaseRunner and a sharing parameter of type model.Sharing. It inserts the sharing record into the "sharing" table, including fields such as ID, enabled, token, modified_by, and update_at. If the database type is MySQL, it includes a "ON DUPLICATE KEY UPDATE" clause, otherwise, it includes an "ON CONFLICT" clause. The method returns an error if the operation fails.

getSharing

This method is used to retrieve a sharing record from the database. It takes a db parameter of type sq.BaseRunner and a boardID parameter of type string. It selects the relevant fields from the "sharing" table based on the provided boardID and returns a model.Sharing object representing the retrieved sharing record. If the record does not exist or an error occurs, it returns nil along with the respective error.

package sqlstore

import (
	"github.com/mattermost/focalboard/server/model"
	"github.com/mattermost/focalboard/server/utils"

	sq "github.com/Masterminds/squirrel"
)

func (s *SQLStore) upsertSharing(db sq.BaseRunner, sharing model.Sharing) error {
	now := utils.GetMillis()

	query := s.getQueryBuilder(db).
		Insert(s.tablePrefix+"sharing").
		Columns(
			"id",
			"enabled",
			"token",
			"modified_by",
			"update_at",
		).
		Values(
			sharing.ID,
			sharing.Enabled,
			sharing.Token,
			sharing.ModifiedBy,
			now,
		)
	if s.dbType == model.MysqlDBType {
		query = query.Suffix("ON DUPLICATE KEY UPDATE enabled = ?, token = ?, modified_by = ?, update_at = ?",
			sharing.Enabled, sharing.Token, sharing.ModifiedBy, now)
	} else {
		query = query.Suffix(
			`ON CONFLICT (id)
			 DO UPDATE SET enabled = EXCLUDED.enabled, token = EXCLUDED.token, modified_by = EXCLUDED.modified_by, update_at = EXCLUDED.update_at`,
		)
	}

	_, err := query.Exec()
	return err
}

func (s *SQLStore) getSharing(db sq.BaseRunner, boardID string) (*model.Sharing, error) {
	query := s.getQueryBuilder(db).
		Select(
			"id",
			"enabled",
			"token",
			"modified_by",
			"update_at",
		).
		From(s.tablePrefix + "sharing").
		Where(sq.Eq{"id": boardID})
	row := query.QueryRow()
	sharing := model.Sharing{}

	err := row.Scan(
		&sharing.ID,
		&sharing.Enabled,
		&sharing.Token,
		&sharing.ModifiedBy,
		&sharing.UpdateAt,
	)
	if err != nil {
		return nil, err
	}

	return &sharing, nil
}