public static function toSql(string $format, $data = null)
    {
        if ($data === null) {
            return null;
        }

        switch ($format) {
            case self::NONE:
                return $data;
            case self::TIME:
            case self::TIMEU:
            case self::TIMETZ:
            case self::TIMEUTZ:
            case self::DATE:
            case self::DATETIME:
            case self::DATETIMEU:
            case self::DATETIMETZ:
            case self::DATETIMEUTZ:
            case self::TIMESTAMP:
                if (empty($data)) {
                    $result = null;
                } elseif (!is_object($data)) {
                    $datetime = new DateTime($data);
                    $result = $datetime->format($format);
                } elseif ($data instanceof DateTime) {
                    $result = $data->format($format);
                } else {
                    throw new FormatException('Unknown datetime object: ' . get_class($data));
                }

                return $result;
            case self::INT:
                return (integer)$data;
            case self::BOOL:
                return (integer)$data;
            case self::FLOAT:
                if (strpos($data, ',') > strpos($data, '.')) {
                    $data = str_replace(array('.', ','), array('', '.'), $data);
                } elseif(strpos($data, '.') > strpos($data, ',')) {
                    $data = str_replace(',', '', $data);
                }

                return floatval($data);
            case self::STRING:
                return (string)$data;
            case self::ALPHANUMERIC:
                return preg_replace('/[^a-zA-Z0-9_ ]/', '', $data);
            case self::SERIALIZED:
                return serialize($data);
            case self::JSON:
                return json_encode($data);
            case self::CSV:
                return implode(',', $data);
            default:
                throw new FormatException ('Unknown format: ' . $format);
        }
    }