sql

package
v0.0.0-...-d9f4117 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 11 Imported by: 0

README

Please note, this package has been deprecated. Replacement can be found under github.com/Azure-Samples/azure-sdk-for-go-samples/tree/main/sdk. We strongly encourage you to upgrade to continue receiving updates. See Migration Guide for guidance on upgrading. Refer to our deprecation policy for more details.


services: mssql platforms: go author: joshgav

Azure MSSQL Samples

This // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information.

package demonstrates how to manage SQL databases and their data.

Contents

  • How to run all samples
  • Management
    • CreateServer
    • CreateDatabase
    • CreateFirewallRules
  • Data plane
    • Open
    • CreateTable
    • Insert
    • Query

How to run all samples

  1. Get this repo and all dependencies.
export PROJECT=github.com/Azure-Samples/azure-sdk-for-go-samples/services/compute
go get -u $PROJECT
cd ${GOPATH}/src/${PROJECT}
dep ensure
  1. Create an Azure service principal with the [Azure CLI][] command az ad sp create-for-rbac.
  2. Set the following environment variables based on output properties of this command. You can fill in these variables in .env.tpl in this directory and rename that to .env.
EnvVar Value
AZURE_CLIENT_ID service principal/application ID
AZURE_CLIENT_SECRET service principal/application secret
AZURE_TENANT_ID your tenant id
AZURE_SUBSCRIPTION_ID your subscription ID
AZURE_BASE_GROUP_NAME base resource group name
AZURE_LOCATION_DEFAULT location for all resources
  1. Run the sample. go test -v

More information

Please refer to Azure SDK for Go for more information.


This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Documentation

Overview

Example (CreateDatabase)

Example_createDatabase creates a SQL server and database, then creates a table and inserts a record.

var groupName = config.GenerateGroupName("DatabaseQueries")
config.SetGroupName(groupName)

serverName = strings.ToLower(serverName)

ctx := context.Background()
defer resources.Cleanup(ctx)

_, err := resources.CreateGroup(ctx, config.GroupName())
if err != nil {
	util.LogAndPanic(err)
}

_, err = CreateServer(ctx, serverName, dbLogin, dbPassword)
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot create sql server: %+v", err))
}
util.PrintAndLog("sql server created")

_, err = CreateDB(ctx, serverName, dbName)
if err != nil {
	util.LogAndPanic(fmt.Errorf("cannot create sql database: %+v", err))
}
util.PrintAndLog("database created")

err = CreateFirewallRules(ctx, serverName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("database firewall rules set")

err = testSQLDataplane(serverName, dbName, dbLogin, dbPassword)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("database operations performed")
Output:

sql server created
database created
database firewall rules set
database operations performed

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDB

func CreateDB(ctx context.Context, serverName, dbName string) (db sql.Database, err error)

CreateDB creates a new SQL Database on a given server

func CreateFirewallRules

func CreateFirewallRules(ctx context.Context, serverName string) error

CreateFirewallRules creates new firewall rules for a given server

func CreateServer

func CreateServer(ctx context.Context, serverName, dbLogin, dbPassword string) (server sql.Server, err error)

CreateServer creates a new SQL Server

func CreateTable

func CreateTable(db *sql.DB) error

CreateTable creates an SQL table

func DeleteDB

func DeleteDB(ctx context.Context, serverName, dbName string) (autorest.Response, error)

DeleteDB deletes an existing database from a server

func Insert

func Insert(db *sql.DB) error

Insert adds a row to the SQL datablase

func Open

func Open(server, database, username, password string) (*sql.DB, error)

Open opens a connection to the SQL server

func Query

func Query(db *sql.DB) error

Query queries the SQL database

Types

This section is empty.

Jump to

Keyboard shortcuts

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