Property Expression Parsing
The following discussion of the experiments with alternate property expression parsing is very much a work in progress, and subject to sudden changes.

The parsing of property value expressions is handled by two closely related classes: PropertyTokenizer and its subclass, PropertyParser. PropertyTokenizer, as the name suggests, handles the tokenizing of the expression, handing tokens back to its subclass, PropertyParser. PropertyParser, in turn, returns a PropertyValueList, a list of PropertyValues.

The tokenizer and parser rely in turn on the datatype definition from the org.apache.fop.datatypes package and the datatype static final int constants from PropertyConsts.

The data types currently defined in org.apache.fop.datatypes include:

Numbers and lengths
Numeric The fundamental numeric data type. Numerics of various types are constructed by the classes listed below.
Constructor classes for Numeric
Angle In degrees(deg), gradients(grad) or radians(rad)
Ems Relative length in ems
Frequency In hertz(Hz) or kilohertz(kHz)
IntegerType
Length In centimetres(cm), millimetres(mm), inches(in), points(pt), picas(pc) or pixels(px)
Percentage
Time In seconds(s) or milliseconds(ms)
Strings
StringType Base class for data types which result in a String.
Literal A subclass of StringType for literals which exceed the constraints of an NCName.
MimeType A subclass of StringType for literals which represent a mime type.
UriType A subclass of StringType for literals which represent a URI, as specified by the argument to url().
NCName A subclass of StringType for literals which meet the constraints of an NCName.
Country An RFC 3066/ISO 3166 country code.
Language An RFC 3066/ISO 639 language code.
Script An ISO 15924 script code.
Enumerated types
EnumType An integer representing one of the tokens in a set of enumeration values.
MappedEnumType A subclass of EnumType. Maintains a String with the value to which the associated "raw" enumeration token maps. E.g., the font-size enumeration value "medium" maps to the String "12pt".
Colors
ColorType Maintains a four-element array of float, derived from the name of a standard colour, the name returned by a call to system-color(), or an RGB specification.
Fonts
FontFamilySet Maintains an array of Strings containing a prioritized list of possibly generic font family names.
Pseudo-types
A variety of pseudo-types have been defined as convenience types for frequently appearing enumeration token values, or for other special purposes.
Inherit For values of inherit.
Auto For values of auto.
None For values of none.
Bool For values of true/false.
FromNearestSpecified Created to ensure that, when associated with a shorthand, the from-nearest-specified-value() core function is the sole component of the expression.
FromParent Created to ensure that, when associated with a shorthand, the from-parent() core function is the sole component of the expression.

The tokenizer returns one of the following token values:

static final int EOF = 0 ,NCNAME = 1 ,MULTIPLY = 2 ,LPAR = 3 ,RPAR = 4 ,LITERAL = 5 ,FUNCTION_LPAR = 6 ,PLUS = 7 ,MINUS = 8 ,MOD = 9 ,DIV = 10 ,COMMA = 11 ,PERCENT = 12 ,COLORSPEC = 13 ,FLOAT = 14 ,INTEGER = 15 ,ABSOLUTE_LENGTH = 16 ,RELATIVE_LENGTH = 17 ,TIME = 18 ,FREQ = 19 ,ANGLE = 20 ,INHERIT = 21 ,AUTO = 22 ,NONE = 23 ,BOOL = 24 ,URI = 25 ,MIMETYPE = 26 // NO_UNIT is a transient token for internal use only. It is // never set as the end result of parsing a token. ,NO_UNIT = 27 ;

Most of these tokens are self-explanatory, but a few need further comment.

AUTO
Because of its frequency of occurrence, and the fact that it is always the initial value for any property which supports it, AUTO has been promoted into a pseudo-type with its on datatype class. Therefore, it is also reported as a token.
NONE
Similarly to AUTO, NONE has been promoted to a pseudo-type because of its frequency.
BOOL
There is a de facto boolean type buried in the enumeration types for many of the properties. It had been specified as a type in its own right in this code.
MIMETYPE
The property content-type introduces this complication. It can have two values of the form content-type:mime-type (e.g. content-type="content-type:xml/svg") or namespace-prefix:prefix (e.g. content-type="namespace-prefix:svg"). The experimental code reduces these options to the payload in each case: an NCName in the case of a namespace prefix, and a MIMETYPE in the case of a content-type specification. NCNames cannot contain a "/".

The parser retuns a PropertyValueList, necessary because of the possibility that a list of PropertyValue elements may be returned from the expressions of soem properties.

PropertyValueLists may contain PropertyValues or other PropertyValueLists. This latter provision is necessitated for the peculiar case of of text-shadow, which may contain whitespace separated sublists of either two or three elements, separated from one another by commas. To accommodate this peculiarity, comma separated elements are added to the top-level list, while whitespace separated values are always collected into sublists to be added to the top-level list.

Other special cases include the processing of the core functions from-parent() and from-nearest-specified-value() when these function calls are assigned to a shorthand property, or used with a shorthand property name as an argument. In these cases, the function call must be the sole component of the expression. The pseudo-element classes FromParent and FromNearestSpecified are generated in these circumstances so that an exception will be thrown if they are involved in expression evaluation with other components. (See Rec. Section 5.10.4 Property Value Functions.)

The experimental code is a simple extension of the existing parser code, which itself borrowed heavily from James Clark's XT processor.

e_full_warning'>artonge/fix/storage_full_warning Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
blob: 2466493c1fa54cb0e752fe79f8e75c85ebda4bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
 * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\DB\QueryBuilder\FunctionBuilder;

use OC\DB\Connection;
use OC\DB\QueryBuilder\QueryFunction;
use OC\DB\QueryBuilder\QuoteHelper;
use OCP\DB\QueryBuilder\IFunctionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection;

class FunctionBuilder implements IFunctionBuilder {
	/** @var IDBConnection|Connection */
	protected $connection;

	/** @var IQueryBuilder */
	protected $queryBuilder;

	/** @var QuoteHelper */
	protected $helper;

	public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder, QuoteHelper $helper) {
		$this->connection = $connection;
		$this->queryBuilder = $queryBuilder;
		$this->helper = $helper;
	}

	public function md5($input): IQueryFunction {
		return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')');
	}

	public function concat($x, ...$expr): IQueryFunction {
		$args = func_get_args();
		$list = [];
		foreach ($args as $item) {
			$list[] = $this->helper->quoteColumnName($item);
		}
		return new QueryFunction(sprintf('CONCAT(%s)', implode(', ', $list)));
	}

	public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
		$separator = $this->connection->quote($separator);
		return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ' SEPARATOR ' . $separator . ')');
	}

	public function substring($input, $start, $length = null): IQueryFunction {
		if ($length) {
			return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')');
		} else {
			return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')');
		}
	}

	public function sum($field): IQueryFunction {
		return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')');
	}

	public function lower($field): IQueryFunction {
		return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')');
	}

	public function add($x, $y): IQueryFunction {
		return new QueryFunction($this->helper->quoteColumnName($x) . ' + ' . $this->helper->quoteColumnName($y));
	}

	public function subtract($x, $y): IQueryFunction {
		return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y));
	}

	public function count($count = '', $alias = ''): IQueryFunction {
		$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
		$quotedName = $count === '' ? '*' : $this->helper->quoteColumnName($count);
		return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
	}

	public function octetLength($field, $alias = ''): IQueryFunction {
		$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
		$quotedName = $this->helper->quoteColumnName($field);
		return new QueryFunction('OCTET_LENGTH(' . $quotedName . ')' . $alias);
	}

	public function charLength($field, $alias = ''): IQueryFunction {
		$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
		$quotedName = $this->helper->quoteColumnName($field);
		return new QueryFunction('CHAR_LENGTH(' . $quotedName . ')' . $alias);
	}

	public function max($field): IQueryFunction {
		return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
	}

	public function min($field): IQueryFunction {
		return new QueryFunction('MIN(' . $this->helper->quoteColumnName($field) . ')');
	}

	public function greatest($x, $y): IQueryFunction {
		return new QueryFunction('GREATEST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
	}

	public function least($x, $y): IQueryFunction {
		return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
	}
}