db

package
v0.0.0-...-b66c160 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package db contains functions related to database.

  • db.go includes conneting to db, query and insertion.
  • dbschema.go contains definitions of struct for db tables. Currently it only contains Module struct.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close function closes db connection

func ConnectDB

func ConnectDB() error

ConnectDB establishes connection to database, *db* variable is assigned when opening database. This should only be called once before any other database function is called.

Users need set environment variables for connection, including

  • DB_HOST: host address of target db instances, by default: localhost.
  • DB_PORT: port number of postgres db, by default: 5432.
  • DB_USERNAME: username of database, error would be returned if not set.
  • DB_PWD: password of target database, error would be returned if not set.
  • DB_NAME: name of database for connection, error would be returned if not set.
  • DB_SOCKER_DIR: directory of Unix socket in Cloud Run which serves as Cloud SQL Auth proxy to connect to postgres database. If service is deployed on Cloud Run, just use the default value. By default, it is set to `/cloudsql`.

func DeleteFeatureBundle

func DeleteFeatureBundle(orgName string, name string, version string) error

DeleteFeatureBundle takes three pointer of string, orgName, name, version, whose combination is key of one FeatureBundle in DB's FeatureBundle table. If deletion fails, an non-nil error is returned. If the number of rows affected by this deletion is not 1, an error is also returned.

func DeleteModule

func DeleteModule(orgName string, name string, version string) error

DeleteModule takes three string, orgName, name, version, whose combination is key of one Module in DB's Module table. If deletion fails, an non-nil error is returned. If the number of rows affected by this deletion is not 1, an error is also returned.

func FormatQueryStr

func FormatQueryStr(parmNames []string, baseQuery string) string

FormatQueryStr is used to generate query statement string based on query parameters. * parmNames is a list of names of all non-nil query parameters. * baseQuery is query statement without any query parameters.

func InsertFeatureBundle

func InsertFeatureBundle(orgName string, name string, version string, data string) error

InsertFeatureBundle inserts FeatureBundle into database given values of four field of FeatureBundle schema. Or if there is existing FeatureBundle with existing key (orgName, name, version), update data field. Error is returned when insertion failed.

func InsertModule

func InsertModule(orgName string, name string, version string, data string) error

InsertModule inserts module into database given values of four field of MODULE schema. Or if there is existing module with existing key (orgName, name, version), update data field. Error is returned when insertion failed.

Types

type FeatureBundle

type FeatureBundle struct {
	OrgName string // OrgName column refers to name of organization's name holding this FeatureBundle.
	Name    string // Namme column refers to name of this FeatureBundle.
	Version string // Version column refers to version of this FeatureBundle.
	Data    string // Data column refers to json format string of this FeatureBundle in YANG schema.
}

FeatureBundle is struct of FeatureBundle table in db schema.

func QueryFeatureBundlesByKey

func QueryFeatureBundlesByKey(name *string, version *string) ([]FeatureBundle, error)

QueryFeatureBundlesByKey queries feature-bundles by its key (name, version), it is possible that If both parameters are null, this equals query for all feature-bundles. Return slice of db FeatureBundle struct each field of which corresponds to one column in Error is returned when query or reading data failed.

func QueryFeatureBundlesByOrgName

func QueryFeatureBundlesByOrgName(orgName *string) ([]FeatureBundle, error)

QueryFeatureBundlesByOrgName queries feature-bundles of organization with *orgName* from database. If orgName is null then directly query all feature-bundles. Return slice of db FeatureBundle struct each field of which corresponds to one column in db. Error is returned when query or reading data failed.

func ReadFeatureBundlesByRow

func ReadFeatureBundlesByRow(rows *sql.Rows) ([]FeatureBundle, error)

ReadFeatureBundlesByRow scans from queried FeatureBundles from rows one by one, rows are closed inside. Return slice of db FeatureBundle struct each field of which corresponds to one column in db. Error is returned when scan rows failed.

type Module

type Module struct {
	OrgName string // OrgName column refers to name of organization's name holding this Module.
	Name    string // Namme column refers to name of this Module.
	Version string // Version column refers to version of this Module.
	Summary string // Version column refers to summary of this Module.
	Data    string // Data column refers to json format string of this Module in YANG schema.
}

Module is struct of Module table in db schema.

func QueryModulesByKey

func QueryModulesByKey(name *string, version *string) ([]Module, error)

QueryModulesByKey queries modules by its key (name, version), it is possible that parameters are null. If both parameters are null, this equals query for all modules. Return slice of db Module struct each field of which corresponds to one column in db. Error is returned when query or reading data failed.

func QueryModulesByOrgName

func QueryModulesByOrgName(orgName *string) ([]Module, error)

QueryModulesByOrgName queries modules of organization with *orgName* from database. If orgName is null then directly query all modules. Return slice of db Module struct each field of which corresponds to one column in db. Error is returned when query or reading data failed.

func ReadModulesByRow

func ReadModulesByRow(rows *sql.Rows) ([]Module, error)

ReadModulesByRow scans from queried modules from rows one by one, rows are closed inside. Return slice of db Module struct each field of which corresponds to one column in db. Error is returned when scan rows failed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL