text:
<?php

namespace DreamFactory\Core\SqlDb\Resources;

use DB;
use DreamFactory\Core\Database\Components\Expression;
use DreamFactory\Core\Database\Enums\DbFunctionUses;
use DreamFactory\Core\Database\Resources\BaseDbTableResource;
use DreamFactory\Core\Database\Schema\ColumnSchema;
use DreamFactory\Core\Database\Schema\RelationSchema;
use DreamFactory\Core\Database\Schema\TableSchema;
use DreamFactory\Core\Enums\ApiOptions;
use DreamFactory\Core\Enums\DbComparisonOperators;
use DreamFactory\Core\Enums\DbLogicalOperators;
use DreamFactory\Core\Exceptions\BadRequestException;
use DreamFactory\Core\Exceptions\BatchException;
use DreamFactory\Core\Exceptions\InternalServerErrorException;
use DreamFactory\Core\Exceptions\NotFoundException;
use DreamFactory\Core\Exceptions\RestException;
use DreamFactory\Core\Utility\Session;
use DreamFactory\Core\Enums\Verbs;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;

/**
 * Class Table
 *
 * @package DreamFactory\Core\SqlDb\Resources
 */
class Table extends BaseDbTableResource
{
    //*************************************************************************
    //	Members
    //*************************************************************************

    /**
     * @var null | bool
     */
    protected $transaction = null;

    //*************************************************************************
    //	Methods
    //*************************************************************************

    /**
     * {@inheritdoc}
     */
    public function updateRecordsByFilter($table, $record, $filter = null, $params = [], $extras = [])
    {
        $record = static::validateAsArray($record, null, false, 'There are no fields in the record.');

        $idFields = array_get($extras, ApiOptions::ID_FIELD);
        $idTypes = array_get($extras, ApiOptions::ID_TYPE);
        $related = array_get($extras, ApiOptions::RELATED);
        $allowRelatedDelete = array_get_bool($extras, ApiOptions::ALLOW_RELATED_DELETE);
        $ssFilters = array_get($extras, 'ss_filters');

        try {
            if (!$tableSchema = $this->parent->getTableSchema($table)) {
                throw new NotFoundException("Table '$table' does not exist in the database.");
            }

            $fieldsInfo = $tableSchema->getColumns(true);
            $idsInfo = $this->getIdsInfo($table, $fieldsInfo, $idFields, $idTypes);
            $relatedInfo = $tableSchema->getRelations(true);
            $parsed = $this->parseRecord($record, $fieldsInfo, $ssFilters, true);

            // build filter string if necessary, add server-side filters if necessary
            $builder = $this->parent->getConnection()->table($tableSchema->internalName);
            $this->convertFilterToNative($builder, $filter, $params, $ssFilters, $fieldsInfo);

            if (!empty($parsed)) {
                $builder->update($parsed);
            }

            $results = $this->runQuery($table, $builder, $extras);

            if (!empty($relatedInfo)) {
                // update related info
                foreach ($results as $row) {
                    $this->checkForIds($row, $idsInfo, $extras);
                    $this->updatePostRelations($table, array_merge($row, $record), $relatedInfo, $allowRelatedDelete);
                }
                // get latest with related changes if requested
                if (!empty($related)) {
                    $results = $this->runQuery($table, $builder, $extras);
                }
            }

            return $results;
        } catch (RestException $ex) {
            throw $ex;
        } catch (\Exception $ex) {
            throw new InternalServerErrorException("Failed to update records in '$table'.\n{$ex->getMessage()}");
        }
    }

    /**
     * {@inheritdoc}
     */
    public function patchRecordsByFilter($table, $record, $filter = null, $params = [], $extras = [])
    {
        // currently the same as update here
        return $this->updateRecordsByFilter($table, $record, $filter, $params, $extras);
    }

    /**
     * {@inheritdoc}
     */
    public function truncateTable($table, $extras = [])
    {
        // truncate the table, return success
        try {
            if (!$tableSchema = $this->parent->getTableSchema($table)) {
                throw new NotFoundException("Table '$table' does not exist in the database.");
            }
            // build filter string if necessary, add server-side filters if necessary
            $builder = $this->parent->getConnection()->table($tableSchema->internalName);
            $ssFilters = array_get($extras, 'ss_filters');
            $params = [];
            $serverFilter = $this->buildQueryStringFromData($ssFilters);
            if (!empty($serverFilter)) {
                Session::replaceLookups($serverFilter);
                $filterString = $this->parseFilterString($serverFilter, $params, $this->tableFieldsInfo);
                $builder->whereRaw($filterString, $params);
                $builder->delete();
            } else {
                $builder->truncate();
            }

            return ['success' => true];
        } catch (RestException $ex) {
            throw $ex;
        } catch (\Exception $ex) {
            throw new InternalServerErrorException("Failed to delete records from '$table'.\n{$ex->getMessage()}");
        }
    }

    /**
     * {@inheritdoc}
     */
    public function deleteRecordsByFilter($table, $filter, $params = [], $extras = [])
    {
        if (empty($filter)) {
            throw new BadRequestException("Filter for delete request can not be empty.");
        }

        $idFields = array_get($extras, ApiOptions::ID_FIELD);
        $idTypes = array_get($extras, ApiOptions::ID_TYPE);
        $ssFilters = array_get($extras, 'ss_filters');

        try {
            if (!$tableSchema = $this->parent->getTableSchema($table)) {
                throw new NotFoundException("Table '$table' does not exist in the database.");
            }
            $fieldsInfo = $tableSchema->getColumns(true);
            /*$idsInfo = */
            $this->getIdsInfo($table, $fieldsInfo, $idFields, $idTypes);

            // build filter string if necessary, add server-side filters if necessary
            $builder = $this->parent->getConnection()->table($tableSchema->internalName);
            $this->convertFilterToNative($builder, $filter, $params, $ssFilters, $fieldsInfo);

            $results = $this->runQuery($table, $builder, $extras);

            $builder->delete();

            return $results;
        } catch (RestException $ex) {
            throw $ex;
        } catch (\Exception $ex) {
            throw new InternalServerErrorException("Failed to delete records from '$table'.\n{$ex->getMessage()}");
        }
    }

    /**
     * {@inheritdoc}
     */
    public function retrieveRecordsByFilter($table, $filter = null, $params = [], $extras = [])
    {
        $ssFilters = array_get($extras, 'ss_filters');

        try {
            $tableSchema = $this->parent->getTableSchema($table);
            if (!$tableSchema) {
                throw new NotFoundException("Table '$table' does not exist in the database.");
            }

            $fieldsInfo = $tableSchema->getColumns(true);

            // build filter string if necessary, add server-side filters if necessary
            $builder = $this->parent->getConnection()->table($tableSchema->internalName);
            $this->convertFilterToNative($builder, $filter, $params, $ssFilters, $fieldsInfo);

            return $this->runQuery($table, $builder, $extras);
        } catch (RestException $ex) {
            throw $ex;
        } catch (\Exception $ex) {
            throw new InternalServerErrorException("Failed to retrieve records from '$table'.\n{$ex->getMessage()}");
        }
    }

    // Helper methods

    protected function restrictFieldsToDefined()
    {
        return true;
    }

    /**
     * @param              $table
     * @param Builder      $builder
     * @param array        $extras
     * @return int|array
     * @throws BadRequestException
     * @throws InternalServerErrorException
     * @throws NotFoundException
     * @throws RestException
     */
    protected function runQuery($table, Builder $builder, $extras)
    {
        $schema = $this->parent->getTableSchema($table);
        if (!$schema) {
            throw new NotFoundException("Table '$table' does not exist in the database.");
        }

        $limit = intval(array_get($extras, ApiOptions::LIMIT, 0));
        $offset = intval(array_get($extras, ApiOptions::OFFSET, 0));
        $countOnly = array_get_bool($extras, ApiOptions::COUNT_ONLY);
        $includeCount = array_get_bool($extras, ApiOptions::INCLUDE_COUNT);

        $maxAllowed = $this->getMaxRecordsReturnedLimit();
        $needLimit = false;
        if (($limit < 1) || ($limit > $maxAllowed)) {
            // impose a limit to protect server
            $limit = $maxAllowed;
            $needLimit = true;
        }

        // count total records
        $count = 0;
        if ($countOnly || $includeCount || $needLimit) {
            $count = $builder->count([DB::raw('1')]);
        }

        if ($countOnly) {
            return $count;
        }

        // apply the selected fields
        $select = $this->parseSelect($schema, $extras);
        $builder->select($select);

        // apply the rest of the parameters
        $order = trim(array_get($extras, ApiOptions::ORDER));
        if (!empty($order)) {
            if (false !== strpos($order, ';')) {
                throw new BadRequestException('Invalid order by clause in request.');
            }
            $commas = explode(',', $order);
            switch (count($commas)) {
                case 0:
                    break;
                case 1:
                    $spaces = explode(' ', $commas[0]);
                    $orderField = $spaces[0];
                    $direction = (isset($spaces[1]) ? $spaces[1] : 'asc');
                    $builder->orderBy($orderField, $direction);
                    break;
                default:
                    // todo need to validate format here first
                    if(stripos($order, 'sleep(') !== false){
                        throw new BadRequestException('Use of the sleep() function not supported.');
                    }
                    $builder->orderByRaw($order);
                    break;
            }
        }
        $group = trim(array_get($extras, ApiOptions::GROUP));
        if (!empty($group)) {
            $group = static::fieldsToArray($group);
            $groups = $this->parseGroupBy($schema, $group);
            $builder->groupBy($groups);
        }
        $builder->take($limit);
        $builder->skip($offset);

        $result = $this->getQueryResults($schema, $builder, $extras);

        if (!empty($result)) {
            $related = array_get($extras, ApiOptions::RELATED);
            if (!empty($related) || $schema->fetchRequiresRelations) {
                if (ApiOptions::FIELDS_ALL !== $related) {
                    $related = static::fieldsToArray($related);
                }
                $refresh = array_get_bool($extras, ApiOptions::REFRESH);
                /** @type RelationSchema[] $availableRelations */
                $availableRelations = $schema->getRelations(true);
                if (!empty($availableRelations)) {
                    // until this is refactored to collections
                    $data = $result->toArray();
                    $this->retrieveRelatedRecords($schema, $availableRelations, $related, $data, $refresh);
                    $result = collect($data);
                }
            }
        }

        $meta = [];
        if ($includeCount || $needLimit) {
            if ($includeCount || $count > $maxAllowed) {
                $meta['count'] = $count;
            }
            if (($count - $offset) > $limit) {
                $meta['next'] = $offset + $limit;
            }
        }

        if (array_get_bool($extras, ApiOptions::INCLUDE_SCHEMA)) {
            try {
                $meta['schema'] = $schema->toArray(true);
            } catch (RestException $ex) {
                throw $ex;
            } catch (\Exception $ex) {
                throw new InternalServerErrorException("Error describing database table '$table'.\n" .
                    $ex->getMessage(), $ex->getCode());
            }
        }

        $data = $result->toArray();
        if (!empty($meta)) {
            $data['meta'] = $meta;
        }

        return $data;
    }

    /**
     * @param TableSchema $schema
     * @param Builder     $builder
     * @param array       $extras
     * @return Collection
     */
    protected function getQueryResults(TableSchema $schema, Builder $builder, $extras)
    {
        $result = $builder->get();

        $result->transform(function ($item) use ($schema) {
            $item = (array)$item;
            foreach ($item as $field => &$value) {
                if ($fieldInfo = $schema->getColumn($field, true)) {
                    $value = $this->parent->getSchema()->typecastToClient($value, $fieldInfo);
                }
            }

            return $item;
        });

        return $result;
    }

    /**
     * Take in a ANSI SQL filter string (WHERE clause)
     * or our generic NoSQL filter array or partial record
     * and parse it to the service's native filter criteria.
     * The filter string can have substitution parameters such as
     * ':name', in which case an associative array is expected,
     * for value substitution.
     *
     * @param \Illuminate\Database\Query\Builder $builder
     * @param string | array                     $filter       SQL WHERE clause filter string
     * @param array                              $params       Array of substitution values
     * @param array                              $ss_filters   Server-side filters to apply
     * @param array                              $avail_fields All available fields for the table
     *
     * @throws \DreamFactory\Core\Exceptions\BadRequestException
     * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
     */
    protected function convertFilterToNative(
        Builder $builder,
        $filter,
        $params = [],
        $ss_filters = [],
        $avail_fields = []
    ) {
        // interpret any parameter values as lookups
        $params = (is_array($params) ? static::interpretRecordValues($params) : []);
        $serverFilter = $this->buildQueryStringFromData($ss_filters);

        $outParams = [];
        if (empty($filter)) {
            $filter = $serverFilter;
        } elseif (is_string($filter)) {
            if (!empty($serverFilter)) {
                $filter = '(' . $filter . ') ' . DbLogicalOperators::AND_STR . ' (' . $serverFilter . ')';
            }
        } elseif (is_array($filter)) {
            // todo parse client filter?
            $filter = '';
            if (!empty($serverFilter)) {
                $filter = '(' . $filter . ') ' . DbLogicalOperators::AND_STR . ' (' . $serverFilter . ')';
            }
        }

        Session::replaceLookups($filter);
        $filterString = $this->parseFilterString($filter, $outParams, $avail_fields, $params);
        if (!empty($filterString)) {
            $builder->whereRaw($filterString, $outParams);
        }
    }

    /**
     * @param       $filter_info
     *
     * @return null|string
     * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
     */
    protected function buildQueryStringFromData($filter_info)
    {
        $filters = array_get($filter_info, 'filters');
        if (empty($filters)) {
            return null;
        }

        $sql = '';
        $combiner = array_get($filter_info, 'filter_op', DbLogicalOperators::AND_STR);
        foreach ($filters as $key => $filter) {
            if (!empty($sql)) {
                $sql .= " $combiner ";
            }

            $name = array_get($filter, 'name');
            $op = strtoupper(array_get($filter, 'operator'));
            if (empty($name) || empty($op)) {
                // log and bail
                throw new InternalServerErrorException('Invalid server-side filter configuration detected.');
            }

            if (DbComparisonOperators::requiresNoValue($op)) {
                $sql .= "($name $op)";
            } else {
                $value = array_get($filter, 'value');
                $sql .= "($name $op $value)";
            }
        }

        return $sql;
    }

    /**
     * @param string         $filter
     * @param array          $out_params
     * @param ColumnSchema[] $fields_info
     * @param array          $in_params
     *
     * @return string
     * @throws \DreamFactory\Core\Exceptions\BadRequestException
     * @throws \Exception
     */
    protected function parseFilterString($filter, array &$out_params, $fields_info, array $in_params = [])
    {
        if (empty($filter)) {
            return null;
        }

        $filter = trim($filter);
        // todo use smarter regex
        // handle logical operators first
        $logicalOperators = DbLogicalOperators::getDefinedConstants();
        foreach ($logicalOperators as $logicalOp) {
            if (DbLogicalOperators::NOT_STR === $logicalOp) {
                // NOT(a = 1)  or NOT (a = 1)format
                if ((0 === stripos($filter, $logicalOp . ' (')) || (0 === stripos($filter, $logicalOp . '('))) {
                    $parts = trim(substr($filter, 3));
                    $parts = $this->parseFilterString($parts, $out_params, $fields_info, $in_params);

                    return static::localizeOperator($logicalOp) . $parts;
                }
            } else {
                // (a = 1) AND (b = 2) format or (a = 1)AND(b = 2) format
                $filter = str_ireplace(')' . $logicalOp . '(', ') ' . $logicalOp . ' (', $filter);
                $paddedOp = ') ' . $logicalOp . ' (';
                if (false !== $pos = stripos($filter, $paddedOp)) {
                    $left = trim(substr($filter, 0, $pos)) . ')'; // add back right )
                    $right = '(' . trim(substr($filter, $pos + strlen($paddedOp))); // adding back left (
                    $left = $this->parseFilterString($left, $out_params, $fields_info, $in_params);
                    $right = $this->parseFilterString($right, $out_params, $fields_info, $in_params);

                    return $left . ' ' . static::localizeOperator($logicalOp) . ' ' . $right;
                }
            }
        }

        $wrap = false;
        if ((0 === strpos($filter, '(')) && ((strlen($filter) - 1) === strrpos($filter, ')'))) {
            // remove unnecessary wrapping ()
            $filter = substr($filter, 1, -1);
            $wrap = true;
        }

        // Some scenarios leave extra parens dangling
        $pure = trim($filter, '()');
        $pieces = explode($pure, $filter);
        $leftParen = (!empty($pieces[0]) ? $pieces[0] : null);
        $rightParen = (!empty($pieces[1]) ? $pieces[1] : null);
        $filter = $pure;

        // the rest should be comparison operators
        // Note: order matters here!
        $sqlOperators = DbComparisonOperators::getParsingOrder();
        foreach ($sqlOperators as $sqlOp) {
            $paddedOp = static::padOperator($sqlOp);
            if (false !== $pos = stripos($filter, $paddedOp)) {
                $field = trim(substr($filter, 0, $pos));
                $negate = false;
                if (false !== strpos($field, ' ')) {
                    $parts = explode(' ', $field);
                    $partsCount = count($parts);
                    if (($partsCount > 1) &&
                        (0 === strcasecmp($parts[$partsCount - 1], trim(DbLogicalOperators::NOT_STR)))
                    ) {
                        // negation on left side of operator
                        array_pop($parts);
                        $field = implode(' ', $parts);
                        $negate = true;
                    }
                }
                /** @type ColumnSchema $info */
                if (null === $info = array_get($fields_info, strtolower($field))) {
                    // This could be SQL injection attempt or bad field
                    throw new BadRequestException("Invalid or unparsable field in filter request: '$field'");
                }

                // make sure we haven't chopped off right side too much
                $value = trim(substr($filter, $pos + strlen($paddedOp)));
                if ((0 !== strpos($value, "'")) &&
                    (0 !== $lpc = substr_count($value, '(')) &&
                    ($lpc !== $rpc = substr_count($value, ')'))
                ) {
                    // add back to value from right
                    $parenPad = str_repeat(')', $lpc - $rpc);
                    $value .= $parenPad;
                    $rightParen = preg_replace('/\)/', '', $rightParen, $lpc - $rpc);
                }
                if (DbComparisonOperators::requiresValueList($sqlOp)) {
                    if ((0 === strpos($value, '(')) && ((strlen($value) - 1) === strrpos($value, ')'))) {
                        // remove wrapping ()
                        $value = substr($value, 1, -1);
                        $parsed = [];
                        foreach (explode(',', $value) as $each) {
                            $parsed[] = $this->parseFilterValue(trim($each), $info, $out_params, $in_params);
                        }
                        $value = '(' . implode(',', $parsed) . ')';
                    } else {
                        throw new BadRequestException('Filter value lists must be wrapped in parentheses.');
                    }
                } elseif (DbComparisonOperators::requiresNoValue($sqlOp)) {
                    $value = null;
                } else {
                    static::modifyValueByOperator($sqlOp, $value);
                    $value = $this->parseFilterValue($value, $info, $out_params, $in_params);
                }

                $sqlOp = static::localizeOperator($sqlOp);
                if ($negate) {
                    $sqlOp = DbLogicalOperators::NOT_STR . ' ' . $sqlOp;
                }

                if ($function = $info->getDbFunction(DbFunctionUses::FILTER)) {
                    $out = $this->parent->getConnection()->raw($function);
                } else {
                    $out = $info->quotedName;
                }
                $out .= " $sqlOp";
                $out .= (isset($value) ? " $value" : null);
                if ($leftParen) {
                    $out = $leftParen . $out;
                }
                if ($rightParen) {
                    $out .= $rightParen;
                }

                return ($wrap ? '(' . $out . ')' : $out);
            }
        }

        // This could be SQL injection attempt or unsupported filter arrangement
        throw new BadRequestException('Invalid or unparsable filter request.');
    }

    /**
     * @param mixed        $value
     * @param ColumnSchema $info
     * @param array        $out_params
     * @param array        $in_params
     *
     * @return int|null|string
     * @throws BadRequestException
     */
    protected function parseFilterValue($value, ColumnSchema $info, array &$out_params, array $in_params = [])
    {
        // if a named replacement parameter, un-name it because Laravel can't handle named parameters
        if (is_array($in_params) && (0 === strpos($value, ':'))) {
            if (array_key_exists($value, $in_params)) {
                $value = $in_params[$value];
            }
        }

        // remove quoting on strings if used, i.e. 1.x required them
        if (is_string($value)) {
            if ((0 === strcmp("'" . trim($value, "'") . "'", $value)) ||
                (0 === strcmp('"' . trim($value, '"') . '"', $value))
            ) {
                $value = substr($value, 1, -1);
            } elseif ((0 === strpos($value, '(')) && ((strlen($value) - 1) === strrpos($value, ')'))) {
                // function call
                return $value;
            }
        }

        // anything else schema specific
        $value = $this->parent->getSchema()->typecastToNative($value, $info);

        $out_params[] = $value;
        $value = '?';

        return $value;
    }

    /**
     * @inheritdoc
     */
    protected function getCurrentTimestamp()
    {
        return $this->parent->getSchema()->getTimestampForSet();
    }

    /**
     * @inheritdoc
     */
    protected function parseValueForSet($value, $field_info, $for_update = false)
    {
        if ($value instanceof Expression) {
            // todo need to wrangle in expression parameters somehow
            return DB::raw($value->expression);
        } else {
            return parent::parseValueForSet($value, $field_info, $for_update);
        }
    }

    /**
     * @param array $record
     *
     * @return array
     */
    public static function interpretRecordValues($record)
    {
        if (!is_array($record) || empty($record)) {
            return $record;
        }

        foreach ($record as $field => $value) {
            Session::replaceLookups($value);
            static::valueToExpression($value);
            $record[$field] = $value;
        }

        return $record;
    }

    /**
     * @param $value
     */
    public static function valueToExpression(&$value)
    {
        if (is_array($value) && isset($value['expression'])) {
            $expression = $value['expression'];
            $params = [];
            if (is_array($expression) && isset($expression['value'])) {
                $params = isset($expression['params']) ? $expression['params'] : [];
                $expression = $expression['value'];
            }

            $value = new Expression($expression, $params);
        }
    }

    /**
     * @param ColumnSchema $field
     *
     * @return \Illuminate\Database\Query\Expression|string
     */
    protected function parseFieldForSelect($field)
    {
        if ($function = $field->getDbFunction(DbFunctionUses::SELECT)) {
            return $this->parent->getConnection()->raw($function . ' AS ' . $field->getName(true, true));
        }

        $out = $field->name;
        if (!empty($field->alias)) {
            $out .= ' AS ' . $field->alias;
        }

        return $out;
    }

    protected static function fieldsToArray($fields)
    {
        if (empty($fields) || (ApiOptions::FIELDS_ALL === $fields)) {
            return [];
        }

        return (!is_array($fields)) ? array_map('trim', explode(',', trim($fields, ','))) : $fields;
    }

    /**
     * @param  TableSchema $schema
     * @param  array|null  $extras
     *
     * @return array
     * @throws \DreamFactory\Core\Exceptions\BadRequestException
     * @throws \Exception
     */
    protected function parseSelect($schema, $extras)
    {
        $fields = array_get($extras, ApiOptions::FIELDS);
        if (empty($fields)) {
            // minimally return the id fields
            $fields = array_get($extras, ApiOptions::ID_FIELD);
            if (empty($fields)) {
                $fields = $schema->getPrimaryKey();
                // if still nothing, return everything
                if (empty($fields)) {
                    $fields = ApiOptions::FIELDS_ALL;
                }
            }
        }
        $outArray = [];
        if (ApiOptions::FIELDS_ALL === $fields) {
            foreach ($schema->getColumns() as $fieldInfo) {
                if ($fieldInfo->isAggregate) {
                    continue;
                }
                $out = $this->parseFieldForSelect($fieldInfo);
                if (is_array($out)) {
                    $outArray = array_merge($outArray, $out);
                } else {
                    $outArray[] = $out;
                }
            }
        } else {
            $fields = static::fieldsToArray($fields);
            $related = array_get($extras, ApiOptions::RELATED);
            $allRelated = ('*' === $related);
            $related = static::fieldsToArray($related);
            if ($allRelated || !empty($related) || $schema->fetchRequiresRelations) {
                // add any required relationship mapping fields
                foreach ($schema->getRelations() as $relation) {
                    if ($relation->alwaysFetch || $allRelated || array_key_exists($relation->getName(true), $related)) {
                        foreach ($relation->field as $relField) {
                            if ($fieldInfo = $schema->getColumn($relField)) {
                                $relationField = $fieldInfo->getName(true); // account for aliasing
                                if (false === array_search($relationField, $fields)) {
                                    $fields[] = $relationField;
                                }
                            }
                        }
                    }
                }
            }
            foreach ($fields as $field) {
                if ($fieldInfo = $schema->getColumn($field, true)) {
                    $out = $this->parseFieldForSelect($fieldInfo);
                    if (is_array($out)) {
                        $outArray = array_merge($outArray, $out);
                    } else {
                        $outArray[] = $out;
                    }
                } else {
                    throw new BadRequestException('Invalid field requested: ' . $field);
                }
            }
        }

        return $outArray;
    }

    /**
     * @param  TableSchema $schema
     * @param  array       $fields
     *
     * @return array
     * @throws \DreamFactory\Core\Exceptions\BadRequestException
     * @throws \Exception
     */
    protected function parseGroupBy($schema, $fields = null)
    {
        $outArray = [];
        if (!empty($fields)) {
            foreach ($fields as $field) {
                if ($fieldInfo = $schema->getColumn($field, true)) {
                    $outArray[] = $fieldInfo->name;
                } else {
                    if (false !== strpos($field, ';')) {
                        throw new BadRequestException('Invalid group by clause in request.');
                    }
                    $outArray[] = DB::raw($field); // todo better checks on group by clause
                }
            }
        }

        return $outArray;
    }

    /**
     * @param      $table
     * @param null $fields_info
     * @param null $requested_fields
     * @param null $requested_types
     *
     * @return array|\DreamFactory\Core\Database\Schema\ColumnSchema[]
     * @throws \DreamFactory\Core\Exceptions\BadRequestException
     */
    protected function getIdsInfo($table, $fields_info = null, &$requested_fields = null, $requested_types = null)
    {
        $idsInfo = [];
        if (empty($requested_fields)) {
            $requested_fields = [];
            /** @type ColumnSchema[] $idsInfo */
            $idsInfo = static::getPrimaryKeys($fields_info);
            foreach ($idsInfo as $info) {
                $requested_fields[] = $info->getName(true);
            }
        } else {
            if (false !== $requested_fields = static::validateAsArray($requested_fields, ',')) {
                foreach ($requested_fields as $field) {
                    $ndx = strtolower($field);
                    if (isset($fields_info[$ndx])) {
                        $idsInfo[] = $fields_info[$ndx];
                    }
                }
            }
        }

        return $idsInfo;
    }

    /**
     * @param $avail_fields
     *
     * @return string
     */
    public static function getPrimaryKeyFieldFromDescribe($avail_fields)
    {
        foreach ($avail_fields as $field_info) {
            if ($field_info->isPrimaryKey) {
                return $field_info->name;
            }
        }

        return '';
    }

    /**
     * @param array   $avail_fields
     * @param boolean $names_only Return only an array of names, otherwise return all properties
     *
     * @return array
     */
    public static function getPrimaryKeys($avail_fields, $names_only = false)
    {
        $keys = [];
        foreach ($avail_fields as $info) {
            if ($info->isPrimaryKey) {
                $keys[] = ($names_only ? $info->name : $info);
            }
        }

        return $keys;
    }

    /**
     * {@inheritdoc}
     */
    protected function addToTransaction(
        $record = null,
        $id = null,
        $extras = null,
        $rollback = false,
        $continue = false,
        $single = false
    ) {
        $dbConn = $this->parent->getConnection();
        if ($rollback) {
            // sql transaction really only for rollback scenario, not batching
            if (0 >= $dbConn->transactionLevel()) {
                $dbConn->beginTransaction();
            }
        }

        if (!isset($extras['limit'])) {
            $extras['limit'] = 1;
        }
        $ssFilters = array_get($extras, 'ss_filters');
        $updates = array_get($extras, 'updates');
        $idFields = array_get($extras, 'id_fields');
        $needToIterate = ($single || !$continue || (1 < count($this->tableIdsInfo)));

        $related = array_get($extras, 'related');
        $requireMore = array_get_bool($extras, 'require_more') || !empty($related);
        $allowRelatedDelete = array_get_bool($extras, 'allow_related_delete');
        $relatedInfo = $this->describeTableRelated($this->transactionTable);

        $builder = $dbConn->table($this->transactionTableSchema->internalName);
        $match = [];
        if (!is_null($id)) {
            if (is_array($id)) {
                $match = $id;
                foreach ($idFields as $name) {
                    $builder->where($name, array_get($id, $name));
                }
            } else {
                $name = array_get($idFields, 0);
                $match[$name] = $id;
                $builder->where($name, $id);
            }
        }

        $serverFilter = $this->buildQueryStringFromData($ssFilters);
        if (!empty($serverFilter)) {
            Session::replaceLookups($serverFilter);
            $params = [];
            $filterString = $this->parseFilterString($serverFilter, $params, $this->tableFieldsInfo);
            $builder->whereRaw($filterString, $params);
        }

        $out = [];
        switch ($this->getAction()) {
            case Verbs::POST:
                // need the id back in the record
                if (!empty($match)) {
                    $record = array_merge($record, $match);
                }

                if (!empty($relatedInfo)) {
                    $this->updatePreRelations($record, $relatedInfo);
                }

                $parsed = $this->parseRecord($record, $this->tableFieldsInfo, $ssFilters);
                if (empty($parsed)) {
                    throw new BadRequestException('No valid fields were found in record.');
                }

                if (is_null($id) && (1 === count($this->tableIdsInfo)) && $this->tableIdsInfo[0]->autoIncrement) {
                    $idName = $this->tableIdsInfo[0]->name;
                    $id[$idName] = $builder->insertGetId($parsed, $idName);
                    $record[$idName] = $id[$idName];
                } else {
                    if (!$builder->insert($parsed)) {
                        throw new InternalServerErrorException("Record insert failed.");
                    }
                }

                if (!empty($relatedInfo)) {
                    $this->updatePostRelations(
                        $this->transactionTable,
                        $record,
                        $relatedInfo,
                        $allowRelatedDelete
                    );
                }

                $idName =
                    (isset($this->tableIdsInfo, $this->tableIdsInfo[0], $this->tableIdsInfo[0]->name))
                        ? $this->tableIdsInfo[0]->name : null;
                $out = (is_array($id)) ? $id : [$idName => $id];

                // add via record, so batch processing can retrieve extras
                if ($requireMore) {
                    parent::addToTransaction($id);
                }
                break;

            case Verbs::PUT:
            case Verbs::PATCH:
                if (!empty($updates)) {
                    $record = $updates;
                }

                if (!empty($relatedInfo)) {
                    $this->updatePreRelations($record, $relatedInfo);
                }

                $parsed = $this->parseRecord($record, $this->tableFieldsInfo, $ssFilters, true);
                if (!empty($parsed)) {
                    if (!empty($match) && $this->parent->upsertAllowed() && !$builder->exists()) {
                        if (!$builder->insert(array_merge($match, $parsed))) {
                            throw new InternalServerErrorException("Record upsert failed.");
                        }
                    } else {
                        $parsed = array_except($parsed, array_keys($match));
                        $rows = $builder->update($parsed);
                        if (0 >= $rows) {
                            // could have just not updated anything, or could be bad id
                            if (empty($this->runQuery($this->transactionTable, $builder, $extras))) {
                                throw new NotFoundException("Record with identifier '" .
                                    print_r($id, true) .
                                    "' not found.");
                            }
                        }
                    }
                }

                if (!empty($relatedInfo)) {
                    // need the id back in the record
                    if (!empty($match)) {
                        $record = array_merge($record, $match);
                    }
                    $this->updatePostRelations(
                        $this->transactionTable,
                        $record,
                        $relatedInfo,
                        $allowRelatedDelete
                    );
                }

                $idName =
                    (isset($this->tableIdsInfo, $this->tableIdsInfo[0], $this->tableIdsInfo[0]->name))
                        ? $this->tableIdsInfo[0]->name : null;
                $out = (is_array($id)) ? $id : [$idName => $id];

                // add via record, so batch processing can retrieve extras
                if ($requireMore) {
                    parent::addToTransaction($id);
                }
                break;

            case Verbs::DELETE:
                if (!$needToIterate) {
                    return parent::addToTransaction(null, $id);
                }

                // add via record, so batch processing can retrieve extras
                if ($requireMore) {
                    $result = $this->runQuery(
                        $this->transactionTable,
                        $builder,
                        $extras
                    );
                    if (empty($result)) {
                        // bail, we know it isn't there
                        throw new NotFoundException("Record with identifier '" . print_r($id, true) . "' not found.");
                    }

                    $out = $result[0];
                }

                if (1 > $builder->delete()) {
                    // wasn't anything there to delete
                    throw new NotFoundException("Record with identifier '" . print_r($id, true) . "' not found.");
                }

                if (empty($out)) {
                    $idName =
                        (isset($this->tableIdsInfo, $this->tableIdsInfo[0], $this->tableIdsInfo[0]->name))
                            ? $this->tableIdsInfo[0]->name : null;
                    $out = (is_array($id)) ? $id : [$idName => $id];
                }
                break;

            case Verbs::GET:
                if (!$needToIterate) {
                    return parent::addToTransaction(null, $id);
                }

                $result = $this->runQuery($this->transactionTable, $builder, $extras);
                if (empty($result)) {
                    throw new NotFoundException("Record with identifier '" . print_r($id, true) . "' not found.");
                }

                $out = $result[0];
                break;
        }

        return $out;
    }

    /**
     * {@inheritdoc}
     */
    protected function commitTransaction($extras = null)
    {
        $dbConn = $this->parent->getConnection();
        if (empty($this->batchRecords) && empty($this->batchIds)) {
            if (0 < $dbConn->transactionLevel()) {
                $dbConn->commit();
            }

            return null;
        }

        $updates = array_get($extras, 'updates');
        $ssFilters = array_get($extras, 'ss_filters');
        $related = array_get($extras, 'related');
        $requireMore = array_get_bool($extras, 'require_more') || !empty($related);
        $allowRelatedDelete = array_get_bool($extras, 'allow_related_delete');
        $relatedInfo = $this->describeTableRelated($this->transactionTable);

        $builder = $dbConn->table($this->transactionTableSchema->internalName);

        /** @type ColumnSchema $idName */
        $idName = (isset($this->tableIdsInfo, $this->tableIdsInfo[0])) ? $this->tableIdsInfo[0] : null;
        if (empty($idName)) {
            throw new BadRequestException('No valid identifier found for this table.');
        }

        if (!empty($this->batchRecords)) {
            if (is_array($this->batchRecords[0])) {
                $temp = [];
                foreach ($this->batchRecords as $record) {
                    $temp[] = array_get($record, $idName->getName(true));
                }

                $builder->whereIn($idName->name, $temp);
            } else {
                $builder->whereIn($idName->name, $this->batchRecords);
            }
        } else {
            $builder->whereIn($idName->name, $this->batchIds);
        }

        $serverFilter = $this->buildQueryStringFromData($ssFilters);
        if (!empty($serverFilter)) {
            Session::replaceLookups($serverFilter);
            $params = [];
            $filterString = $this->parseFilterString($serverFilter, $params, $this->tableFieldsInfo);
            $builder->whereRaw($filterString, $params);
        }

        $out = [];
        $action = $this->getAction();
        if (!empty($this->batchRecords)) {
            if (1 == count($this->tableIdsInfo)) {
                // records are used to retrieve extras
                // ids array are now more like records
                $result = $this->runQuery($this->transactionTable, $builder, $extras);
                if (empty($result)) {
                    throw new NotFoundException('No records were found using the given identifiers.');
                }

                $out = $result;
            } else {
                $out = $this->retrieveRecords($this->transactionTable, $this->batchRecords, $extras);
            }

            $this->batchRecords = [];
        } elseif (!empty($this->batchIds)) {
            switch ($action) {
                case Verbs::PUT:
                case Verbs::PATCH:
                    if (!empty($updates)) {
                        $parsed = $this->parseRecord($updates, $this->tableFieldsInfo, $ssFilters, true);
                        if (!empty($parsed)) {
                            $rows = $builder->update($parsed);
                            if (count($this->batchIds) !== $rows) {
                                throw new BadRequestException('Batch Error: Not all requested records could be updated.');
                            }
                        }

                        foreach ($this->batchIds as $id) {
                            if (!empty($relatedInfo)) {
                                $this->updatePostRelations(
                                    $this->transactionTable,
                                    array_merge($updates, [$idName->getName(true) => $id]),
                                    $relatedInfo,
                                    $allowRelatedDelete
                                );
                            }
                        }

                        if ($requireMore) {
                            $result = $this->runQuery(
                                $this->transactionTable,
                                $builder,
                                $extras
                            );

                            $out = $result;
                        }
                    }
                    break;

                case Verbs::DELETE:
                    $result = $this->runQuery(
                        $this->transactionTable,
                        $builder,
                        $extras
                    );
                    if (count($this->batchIds) !== count($result)) {
                        foreach ($this->batchIds as $index => $id) {
                            $found = false;
                            foreach ($result as $record) {
                                if ($id == array_get($record, $idName->getName(true))) {
                                    $out[$index] = $record;
                                    $found = true;
                                    break;
                                }
                            }
                            if (!$found) {
                                $out[$index] = new NotFoundException("Record with identifier '" . print_r($id,
                                        true) . "' not found.");
                            }
                        }
                    } else {
                        $out = $result;
                    }

                    $rows = $builder->delete();
                    if (count($this->batchIds) !== $rows) {
                        throw new BatchException($out, 'Batch Error: Not all requested records could be deleted.');
                    }
                    break;

                case Verbs::GET:
                    $result = $this->runQuery(
                        $this->transactionTable,
                        $builder,
                        $extras
                    );

                    if (count($this->batchIds) !== count($result)) {
                        foreach ($this->batchIds as $index => $id) {
                            $found = false;
                            foreach ($result as $record) {
                                if ($id == array_get($record, $idName->getName(true))) {
                                    $out[$index] = $record;
                                    $found = true;
                                    break;
                                }
                            }
                            if (!$found) {
                                $out[$index] = new NotFoundException("Record with identifier '" . print_r($id,
                                        true) . "' not found.");
                            }
                        }

                        throw new BatchException($out, 'Batch Error: Not all requested records could be retrieved.');
                    }

                    $out = $result;
                    break;

                default:
                    break;
            }

            if (empty($out)) {
                $out = [];
                foreach ($this->batchIds as $id) {
                    $out[] = [$idName->getName(true) => $id];
                }
            }

            $this->batchIds = [];
        }

        if (0 < $dbConn->transactionLevel()) {
            $dbConn->commit();
        }

        return $out;
    }

    /**
     * {@inheritdoc}
     */
    protected function rollbackTransaction()
    {
        $dbConn = $this->parent->getConnection();
        if (0 < $dbConn->transactionLevel()) {
            $dbConn->rollBack();
        }

        return true;
    }
}


text:
#import <Foundation/Foundation.h>
#import "NuCell.h"
#import "NuBlock.h"
#import "NuOperator.h"

/*!
    @class NuEnumerable
    @abstract The NuEnumerable mixin class.
    @discussion This class implements methods that act on enumerated collections of objects.
    It is designed to be mixed into a class using the include method that Nu adds to NSObject.
    The receiving class must have an objectEnumerator method that returns an NSEnumerator.
    Some methods in this class take a callable object as an argument; callable objects are those
    that have evalWithArguments:context: defined.
 */
@interface NuEnumerable : NSObject
{
}

/*! Iterate over each member of a collection, evaluating the provided callable item for each member. */
- (id) each:(id) callable;
/*! Iterate over each member of a collection, evaluating the provided block for each member.
    The block is expected to take two arguments: the member and its index. */
- (id) eachWithIndex:(NuBlock *) block;
/*! Iterate over each member of a collection, returning an array containing the elements for which the provided block evaluates non-nil. */
- (NSArray *) select:(NuBlock *) block;
/*! Iterate over each member of a collection, returning the first element for which the provided block evaluates non-nil. */
- (id) find:(NuBlock *) block;
/*! Iterate over each member of a collection, applying the provided block to each member, and returning an array of the results. */
- (NSArray *) map:(id) callable;
/*! Iterate over each member of a collection, using the provided callable to combine members into a single return value.
*/

- (id) reduce:(id) callable from:(id) initial;

/*! Iterate over each member of a collection, applying the provided selector to each member, and returning an array of the results. */
- (NSArray *) mapSelector:(SEL) selector;
@end

@interface NSArray (Enumeration)
/*! Repeatedly apply a function of two arguments to the elements of an array,
working from right to left and beginning with the specified inital value. */
- (id) reduceLeft:(id)callable from:(id) initial;
/*! Iterate over each member of an array in reverse order and beginning with the lastObject, evaluating the provided block for each member. */
- (id) eachInReverse:(id) callable;
/*! Return a sorted array using the specified block to compare array elements.
The block should return -1, 0, or 1. */
- (NSArray *) sortedArrayUsingBlock:(NuBlock *) block;
@end

redpajama_set_name: RedPajamaGithub


text:
<?php

declare(strict_types=1);

namespace Linna\DataMapper;

/**
 * Fetch Limit Interface.
 *
 *
Contain an optional method for basic Mapper.

 */
interface FetchLimitInterface
{
    /**
     * Fetch a list of domain objects stored in persistent storage for a specific domain using a limit clausole.
     *
     *
This method must return an array containing all instances, in interval specified by the limit, of domain
     * objects for a specific domain or a void array.

     *
     * @param int $offset   Offset of the first row to return.
     * @param int $rowCount Maximum number of rows to return.
     *
     * @return array<mixed> The array with domain objects or void.
     */
    public function fetchLimit(int $offset, int $rowCount): array;
}


text:
var express      = require('express'),
    bodyParser   = require('body-parser'),
    cookieParser = require('cookie-parser'),

    recovery     = require('./middlewares/recovery.js'),
    logger       = require('./middlewares/logger.js'),
    favicon      = require('./middlewares/favicon.js');

//////////
// Code //

// Creating the sub-app.
var app = express();

// Registering all of the middlewares.
app.use(bodyParser.json());                                  // Parsing the body (POST requests) to be used in a given route.
app.use(cookieParser());                                     // Parsing cookies out to be used in a given route.
app.use(favicon.middleware);                                 // Redirecting the favicon to the correct location.
app.use(recovery.middleware);                                // Recovering from a crash in the app.
app.use('/static/', express.static(__dirname + '/static/')); // Serving files from the static directory.
app.use(logger.middleware);                                  // Logging all of the requests.

/////////////
// Exports //
module.exports = app;


text:
package goparsec

import (
	"testing"
)

func TestBinds_(t *testing.T) {
	data := "int opt int"
	st := MemoryParseState(data)
	checker := Binds_(String("int"), Spaces, String("opt"), Spaces, String("int"), Eof)
	_, err := checker(st)
	if err != nil {
		t.Fatalf("expect the Binds_ checker success but %v", err)
	}
}

func TestBindsFail_(t *testing.T) {
	data := "int opt float"
	st := MemoryParseState(data)
	checker := Binds_(String("int"), Spaces, String("opt"), Spaces, String("int"), Eof)
	_, err := checker(st)
	if err == nil {
		t.Fatalf("expect the Binds_ checker failed at \"%s\"", data)
	}
}


text:
package org.apache.batik.css.engine.value.svg12;

import org.apache.batik.css.engine.value.AbstractValue;
import org.w3c.dom.DOMException;
import org.w3c.dom.css.CSSValue;

/**
 * This class represents an device-specific color value.
 *
 * @version $Id$
 */
public class DeviceColor extends AbstractValue {

    public static final String DEVICE_GRAY_COLOR_FUNCTION = "device-gray";
    public static final String DEVICE_RGB_COLOR_FUNCTION = "device-rgb";
    public static final String DEVICE_CMYK_COLOR_FUNCTION = "device-cmyk";
    public static final String DEVICE_NCHANNEL_COLOR_FUNCTION = "device-nchannel";

    protected boolean nChannel;

    /**
     * The color count.
     */
    protected int count;

    /**
     * The colors.
     */
    protected float[] colors = new float[5];

    /**
     * Creates a new DeviceColor.
     * @param nChannel true for a device-nchannel() color, false for Gray, RGB and CMYK
     */
    public DeviceColor(boolean nChannel) {
        this.nChannel = nChannel;
    }

    /**
     * Implements {@link
     * org.apache.batik.css.engine.value.Value#getCssValueType()}.
     */
    public short getCssValueType() {
        return CSSValue.CSS_CUSTOM;
    }

    /**
     * Indicates whether this color uses an N-Channel color space.
     * @return true if N-Channel is used
     */
    public boolean isNChannel() {
        return this.nChannel;
    }

    /**
     * Returns the number of colors.
     */
    public int getNumberOfColors() throws DOMException {
        return count;
    }

    /**
     * Returns the color at the given index.
     */
    public float getColor(int i) throws DOMException {
        return colors[i];
    }

    /**
     *  A string representation of the current value.
     */
    public String getCssText() {
        StringBuffer sb = new StringBuffer( count * 8 );
        if (nChannel) {
            sb.append(DEVICE_NCHANNEL_COLOR_FUNCTION);
        } else {
            switch (count) {
            case 1:
                sb.append(DEVICE_GRAY_COLOR_FUNCTION);
                break;
            case 3:
                sb.append(DEVICE_RGB_COLOR_FUNCTION);
                break;
            case 4:
                sb.append(DEVICE_CMYK_COLOR_FUNCTION);
                break;
            default:
                throw new IllegalStateException("Invalid number of components encountered");
            }
        }
        sb.append('(');
        for (int i = 0; i < count; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(colors[i]);
        }
        sb.append( ')' );
        return sb.toString();
    }

    /**
     * Appends a color to the list.
     */
    public void append(float c) {
        if (count == colors.length) {
            float[] t = new float[count * 2];
            System.arraycopy( colors, 0, t, 0, count );
            colors = t;
        }
        colors[count++] = c;
    }

    /** {@inheritDoc} */
    public String toString() {
        return getCssText();
    }

}


text:
describe Mixin::Enum::Accessor do
  let(:subject_module) do
    Module.new do
      include Mixin::Enum::Accessor
      const_set(:REQUESTED,    1)
      const_set(:IN_PROGRESS,  2)
      const_set(:UNDER_REVIEW, 3)
      const_set(:APPROVED,     4)
      const_set(:REJECTED,     5)
      const_set(:CANCELLED,    6)
    end
  end

  describe '.values' do
    subject { subject_module.values }

    it 'returns all values' do
      is_expected.to contain_exactly(1, 2, 3, 4, 5, 6)
    end
  end

  describe '.all' do
    subject { subject_module.all }

    it 'returns a hash of defined enumerations' do
      is_expected.to match(
        REQUESTED:    1,
        IN_PROGRESS:  2,
        UNDER_REVIEW: 3,
        APPROVED:     4,
        REJECTED:     5,
        CANCELLED:    6
      )
    end
  end
end


text:
angular.module('myApp',[])
.factory('UserService',function($http) {
  var current_user;
  return {
    getCurrentUser: function() {
      return current_user;
    },
    setCurrentUser: function(user) {
      current_user = user;
    }
  }
});


text:
<?php

namespace User\Model;

class Resume
{
    public $id;
    public $userid;
    public $title;
    public $resume;

    public function exchangeArray($data)
    {
        $this->id     = (!empty($data['id'])) ? $data['id'] : null;
        $this->userid     = (!empty($data['userid'])) ? $data['userid'] : null;
        $this->title     = (!empty($data['title'])) ? $data['title'] : null;
        $this->resume     = (!empty($data['resume'])) ? $data['resume'] : null;

    }
}


text:
#ifndef CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H
#define CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H

#include "ctkCmdLineModuleBackend.h"

#include "ctkCommandLineModulesBackendFunctionPointerExport.h"
#include "ctkCmdLineModuleBackendFPTypeTraits.h"
#include "ctkCmdLineModuleBackendFPUtil_p.h"

#include <QScopedPointer>
#include <QSharedPointer>
#include <QString>
#include <QUrl>
#include <QMetaType>

#include <QDebug>

namespace ctk {
namespace CmdLineModuleBackendFunctionPointer {

struct FunctionPointerProxy;

template<typename T>
QString GetParameterTypeName();

struct ImageType {};

// default parameter description
template<typename T, typename Enable = void>
struct CreateXmlFor
{
  static QString parameter(int index, const QString& typeName, const QString& label = QString(), const QString& description = QString())
  {
    QString xmlParameter;
    QTextStream str(&xmlParameter);
    str << "    <" << typeName << ">\n";
    str << "      <name>" << QString("param%1").arg(index) << "</name>\n";
    str << "      <index>" << index << "</index>\n";
    str << "      <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
    str << "      " << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "\n";
    str << "    </" << typeName << ">\n";
    return xmlParameter;
  }
};

// specialization for input image types
template<typename T>
struct CreateXmlFor<T, typename EnableIf<IsBaseOf<ImageType, T>::value >::Type >
{
  static QString parameter(int index, const QString& typeName, const QString& label = QString(), const QString& description = QString())
  {
    QString xmlParameter;
    QTextStream str(&xmlParameter);
    str << "    <" << typeName << ">\n";
    str << "      <name>" << QString("param%1").arg(index) << "</name>\n";
    str << "      <index>" << index << "</index>\n";
    str << "      <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
    str << "      " << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "\n";
    str << "      <channel>input</channel>\n";
    str << "    </" << typeName << ">\n";
    return xmlParameter;
  }
};

}
}

Q_DECLARE_METATYPE(QList<int>*)

struct ctkCmdLineModuleBackendFunctionPointerPrivate;

/**
 * \class ctkCmdLineModuleBackendFunctionPointer
 * \brief Provides a back-end implementation to enable directly calling a function pointer.
 * \ingroup CommandLineModulesBackendFunctionPointer_API
 *
 * \warning This back-end is highly experimental and will not work for most function pointers when
 *          trying to register them via registerFunctionPointer().
 */
class CTK_CMDLINEMODULEBACKENDFP_EXPORT ctkCmdLineModuleBackendFunctionPointer : public ctkCmdLineModuleBackend
{

public:

  class DescriptionPrivate;

  class Description
  {
  public:

    Description();
    ~Description();

    QUrl moduleLocation() const;

    QString moduleCategory() const;
    void setModuleCategory(const QString &category);

    QString moduleTitle() const;
    void setModuleTitle(const QString& title);

    QString moduleDescription() const;
    void setModuleDescription(const QString& description);

    QString moduleVersion() const;
    void setModuleVersion(const QString& version);

    QString moduleContributor() const;
    void setModuleContributor(const QString& contributor);

  private:

    friend class ctkCmdLineModuleBackendFunctionPointer;
    friend class ctkCmdLineModuleFunctionPointerTask;
    Description(const QUrl& location, const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& fpProxy);

    QSharedPointer<DescriptionPrivate> d;

  };

  ctkCmdLineModuleBackendFunctionPointer();
  ~ctkCmdLineModuleBackendFunctionPointer();

  virtual QString name() const;
  virtual QString description() const;

  virtual QList<QString> schemes() const;

  virtual qint64 timeStamp(const QUrl &location) const;

  virtual QByteArray rawXmlDescription(const QUrl& location, int timeout);

  QList<QUrl> registeredFunctionPointers() const;

  template<typename A>
  Description* registerFunctionPointer(const QString& title, void (*fp)(A),
                                       const QString& paramLabel = QString(), const QString& paramDescr = QString())
  {
    typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits::RawType RawTypeA;

    QList<QString> params;
    params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeA>::
              parameter(0,
                        ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeA>(),
                        paramLabel, paramDescr);
    return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
  }

  template<typename A, typename B>
  Description* registerFunctionPointer(const QString& title, void (*fp)(A,B),
                                       const QString& paramLabel0 = QString(), const QString& paramDescr0 = QString(),
                                       const QString& paramLabel1 = QString(), const QString& paramDescr1 = QString())
  {
    typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits::RawType RawTypeA;
    typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits::RawType RawTypeB;

    QList<QString> params;
    params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeA>::
              parameter(0,
                        ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeA>(),
                        paramLabel0, paramDescr0);
    params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeB>::
              parameter(1,
                        ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeB>(),
                        paramLabel1, paramDescr1);
    return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
  }

protected:

  virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* frontend);

  virtual QList<QVariant> arguments(ctkCmdLineModuleFrontend* frontend) const;

private:

  Description* registerFunctionPointerProxy(const QString &title,
                                            const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& proxy,
                                            const QList<QString>& params);

  QScopedPointer<ctkCmdLineModuleBackendFunctionPointerPrivate> d;

};

#endif // CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H


text:
from setuptools import setup, find_packages

setup(
    name='registry_scraper',
    version='0.1.0',
    packages=find_packages(exclude=['tests']),
    install_requires=[
        'boto==2.38.0'
    ],
    entry_points={
        'console_scripts': [
            'scrape = registry_scraper.__main__:main'
        ]
    }
)


text:
WiP - Zip Recruiter PHP Library
===============================
[![Build Status](https://travis-ci.org/caseyw/ziprecruiter-php.svg?branch=master)](https://travis-ci.org/caseyw/ziprecruiter-php)

Small project needed to interface with Zip Recruiter.

Please stand by. Docs to come.


text:
@charset "UTF-8";

/*  Layout
-----------------------------------------------------------------------------*/
/* Body */
body {
  font-family:"wf_SegoeUILight","wf_SegoeUI","Segoe UI Light","Segoe WP Light","Segoe UI","Segoe","Segoe WP","Tahoma","Verdana","Arial","sans-serif";
  font-size: 18px;
  line-height: 100%;
  margin: 0;
  color: #e8fdff;
}

/* Wrapper */
#wrapper {
  margin: 0 auto;
  width: 100%;
}

/* Header */
#header {

}

#header #header_top{
height:70px;
width:100%;
display:block;
background:#3e4095;
}
#header #header_right{
width : 50%;
float:right;
}
#header_top #header_top_content{
width:80%;
min-width:1000px;
margin : 0px auto;
}
#header #header_left{
width : 50%;
float:left;
height: 70px;
}
#header #header_left a{
	height: 70px;
	line-height: 84px;
	font-size: 14px;
}
#header #header_left a img{
padding :19px 5px 19px 0px;
float: left
}
#header #header_right .header_navi{
float: right;
display:block;
padding :10px 0px;
}
#header #header_right .header_navi li{
 float: left;
 height : 50px;
 line-height:50px;
 padding : 0px 15px;
 opacity:1;
 cursor: pointer;
}
#header #header_right .header_navi li:hover,#header #header_left .header_navi li.active{
 opacity:0.7;
}
#header #header_right .header_navi li a{
/*color : #fff;*/
font-size : 18px;
line-height: 18px;
}
#header #header_right .header_navi .small{
padding : 0px 4px!important;
}
#header #header_right .header_navi .line_left{
border-left: 1px solid #fff;
 padding : 0px 4px 0px 15px!important;
}
#header #header_right .header_navi .small a img{
vertical-align:middle;

}
#header #header_right .header_navi .end{
padding-right:0px!important;
}
#header #header_bottom{
	height:51px;
	width:100%;
	display:block;
	position: absolute;
	z-index: 10;
}
#header_bottom #header_bottom_content{
width:1000px;
margin : 0px auto;
}
#header_bottom .header_navi{
	width: 100%;
	height: 50px;
	padding-top: 1px;
}
#header_bottom .header_navi li{
	float: left;
	width: 142px;
	margin-right: 1px;
	height: 50px;
	line-height: 50px;
	cursor: pointer;
	text-align: center;
	opacity: 1;

}
#header_bottom .header_navi li a{
	/*color: #e8fdff;*/
}
#header_bottom .header_navi li,#header_bottom .header_navi li.active{
	opacity: 0.7;
}
#header_bottom .header_navi li.end{
	margin-right:0px;
}
#header_bottom .header_navi li.pink{
	background: rgba(237,20,91,0.5);
}
#header_bottom .header_navi li.pink:hover{
	background: rgba(237,20,91,0.3);
}
#header_bottom .header_navi li.violet{
	background: rgba(102,45,145,0.5);
}
#header_bottom .header_navi li.violet:hover{
	background: rgba(102,45,145,0.3);
}
#header_bottom .header_navi li.blue{
	background: rgba(0,174,239,0.5);
}
#header_bottom .header_navi li.blue:hover{
	background: rgba(0,174,239,0.3);
}
#header_bottom .header_navi li.green{
	background: rgba(57,181,74,0.5);
}
#header_bottom .header_navi li.green:hover{
	background: rgba(57,181,74,0.3);
}
#header_bottom .header_navi li.yellow{
	background: rgba(107,160,0,0.5);
}
#header_bottom .header_navi li.yellow:hover{
	background: rgba(107,160,0,0.3);
}
#header_bottom .header_navi li.orange{
	background: rgba(242,101,32,0.5);
}
#header_bottom .header_navi li.orange:hover{
	background: rgba(242,101,32,0.3);
}
#header_bottom .header_navi li.red{
	background: rgba(219,15,23,0.5);
}
#header_bottom .header_navi li.red:hover{
	background: rgba(219,15,23,0.3);
}
/*slideshow*/
#slideshow{
	width: 100%;
	/*height: 435px;*/
}
#slideshow #slideshow_content:hover .control_manager{
	zoom: 1;
	opacity: 1;

}
#slideshow #slideshow_content{
	width: 1000px;
	margin: 0px auto;
	position: relative;
	cursor: pointer;
}
#slideshow_inner{
	width: 100%;
	display: block;
	position: relative;
	height: 435px;
}
#slideshow_content #slideshow_inner p{
	position: absolute;
	left: 0px;

}

#slideshow_control .control_manager{
	bottom: 170px;
	position: absolute;
	z-index: 10;
	cursor: pointer;
	opacity: 0;
	-webkit-transition: opacity 0.7s ease-in-out;
    -moz-transition: opactiy 0.7s ease-in-out;
    -ms-transition: opacity 0.7s ease-in-out;
    -o-transition: opacity 0.7s ease-in-out;
    transition: opacity 0.7s ease-in-out;
	/*display: none;*/
}
#slideshow_control .control_right{
	right: 18px;
}
#slideshow_control .control_left{
	left: 18px;
}
#slideshow_control .control_navi{

}
#slideshow_control #control_inner{
	bottom:0px;
	position: absolute;
	z-index: 10;
	width: 1000px;
	/*bottom: 0px;*/
}
#slideshow_control .control_navi{
	margin: 0 auto;
	/*width: 115px;*/
	height: 20px;
	display: block;
}
#slideshow_control .control_navi li{
	text-indent: -9999px;
	cursor: pointer;
	width: 13px;
	height: 13px;
	float: left;
	margin: 0px 5px;
	background: url(../../images/Epoint_bg01.png) top left no-repeat;
}
#slideshow_control .control_navi li:hover,#slideshow_control .control_navi li.active{
	background: url(../../images/Epoint_bg02.png) top left no-repeat;
}
/* Footer */
#footer {
width: 100%;
height: 70px;
background: #3e4095;
}
#footer #footer_contents{
	width: 1000px;
	margin: 0 auto;
}
#left_footer{
	width: 500px;
	float: left;
}
#left_footer a{
	color: #ffe6bb;
	height: 70px;
	line-height: 70px;
}
#right_footer{
	width: 500px;
	float: right;
}
#right_footer .navi_footer{
	height: 48px;
	margin: 11px 0px;
}
#right_footer .navi_footer li {
	width: 48px;
	margin-left: 15px;
	float: right;
}
/* Main */
#main {
	width: 100%;
}
#main_content{
	width: 1000px;
	margin: 0 auto;
}
#main_left{

}
.


text:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--

-->
<configuration>
<property>
    <name>hbase.master</name>
    <value>128.122.80.149:60000</value>
</property>
<property>
    <name>hbase.rootdir</name>
    <value>hdfs://128.122.80.149:54310/hbase</value>
</property>
<property>
    <name>hbase.cluster.distributed</name>
    <value>true</value>
</property>
<property>
        <name>dfs.replication</name>
        <value>1</value>
</property>
<property>
	<name>hbase.zookeeper.quorum</name>
	<value>128.122.80.149</value>
</property>
<property>
	<name>hbase.zookeeper.property.clientPort</name>
	<value>2181</value>
</property>
</configuration>


text:
var repo = {
    select: function (id) {
        console.log('Getting task ' + id);
        return {
            name: 'new task from db'
        }
    },
    save: function (task) {
        console.log('Saving ' + task.name + ' to the db');
    }

}

repo.execute = function(name){
    var args = Array.prototype.slice.call(arguments, 1);

    if(repo[name]){
        return repo[name].apply(repo, args)
    }
    if(name==='get'){
        return repo['select'].apply(repo, args)
    }
    return false;
};

var task = repo.execute('get', 1);
console.log(task);


text:
:man_page: mongoc_uri_get_read_concern

mongoc_uri_get_read_concern()
=============================

Synopsis
--------

.. code-block:: c

  const mongoc_read_concern_t *
  mongoc_uri_get_read_concern (const mongoc_uri_t *uri);

Parameters
----------

* ``uri``: A :symbol:`mongoc_uri_t`.

Description
-----------

Fetches a read concern that is owned by the URI instance. This read concern is configured based on URI parameters.

Returns
-------

Returns a :symbol:`mongoc_read_concern_t` that should not be modified or freed.


text:
package fromjava.innerclass;

import jakarta.jws.WebService;
import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 * @author JAX-RPC Development Team
 */
@WebService(targetNamespace="portypeNamespace")
public interface EchoIF extends Remote {
    public Bar echoBar(Bar bar) throws RemoteException;
    public String echoString(String str) throws RemoteException;
}


text:
> **NB:** On Azure Web Apps, Sitecore does not support scaling out App Service Plan due to shared `WWWROOT` directory between instances. Sitecore application has certain types of data that requires persistence in the file system.

After developing a Sitecore solution in Visual Studio, it is possible to manually publish this solution to the Microsoft Azure Cloud Platform.

This article provides a list of techniques that can be used to deploy a Sitecore solution to Microsoft Azure using Microsoft Visual Studio.

> **Important:** It is highly recommended that you get acquainted with the [Compute Hosting Options Provided by Azure](http://azure.microsoft.com/en-us/documentation/articles/fundamentals-application-models/) and [Microsoft Azure Fundamentals](http://www.microsoftvirtualacademy.com/colleges/Azure-fundamentals) before following the instructions in this article.

**Requirements:**
- A work or school account / Microsoft account and a Microsoft Azure subscription with the following Azure services enabled:
  - [Azure Web App](https://msdn.microsoft.com/en-us/library/azure/dn948515.aspx)
  - [Azure Storage](https://msdn.microsoft.com/en-us/library/azure/gg433040.aspx)
  - [Azure SQL Database](https://msdn.microsoft.com/en-us/library/azure/ee336279.aspx)
- Microsoft Visual Studio 2013 / 2015
- Microsoft Azure SDK 2.8.2 for .NET or newer
- Microsoft SQL Server Management Studio 2014 or newer
- MongoDB 2.6.1
- Sitecore® Experience Platform™ 8.0 rev. 160115 (8.0 Update-7) or higher

> **Note:** To download the latest version of the Microsoft Azure SDK and Tool for Visual Studio, follow this link: http://azure.microsoft.com/en-us/downloads/

## Instructions

The recommended approach to deploy a Sitecore solution to Microsoft Azure using Visual Studio is as follows:

1. Log in to the **Microsoft Azure Portal** using the https://portal.azure.com URL.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-01.png)

2. In the **Jumpbar**, click the **New** button, then select the **Web + Mobile** section and click the **Web App** button. The **Web App** blade appears.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-02.png)

3. In the **Web App** blade, fill in the **URL** field and configure the other section if needed, then click the **Create** button.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-03.png)

4. In the **Startboard**, click on the **Web App** tile.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-04.png)

5. In the **Web App** blade, click the **All settings** button and select the **Application settings** section.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-05.png)

6. In the **Application settings** blade, configure the following groups and save the changes.

   - In the **General settings** group:
     + Set the **PHP version** switcher to the **Off** value.
     + Set the **Platform** switcher to the the **64-bit** value.
     + Set the **Always On** switcher to the the **On** value.
     + \[Optional\] Set the **Remote debugging** switcher to the **On** value and select the Visual Studio version.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-06.png)

   - In the **Connection strings** group:
     + Add the `core`, `master`, `web`, `reporting` and `session` SQL Database connection strings.
     + Add the `analytics`, `tracking.live`, `tracking.history` and `tracking.contact` MongoDB connection strings.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/AzurePortal-WebApp-07.png)

   > **Note:** For information on deploying Sitecore databases to Azure, see the section [How to deploy Sitecore databases to Azure SQL Database](how-to-deploy-sitecore-databases-to-azure-sql-database.md).

   ```xml
   Server=tcp:{server-name}.database.windows.net,1433;Database=Sitecore.Core;User ID={server-admin-login}@{server-name};Password={password};Trusted_Connection=False;Encrypt=True
   Server=tcp:{server-name}.database.windows.net,1433;Database=Sitecore.Master;User ID={server-admin-login}@{server-name};Password={password};Trusted_Connection=False;Encrypt=True
   Server=tcp:{server-name}.database.windows.net,1433;Database=Sitecore.Web;User ID={server-admin-login}@{server-name};Password={password};Trusted_Connection=False;Encrypt=True
   Server=tcp:{server-name}.database.windows.net,1433;Database=Sitecore.Reporting;User ID={server-admin-login}@{server-name};Password={password};Trusted_Connection=False;Encrypt=True
   Server=tcp:{server-name}.database.windows.net,1433;Database=Sitecore.Session;User ID={server-admin-login}@{server-name};Password={password};Trusted_Connection=False;Encrypt=True

   mongodb://{user-name}:{password}@{host}/sitecore_analytics
   mongodb://{user-name}:{password}@{host}/sitecore_tracking_lives
   mongodb://{user-name}:{password}@{host}/sitecore_tracking_historys
   mongodb://{user-name}:{password}@{host}/sitecore_tracking_contact
   ```

   > **Note:** Use the above templates as an example of default Sitecore connection strings:
   > - For SQL Database connection strings, replace the `{server-name}` with the name of your Azure SQL Database service. The `{server-admin-login}` and `{password}` with SQL Server account credentials.
   > - For MongoDB connection strings, replace the `{host}` with the URL of your Mongo service. The `{user-name}` and `{password}` with your Mongo account credentials.

7. In the **ASP.NET Web Application** project, include the default Sitecore files, directories and subdirectories.

   > **Note:** For information on creating an ASP.NET Web Application project for Sitecore, see the section [How To Create ASP.NET Web Application Project](how-to-create-aspnet-web-application-project.md).

   - For a **Content Delivery** environment:

     + \App_Browsers
     + \App_Config
     + \App_Data
     + \Areas
     + \bin
     + \Controllers
     + \layouts
     + \Models
     + \sitecore\services
     + \sitecore modules
     + \sitecore_files
     + \temp
     + \upload
     + \Views
     + \xsl
     + \Default.aspx
     + \default.css
     + \default.htm.sitedown
     + \default.js
     + \Global.asax
     + \Web.config
     + \webedit.css

   - For a **Content Management** environment, additionally include the following directories and files:

     + \sitecore\admin
     + \sitecore\Copyright
     + \sitecore\debug
     + \sitecore\images
     + \sitecore\login
     + \sitecore\portal
     + \sitecore\samples
     + \sitecore\service
     + \sitecore\shell
     + \sitecore\blocked.aspx
     + \sitecore\default.aspx
     + \sitecore\no.css

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/VS-01.png)

   > **Important:** Visual Studio may freeze when including the entire `\sitecore\shell` directory at once because it contains a lot of files and subdirectories. Try to split all the subdirectories into portions, and then add them one by one.

8. For a **Content Management** environment, Visual Studio may throw the following build errors:

   ```
   Build: Cannot compile external modules unless the '--module' flag is provided.
   ```

   To address the above error, in the **ASP.NET Web Application** project's properties, switch to the **TypeScript Build** section and set the **Module system** property to the **AMD** value. Otherwise, exclude the following files from the project:
   - \sitecore\shell\client\Speak\Assets\lib\core\1.2\SitecoreSpeak.ts
   - \sitecore\shell\client\Speak\Assets\lib\core\1.2\SitecoreSpeak.d.ts

   ```
   Project file must include the .NET Framework assembly 'WindowsBase, PresentationCore, PresentationFramework' in the reference list.
   ```

   To address the above error, in the **Solution Explorer** window, set the **Build Action** property to the **Content** value for the following item:
   - \sitecore\shell\ClientBin\EmptySplashScreen.xaml

9. In the **Visual Studio**, click the **Tools** -> **NuGet Package Manager** -> **Packages Manager Console**. Run the following command in the **Package Manager Console** window against the **ASP.NET Web Application** project:

   ```PowerShell
   Install-Package Sitecore.Azure.Setup -Version 8.0.2
   ```

10. In the **ASP.NET Web Application** project, right-click the `App_Data` item. Add the `license.xml` and `webdav.lic` files using the **Add** -> **Existing Item...** command in the context menu.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/VS-02.png)

11. In the **ASP.NET Web Application** project, right-click the `Web.Debug.config` and then `Web.Release.config` files. Use the **Preview Transform** command in the context menu to check that all transformations look correct as you expect them to be.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/VS-03.png)

   > **Note:** For more details about Web.config transformation syntax for Web Project Deployment using Visual Studio, see the MSDN website: http://msdn.microsoft.com/en-us/library/dd465326.aspx

12. Right-click the **ASP.NET Web Application** project, and then click the **Publish...**  in the context menu. The **Publish Web** dialog box appears.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/VS-04.png)

13. In the **Publish Web** dialog box, in the **Profile** step, click the **Microsoft Azure Web Apps** option and select the Web App that is created in created in Microsoft Azure Portal.

   ![](./media/how-to-deploy-sitecore-80-solution-to-microsoft-azure-web-app-using-visual-studio/VS-05.png)

   > **Note:** For the basic information about the Publish Web Wizard, see the MSDN website: https://msdn.microsoft.com/en-us/library/dd465337(v=vs.110).aspx