Пример #1
0
function modify_rows($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'modify_rows';
    # merge opts
    $opts = merge_opts($opts, $pipe);
    # get data opt
    $data = get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_set_type($cmd, $data, 'data', 'array')) {
        return false;
    }
    # get fields opt
    $res_fields = get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_set_type($cmd, $res_fields, 'fields', 'array_of_strings')) {
        return false;
    }
    # get limit opt
    $limit = get_opt_config_value($prefix, $opts, 'limit');
    if (!check_opt_set_type($cmd, $limit, 'limit', 'integer')) {
        return false;
    }
    # get limit opt
    $offset = get_opt_config_value($prefix, $opts, 'offset', 0);
    if (!check_opt_set_type($cmd, $offset, 'offset', 'integer')) {
        return false;
    }
    # display message
    debug_echo($cmd, "modifying rows (limit = {$limit}, offset = {$offset})");
    # set up end
    $end = $offset + $limit;
    # process the data
    $res_data = array();
    for ($i = $offset; $i < $end; $i++) {
        $res_data[] = $data[$i];
    }
    # build response object
    return build_result_data($res_fields, $res_data);
}
Пример #2
0
function build_source_data($prefix, $opts, $cmd)
{
    # get data opt
    $data =& get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_if_set_type($cmd, $data, 'type', 'array')) {
        return false;
    }
    # get fields opt
    $fields =& get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_if_set_type($cmd, $fields, 'type', 'array_of_strings')) {
        return false;
    }
    # get indexes opt
    $indexes =& get_opt_config_value($prefix, $opts, 'indexes');
    if (!check_opt_if_set_type($cmd, $indexes, 'type', 'array')) {
        return false;
    }
    # check if data and fields are set
    if (!is_array($data) || !is_array($fields)) {
        return false;
    }
    return build_result_data($fields, $data, $indexes);
}
Пример #3
0
function load_data_from_doc_file($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = $cmd;
    # merge opts
    $opts = merge_opts($opts, $pipe, 'doc_type');
    # get file opt
    $file = get_opt($prefix, $opts, 'file');
    if (!check_opt_set_type($cmd, $file, 'file', 'string')) {
        return false;
    }
    # get defined fields opt
    $defined_fields = get_opt($prefix, $opts, 'defined_fields');
    if (!check_opt_if_set_type($cmd, $defined_fields, 'defined_fields', 'array_of_strings')) {
        return false;
    }
    # get save fields
    $save_fields = get_opt($prefix, $opts, 'save_fields');
    if (!check_opt_if_set_type($cmd, $save_fields, 'save_fields', 'array_of_strings')) {
        return false;
    }
    # get doc type opt
    $doc_type = get_opt($prefix, $opts, 'doc_type', 'json');
    if (!check_opt_set_type($cmd, $doc_type, 'doc_type', 'document_file_type')) {
        return false;
    }
    $doc_type_upper = strtoupper($doc_type);
    # get data structure opt
    $data_structure = get_opt($prefix, $opts, 'data_structure', 'list_of_objects');
    if (!check_opt_set_type($cmd, $data_structure, 'data_structure', 'data_structure_type')) {
        return false;
    }
    # get limit opt
    $limit = get_opt($prefix, $opts, 'limit', 0);
    if (!check_opt_set_type($cmd, $limit, 'limit', 'positive_integer')) {
        return false;
    }
    # get offset opt
    $offset = get_opt($prefix, $opts, 'offset', 0);
    if (!check_opt_set_type($cmd, $offset, 'offset', 'positive_integer')) {
        return false;
    }
    # check the file exists
    if (!file_exists($file)) {
        return error($cmd, "file does not exist : {$file}");
    }
    # display generic info about the data
    $data_structure_str = str_replace('_', ' ', $data_structure);
    debug_echo($cmd, "creating data from {$doc_type_upper} file : {$file}");
    debug_echo($cmd, "source data of type '{$data_structure_str}'");
    # read the file into memory
    $data_str = @file_get_contents($file);
    if (!is_string($data_str)) {
        return error($cmd, "could not read file : {$file}");
    }
    # load data depending on type
    switch ($doc_type) {
        case 'json':
            $data = @json_decode($data_str, true);
            break;
        case 'yaml':
            $data = @yaml_decode($data_str);
            break;
    }
    if (!$data) {
        return error($cmd, "invalid {$doc_type_upper} in file : {$file}");
    }
    $data_count = count($data);
    # set up end point
    if ($limit == 0) {
        $end = $data_count;
    } else {
        $end = $offset + $limit;
    }
    # set up the fields and build data
    $res_data = array();
    switch ($data_structure) {
        case 'list_of_columns':
            # set up fields
            if ($defined_fields) {
                $defined_fields_source = '(from the config)';
            } else {
                $defined_fields = array_shift($data);
                $defined_fields_source = '(from the source file)';
            }
            # set up response field indexes
            if ($save_fields) {
                $res_fields = $save_fields;
                $res_field_count = count($res_fields);
                $res_field_indexes = build_field_indexes($defined_fields, $res_fields, $cmd);
                if ($res_field_indexes === false) {
                    return;
                }
            } else {
                $res_fields = $defined_fields;
            }
            # display info about the fields
            debug_echo($cmd, "defined fields {$defined_fields_source} : ");
            debug_dump_list($defined_fields, true);
            if ($save_fields) {
                debug_echo($cmd, "saved fields :");
                debug_dump_list($save_fields, true);
            }
            # gather the data
            # TODO: change for having offset / limit
            $res_data = array();
            if ($save_fields) {
                $res_data = array();
                for ($i = $offset; $i < $end; $i++) {
                    $line = $data[$i];
                    $new_line = array();
                    for ($j = 0; $j < $res_field_count; $j++) {
                        $new_line[] = $line[$res_field_indexes[$j]];
                    }
                    $res_data[] = $new_line;
                }
            } elseif ($offset == 0 && $limit == 0) {
                $res_data = $data;
            } else {
                $res_data = array();
                for ($i = $offset; $i < $end; $i++) {
                    $res_data[] = $data[$i];
                }
            }
            break;
        case 'list_of_objects':
            $res_data = array();
            if ($save_fields) {
                # set up result fields
                $res_fields = $save_fields;
                $res_fields_count = count($res_fields);
                # display info about what fields will be saved
                debug_echo($cmd, "saved fields :");
                debug_dump_list($res_fields, true);
                # build the data
                for ($i = $offset; $i < $end; $i++) {
                    $obj = $data[$i];
                    $new_line = array();
                    for ($j = 0; $j < $res_fields_count; $j++) {
                        $new_line[] = @$obj[$res_fields[$j]];
                    }
                    $res_data[] = $new_line;
                }
                $res_fields = $save_fields;
            } else {
                # display info about saved files
                debug_echo($cmd, "saving all fields - they will be listed as they are added");
                # build the data
                $res_fields = array();
                $res_fields_count = 0;
                for ($i = $offset; $i < $end; $i++) {
                    $obj = $data[$i];
                    $new_line = array();
                    # save all the existing data in order
                    for ($j = 0; $j < $res_fields_count; $j++) {
                        $new_line[] = @$obj[$res_fields[$j]];
                    }
                    # index and save any new fields
                    $res_fields_added = array();
                    foreach ($obj as $key => $value) {
                        if (in_array($key, $res_fields)) {
                            continue;
                        }
                        $res_fields_added[] = $key;
                        $res_fields[] = $key;
                        $res_fields_count++;
                        $new_line[] = $value;
                    }
                    $res_data[$i] = $new_line;
                    # display list of fields added (if any)
                    if ($res_fields_added) {
                        $line_no = $i + 1;
                        debug_echo($cmd, "the following fields were added on row {$line_no} :");
                        debug_dump_list($res_fields_added, true);
                    }
                }
                # add back in any empty fields
                for ($i = 0; $i < count($res_data); $i++) {
                    $line =& $res_data[$i];
                    $line_fields_count = count($line);
                    if ($line_fields_count == $res_fields_count) {
                        break;
                    }
                    for ($j = $line_fields_count; $j < $res_fields_count; $j++) {
                        $line[] = '';
                    }
                }
            }
            break;
    }
    # detail results
    $line_count = count($res_data);
    debug_echo($cmd, "creation of data from JSON file complete ({$line_count} lines processed)");
    # create result
    return build_result_data($res_fields, $res_data);
}
Пример #4
0
function modify_fields($opts, $pipe, $cmd = __FUNCTION__, $opt_prefix = false)
{
    # set prefix
    $prefix = 'modify_fields';
    # merge opts
    $opts = merge_opts($opts, $pipe);
    # adjust opt prefix
    $cmd = adjust_opt_prefix($cmd, $opts, $opt_prefix, 'add_fields');
    # get data opt
    $data =& get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_set_type($cmd, $data, 'data', 'array')) {
        return false;
    }
    # get fields opt
    $fields = get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_set_type($cmd, $fields, 'fields', 'array_of_strings')) {
        return false;
    }
    # get indexes fields opt
    $indexes =& get_opt_config_value($prefix, $opts, 'indexes');
    if (!check_opt_if_set_type($cmd, $indexes, 'indexes', 'array')) {
        return false;
    }
    # get map fields opt
    $map_fields = get_opt_config_value($prefix, $opts, 'map');
    if (!check_opt_if_set_type($cmd, $map_fields, 'map', 'string,array')) {
        return false;
    }
    if (is_string($map_fields)) {
        $map_fields = yaml_decode($map_fields);
    }
    # get swap fields opt
    $swap_fields = get_opt_config_value($prefix, $opts, 'swap');
    if (!check_opt_if_set_type($cmd, $swap_fields, 'swap', 'string,array')) {
        return false;
    }
    if (is_string($swap_fields)) {
        $swap_fields = yaml_decode($swap_fields);
    }
    # get add fields opt
    $add_fields = get_opt_config_value($prefix, $opts, 'add');
    if (!check_opt_if_set_type($cmd, $add_fields, 'add', 'array')) {
        return false;
    }
    # get delete fields opt
    $delete_fields = get_opt_config_value($prefix, $opts, 'delete');
    if (!check_opt_if_set_type($cmd, $delete_fields, 'delete', 'array_of_strings')) {
        return false;
    }
    # get reorder fields opt
    $reorder_fields = get_opt_config_value($prefix, $opts, 'reorder');
    if (!check_opt_if_set_type($cmd, $reorder_fields, 'reorder', 'array_of_strings')) {
        return false;
    }
    # perform the operations
    if ($map_fields && !map_data_fields($data, $fields, $map_fields, $cmd)) {
        return false;
    }
    if ($swap_fields && !swap_data_fields($fields, $swap_fields, $cmd)) {
        return false;
    }
    if ($add_fields && !add_data_fields($data, $fields, $add_fields, $cmd)) {
        return false;
    }
    if ($delete_fields && !delete_data_fields($data, $fields, $delete_fields, $cmd)) {
        return false;
    }
    if ($reorder_fields && !reorder_data_fields($data, $fields, $reorder_fields, $cmd)) {
        return false;
    }
    # create indexes
    /*
    var_dump ($fields);  
    var_dump ($data[0]);
    exit;
    */
    # set the response
    return build_result_data($fields, $data, $indexes);
}