/** * Expression constructor. * @param PixelDb $db * @param string $column Column to order. * @param string $direction The direction of the order. * @param bool $first True if its the first order by expression. */ public function __construct(PixelDb $db, $column, $direction = self::ORDER_DIRECTION_ASCENDING, $first = true) { parent::__construct($db); if ($first) { $db->getSqlBuffer()->append(' ORDER BY '); } else { $db->getSqlBuffer()->append(', '); } $db->getSqlBuffer()->append($column . ' ' . $direction); }
/** * SelectExpression constructor. * @param PixelDb $db * @param string[] $fields List of fields to select. * @param string $prefix Prefix for the fields. Useful for full field name. * @param bool $first True to include SELECT FROM. */ public function __construct(PixelDb $db, $fields, $prefix = '', $first = true) { parent::__construct($db); if ($first) { $db->getSqlBuffer()->append('SELECT '); } else { $db->getSqlBuffer()->append(', '); } if ($prefix != '') { $prefixedFields = []; foreach ($fields as $field) { array_push($prefixedFields, $prefix . $field); } $db->getSqlBuffer()->append(join(', ', $prefixedFields)); } else { $db->getSqlBuffer()->append(join(', ', $fields)); } }
/** * Expression constructor. * @param PixelDb $db * @param string[] $mappings Mapping of the fields and its values. e.g. ['user_id'=>$userId] * @param string $function Function for the mappings. * @param string $mappingPrefix Prefix for the mapping to prevent conflicts. * @param bool $first */ public function __construct(PixelDb $db, $mappings, $function = null, $mappingPrefix = 'p_', $first = true) { parent::__construct($db); if ($first) { $db->getSqlBuffer()->append(' SET '); } else { $db->getSqlBuffer()->append(', '); } $parameterString = ''; if ($function != null) { foreach ($mappings as $map => $value) { $parameterString .= $map . ' = ' . $function . '(:' . $mappingPrefix . $map . '), '; } } else { foreach ($mappings as $map => $value) { $parameterString .= $map . ' = :' . $mappingPrefix . $map . ', '; } } $parameterString = rtrim($parameterString, ', '); $db->getSqlBuffer()->append($parameterString); $db->addParameter($mappings, $mappingPrefix); }
/** * Expression constructor. * @param PixelDb $db * @param string $column The column to compare. * @param string $value The value expected. * @param string $function Function that should be applied to the value. * @param string $operator Operator for the comparison, use WhereExpression::OPERATOR_ * @param string $prefix Prefix for parameter. * @param string $join The join used to combine multiple where expression. */ public function __construct(PixelDb $db, $column, $value, $function = null, $operator = WhereExpression::OPERATOR_EQ, $prefix = 'p_', $join = ' WHERE ') { parent::__construct($db); $db->getSqlBuffer()->append($join); if ($function == null) { switch ($operator) { case WhereExpression::OPERATOR_LIKE_STARTS: $db->getSqlBuffer()->append($column . ' LIKE %:' . $prefix . $column); break; case WhereExpression::OPERATOR_LIKE_ENDS: $db->getSqlBuffer()->append($column . ' LIKE :' . $prefix . $column . '%'); break; case WhereExpression::OPERATOR_LIKE: $db->getSqlBuffer()->append($column . ' LIKE %:' . $prefix . $column . '%'); break; default: $db->getSqlBuffer()->append($column . ' ' . $operator . ' :' . $prefix . $column); break; } } else { switch ($operator) { case WhereExpression::OPERATOR_LIKE_STARTS: $db->getSqlBuffer()->append($column . ' LIKE %' . $function . '(:' . $prefix . $column . ')'); break; case WhereExpression::OPERATOR_LIKE_ENDS: $db->getSqlBuffer()->append($column . ' LIKE ' . $function . '(:' . $prefix . $column . ')%'); break; case WhereExpression::OPERATOR_LIKE: $db->getSqlBuffer()->append($column . ' LIKE %' . $function . '(:' . $prefix . $column . ')%'); break; default: $db->getSqlBuffer()->append($column . ' ' . $operator . ' ' . $function . '(:' . $prefix . $column . ')'); break; } } $db->addParameter([$column => $value], $prefix); }
/** * FromExpression constructor. * @param \Myriatek\PixelDb\PixelDb $db * @param string[] $tables Name of the table. * @param bool $first True if the first from statement. */ public function __construct(PixelDb $db, $tables, $first = true) { parent::__construct($db); $db->getSqlBuffer()->append(' FROM ' . join(', ', $tables)); }
/** * Expression constructor. * @param PixelDb $db * @param string $tables Table to update. */ public function __construct(PixelDb $db, $tables) { parent::__construct($db); $db->getSqlBuffer()->append('UPDATE ' . $tables); }
/** * Expression constructor. * @param PixelDb $db * @param int $start Limit start. * @param int $end Limit length. */ public function __construct(PixelDb $db, $start, $end = 1) { parent::__construct($db); $db->getSqlBuffer()->append(' LIMIT ' . $start . ', ' . $end); }