Class ParsedTemplateString
java.lang.Object
io.nosqlbench.virtdata.core.templates.ParsedTemplateString
A ParsedTemplate represents any string provided by a user which is meant to be a prototype for value used in an operation. A ParsedTemplate can represent string values with various levels of interpolation:
- A single literal String value, such as
This is the simplest case, as there are no no bindings nor captures.select * from tablefoo; - A value with named bindings interpolated into the string like
In this case, a single named binding point is specified. The userid value will be provided when the string needs to be fully composed before use.select * from tablefoo where userid={userid}; - A value with named captures like
In this case, a single named capture is specified -- The field name username will be read from some native result form and saved into memory for later use.select [username] from tablefoo where userid=32;A single binding reference:
{username}
This form is the simplest way to specify that a referenced binding should be used in a field.
This form does not presume that you want the value to be read as a String. The type of
value returned by bindings is used as-is, thus if you need to use a binding reference
as a string, make sure your binding recipe returns a String directly. (All of the other forms
presume to call .toString() automatically on any binding values.)
Details
Grammars used by native drivers can be decorated with named injection and extraction points for data, known respectively asBindPoints and CapturePoints.
The syntax used for bind points and capture points is standard across all
driver adapters. As such, this class captures the definition of
decorative syntax and the rules for parsing them out.
The key responsibilities of ParsedTemplate are:
- recognize bind points within statement templates
- recognize capture points within statement templates
- render statement templates with bind and capture points elided using a native syntax for variables
- provide metadata to drivers about defined bind and capture points
- provide a text template for re-assembly with injected data
orError()
should always called before it is used.
Validity Checks
A parsed template is considered to be valid if and only if the raw template contained only named anchors which were defined in the provided bindings. Extra bindings are not presumed to make a template invalid, but this interpretation is left to the caller for extra checks if needed.Parsed Details
After parsing, the following details are available:Parsed Spans
This is an alternating list of the literal sections of the raw template interspersed with the anchor names. This list always starts and ends with a literal section, so will always contain an odd number of elements. (some span sections may be empty if necessary, but not null)Specific Bindings
These are the binding names and definitions which were used in a named anchor and also found in the provided bindings. If an anchor references a binding which is not provided, then it will not be in this map.Missing Bindings
This is a list of binding names which were found in the raw template but which were not found in the provided bindings by name.Extra Bindings
This is a list of binding names which were provided by the user, but which were not used in the raw template by name.-
Constructor Summary
ConstructorsConstructorDescriptionParsedTemplateString(String rawtemplate, Map<String, String> availableBindings) Parse the given raw template, check the bind points against the provided bindings, and provide detailed template checks for validity. -
Method Summary
Modifier and TypeMethodDescriptionReturns the parsed template as a single binding spec if and only if the pattern matches a single binding anchor with no prefix or suffix.Get the named anchors and their associated binding specifiers as found in the raw template.Returns a list of binding names which were referenced in eithergetPositionalStatement(Function<String, String> tokenFormatter) Return the statement that can be used as-is by any driver specific version.String[]getSpans()Return the parsed template in (literal, variable, ..., ..., literal) form.getStmt()getType()booleanhasError()static ParsedTemplateStringorError()toString()
-
Constructor Details
-
ParsedTemplateString
Parse the given raw template, check the bind points against the provided bindings, and provide detailed template checks for validity.- Parameters:
rawtemplate- A string template which contains optionally embedded named anchorsavailableBindings- The bindings which are provided by the user to fulfill the named anchors in this raw template
-
-
Method Details
-
of
-
getType
-
orError
-
toString
-
hasError
public boolean hasError()- Returns:
- true if the parsed statement is not usable.
-
getMissing
Returns a list of binding names which were referenced in either{anchor}or?anchor
form, but which were not present in the provided bindings map. If any binding names are present in the returned set, then this binding will not be usable.- Returns:
- A list of binding names which were referenced but not defined*
-
getAnchors
-
getBindPoints
Get the named anchors and their associated binding specifiers as found in the raw template.- Returns:
- A list of bind points
- Throws:
InvalidParameterException- if the template has an error, such as an anchor which has no provided binding.
-
getPositionalStatement
Return the statement that can be used as-is by any driver specific version. This uses the anchor token as provided to yield a version of the statement which contains positional anchors, but no named bindings.- Parameters:
tokenFormatter- The mapping from a token name to a place holder- Returns:
- A driver or usage-specific format of the statement, with anchors
-
getPositionalStatement
-
getSpans
Return the parsed template in (literal, variable, ..., ..., literal) form.- Returns:
- the sections of spans within the template
-
asBinding
-
getCaptures
-
getStmt
-