<commit_before>/*
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package spansql

// This file holds SQL methods for rendering the types in types.go
// as the SQL dialect that this package parses.

import (
	"strconv"
	"strings"
)

func (ct CreateTable) SQL() string {
	str := "CREATE TABLE " + ID(ct.Name).SQL() + " (\n"
	for _, c := range ct.Columns {
		str += "  " + c.SQL() + ",\n"
	}
	for _, tc := range ct.Constraints {
		str += "  " + tc.SQL() + ",\n"
	}
	str += ") PRIMARY KEY("
	for i, c := range ct.PrimaryKey {
		if i > 0 {
			str += ", "
		}
		str += c.SQL()
	}
	str += ")"
	if il := ct.Interleave; il != nil {
		str += ",\n  INTERLEAVE IN PARENT " + ID(il.Parent).SQL() + " ON DELETE " + il.OnDelete.SQL()
	}
	return str
}

func (ci CreateIndex) SQL() string {
	str := "CREATE"
	if ci.Unique {
		str += " UNIQUE"
	}
	if ci.NullFiltered {
		str += " NULL_FILTERED"
	}
	str += " INDEX " + ID(ci.Name).SQL() + " ON " + ID(ci.Table).SQL() + "("
	for i, c := range ci.Columns {
		if i > 0 {
			str += ", "
		}
		str += c.SQL()
	}
	str += ")"
	if len(ci.Storing) > 0 {
		str += " STORING (" + idList(ci.Storing) + ")"
	}
	if ci.Interleave != "" {
		str += ", INTERLEAVE IN " + ID(ci.Interleave).SQL()
	}
	return str
}

func (dt DropTable) SQL() string {
	return "DROP TABLE " + ID(dt.Name).SQL()
}

func (di DropIndex) SQL() string {
	return "DROP INDEX " + ID(di.Name).SQL()
}

func (at AlterTable) SQL() string {
	return "ALTER TABLE " + ID(at.Name).SQL() + " " + at.Alteration.SQL()
}

func (ac AddColumn) SQL() string {
	return "ADD COLUMN " + ac.Def.SQL()
}

func (dc DropColumn) SQL() string {
	return "DROP COLUMN " + ID(dc.Name).SQL()
}

func (ac AddConstraint) SQL() string {
	return "ADD " + ac.Constraint.SQL()
}

func (dc DropConstraint) SQL() string {
	return "DROP CONSTRAINT " + ID(dc.Name).SQL()
}

func (sod SetOnDelete) SQL() string {
	return "SET ON DELETE " + sod.Action.SQL()
}

func (od OnDelete) SQL() string {
	switch od {
	case NoActionOnDelete:
		return "NO ACTION"
	case CascadeOnDelete:
		return "CASCADE"
	}
	panic("unknown OnDelete")
}

func (ac AlterColumn) SQL() string {
	return "ALTER COLUMN " + ac.Def.SQL()
}

func (d *Delete) SQL() string {
	return "DELETE FROM " + ID(d.Table).SQL() + " WHERE " + d.Where.SQL()
}

func (cd ColumnDef) SQL() string {
	str := ID(cd.Name).SQL() + " " + cd.Type.SQL()
	if cd.NotNull {
		str += " NOT NULL"
	}
	if cd.Type.Base == Timestamp && cd.AllowCommitTimestamp != nil {
		if *cd.AllowCommitTimestamp {
			str += " OPTIONS (allow_commit_timestamp = true)"
		} else {
			str += " OPTIONS (allow_commit_timestamp = null)"
		}
	}
	return str
}

func (tc TableConstraint) SQL() string {
	var str string
	if tc.Name != "" {
		str += "CONSTRAINT " + ID(tc.Name).SQL()
	}
	str += tc.ForeignKey.SQL()
	return str<commit_msg>spanner/spansql: add space between constraint name and foreign key def<commit_after>/*
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package spansql

// This file holds SQL methods for rendering the types in types.go
// as the SQL dialect that this package parses.

import (
	"strconv"
	"strings"
)

func (ct CreateTable) SQL() string {
	str := "CREATE TABLE " + ID(ct.Name).SQL() + " (\n"
	for _, c := range ct.Columns {
		str += "  " + c.SQL() + ",\n"
	}
	for _, tc := range ct.Constraints {
		str += "  " + tc.SQL() + ",\n"
	}
	str += ") PRIMARY KEY("
	for i, c := range ct.PrimaryKey {
		if i > 0 {
			str += ", "
		}
		str += c.SQL()
	}
	str += ")"
	if il := ct.Interleave; il != nil {
		str += ",\n  INTERLEAVE IN PARENT " + ID(il.Parent).SQL() + " ON DELETE " + il.OnDelete.SQL()
	}
	return str
}

func (ci CreateIndex) SQL() string {
	str := "CREATE"
	if ci.Unique {
		str += " UNIQUE"
	}
	if ci.NullFiltered {
		str += " NULL_FILTERED"
	}
	str += " INDEX " + ID(ci.Name).SQL() + " ON " + ID(ci.Table).SQL() + "("
	for i, c := range ci.Columns {
		if i > 0 {
			str += ", "
		}
		str += c.SQL()
	}
	str += ")"
	if len(ci.Storing) > 0 {
		str += " STORING (" + idList(ci.Storing) + ")"
	}
	if ci.Interleave != "" {
		str += ", INTERLEAVE IN " + ID(ci.Interleave).SQL()
	}
	return str
}

func (dt DropTable) SQL() string {
	return "DROP TABLE " + ID(dt.Name).SQL()
}

func (di DropIndex) SQL() string {
	return "DROP INDEX " + ID(di.Name).SQL()
}

func (at AlterTable) SQL() string {
	return "ALTER TABLE " + ID(at.Name).SQL() + " " + at.Alteration.SQL()
}

func (ac AddColumn) SQL() string {
	return "ADD COLUMN " + ac.Def.SQL()
}

func (dc DropColumn) SQL() string {
	return "DROP COLUMN " + ID(dc.Name).SQL()
}

func (ac AddConstraint) SQL() string {
	return "ADD " + ac.Constraint.SQL()
}

func (dc DropConstraint) SQL() string {
	return "DROP CONSTRAINT " + ID(dc.Name).SQL()
}

func (sod SetOnDelete) SQL() string {
	return "SET ON DELETE " + sod.Action.SQL()
}

func (od OnDelete) SQL() string {
	switch od {
	case NoActionOnDelete:
		return "NO ACTION"
	case CascadeOnDelete:
		return "CASCADE"
	}
	panic("unknown OnDelete")
}

func (ac AlterColumn) SQL() string {
	return "ALTER COLUMN " + ac.Def.SQL()
}

func (d *Delete) SQL() string {
	return "DELETE FROM " + ID(d.Table).SQL() + " WHERE " + d.Where.SQL()
}

func (cd ColumnDef) SQL() string {
	str := ID(cd.Name).SQL() + " " + cd.Type.SQL()
	if cd.NotNull {
		str += " NOT NULL"
	}
	if cd.Type.Base == Timestamp && cd.AllowCommitTimestamp != nil {
		if *cd.AllowCommitTimestamp {
			str += " OPTIONS (allow_commit_timestamp = true)"
		} else {
			str += " OPTIONS (allow_commit_timestamp = null)"
		}
	}
	return str
}

func (tc TableConstraint) SQL() string {
	var str string
	if tc.Name != "" {
		str += "CONSTRAINT " + ID(tc.Name).SQL() + " "
	}
	str += tc.ForeignKey.SQL()
	return str
{'fraction_non_alphanumeric': 0.1624418779061047, 'fraction_numerical': 0.0032998350082495873, 'mean_word_length': 3.236340533672173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '623939', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2434, 'n_words': 742}