bool TRI_ValueStringAql (TRI_string_buffer_t* const buffer,
                         const TRI_aql_value_t* const value,
                         const TRI_aql_value_type_e type) {
  switch (type) {
    case TRI_AQL_TYPE_FAIL:
      return (TRI_AppendStringStringBuffer(buffer, "fail") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_NULL:
      return (TRI_AppendStringStringBuffer(buffer, "null") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_BOOL:
      return (TRI_AppendStringStringBuffer(buffer, value->_value._bool ? "true" : "false") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_INT:
      return (TRI_AppendInt64StringBuffer(buffer, value->_value._int) == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_DOUBLE:
      return (TRI_AppendDoubleStringBuffer(buffer, value->_value._double) == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_STRING: {
      if (TRI_AppendCharStringBuffer(buffer, '"') != TRI_ERROR_NO_ERROR) {
        return false;
      }

      if (TRI_AppendStringStringBuffer(buffer, value->_value._string) != TRI_ERROR_NO_ERROR) {
        return false;
      }

      return (TRI_AppendCharStringBuffer(buffer, '"') == TRI_ERROR_NO_ERROR);
    }
  }

  return false;
}
Exemple #2
0
char* TRI_StringDouble (double value) {
  TRI_string_buffer_t buffer;

  TRI_InitStringBuffer(&buffer, TRI_CORE_MEM_ZONE);
  TRI_AppendDoubleStringBuffer(&buffer, value);

  return buffer._buffer;
}
bool TRI_ValueJavascriptAql (TRI_string_buffer_t* const buffer,
                             const TRI_aql_value_t* const value,
                             const TRI_aql_value_type_e type) {
  switch (type) {
    case TRI_AQL_TYPE_FAIL:
      return (TRI_AppendStringStringBuffer(buffer, "fail") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_NULL:
      return (TRI_AppendStringStringBuffer(buffer, "null") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_BOOL:
      return (TRI_AppendStringStringBuffer(buffer, value->_value._bool ? "true" : "false") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_INT:
      return (TRI_AppendInt64StringBuffer(buffer, value->_value._int) == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_DOUBLE:
      return (TRI_AppendDoubleStringBuffer(buffer, value->_value._double) == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_STRING: {
      char* escapedString;
      size_t outLength;

      if (TRI_AppendCharStringBuffer(buffer, '"') != TRI_ERROR_NO_ERROR) {
        return false;
      }

      escapedString = TRI_EscapeUtf8StringZ(TRI_UNKNOWN_MEM_ZONE,
                                            value->_value._string,
                                            strlen(value->_value._string),
                                            false,
                                            &outLength);
      if (escapedString == NULL) {
        return false;
      }

      if (TRI_AppendStringStringBuffer(buffer, escapedString) != TRI_ERROR_NO_ERROR) {
        TRI_Free(TRI_UNKNOWN_MEM_ZONE, escapedString);

        return false;
      }

      TRI_Free(TRI_UNKNOWN_MEM_ZONE, escapedString);

      return (TRI_AppendCharStringBuffer(buffer, '"') == TRI_ERROR_NO_ERROR);
    }
  }

  return false;
}
int TRI_AppendCsvDoubleStringBuffer (TRI_string_buffer_t * self, double d) {
  int res;

  res = TRI_AppendDoubleStringBuffer(self, d);

  if (res != TRI_ERROR_NO_ERROR) {
    return res;
  }

  res = Reserve(self, 1);

  if (res != TRI_ERROR_NO_ERROR) {
    return res;
  }

  AppendChar(self, ';');
  return TRI_ERROR_NO_ERROR;
}
bool TRI_ValueJavascriptAql (TRI_string_buffer_t* const buffer,
                             const TRI_aql_value_t* const value,
                             const TRI_aql_value_type_e type) {
    switch (type) {
    case TRI_AQL_TYPE_FAIL: {
        return (TRI_AppendString2StringBuffer(buffer, "fail", 4) == TRI_ERROR_NO_ERROR);
    }

    case TRI_AQL_TYPE_NULL: {
        return (TRI_AppendString2StringBuffer(buffer, "null", 4) == TRI_ERROR_NO_ERROR);
    }

    case TRI_AQL_TYPE_BOOL: {
        if (value->_value._bool) {
            return (TRI_AppendString2StringBuffer(buffer, "true", 4) == TRI_ERROR_NO_ERROR);
        }
        else {
            return (TRI_AppendString2StringBuffer(buffer, "false", 5) == TRI_ERROR_NO_ERROR);
        }
    }

    case TRI_AQL_TYPE_INT: {
        return (TRI_AppendInt64StringBuffer(buffer, value->_value._int) == TRI_ERROR_NO_ERROR);
    }

    case TRI_AQL_TYPE_DOUBLE: {
        return (TRI_AppendDoubleStringBuffer(buffer, value->_value._double) == TRI_ERROR_NO_ERROR);
    }

    case TRI_AQL_TYPE_STRING: {
        if (TRI_AppendCharStringBuffer(buffer, '"') != TRI_ERROR_NO_ERROR) {
            return false;
        }

        if (TRI_AppendJsonEncodedStringStringBuffer(buffer, value->_value._string, false) != TRI_ERROR_NO_ERROR) {
            return false;
        }

        return (TRI_AppendCharStringBuffer(buffer, '"') == TRI_ERROR_NO_ERROR);
    }
    }

    return false;
}
Exemple #6
0
void TRI_ConvertQueryJavascript (TRI_query_javascript_converter_t* converter, 
                                 const TRI_query_node_t* const node,
                                 TRI_associative_pointer_t* bindParameters) {
  TRI_query_node_t* lhs;
  TRI_query_node_t* rhs;
  TRI_bind_parameter_t* parameter;
  char* escapedString;
  size_t outLength;

  assert(converter);
  assert(bindParameters);

  if (!node) {
    return;
  }

  lhs = node->_lhs;
  rhs = node->_rhs;

  switch (node->_type) {
    case TRI_QueryNodeValueUndefined:
      TRI_AppendStringStringBuffer(converter->_buffer, "undefined");
      return;
    case TRI_QueryNodeValueNull:
      TRI_AppendStringStringBuffer(converter->_buffer, "null");
      return;
    case TRI_QueryNodeValueBool:
      TRI_AppendStringStringBuffer(converter->_buffer, 
                                   node->_value._boolValue ? "true" : "false");
      return;
    case TRI_QueryNodeValueString:
      TRI_AppendCharStringBuffer(converter->_buffer, '"');
      escapedString = TRI_EscapeUtf8String(
        node->_value._stringValue, 
        strlen(node->_value._stringValue), 
        false, 
        &outLength
      );
      if (escapedString) {
        TRI_AppendStringStringBuffer(converter->_buffer, escapedString);
        TRI_Free(escapedString); 
      }
      TRI_AppendCharStringBuffer(converter->_buffer, '"');
      return;
    case TRI_QueryNodeValueNumberInt:
      TRI_AppendInt64StringBuffer(converter->_buffer, node->_value._intValue);
      return;
    case TRI_QueryNodeValueNumberDouble:
      TRI_AppendDoubleStringBuffer(converter->_buffer, node->_value._doubleValue);
      return;
    case TRI_QueryNodeValueNumberDoubleString:
      TRI_AppendStringStringBuffer(converter->_buffer, node->_value._stringValue);
      return;
    case TRI_QueryNodeValueArray:
      TRI_AppendCharStringBuffer(converter->_buffer, '[');
      TRI_WalkListQueryJavascript(converter, rhs, bindParameters, ',', 0);
      TRI_AppendCharStringBuffer(converter->_buffer, ']');
      return; 
    case TRI_QueryNodeValueDocument:
      TRI_AppendCharStringBuffer(converter->_buffer, '{');
      TRI_WalkListQueryJavascript(converter, rhs, bindParameters, ',', 0);
      TRI_AppendCharStringBuffer(converter->_buffer, '}');
      return; 
    case TRI_QueryNodeValueParameterNamed:
      parameter = (TRI_bind_parameter_t*) 
        TRI_LookupByKeyAssociativePointer(bindParameters, 
                                          node->_value._stringValue);
      assert(parameter);  
      TRI_StringifyJson(converter->_buffer, parameter->_data); 
      return;
    case TRI_QueryNodeValueIdentifier:
      TRI_AppendStringStringBuffer(converter->_buffer, 
                                   node->_value._stringValue);
      return;
    case TRI_QueryNodeValueNamedValue:
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      TRI_AppendCharStringBuffer(converter->_buffer, ':');
      TRI_ConvertQueryJavascript(converter, rhs, bindParameters);
      return;
    case TRI_QueryNodeReferenceCollectionAlias:
      if (!converter->_prefix) {
        TRI_AppendStringStringBuffer(converter->_buffer, "$['");
        TRI_AppendStringStringBuffer(converter->_buffer, 
                                     node->_value._stringValue);
        TRI_AppendStringStringBuffer(converter->_buffer, "']");
      }
      else {
        TRI_AppendStringStringBuffer(converter->_buffer, "$['");
        TRI_AppendStringStringBuffer(converter->_buffer, 
                                     converter->_prefix);
        TRI_AppendStringStringBuffer(converter->_buffer, "'].");
        TRI_AppendStringStringBuffer(converter->_buffer, 
                                     node->_value._stringValue);
      }
      return;
    case TRI_QueryNodeUnaryOperatorPlus:
    case TRI_QueryNodeUnaryOperatorMinus:
    case TRI_QueryNodeUnaryOperatorNot:
      TRI_AppendStringStringBuffer(
        converter->_buffer, 
        TRI_QueryNodeGetUnaryOperatorString(node->_type)
      );
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      return;
    case TRI_QueryNodeBinaryOperatorAnd: 
    case TRI_QueryNodeBinaryOperatorOr: 
    case TRI_QueryNodeBinaryOperatorIdentical: 
    case TRI_QueryNodeBinaryOperatorUnidentical: 
    case TRI_QueryNodeBinaryOperatorEqual: 
    case TRI_QueryNodeBinaryOperatorUnequal: 
    case TRI_QueryNodeBinaryOperatorLess: 
    case TRI_QueryNodeBinaryOperatorGreater: 
    case TRI_QueryNodeBinaryOperatorLessEqual:
    case TRI_QueryNodeBinaryOperatorGreaterEqual:
    case TRI_QueryNodeBinaryOperatorAdd:
    case TRI_QueryNodeBinaryOperatorSubtract:
    case TRI_QueryNodeBinaryOperatorMultiply:
    case TRI_QueryNodeBinaryOperatorDivide:
    case TRI_QueryNodeBinaryOperatorModulus:
    case TRI_QueryNodeBinaryOperatorIn:
      TRI_AppendCharStringBuffer(converter->_buffer, '(');
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      TRI_AppendStringStringBuffer(
        converter->_buffer, 
        TRI_QueryNodeGetBinaryOperatorString(node->_type)
      );
      TRI_ConvertQueryJavascript(converter, rhs, bindParameters);
      TRI_AppendCharStringBuffer(converter->_buffer, ')');
      return;
    case TRI_QueryNodeContainerMemberAccess:
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      TRI_WalkListQueryJavascript(converter, rhs, bindParameters, '.', 1);
      return;
    case TRI_QueryNodeContainerTernarySwitch:
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      TRI_AppendCharStringBuffer(converter->_buffer, ':');
      TRI_ConvertQueryJavascript(converter, rhs, bindParameters);
      return;
    case TRI_QueryNodeControlFunctionCall:
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      TRI_AppendCharStringBuffer(converter->_buffer, '(');
      TRI_WalkListQueryJavascript(converter, rhs, bindParameters, ',', 0);
      TRI_AppendCharStringBuffer(converter->_buffer, ')');
      return;
    case TRI_QueryNodeControlTernary:
      TRI_AppendCharStringBuffer(converter->_buffer, '(');
      TRI_ConvertQueryJavascript(converter, lhs, bindParameters);
      TRI_AppendCharStringBuffer(converter->_buffer, '?');
      TRI_ConvertQueryJavascript(converter, rhs, bindParameters);
      TRI_AppendCharStringBuffer(converter->_buffer, ')');
      return;
    default:
      return;
  }
}
Exemple #7
0
static int StringifyJson (TRI_memory_zone_t* zone,
                          TRI_string_buffer_t* buffer, 
                          TRI_json_t const* object,
                          bool braces) {
  size_t n;
  size_t i;
  size_t outLength;
  char* ptr;
  int res;

  switch (object->_type) {
    case TRI_JSON_UNUSED:
      break;

    case TRI_JSON_NULL:
      res = TRI_AppendStringStringBuffer(buffer, "null");

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_BOOLEAN:
      if (object->_value._boolean) {
        res = TRI_AppendStringStringBuffer(buffer, "true");
      }
      else {
        res = TRI_AppendStringStringBuffer(buffer, "false");
      }

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_NUMBER:
      res = TRI_AppendDoubleStringBuffer(buffer, object->_value._number);

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_STRING:
      res = TRI_AppendStringStringBuffer(buffer, "\"");

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      ptr = TRI_EscapeUtf8StringZ(zone, 
                                  object->_value._string.data,
                                  object->_value._string.length - 1, 
                                  false,
                                  &outLength);

      if (ptr == NULL) {
        return TRI_ERROR_OUT_OF_MEMORY;
      }

      res = TRI_AppendString2StringBuffer(buffer, ptr, outLength);

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      TRI_Free(zone, ptr);

      res = TRI_AppendStringStringBuffer(buffer, "\"");

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_ARRAY:
      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "{");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      n = object->_value._objects._length;

      for (i = 0;  i < n;  i += 2) {
        if (0 < i) {
          res = TRI_AppendStringStringBuffer(buffer, ",");

          if (res != TRI_ERROR_NO_ERROR) {
            return res;
          }
        }

        res = StringifyJson(zone, buffer, TRI_AtVector(&object->_value._objects, i), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }

        res = TRI_AppendCharStringBuffer(buffer, ':');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }

        res = StringifyJson(zone, buffer, TRI_AtVector(&object->_value._objects, i + 1), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "}");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;

    case TRI_JSON_LIST:
      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "[");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      n = object->_value._objects._length;

      for (i = 0;  i < n;  ++i) {
        if (0 < i) {
          res = TRI_AppendStringStringBuffer(buffer, ",");

          if (res != TRI_ERROR_NO_ERROR) {
            return res;
          }
        }

        res = StringifyJson(zone, buffer, TRI_AtVector(&object->_value._objects, i), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "]");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;
  }

  return TRI_ERROR_NO_ERROR;
}
Exemple #8
0
static int StringifyJson (TRI_memory_zone_t* zone,
                          TRI_string_buffer_t* buffer,
                          TRI_json_t const* object,
                          bool braces) {
  int res;

  switch (object->_type) {
    case TRI_JSON_UNUSED: {
      break;
    }

    case TRI_JSON_NULL: {
      res = TRI_AppendString2StringBuffer(buffer, "null", 4); // strlen("null")

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;
    }

    case TRI_JSON_BOOLEAN: {
      if (object->_value._boolean) {
        res = TRI_AppendString2StringBuffer(buffer, "true", 4); // strlen("true")
      }
      else {
        res = TRI_AppendString2StringBuffer(buffer, "false", 5); // strlen("false")
      }

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;
    }

    case TRI_JSON_NUMBER: {
      res = TRI_AppendDoubleStringBuffer(buffer, object->_value._number);

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;
    }

    case TRI_JSON_STRING:
    case TRI_JSON_STRING_REFERENCE: {
      res = TRI_AppendCharStringBuffer(buffer, '\"');

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      if (object->_value._string.length > 0) {
        // optimisation for the empty string
        res =  TRI_AppendJsonEncodedStringStringBuffer(buffer, object->_value._string.data, false);

        if (res != TRI_ERROR_NO_ERROR) {
          return TRI_ERROR_OUT_OF_MEMORY;
        }
      }

      res = TRI_AppendCharStringBuffer(buffer, '\"');

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;
    }

    case TRI_JSON_ARRAY: {
      size_t i, n;

      if (braces) {
        res = TRI_AppendCharStringBuffer(buffer, '{');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      n = object->_value._objects._length;

      for (i = 0;  i < n;  i += 2) {
        if (0 < i) {
          res = TRI_AppendCharStringBuffer(buffer, ',');

          if (res != TRI_ERROR_NO_ERROR) {
            return res;
          }
        }

        res = StringifyJson(zone, buffer, (const TRI_json_t*) TRI_AtVector(&object->_value._objects, i), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }

        res = TRI_AppendCharStringBuffer(buffer, ':');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }

        res = StringifyJson(zone, buffer, (const TRI_json_t*) TRI_AtVector(&object->_value._objects, i + 1), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      if (braces) {
        res = TRI_AppendCharStringBuffer(buffer, '}');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;
    }

    case TRI_JSON_LIST: {
      size_t i, n;

      if (braces) {
        res = TRI_AppendCharStringBuffer(buffer, '[');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      n = object->_value._objects._length;

      for (i = 0;  i < n;  ++i) {
        if (0 < i) {
          res = TRI_AppendCharStringBuffer(buffer, ',');

          if (res != TRI_ERROR_NO_ERROR) {
            return res;
          }
        }

        res = StringifyJson(zone, buffer, (const TRI_json_t*) TRI_AtVector(&object->_value._objects, i), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      if (braces) {
        res = TRI_AppendCharStringBuffer(buffer, ']');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;
    }
  }

  return TRI_ERROR_NO_ERROR;
}