Documentation ¶
Index ¶
- func AddDelimiter(stmt string) string
- func AppendToFile(filePath, contents string) (bytesWritten int, created bool, err error)
- func CanParse(input string) (bool, error)
- func FileNameForObject(objectName string) string
- func HostDefaultDirName(hostname string, port int) string
- func MakeTestDirectory(t *testing.T, path string)
- func NeedSpecialDelimiter(key tengo.ObjectKey, stmt string) bool
- func ParentOptionFiles(dirPath string, baseConfig *mybase.Config) ([]*mybase.File, string, error)
- func PathForObject(dirPath, objectName string) string
- func ReadTestFile(t *testing.T, filename string) string
- func RemoveTestDirectory(t *testing.T, path string)
- func RemoveTestFile(t *testing.T, filename string)
- func WriteTestFile(t *testing.T, filename, contents string)
- type Dir
- func (dir *Dir) BaseName() string
- func (dir *Dir) CreateOptionFile(optionFile *mybase.File) (err error)
- func (dir *Dir) CreateSubdir(name string, optionFile *mybase.File) (*Dir, error)
- func (dir *Dir) Delete() error
- func (dir *Dir) FirstInstance() (*tengo.Instance, error)
- func (dir *Dir) Generator() (major, minor, patch int, edition string)
- func (dir *Dir) HasFile(name string) (bool, error)
- func (dir *Dir) HasSchema() bool
- func (dir *Dir) Hostnames() ([]string, error)
- func (dir *Dir) InstanceDefaultParams() (string, error)
- func (dir *Dir) Instances() ([]*tengo.Instance, error)
- func (dir *Dir) NamedSchemaStatements() []*Statement
- func (dir *Dir) RelPath() string
- func (dir *Dir) SchemaNames(instance *tengo.Instance) (names []string, err error)
- func (dir *Dir) String() string
- func (dir *Dir) Subdirs() ([]*Dir, error)
- func (dir *Dir) ValidateInstance(instance *tengo.Instance) error
- type DuplicateDefinitionError
- type LogicalSchema
- type SQLFile
- func (sf SQLFile) Create(contents string) error
- func (sf SQLFile) Delete() error
- func (sf SQLFile) Exists() (bool, error)
- func (sf SQLFile) Path() string
- func (sf SQLFile) String() string
- func (sf SQLFile) Tokenize() (*TokenizedSQLFile, error)
- func (sf SQLFile) WriteStatements(statements []*Statement) (int, error)
- type Statement
- type StatementType
- type TokenizedSQLFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddDelimiter ¶
AddDelimiter takes the supplied string and appends a delimiter to the end. If the supplied string is a multi-statement routine, delimiter commands will be prepended and appended to the string appropriately. TODO devise a way to avoid using special delimiter for single-routine files
func AppendToFile ¶
AppendToFile appends the supplied string to the file at the given path. If the file already exists and is not newline-terminated, a newline will be added before contents are appended. If the file does not exist, it will be created.
func CanParse ¶
CanParse returns true if the supplied string can be parsed as a type of SQL statement understood by this package. The supplied string should NOT have a delimiter. Note that this method returns false for strings that are entirely whitespace and/or comments.
func FileNameForObject ¶ added in v1.5.1
FileNameForObject returns a string containing the filename to use for the SQLFile representing the supplied object name. Special characters in the objectName will be removed; however, there is no risk of "conflicts" since a single SQLFile can store definitions for multiple objects.
func HostDefaultDirName ¶
HostDefaultDirName returns a default relative directory name to use for the supplied instance host and port. Intended for use in situations where a user can optionally supply an arbitrary name, but they have not done so.
func MakeTestDirectory ¶
MakeTestDirectory wraps os.MkdirAll. If an error occurs, it is fatal to the test.
func NeedSpecialDelimiter ¶ added in v1.5.1
NeedSpecialDelimiter returns true if the statement requires used of a nonstandard delimiter.
func ParentOptionFiles ¶
ParentOptionFiles returns a slice of *mybase.File, corresponding to the option files in the specified path's parent dir hierarchy. Evaluation of parent dirs stops once we hit either a directory containing .git, the user's home directory, or the root of the filesystem. The result is ordered such that the closest-to-root dir's File is returned first and this dir's direct parent File last. The return value excludes dirPath's file, as well as the home directory's, as these are presumed to be parsed elsewhere. The files will be read and parsed, using baseConfig to know which options are defined and valid. An absolute path to the "repo base" is also returned as a string. This will typically be either a dir containing a .git subdir, or the rootmost dir containing a .skeema file; failing that, it will be the supplied dirPath.
func PathForObject ¶
PathForObject returns a string containing a path to use for the SQLFile representing the supplied object name. Special characters in the objectName will be removed; however, there is no risk of "conflicts" since a single SQLFile can store definitions for multiple objects.
func ReadTestFile ¶
ReadTestFile wraps ioutil.ReadFile. If an error occurs, it is fatal to the test.
func RemoveTestDirectory ¶
RemoveTestDirectory wraps os.RemoveAll. If an error occurs, it is fatal to the test.
func RemoveTestFile ¶
RemoveTestFile deletes a file (or directory). If an error occurs, it is fatal to the test.
func WriteTestFile ¶
WriteTestFile wraps ioutil.WriteFile. If an error occurs, it is fatal to the test.
Types ¶
type Dir ¶
type Dir struct { Path string Config *mybase.Config OptionFile *mybase.File SQLFiles []SQLFile LogicalSchemas []*LogicalSchema // for now, always 0 or 1 elements; 2+ in same dir to be supported in future ParseError error // any fatal error found parsing dir's config or contents IgnoredStatements []*Statement // statements with unknown type / not supported by this package // contains filtered or unexported fields }
Dir is a parsed representation of a directory that may have contained a .skeema config file and/or *.sql files.
func ParseDir ¶
ParseDir parses the specified directory, including all *.sql files in it, its .skeema config file, and all .skeema config files of its parent directory hierarchy. Evaluation of parent dirs stops once we hit either a directory containing .git, the user's home directory, or the root of the filesystem. Config sources are ordered such that the closest-to-root-dir's .skeema file is added first (and the current working dir's last), meaning that options "cascade" down the fs hierarchy and can be overridden by child directories.
func (*Dir) CreateOptionFile ¶
CreateOptionFile adds the supplied option file to dir. It is an error if dir already has an option file.
func (*Dir) CreateSubdir ¶
CreateSubdir creates a subdirectory with the supplied name and optional config file. If the directory already exists, it is an error if it already contains any *.sql files or a .skeema file.
func (*Dir) FirstInstance ¶
FirstInstance returns at most one tengo.Instance based on the directory's configuration. If the config maps to multiple instances, only the first will be returned. If the config maps to no instances, nil will be returned. The instance WILL be checked for connectivity. If multiple instances are returned and some have connectivity issues, the first reachable instance will be returned.
func (*Dir) Generator ¶
Generator returns the version and edition of Skeema used to init or most most recently pull this dir's contents. If this cannot be determined, all results will be zero values.
func (*Dir) HasSchema ¶
HasSchema returns true if this dir maps to at least one schema, either by stating a "schema" option in this dir's option file for the current environment, and/or by having *.sql files that explicitly mention a schema name.
func (*Dir) Hostnames ¶
Hostnames returns 0 or more hosts that the directory maps to. This properly handles the host option being set to a comma-separated list of multiple hosts, or the host-wrapper option being used to shell out to an external script to obtain hosts.
func (*Dir) InstanceDefaultParams ¶
InstanceDefaultParams returns a param string for use in constructing a DSN. Any overrides specified in the config for this dir will be taken into account. The returned string will already be in the correct format (HTTP query string). An error will be returned if the configuration tried manipulating params that should not be user-specified. Note that these vars are used as the *default* params for an Instance, but individual callsites can still override things as needed. For example, Tengo will automatically manipulate a few params whenever querying information_schema or running SHOW CREATE.
func (*Dir) Instances ¶
Instances returns 0 or more tengo.Instance pointers, based on the directory's configuration. The Instances will NOT be checked for connectivity. However, if the configuration is invalid (for example, illegal hostname or invalid connect-options), an error will be returned instead of any instances.
func (*Dir) NamedSchemaStatements ¶
NamedSchemaStatements returns a slice of Statements in the dir that are a USE command, or are a CREATE that specify a schema name. Such statements are not yet fully supported by most Skeema packages.
func (*Dir) RelPath ¶
RelPath attempts to return the directory path relative to the dir's repoBase. If this cannot be determined, the BaseName is returned. This method is intended for situations when the dir's location within its repo is more relevant than the dir's absolute path.
func (*Dir) SchemaNames ¶
SchemaNames interprets the value of the dir's "schema" option, returning one or more schema names that the statements in dir's *.sql files will be applied to, in cases where no schema name is explicitly specified in SQL statements. If the ignore-schema option is set, it will filter out matching results from the returned slice. An instance must be supplied since the value may be instance-specific.
func (*Dir) Subdirs ¶
Subdirs reads the list of direct, non-hidden subdirectories of dir, parses them (*.sql and .skeema files), and returns them. An error will be returned if there are problems reading dir's the directory list. Otherwise, err is nil, but some of the returned Dir values will have a non-nil ParseError if any problems were encountered in that subdir.
func (*Dir) ValidateInstance ¶
ValidateInstance confirms the supplied instance is (or has been) reachable, and applies any dir-configured Flavor override if the instance's flavor cannot be auto-detected. An error will be returned if the instance is not reachable. Otherwise, the return value will be nil, but any flavor mismatches/problems will be logged.
type DuplicateDefinitionError ¶
type DuplicateDefinitionError struct { ObjectKey tengo.ObjectKey FirstFile string FirstLine int DupeFile string DupeLine int }
DuplicateDefinitionError is an error returned when Dir.parseContents() encounters multiple CREATE statements for the same exact object.
func (DuplicateDefinitionError) Error ¶
func (dde DuplicateDefinitionError) Error() string
Error satisfies the builtin error interface.
type LogicalSchema ¶
type LogicalSchema struct { Name string CharSet string Collation string Creates map[tengo.ObjectKey]*Statement Alters []*Statement // Alterations that are run after the Creates }
LogicalSchema represents a set of statements from *.sql files in a directory that all operated on the same schema. Note that Name is often blank, which means "all SQL statements in this dir that don't have an explicit USE statement before them". This "nameless" LogicalSchema is mapped to schema names based on the "schema" option in the dir's OptionFile.
func (*LogicalSchema) AddStatement ¶
func (logicalSchema *LogicalSchema) AddStatement(stmt *Statement) error
AddStatement adds the supplied statement into the appropriate data structure within the receiver. This is useful when assembling a new logical schema. An error will be returned if a duplicate CREATE object name/type pair is added.
type SQLFile ¶
SQLFile represents a file containing zero or more SQL statements.
func (SQLFile) Tokenize ¶
func (sf SQLFile) Tokenize() (*TokenizedSQLFile, error)
Tokenize reads the file and splits it into statements, returning a TokenizedSQLFile that wraps sf with the statements added. Statements preserve their whitespace and semicolons; the return value exactly represents the entire file. Some of the returned "statements" may just be comments and/or whitespace, since any comments and/or whitespace between SQL statements gets split into separate Statement values.
type Statement ¶
type Statement struct { File string LineNo int CharNo int Text string // includes trailing Delimiter and newline DefaultDatabase string // only populated if an explicit USE command was encountered Type StatementType ObjectType tengo.ObjectType ObjectName string ObjectQualifier string FromFile *TokenizedSQLFile // contains filtered or unexported fields }
Statement represents a logical instruction in a file, consisting of either an SQL statement, a command (e.g. "USE some_database"), or whitespace and/or comments between two separate statements or commands.
func (*Statement) Body ¶
Body returns the Statement's Text, without any trailing delimiter, whitespace, or qualified schema name.
func (*Statement) Location ¶
Location returns the file, line number, and character number where the statement was obtained from
func (*Statement) ObjectKey ¶
ObjectKey returns a tengo.ObjectKey for the object affected by this statement.
func (*Statement) Remove ¶
func (stmt *Statement) Remove()
Remove removes the statement from the list of statements in stmt.FromFile. It does not rewrite the file though.
func (*Statement) SplitTextBody ¶
SplitTextBody returns Text with its trailing delimiter and whitespace (if any) separated out into a separate string.
type StatementType ¶
type StatementType int
StatementType indicates the type of a SQL statement found in a SQLFile. Parsing of types is very rudimentary, which can be advantageous for linting purposes. Otherwise, SQL errors or typos would prevent type detection.
const ( StatementTypeUnknown StatementType = iota StatementTypeNoop // entirely whitespace and/or comments StatementTypeCommand // currently just USE or DELIMITER StatementTypeCreate StatementTypeAlter // not actually ever parsed yet )
Constants enumerating different types of statements
type TokenizedSQLFile ¶
TokenizedSQLFile represents a SQLFile that has been tokenized into statements successfully.
func NewTokenizedSQLFile ¶
func NewTokenizedSQLFile(sf SQLFile, statements []*Statement) *TokenizedSQLFile
NewTokenizedSQLFile creates a TokenizedSQLFile whose statements have a FromFile pointer linking back to the TokenizedSQLFile. This permits easy mutation of the statements and rewriting of the file.
func (*TokenizedSQLFile) Rewrite ¶
func (tsf *TokenizedSQLFile) Rewrite() (int, error)
Rewrite rewrites the SQLFile with the current statements, returning the number of bytes written. If the file's statements now only consist of comments, whitespace, and commands (e.g. USE, DELIMITER) then the file will be deleted instead, and a length of 0 will be returned.