1/*
2Copyright 2014 SAP SE
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package driver
18
19import (
20	"crypto/rand"
21	"fmt"
22	"io"
23	"regexp"
24	"strconv"
25)
26
27var reSimple = regexp.MustCompile("^[_A-Z][_#$A-Z0-9]*$")
28
29// Identifier in hdb SQL statements like schema or table name.
30type Identifier string
31
32// RandomIdentifier returns a random Identifier prefixed by the prefix parameter.
33// This function is used to generate database objects with random names for test and example code.
34func RandomIdentifier(prefix string) Identifier {
35	b := make([]byte, 16)
36	if _, err := io.ReadFull(rand.Reader, b); err != nil {
37		panic(err.Error()) // rand should never fail
38	}
39	return Identifier(fmt.Sprintf("%s%x", prefix, b))
40}
41
42// String implements Stringer interface.
43func (i Identifier) String() string {
44	s := string(i)
45	if reSimple.MatchString(s) {
46		return s
47	}
48	return strconv.Quote(s)
49}
50