static void cg_json_create_proc()

in sources/cg_json_schema.c [2136:2250]


static void cg_json_create_proc(ast_node *ast, ast_node *misc_attrs) {
  Contract(is_ast_create_proc_stmt(ast));
  EXTRACT_ANY_NOTNULL(name_ast, ast->left);
  EXTRACT_STRING(name, name_ast);
  EXTRACT_NOTNULL(proc_params_stmts, ast->right);
  EXTRACT(params, proc_params_stmts->left);
  EXTRACT(stmt_list, proc_params_stmts->right);

  // shared fragments are invisible to the JSON or anything else, they have
  // no external interface.
  uint32_t frag_type = find_proc_frag_type(ast);
  if (frag_type == FRAG_TYPE_SHARED)  {
    return;
  }

  CHARBUF_OPEN(param_buffer);
  charbuf *output = &param_buffer;

  bprintf(output, "\"name\" : \"%s\",\n", name);

  CHARBUF_OPEN(tmp);
    // quote the file as a json style literaj
    CSTR filename = name_ast->filename;
    #ifdef _WIN32
    CSTR slash = strrchr(filename, '\\');
    #else
    CSTR slash = strrchr(filename, '/');
    #endif
    if (slash) {
      filename = slash + 1;
    }
    cg_encode_json_string_literal(filename, &tmp);
    bprintf(output, "\"definedInFile\" : %s,\n",  tmp.ptr);
  CHARBUF_CLOSE(tmp);

  bool_t simple = cg_parameters(output, ast);

  cg_json_dependencies(output, ast);

  if (ast->sem->region) {
    cg_json_emit_region_info(output, ast);
  }

  if (misc_attrs) {
    bprintf(output, ",\n");
    cg_json_misc_attrs(output, misc_attrs);
  }

  if (!stmt_list) {
    output = general;
    cg_json_general_proc(ast, misc_attrs, param_buffer.ptr);
    goto cleanup;
  }

  EXTRACT_STMT_AND_MISC_ATTRS(stmt, nested_misc_attrs, stmt_list);

  // if more than one statement it isn't simple
  if (stmt_list->right) {
    simple = 0;
  }

  // we have to see if it uses shared fragments, this can't be "simple"
  // because the parameters can be synthetic and require assignments and such
  if (simple && is_select_stmt(stmt)) {
    found_shared_fragment = false;
    CHARBUF_OPEN(scratch);
    cg_json_select_stmt(&scratch, stmt); // easy way to walk the tree
    CHARBUF_CLOSE(scratch);
    simple = !found_shared_fragment;
  }

  if (simple && is_select_stmt(stmt)) {
    output = queries;
    cg_begin_proc(output, ast, misc_attrs);
    BEGIN_INDENT(proc, 2);
    bprintf(output, "%s", param_buffer.ptr);
    cg_json_projection(output, stmt);
    cg_json_select_stmt(output, stmt);
    END_INDENT(proc);
  }
  else if (simple && is_insert_stmt(stmt)) {
    bool_t simple_insert = is_simple_insert(stmt);

    output = simple_insert ? inserts : general_inserts;
    cg_begin_proc(output, ast, misc_attrs);
    BEGIN_INDENT(proc, 2);
    bprintf(output, "%s", param_buffer.ptr);
    cg_json_insert_stmt(output, stmt, simple_insert);
    END_INDENT(proc);
  }
  else if (simple && is_delete_stmt(stmt)) {
    output = deletes;
    cg_begin_proc(output, ast, misc_attrs);
    BEGIN_INDENT(proc, 2);
    bprintf(output, "%s", param_buffer.ptr);
    cg_json_delete_stmt(output, stmt);
    END_INDENT(proc);
  }
  else if (simple && is_update_stmt(stmt)) {
    output = updates;
    cg_begin_proc(output, ast, misc_attrs);
    BEGIN_INDENT(proc, 2);
    bprintf(output, "%s", param_buffer.ptr);
    cg_json_update_stmt(output, stmt);
    END_INDENT(proc);
  }
  else {
    output = general;
    cg_json_general_proc(ast, misc_attrs, param_buffer.ptr);
  }

cleanup:
  CHARBUF_CLOSE(param_buffer);
  cg_end_proc(output, ast);
}