[Groonga-commit] pgroonga/pgroonga at d155425 [master] Add pgroonga.command_escape_value()

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Nov 29 23:09:13 JST 2016


Kouhei Sutou	2016-11-29 23:09:13 +0900 (Tue, 29 Nov 2016)

  New Revision: d15542555bacef2cfd75eabf64abbcd10715db36
  https://github.com/pgroonga/pgroonga/commit/d15542555bacef2cfd75eabf64abbcd10715db36

  Message:
    Add pgroonga.command_escape_value()

  Added files:
    src/pgrn_command_escape_value.c
  Modified files:
    pgroonga--1.1.8--1.1.9.sql
    pgroonga.sql
    sources.am
    src/pgroonga.h

  Modified: pgroonga--1.1.8--1.1.9.sql (+7 -0)
===================================================================
--- pgroonga--1.1.8--1.1.9.sql    2016-11-29 18:18:56 +0900 (c186b29)
+++ pgroonga--1.1.8--1.1.9.sql    2016-11-29 23:09:13 +0900 (9af407a)
@@ -1,3 +1,10 @@
+CREATE FUNCTION pgroonga.command_escape_value(value text)
+	RETURNS text
+	AS 'MODULE_PATHNAME', 'pgroonga_command_escape_value'
+	LANGUAGE C
+	IMMUTABLE
+	STRICT;
+
 CREATE FUNCTION pgroonga.query_escape(query text)
 	RETURNS text
 	AS 'MODULE_PATHNAME', 'pgroonga_query_escape'

  Modified: pgroonga.sql (+7 -0)
===================================================================
--- pgroonga.sql    2016-11-29 18:18:56 +0900 (b313fa0)
+++ pgroonga.sql    2016-11-29 23:09:13 +0900 (79d1cc0)
@@ -65,6 +65,13 @@ CREATE FUNCTION pgroonga.flush(indexName cstring)
 	VOLATILE
 	STRICT;
 
+CREATE FUNCTION pgroonga.command_escape_value(value text)
+	RETURNS text
+	AS 'MODULE_PATHNAME', 'pgroonga_command_escape_value'
+	LANGUAGE C
+	IMMUTABLE
+	STRICT;
+
 CREATE FUNCTION pgroonga.query_escape(query text)
 	RETURNS text
 	AS 'MODULE_PATHNAME', 'pgroonga_query_escape'

  Modified: sources.am (+1 -0)
===================================================================
--- sources.am    2016-11-29 18:18:56 +0900 (5f2e587)
+++ sources.am    2016-11-29 23:09:13 +0900 (e4b6755)
@@ -1,6 +1,7 @@
 SRCS =						\
 	src/pgroonga.c				\
 	src/pgrn_column_name.c			\
+	src/pgrn_command_escape_value.c		\
 	src/pgrn_convert.c			\
 	src/pgrn_create.c			\
 	src/pgrn_ctid.c				\

  Added: src/pgrn_command_escape_value.c (+67 -0) 100644
===================================================================
--- /dev/null
+++ src/pgrn_command_escape_value.c    2016-11-29 23:09:13 +0900 (a2ae21c)
@@ -0,0 +1,67 @@
+#include "pgroonga.h"
+
+#include "pgrn_global.h"
+#include "pgrn_groonga.h"
+
+#include <utils/builtins.h>
+
+static grn_ctx *ctx = &PGrnContext;
+static struct PGrnBuffers *buffers = &PGrnBuffers;
+
+PG_FUNCTION_INFO_V1(pgroonga_command_escape_value);
+
+/**
+ * pgroonga.command_escape_value(value text : text
+ */
+Datum
+pgroonga_command_escape_value(PG_FUNCTION_ARGS)
+{
+	text *value = PG_GETARG_TEXT_PP(0);
+	text *escapedValue;
+	grn_obj *escapedValueBuffer;
+	const char *valueCurrent;
+	const char *valueEnd;
+
+	escapedValueBuffer = &(buffers->escape.escapedValue);
+
+	GRN_BULK_REWIND(escapedValueBuffer);
+	GRN_TEXT_PUTC(ctx, escapedValueBuffer, '"');
+	valueCurrent = VARDATA_ANY(value);
+	valueEnd = valueCurrent + VARSIZE_ANY_EXHDR(value);
+	while (valueCurrent < valueEnd)
+	{
+		int charLength = grn_charlen(ctx, valueCurrent, valueEnd);
+
+		if (charLength == 0) {
+			break;
+		}
+		else if (charLength == 1)
+		{
+			switch (*valueCurrent)
+			{
+			case '\\':
+			case '"':
+				GRN_TEXT_PUTC(ctx, escapedValueBuffer, '\\');
+				GRN_TEXT_PUTC(ctx, escapedValueBuffer, *valueCurrent);
+				break;
+			case '\n':
+				GRN_TEXT_PUTS(ctx, escapedValueBuffer, "\\n");
+				break;
+			default:
+				GRN_TEXT_PUTC(ctx, escapedValueBuffer, *valueCurrent);
+				break;
+			}
+		}
+		else
+		{
+			GRN_TEXT_PUT(ctx, escapedValueBuffer, valueCurrent, charLength);
+		}
+
+		valueCurrent += charLength;
+	}
+	GRN_TEXT_PUTC(ctx, escapedValueBuffer, '"');
+
+	escapedValue = cstring_to_text_with_len(GRN_TEXT_VALUE(escapedValueBuffer),
+											GRN_TEXT_LEN(escapedValueBuffer));
+	PG_RETURN_TEXT_P(escapedValue);
+}

  Modified: src/pgroonga.h (+1 -0)
===================================================================
--- src/pgroonga.h    2016-11-29 18:18:56 +0900 (55cf3a7)
+++ src/pgroonga.h    2016-11-29 23:09:13 +0900 (478044b)
@@ -67,6 +67,7 @@ extern Datum PGDLLEXPORT pgroonga_match_positions_byte(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_match_positions_character(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_query_extract_keywords(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_flush(PG_FUNCTION_ARGS);
+extern Datum PGDLLEXPORT pgroonga_command_escape_value(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_query_escape(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_escape_string(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_escape_boolean(PG_FUNCTION_ARGS);
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index