/** * Converts value to DateTime object. * @param string key * @param string format * @return DateTime */ public function asDateTime($key, $format = NULL) { $time = $this[$key]; if ((int) $time === 0) { // '', NULL, FALSE, '0000-00-00', ... return NULL; } $dt = new DibiDateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time); return $format === NULL ? $dt : $dt->format($format); }
/** * Converts value to DateTime object. * @param string key * @param string format * @return DateTime */ public function asDateTime($key, $format = NULL) { $time = $this[$key]; if (!$time instanceof DibiDateTime) { if ((int) $time === 0 && substr((string) $time, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ... return NULL; } $time = new DibiDateTime($time); } return $format === NULL ? $time : $time->format($format); }
/** * Converts values to specified type and format. * @param array * @return void */ private function normalize(array &$row) { foreach ($this->types as $key => $type) { if (!isset($row[$key])) { // NULL continue; } $value = $row[$key]; if ($value === FALSE || $type === dibi::TEXT) { } elseif ($type === dibi::INTEGER) { $row[$key] = is_float($tmp = $value * 1) ? $value : $tmp; } elseif ($type === dibi::FLOAT) { $row[$key] = str_replace(',', '.', ltrim((string) ($tmp = (double) $value), '0')) === ltrim(rtrim(rtrim($value, '0'), '.'), '0') ? $tmp : $value; } elseif ($type === dibi::BOOL) { $row[$key] = (bool) $value && $value !== 'f' && $value !== 'F'; } elseif ($type === dibi::DATE || $type === dibi::DATETIME) { if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ... $value = new DibiDateTime($value); $row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]); } } elseif ($type === dibi::BINARY) { $row[$key] = $this->getResultDriver()->unescape($value, $type); } } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: case dibi::BINARY: if ($this->driverName === 'odbc') { return "'" . str_replace("'", "''", $value) . "'"; } else { return $this->connection->quote($value, $type === dibi::TEXT ? PDO::PARAM_STR : PDO::PARAM_LOB); } case dibi::IDENTIFIER: switch ($this->driverName) { case 'mysql': return '`' . str_replace('`', '``', $value) . '`'; case 'oci': case 'pgsql': return '"' . str_replace('"', '""', $value) . '"'; case 'sqlite': case 'sqlite2': return '[' . strtr($value, '[]', ' ') . ']'; case 'odbc': case 'mssql': return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']'; case 'sqlsrv': return '[' . str_replace(']', ']]', $value) . ']'; default: return $value; } case dibi::BOOL: if ($this->driverName === 'pgsql') { return $value ? 'TRUE' : 'FALSE'; } else { return $value ? 1 : 0; } case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } if ($this->driverName === 'odbc') { return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#"); } else { return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'"); } default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: case dibi::BINARY: return "'" . str_replace("'", "''", $value) . "'"; case dibi::IDENTIFIER: return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']'; case dibi::BOOL: return $value ? 1 : 0; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#"); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: case dibi::BINARY: return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested // TODO: not tested case dibi::IDENTIFIER: // @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm return '"' . str_replace('"', '""', $value) . '"'; case dibi::BOOL: return $value ? 1 : 0; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: return "'" . $this->connection->escapeString($value) . "'"; case dibi::BINARY: return "X'" . bin2hex((string) $value) . "'"; case dibi::IDENTIFIER: return '[' . strtr($value, '[]', ' ') . ']'; case dibi::BOOL: return $value ? 1 : 0; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: if (!is_resource($this->connection)) { throw new DibiException('Lost connection to server.'); } return "'" . pg_escape_string($this->connection, $value) . "'"; case dibi::BINARY: if (!is_resource($this->connection)) { throw new DibiException('Lost connection to server.'); } return "'" . pg_escape_bytea($this->connection, $value) . "'"; case dibi::IDENTIFIER: // @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS return '"' . str_replace('"', '""', $value) . '"'; case dibi::BOOL: return $value ? 'TRUE' : 'FALSE'; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'"); default: throw new InvalidArgumentException('Unsupported type.'); } }
toArray(){return(array)$this;}function asDateTime($key,$format=NULL){$time=$this[$key];if((int)$time===0){return NULL;}$dt=new DibiDateTime(is_numeric($time)?date('Y-m-d H:i:s',$time):$time);return$format===NULL?$dt:$dt->format($format);}function
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: case dibi::BINARY: return "'" . str_replace("'", "''", $value) . "'"; case dibi::IDENTIFIER: // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']'; case dibi::BOOL: return $value ? 1 : 0; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'"); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: if (!is_resource($this->connection)) { throw new DibiException('Lost connection to server.'); } return "'" . mysql_real_escape_string($value, $this->connection) . "'"; case dibi::BINARY: if (!is_resource($this->connection)) { throw new DibiException('Lost connection to server.'); } return "_binary'" . mysql_real_escape_string($value, $this->connection) . "'"; case dibi::IDENTIFIER: // @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html return '`' . str_replace('`', '``', $value) . '`'; case dibi::BOOL: return $value ? 1 : 0; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'"); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: return $this->connection->quote($value, PDO::PARAM_STR); case dibi::BINARY: return $this->connection->quote($value, PDO::PARAM_LOB); case dibi::IDENTIFIER: switch ($this->driverName) { case 'mysql': return '`' . str_replace('`', '``', $value) . '`'; case 'pgsql': return '"' . str_replace('"', '""', $value) . '"'; case 'sqlite': case 'sqlite2': return '[' . strtr($value, '[]', ' ') . ']'; case 'odbc': case 'oci': // TODO: not tested // TODO: not tested case 'mssql': return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']'; case 'sqlsrv': return '[' . str_replace(']', ']]', $value) . ']'; default: return $value; } case dibi::BOOL: return $this->connection->quote($value, PDO::PARAM_BOOL); case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'"); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Converts values to specified type and format. * @param array * @return void */ private function normalize(array &$row) { foreach ($this->types as $key => $type) { if (!isset($row[$key])) { // NULL continue; } $value = $row[$key]; if ($value === FALSE || $type === dibi::TEXT) { } elseif ($type === dibi::INTEGER) { $row[$key] = is_float($tmp = $value * 1) ? $value : $tmp; } elseif ($type === dibi::FLOAT) { $row[$key] = (string) ($tmp = (double) $value) === rtrim(rtrim($value, '0'), '.') ? $tmp : $value; } elseif ($type === dibi::BOOL) { $row[$key] = (bool) $value && $value !== 'f' && $value !== 'F'; } elseif ($type === dibi::DATE || $type === dibi::DATETIME) { if ((int) $value === 0 && substr((string) $value, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ... } elseif (empty($this->formats[$type])) { // return DateTime object (default) $row[$key] = new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value); } elseif ($this->formats[$type] === 'U') { // return timestamp $row[$key] = is_numeric($value) ? (int) $value : strtotime($value); } elseif (is_numeric($value)) { // formatted date $row[$key] = date($this->formats[$type], $value); } else { $value = new DibiDateTime($value); $row[$key] = $value->format($this->formats[$type]); } } elseif ($type === dibi::BINARY) { $row[$key] = $this->getResultDriver()->unescape($value, $type); } } }
/** * Encodes data for use in a SQL statement. * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException */ public function escape($value, $type) { switch ($type) { case dibi::TEXT: return "'" . mysqli_real_escape_string($this->connection, $value) . "'"; case dibi::BINARY: return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'"; case dibi::IDENTIFIER: return '`' . str_replace('`', '``', $value) . '`'; case dibi::BOOL: return $value ? 1 : 0; case dibi::DATE: case dibi::DATETIME: if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { $value = new DibiDateTime($value); } return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'"); default: throw new InvalidArgumentException('Unsupported type.'); } }
/** * Converts value to specified type and format. * @param mixed value * @param int type * @return mixed */ protected function convert($value, $type) { if ($value === NULL || $value === FALSE) { return NULL; } switch ($type) { case dibi::TEXT: return (string) $value; case dibi::BINARY: return $this->getDriver()->unescape($value, $type); case dibi::INTEGER: return is_float($tmp = $value * 1) ? $value : $tmp; case dibi::FLOAT: return (double) $value; case dibi::DATE: case dibi::DATETIME: if ((int) $value === 0) { // '', NULL, FALSE, '0000-00-00', ... return NULL; } elseif ($this->dateFormat === '') { // return DateTime object (default) return new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value); } elseif ($this->dateFormat === 'U') { // return timestamp return is_numeric($value) ? (int) $value : strtotime($value); } elseif (is_numeric($value)) { // formatted date return date($this->dateFormat, $value); } else { $value = new DibiDateTime($value); return $value->format($this->dateFormat); } case dibi::BOOL: return (bool) $value && $value !== 'f' && $value !== 'F'; default: return $value; } }