github.com/cs3org/reva/v2@v2.27.7/pkg/cbox/preferences/sql/sql.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package sql
    20  
    21  import (
    22  	"context"
    23  	"database/sql"
    24  	"fmt"
    25  
    26  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    27  	"github.com/cs3org/reva/v2/pkg/errtypes"
    28  	"github.com/cs3org/reva/v2/pkg/preferences"
    29  	"github.com/cs3org/reva/v2/pkg/preferences/registry"
    30  	"github.com/mitchellh/mapstructure"
    31  )
    32  
    33  func init() {
    34  	registry.Register("sql", New)
    35  }
    36  
    37  type config struct {
    38  	DbUsername string `mapstructure:"db_username"`
    39  	DbPassword string `mapstructure:"db_password"`
    40  	DbHost     string `mapstructure:"db_host"`
    41  	DbPort     int    `mapstructure:"db_port"`
    42  	DbName     string `mapstructure:"db_name"`
    43  }
    44  
    45  type mgr struct {
    46  	c  *config
    47  	db *sql.DB
    48  }
    49  
    50  // New returns an instance of the cbox sql preferences manager.
    51  func New(m map[string]interface{}) (preferences.Manager, error) {
    52  	c := &config{}
    53  	if err := mapstructure.Decode(m, c); err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.DbUsername, c.DbPassword, c.DbHost, c.DbPort, c.DbName))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	return &mgr{
    63  		c:  c,
    64  		db: db,
    65  	}, nil
    66  }
    67  
    68  func (m *mgr) SetKey(ctx context.Context, key, namespace, value string) error {
    69  	user, ok := ctxpkg.ContextGetUser(ctx)
    70  	if !ok {
    71  		return errtypes.UserRequired("preferences: error getting user from ctx")
    72  	}
    73  	query := `INSERT INTO oc_preferences(userid, appid, configkey, configvalue) values(?, ?, ?, ?) ON DUPLICATE KEY UPDATE configvalue = ?`
    74  	params := []interface{}{user.Id.OpaqueId, namespace, key, value, value}
    75  	stmt, err := m.db.Prepare(query)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if _, err = stmt.Exec(params...); err != nil {
    81  		return err
    82  	}
    83  	return nil
    84  }
    85  
    86  func (m *mgr) GetKey(ctx context.Context, key, namespace string) (string, error) {
    87  	user, ok := ctxpkg.ContextGetUser(ctx)
    88  	if !ok {
    89  		return "", errtypes.UserRequired("preferences: error getting user from ctx")
    90  	}
    91  	query := `SELECT configvalue FROM oc_preferences WHERE userid=? AND appid=? AND configkey=?`
    92  	var val string
    93  	if err := m.db.QueryRow(query, user.Id.OpaqueId, namespace, key).Scan(&val); err != nil {
    94  		if err == sql.ErrNoRows {
    95  			return "", errtypes.NotFound(namespace + ":" + key)
    96  		}
    97  		return "", err
    98  	}
    99  	return val, nil
   100  }