• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aimeos / aimeos-base / c3a63337-5581-4e39-8610-b18f323a43f2

09 Aug 2024 10:20AM UTC coverage: 88.621% (-0.04%) from 88.661%
c3a63337-5581-4e39-8610-b18f323a43f2

push

circleci

aimeos
Fixed NULL terms for int and float if value is empty string

2 of 3 new or added lines in 1 file covered. (66.67%)

1690 of 1907 relevant lines covered (88.62%)

6.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.62
/src/Criteria/Expression/Compare/SQL.php
1
<?php
2

3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2024
7
 * @package Base
8
 * @subpackage Common
9
 */
10

11

12
namespace Aimeos\Base\Criteria\Expression\Compare;
13

14

15
/**
16
 * SQL implementation for comparing objects.
17
 *
18
 * @package Base
19
 * @subpackage Common
20
 */
21
class SQL extends Base
22
{
23
        private static $operators = ['=~' => 'LIKE', '~=' => 'LIKE', '==' => '=', '!=' => '<>', '>' => '>', '>=' => '>=', '<' => '<', '<=' => '<=', '-' => '-'];
24
        private \Aimeos\Base\DB\Connection\Iface $conn;
25

26

27
        /**
28
         * Initializes the object.
29
         *
30
         * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection object
31
         * @param string $operator Operator used for the expression
32
         * @param string $name Name of variable or column that should be compared.
33
         * @param mixed $value Value that the variable or column should be compared to
34
         */
35
        public function __construct( \Aimeos\Base\DB\Connection\Iface $conn, string $operator, string $name, $value )
36
        {
37
                if( !isset( self::$operators[$operator] ) ) {
25✔
38
                        throw new \Aimeos\Base\Exception( sprintf( 'Invalid operator "%1$s"', $operator ) );
1✔
39
                }
40

41
                parent::__construct( $operator, $name, $value );
24✔
42
                $this->conn = $conn;
24✔
43
        }
44

45

46
        /**
47
         * Returns the available operators for the expression.
48
         *
49
         * @return array List of available operators
50
         */
51
        public static function getOperators() : array
52
        {
53
                return array_keys( self::$operators );
8✔
54
        }
55

56

57
        /**
58
         * Creates a term string from the given parameters.
59
         *
60
         * @param string|array $name Translated name(s) of the variable or column
61
         * @param string $type Type constant
62
         * @param mixed $value Value that the variable or column should be compared to
63
         * @return string Created term string (name operator value)
64
         */
65
        protected function createTerm( $name, string $type, $value ) : string
66
        {
67
                $op = $this->getOperator();
6✔
68

69
                if( $op === '-' )
6✔
70
                {
71
                        $p = explode( ' - ', $value );
1✔
72

73
                        return $name . ' >= ' . $this->escape( '>=', $type, $p[0] )
1✔
74
                                . ' AND ' . $name . ' <= ' . $this->escape( '<=', $type, $p[1] );
1✔
75
                }
76

77
                if( ( $value = $this->escape( $op, $type, $value ) ) === 'null' ) {
6✔
NEW
78
                        return $this->createNullTerm( $name, $type );
×
79
                }
80

81
                $term = $name . ' ' . self::$operators[$op] . ' ' . $value;
6✔
82

83
                if( in_array( $op, array( '=~', '~=' ), true ) ) {
6✔
84
                        $term .= ' ESCAPE \'#\'';
5✔
85
                }
86

87
                return $term;
6✔
88
        }
89

90

91
        /**
92
         * Creates a term which contains a null value.
93
         *
94
         * @param string|array $name Translated name(s) of the variable or column
95
         * @param string $type Code of the internal value type
96
         * @return string String that can be inserted into a SQL statement
97
         */
98
        protected function createNullTerm( $name, string $type ) : string
99
        {
100
                switch( $this->getOperator() )
4✔
101
                {
102
                        case '==': return $name . ' IS NULL';
4✔
103
                        case '!=': return $name . ' IS NOT NULL';
4✔
104
                }
105

106
                throw new \Aimeos\Base\Exception( sprintf( 'NULL value not allowed for operator "%1$s"', $this->getOperator() ) );
×
107
        }
108

109

110
        /**
111
         * Creates a term from a list of values.
112
         *
113
         * @param string|array $name Translated name(s) of the variable or column
114
         * @param string $type Type constant
115
         * @return string String that can be inserted into a SQL statement
116
         */
117
        protected function createListTerm( $name, string $type ) : string
118
        {
119
                switch( $this->getOperator() )
4✔
120
                {
121
                        case '==':
4✔
122
                                return $name . ' IN ' . $this->createValueList( $type, (array) $this->getValue() );
4✔
123
                        case '!=':
3✔
124
                                return $name . ' NOT IN ' . $this->createValueList( $type, (array) $this->getValue() );
2✔
125
                        default:
126
                                $terms = [];
1✔
127

128
                                foreach( (array) $this->getValue() as $val ) {
1✔
129
                                        $terms[] = $this->createTerm( $name, $type, $val );
1✔
130
                                }
131

132
                                return '(' . implode( ' OR ', $terms ) . ')';
1✔
133
                }
134
        }
135

136

137
        /**
138
         * Creates a list of search values.
139
         *
140
         * @param string $type Type constant
141
         * @param string[] $values Value list for the variable or column name
142
         * @return string String of comma separated values in parenthesis
143
         */
144
        protected function createValueList( string $type, array $values ) : string
145
        {
146
                if( empty( $values ) ) {
4✔
147
                        return '(NULL)';
×
148
                }
149

150
                $operator = $this->getOperator();
4✔
151

152
                foreach( $values as $key => $value ) {
4✔
153
                        $values[$key] = $this->escape( $operator, $type, $value );
4✔
154
                }
155

156
                return '(' . implode( ',', $values ) . ')';
4✔
157
        }
158

159

160
        /**
161
         * Returns the connection object.
162
         *
163
         * return \Aimeos\Base\DB\Connection\Iface Connection object
164
         */
165
        public function getConnection() : \Aimeos\Base\DB\Connection\Iface
166
        {
167
                return $this->conn;
5✔
168
        }
169

170

171
        /**
172
         * Returns the internal type of the function parameter.
173
         *
174
         * @param mixed &$item Reference to parameter value (will be updated if necessary)
175
         * @return string Internal parameter type
176
         * @throws \Aimeos\Base\Exception If an error occurs
177
         */
178
        protected function getParamType( &$item ) : string
179
        {
180
                if( is_null( $item ) ) {
1✔
181
                        return \Aimeos\Base\DB\Statement\Base::PARAM_NULL;
1✔
182
                } elseif( is_float( $item ) ) {
1✔
183
                        return \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT;
1✔
184
                } elseif( is_int( $item ) ) {
1✔
185
                        return \Aimeos\Base\DB\Statement\Base::PARAM_INT;
1✔
186
                }
187

188
                return \Aimeos\Base\DB\Statement\Base::PARAM_STR;
1✔
189
        }
190

191

192
        /**
193
         * Translates a value to another one by a plugin if available.
194
         *
195
         * @param string $name Name of variable or column that should be translated
196
         * @param mixed $value Original value
197
         * @param mixed $type Value type
198
         * @return mixed Translated value
199
         */
200
        protected function translateValue( string $name, $value, $type )
201
        {
202
                if( $plugin = $this->getPlugin( $name ) ) {
6✔
203
                        return $plugin->translate( $value, $type );
1✔
204
                }
205

206
                $types = [\Aimeos\Base\DB\Statement\Base::PARAM_INT, \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT];
6✔
207
                return in_array( $type, $types ) && $value === '' ? null : $value;
6✔
208
        }
209
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc