github.com/polarismesh/polaris@v1.17.8/store/mysql/config_file_template.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 "time" 23 24 "github.com/polarismesh/polaris/common/model" 25 "github.com/polarismesh/polaris/store" 26 ) 27 28 type configFileTemplateStore struct { 29 master *BaseDB 30 slave *BaseDB 31 } 32 33 // CreateConfigFileTemplate create config file template 34 func (cf *configFileTemplateStore) CreateConfigFileTemplate( 35 template *model.ConfigFileTemplate) (*model.ConfigFileTemplate, error) { 36 createSql := ` 37 INSERT INTO config_file_template (name, content, comment, format, create_time 38 , create_by, modify_time, modify_by) 39 VALUES (?, ?, ?, ?, sysdate() 40 , ?, sysdate(), ?) 41 ` 42 _, err := cf.master.Exec(createSql, template.Name, template.Content, template.Comment, template.Format, 43 template.CreateBy, template.ModifyBy) 44 if err != nil { 45 return nil, store.Error(err) 46 } 47 48 return cf.GetConfigFileTemplate(template.Name) 49 } 50 51 // GetConfigFileTemplate get config file template by name 52 func (cf *configFileTemplateStore) GetConfigFileTemplate(name string) (*model.ConfigFileTemplate, error) { 53 querySql := cf.baseSelectConfigFileTemplateSql() + " WHERE name = ?" 54 rows, err := cf.master.Query(querySql, name) 55 if err != nil { 56 return nil, store.Error(err) 57 } 58 59 templates, err := cf.transferRows(rows) 60 if err != nil { 61 return nil, err 62 } 63 if len(templates) > 0 { 64 return templates[0], nil 65 } 66 return nil, nil 67 } 68 69 // QueryAllConfigFileTemplates query all config file templates 70 func (cf *configFileTemplateStore) QueryAllConfigFileTemplates() ([]*model.ConfigFileTemplate, error) { 71 querySql := cf.baseSelectConfigFileTemplateSql() + " ORDER BY id DESC" 72 rows, err := cf.master.Query(querySql) 73 if err != nil { 74 return nil, store.Error(err) 75 } 76 77 templates, err := cf.transferRows(rows) 78 if err != nil { 79 return nil, err 80 } 81 return templates, nil 82 } 83 84 func (cf *configFileTemplateStore) baseSelectConfigFileTemplateSql() string { 85 return ` 86 SELECT id, name, content 87 , IFNULL(comment, ''), format 88 , UNIX_TIMESTAMP(create_time) 89 , IFNULL(create_by, '') 90 , UNIX_TIMESTAMP(modify_time) 91 , IFNULL(modify_by, '') 92 FROM config_file_template 93 ` 94 } 95 96 func (cf *configFileTemplateStore) transferRows(rows *sql.Rows) ([]*model.ConfigFileTemplate, error) { 97 if rows == nil { 98 return nil, nil 99 } 100 defer func() { 101 _ = rows.Close() 102 }() 103 104 var templates []*model.ConfigFileTemplate 105 for rows.Next() { 106 template := &model.ConfigFileTemplate{} 107 var ctime, mtime int64 108 err := rows.Scan(&template.Id, &template.Name, &template.Content, &template.Comment, &template.Format, 109 &ctime, &template.CreateBy, &mtime, &template.ModifyBy) 110 if err != nil { 111 return nil, err 112 } 113 template.CreateTime = time.Unix(ctime, 0) 114 template.ModifyTime = time.Unix(mtime, 0) 115 116 templates = append(templates, template) 117 } 118 119 if err := rows.Err(); err != nil { 120 return nil, err 121 } 122 123 return templates, nil 124 }