github.com/polarismesh/polaris@v1.17.8/store/mysql/config_file_group.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package sqldb
    19  
    20  import (
    21  	"database/sql"
    22  	"encoding/json"
    23  	"time"
    24  
    25  	"github.com/polarismesh/polaris/common/model"
    26  	"github.com/polarismesh/polaris/common/utils"
    27  	"github.com/polarismesh/polaris/store"
    28  )
    29  
    30  type configFileGroupStore struct {
    31  	master *BaseDB
    32  	slave  *BaseDB
    33  }
    34  
    35  // CreateConfigFileGroup 创建配置文件组
    36  func (fg *configFileGroupStore) CreateConfigFileGroup(
    37  	fileGroup *model.ConfigFileGroup) (*model.ConfigFileGroup, error) {
    38  	err := fg.master.processWithTransaction("", func(tx *BaseTx) error {
    39  		if _, err := tx.Exec("DELETE FROM config_file_group WHERE flag = 1 AND namespace = ? AND name = ?",
    40  			fileGroup.Namespace, fileGroup.Name); err != nil {
    41  			return store.Error(err)
    42  		}
    43  
    44  		createSql := `
    45  INSERT INTO config_file_group (name, namespace, comment, create_time, create_by
    46  	, modify_time, modify_by, owner, business, department
    47  	, metadata)
    48  VALUES (?, ?, ?, sysdate(), ?
    49  	, sysdate(), ?, ?, ?, ?, ?)
    50  `
    51  		args := []interface{}{
    52  			fileGroup.Name, fileGroup.Namespace, fileGroup.Comment,
    53  			fileGroup.CreateBy, fileGroup.ModifyBy, fileGroup.Owner, fileGroup.Business,
    54  			fileGroup.Department, utils.MustJson(fileGroup.Metadata),
    55  		}
    56  		if _, err := tx.Exec(createSql, args...); err != nil {
    57  			return store.Error(err)
    58  		}
    59  		return tx.Commit()
    60  	})
    61  	if err != nil {
    62  		return nil, store.Error(err)
    63  	}
    64  
    65  	return fg.GetConfigFileGroup(fileGroup.Namespace, fileGroup.Name)
    66  }
    67  
    68  // UpdateConfigFileGroup 更新配置文件组信息
    69  func (fg *configFileGroupStore) UpdateConfigFileGroup(fileGroup *model.ConfigFileGroup) error {
    70  	updateSql := "UPDATE config_file_group SET comment = ?, modify_time = sysdate(), modify_by = ?, " +
    71  		" business = ?, department = ?, metadata = ? WHERE namespace = ? and name = ?"
    72  
    73  	args := []interface{}{fileGroup.Comment, fileGroup.ModifyBy, fileGroup.Business, fileGroup.Department,
    74  		utils.MustJson(fileGroup.Metadata), fileGroup.Namespace, fileGroup.Name}
    75  
    76  	if _, err := fg.master.Exec(updateSql, args...); err != nil {
    77  		return store.Error(err)
    78  	}
    79  	return nil
    80  }
    81  
    82  // GetConfigFileGroup 获取配置文件组
    83  func (fg *configFileGroupStore) GetConfigFileGroup(namespace, name string) (*model.ConfigFileGroup, error) {
    84  	querySql := fg.genConfigFileGroupSelectSql() + " WHERE namespace = ? AND name = ? AND flag = 0 "
    85  	rows, err := fg.master.Query(querySql, namespace, name)
    86  	if err != nil {
    87  		return nil, store.Error(err)
    88  	}
    89  	cfgs, err := fg.transferRows(rows)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	if len(cfgs) > 0 {
    94  		return cfgs[0], nil
    95  	}
    96  	return nil, nil
    97  }
    98  
    99  // DeleteConfigFileGroup 删除配置文件组
   100  func (fg *configFileGroupStore) DeleteConfigFileGroup(namespace, name string) error {
   101  	deleteSql := "UPDATE config_file_group SET flag = 1 WHERE namespace = ? AND name = ?"
   102  	if _, err := fg.master.Exec(deleteSql, namespace, name); err != nil {
   103  		return store.Error(err)
   104  	}
   105  	return nil
   106  }
   107  
   108  func (fg *configFileGroupStore) GetMoreConfigGroup(firstUpdate bool,
   109  	mtime time.Time) ([]*model.ConfigFileGroup, error) {
   110  
   111  	if firstUpdate {
   112  		mtime = time.Unix(0, 1)
   113  	}
   114  	loadSql := "SELECT id, name, namespace, IFNULL(comment,''), UNIX_TIMESTAMP(create_time), " +
   115  		" IFNULL(create_by,''), UNIX_TIMESTAMP(modify_time), IFNULL(modify_by,''), " +
   116  		" IFNULL(owner,''), IFNULL(business,''), IFNULL(department,''), IFNULL(metadata,'{}'), " +
   117  		" flag FROM config_file_group WHERE modify_time >= ?"
   118  
   119  	rows, err := fg.slave.Query(loadSql, mtime)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	return fg.transferRows(rows)
   124  }
   125  
   126  // CountConfigGroups
   127  func (fg *configFileGroupStore) CountConfigGroups(namespace string) (uint64, error) {
   128  	metricsSql := "SELECT count(*) FROM config_file_group WHERE flag = 0 AND namespace = ?"
   129  	row := fg.master.QueryRow(metricsSql, namespace)
   130  	var total uint64
   131  	if err := row.Scan(&total); err != nil {
   132  		return 0, store.Error(err)
   133  	}
   134  	return total, nil
   135  }
   136  
   137  func (fg *configFileGroupStore) genConfigFileGroupSelectSql() string {
   138  	return "SELECT id, name, namespace, IFNULL(comment,''), UNIX_TIMESTAMP(create_time), " +
   139  		" IFNULL(create_by,''), UNIX_TIMESTAMP(modify_time), IFNULL(modify_by,''), " +
   140  		" IFNULL(owner,''), IFNULL(business,''), IFNULL(department,''), IFNULL(metadata,'{}'), " +
   141  		" flag FROM config_file_group "
   142  }
   143  
   144  func (fg *configFileGroupStore) transferRows(rows *sql.Rows) ([]*model.ConfigFileGroup, error) {
   145  	if rows == nil {
   146  		return nil, nil
   147  	}
   148  	defer rows.Close()
   149  
   150  	var fileGroups []*model.ConfigFileGroup
   151  
   152  	for rows.Next() {
   153  		fileGroup := &model.ConfigFileGroup{}
   154  		var (
   155  			ctime, mtime, flag int64
   156  			metadata           string
   157  		)
   158  		err := rows.Scan(&fileGroup.Id, &fileGroup.Name, &fileGroup.Namespace, &fileGroup.Comment, &ctime,
   159  			&fileGroup.CreateBy, &mtime, &fileGroup.ModifyBy, &fileGroup.Owner, &fileGroup.Business,
   160  			&fileGroup.Department, &metadata, &flag)
   161  		if err != nil {
   162  			return nil, err
   163  		}
   164  		fileGroup.CreateTime = time.Unix(ctime, 0)
   165  		fileGroup.ModifyTime = time.Unix(mtime, 0)
   166  		fileGroup.Metadata = map[string]string{}
   167  		fileGroup.Valid = flag == 0
   168  		_ = json.Unmarshal([]byte(metadata), &fileGroup.Metadata)
   169  		fileGroups = append(fileGroups, fileGroup)
   170  	}
   171  
   172  	if err := rows.Err(); err != nil {
   173  		return nil, err
   174  	}
   175  
   176  	return fileGroups, nil
   177  }