ddl

package
v0.0.0-...-2d490e1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package ddl provides a go representation of Spanner DDL as well as helpers for building and manipulating Spanner DDL. We only implement enough DDL types to meet the needs of HarbourBridge.

Definitions are from https://cloud.google.com/spanner/docs/data-definition-language. Before defining each type, we give the snippet from this definition.

We use go interface types to preserve the structural constraints of Spanner DDL. For example, suppose ScalarType could be either an INT or a STRING with a length specifier:

ScalarType := INT | STRING( length )

then we use a go interface type to encode ScalarType:

type ScalarType interface {
    PrintScalarType() string  // To print ScalarTypes types.
}

and struct types for each case:

type Int struct { }
type String struct { length int64 }

and finally, we define functions:

func (i Int) PrintScalarType() { ... }
func (s String) PrintScalarType() { .. }

The net result is that the only way to build a ScalarType is using Int or String.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct{}

Bool encodes DDL BOOL.

func (Bool) PrintScalarType

func (b Bool) PrintScalarType() string

PrintScalarType unparses Bool.

type Bytes

type Bytes struct{ Len Length }

Bytes encodes DDL BYTES( length ).

func (Bytes) PrintScalarType

func (b Bytes) PrintScalarType() string

PrintScalarType unparses Bytes.

type ColumnDef

type ColumnDef struct {
	Name    string
	T       ScalarType
	IsArray bool // When false, this column has type T; when true, it is an array of type T.
	NotNull bool
	Comment string
}

ColumnDef encodes the following DDL definition:

column_def:
  column_name {scalar_type | array_type} [NOT NULL] [options_def]

func (ColumnDef) PrintColumnDef

func (cd ColumnDef) PrintColumnDef(c Config) (string, string)

PrintColumnDef unparses ColumnDef and returns it as well as any ColumnDef comment. These are returned as separate strings to support formatting needs of PrintCreateTable.

func (ColumnDef) PrintColumnDefType

func (cd ColumnDef) PrintColumnDefType() string

PrintColumnDefType unparses the type encoded in a ColumnDef.

type Config

type Config struct {
	Comments   bool // If true, print comments.
	ProtectIds bool // If true, table and col names are quoted using backticks (avoids reserved-word issue).
}

Config controls how AST nodes are printed (aka unparsed).

type CreateIndex

type CreateIndex struct {
	Name  string
	Table string
	Keys  []IndexKey
}

CreateIndex encodes the following DDL definition:

create index: CREATE [UNIQUE] [NULL_FILTERED] INDEX index_name ON table_name ( key_part [, ...] ) [ storing_clause ] [ , interleave_clause ]

func (CreateIndex) PrintCreateIndex

func (ci CreateIndex) PrintCreateIndex(c Config) string

PrintCreateIndex unparses a CREATE INDEX statement.

type CreateTable

type CreateTable struct {
	Name     string
	ColNames []string             // Provides names and order of columns
	ColDefs  map[string]ColumnDef // Provides definition of columns (a map for simpler/faster lookup during type processing)
	Pks      []IndexKey
	Comment  string
}

CreateTable encodes the following DDL definition:

create_table: CREATE TABLE table_name ([column_def, ...] ) primary_key [, cluster]

func (CreateTable) PrintCreateTable

func (ct CreateTable) PrintCreateTable(config Config) string

PrintCreateTable unparses a CREATE TABLE statement.

type Date

type Date struct{}

Date encodes DDL DATE.

func (Date) PrintScalarType

func (d Date) PrintScalarType() string

PrintScalarType unparses Date.

type Float64

type Float64 struct{}

Float64 encodes DDL FLOAT64.

func (Float64) PrintScalarType

func (g Float64) PrintScalarType() string

PrintScalarType unparses Float64

type IndexKey

type IndexKey struct {
	Col  string
	Desc bool // Default order is ascending i.e. Desc = false.
}

IndexKey encodes the following DDL definition:

primary_key:
  PRIMARY KEY ( [key_part, ...] )
key_part:
   column_name [{ ASC | DESC }]

func (IndexKey) PrintIndexKey

func (pk IndexKey) PrintIndexKey(c Config) string

PrintIndexKey unparses the index keys.

type Int64

type Int64 struct{}

Int64 encodes DDL INT64.

func (Int64) PrintScalarType

func (i Int64) PrintScalarType() string

PrintScalarType unparses Int64

type Int64Length

type Int64Length struct{ Value int64 }

Int64Length wraps an integer length specifier.

func (Int64Length) PrintLength

func (i Int64Length) PrintLength() string

PrintLength unparses Int64Length.

type Length

type Length interface {
	PrintLength() string
}

Length encodes the following Spanner DDL definition:

length:
   { int64_value | MAX }

type MaxLength

type MaxLength struct{}

MaxLength represents "MAX".

func (MaxLength) PrintLength

func (m MaxLength) PrintLength() string

PrintLength unparses MaxLength.

type ScalarType

type ScalarType interface {
	PrintScalarType() string
}

ScalarType encodes the following DDL definition:

scalar_type:
   { BOOL | INT64 | FLOAT64 | STRING( length ) | BYTES( length ) | DATE | TIMESTAMP }

type String

type String struct{ Len Length }

String encodes DDL STRING.

func (String) PrintScalarType

func (s String) PrintScalarType() string

PrintScalarType unparses String

type Timestamp

type Timestamp struct{}

Timestamp encodes DDL TIMESTAMP.

func (Timestamp) PrintScalarType

func (t Timestamp) PrintScalarType() string

PrintScalarType unparses Timestamp

Jump to

Keyboard shortcuts

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