github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/schema/cql/cql.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package cql
    22  
    23  import (
    24  	"bytes"
    25  	"text/template"
    26  
    27  	"github.com/uber-go/dosa"
    28  )
    29  
    30  func uniqueKey(e dosa.EntityDefinition, k *dosa.PrimaryKey) *dosa.PrimaryKey {
    31  	return e.UniqueKey(k)
    32  }
    33  
    34  // typeMap returns the CQL type associated with the given dosa.Type,
    35  // used in the template
    36  func typeMap(t dosa.Type) string {
    37  	switch t {
    38  	case dosa.String:
    39  		return "text"
    40  	case dosa.Blob:
    41  		return "blob"
    42  	case dosa.Bool:
    43  		return "boolean"
    44  	case dosa.Double:
    45  		return "double"
    46  	case dosa.Int32:
    47  		return "int"
    48  	case dosa.Int64:
    49  		return "bigint"
    50  	case dosa.Timestamp:
    51  		return "timestamp"
    52  	case dosa.TUUID:
    53  		return "uuid"
    54  	}
    55  	return "unknown"
    56  }
    57  
    58  // precompile the template for create table
    59  var cqlCreateTableTemplate = template.Must(template.
    60  	New("cqlCreateTable").
    61  	Funcs(map[string]interface{}{"typeMap": typeMap}).
    62  	Funcs(map[string]interface{}{"uniqueKey": uniqueKey}).
    63  	Parse(`create table "{{.Name}}" ({{range .Columns}}"{{- .Name -}}" {{ typeMap .Type -}}, {{end}}primary key {{ .Key }});
    64  {{- range $name, $indexdef := .Indexes }}
    65  create materialized view "{{- $name -}}" as
    66    select * from "{{- $.Name -}}"
    67    where{{range $keynum, $key := $indexdef.Key.PartitionKeys }}{{if $keynum}} AND {{end}} "{{ $key }}" is not null {{- end}}
    68    primary key {{ uniqueKey $ $indexdef.Key }};
    69  {{- end -}}`))
    70  
    71  // ToCQL generates CQL from an EntityDefinition
    72  func ToCQL(e *dosa.EntityDefinition) string {
    73  	var buf bytes.Buffer
    74  	// errors are ignored here, they can only happen from an invalid template, which will get caught in tests
    75  	err := cqlCreateTableTemplate.Execute(&buf, e)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	return buf.String()
    80  }