ddl

package
v1.0.1-0...-1811ebb Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 6 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.

Index

Constants

View Source
const (
	// Types supported by Spanner with google_standard_sql (default) dialect.
	// Bool represent BOOL type.
	Bool string = "BOOL"
	// Bytes represent BYTES type.
	Bytes string = "BYTES"
	// Date represent DATE type.
	Date string = "DATE"
	// Float64 represent FLOAT64 type.
	Float64 string = "FLOAT64"
	// Int64 represent INT64 type.
	Int64 string = "INT64"
	// String represent STRING type.
	String string = "STRING"
	// Timestamp represent TIMESTAMP type.
	Timestamp string = "TIMESTAMP"
	// Numeric represent NUMERIC type.
	Numeric string = "NUMERIC"
	// Json represent JSON type.
	JSON string = "JSON"
	// MaxLength is a sentinel for Type's Len field, representing the MAX value.
	MaxLength = math.MaxInt64
	// StringMaxLength represents maximum allowed STRING length.
	StringMaxLength = 2621440
	// BytesMaxLength represents maximum allowed BYTES length.
	BytesMaxLength        = 10485760
	MaxNonKeyColumnLength = 1677721600

	// Types specific to Spanner with postgresql dialect, when they differ from
	// Spanner with google_standard_sql.
	// PGBytea represent BYTEA type, which is BYTES type in PG.
	PGBytea string = "BYTEA"
	// PGFloat8 represent FLOAT8 type, which is double type in PG.
	PGFloat8 string = "FLOAT8"
	// PGInt8 respresent INT8, which is INT type in PG.
	PGInt8 string = "INT8"
	// PGVarchar represent VARCHAR, which is STRING type in PG.
	PGVarchar string = "VARCHAR"
	// PGTimestamptz represent TIMESTAMPTZ, which is TIMESTAMP type in PG.
	PGTimestamptz string = "TIMESTAMPTZ"
	// Jsonb represents the PG.JSONB type
	PGJSONB string = "JSONB"
	// PGMaxLength represents sentinel for Type's Len field in PG.
	PGMaxLength = 2621440
)

Variables

View Source
var PGSQL_RESERVED_KEYWORD_LIST = []string{}/* 151 elements not displayed */

PGDialect keyword list Assumption is that this list PGSQL dialect uses the same keywords

Functions

func GetPGType

func GetPGType(spType Type) string

func GetSortedTableIdsBySpName

func GetSortedTableIdsBySpName(s Schema) []string

Tables are ordered in alphabetical order with one exception: interleaved tables appear after the definition of their parent table.

TODO: Move this method to mapping.go and preserve the table names in sorted order in conv so that we don't need to order the table names multiple times.

Types

type ColumnDef

type ColumnDef struct {
	Name    string
	T       Type
	NotNull bool
	Comment string
	Id      string
}

ColumnDef encodes the following DDL definition:

column_def:
  column_name 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.

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).
	Tables      bool // If true, print tables
	ForeignKeys bool // If true, print foreign key constraints.
	SpDialect   string
	Source      string //SourceDB information for determining case-sensitivity handling for PGSQL
}

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

type CreateIndex

type CreateIndex struct {
	Name            string
	TableId         string `json:"TableId"`
	Unique          bool
	Keys            []IndexKey
	Id              string
	StoredColumnIds []string
}

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(ct CreateTable, c Config) string

PrintCreateIndex unparses a CREATE INDEX statement.

type CreateTable

type CreateTable struct {
	Name          string
	ColIds        []string // Provides names and order of columns
	ShardIdColumn string
	ColDefs       map[string]ColumnDef // Provides definition of columns (a map for simpler/faster lookup during type processing)
	PrimaryKeys   []IndexKey
	ForeignKeys   []Foreignkey
	Indexes       []CreateIndex
	ParentId      string //if not empty, this table will be interleaved
	Comment       string
	Id            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(spSchema Schema, config Config) string

PrintCreateTable unparses a CREATE TABLE statement.

type Foreignkey

type Foreignkey struct {
	Name           string
	ColIds         []string
	ReferTableId   string
	ReferColumnIds []string
	Id             string
}

Foreignkey encodes the following DDL definition:

   [ CONSTRAINT constraint_name ]
	  FOREIGN KEY ( column_name [, ... ] ) REFERENCES ref_table ( ref_column [, ... ] ) }

func (Foreignkey) PrintForeignKey

func (k Foreignkey) PrintForeignKey(c Config) string

PrintForeignKey unparses the foreign keys.

func (Foreignkey) PrintForeignKeyAlterTable

func (k Foreignkey) PrintForeignKeyAlterTable(spannerSchema Schema, c Config, tableId string) string

PrintForeignKeyAlterTable unparses the foreign keys using ALTER TABLE.

type IndexKey

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

IndexKey encodes the following DDL definition:

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

func (IndexKey) PrintPkOrIndexKey

func (idx IndexKey) PrintPkOrIndexKey(ct CreateTable, c Config) string

PrintPkOrIndexKey unparses the primary or index keys.

type Schema

type Schema map[string]CreateTable

Schema stores a map of table names and Tables.

func NewSchema

func NewSchema() Schema

NewSchema creates a new Schema object.

func (Schema) CheckInterleaved

func (s Schema) CheckInterleaved() bool

CheckInterleaved checks if schema contains interleaved tables.

func (Schema) GetDDL

func (s Schema) GetDDL(c Config) []string

GetDDL returns the string representation of Spanner schema represented by Schema struct. Tables are printed in alphabetical order with one exception: interleaved tables are potentially out of order since they must appear after the definition of their parent table.

type Type

type Type struct {
	Name string
	// Len encodes the following Spanner DDL definition:
	//     length:
	//        { int64_value | MAX }
	Len int64
	// IsArray represents if Type is an array_type or not
	// When false, column has type T; when true, it is an array of type T.
	IsArray bool
}

Type represents the type of a column.

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

func (Type) PGPrintColumnDefType

func (ty Type) PGPrintColumnDefType() string

func (Type) PrintColumnDefType

func (ty Type) PrintColumnDefType() string

PrintColumnDefType unparses the type encoded in a ColumnDef.

Jump to

Keyboard shortcuts

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