From 34641553d0709f33fa6f3ecd200a7e42aae54bd1 Mon Sep 17 00:00:00 2001 From: Itagaki Takahiro Date: Fri, 3 Sep 2010 22:30:14 +0900 Subject: [PATCH] Use v8::Exception class when throwing exceptions. --- expected/plv8.out | 27 ++++++++++++++++++++------- plv8.cc | 6 ++++++ plv8.h | 2 ++ plv8_func.cc | 18 +++++++++--------- sql/plv8.sql | 19 ++++++++++++++----- 5 files changed, 51 insertions(+), 21 deletions(-) mode change 100644 => 100755 plv8.h mode change 100644 => 100755 plv8_func.cc diff --git a/expected/plv8.out b/expected/plv8.out index 91de009..08053c3 100755 --- a/expected/plv8.out +++ b/expected/plv8.out @@ -33,11 +33,11 @@ SELECT unnamed_args(ARRAY['A', 'B'], ARRAY['C', 'D']); CREATE FUNCTION concat_strings(VARIADIC args text[]) RETURNS text AS $$ - var result = ""; - for (var i = 0; i < args.length; i++) - if (args[i] != null) - result += args[i]; - return result; + var result = ""; + for (var i = 0; i < args.length; i++) + if (args[i] != null) + result += args[i]; + return result; $$ LANGUAGE plv8 IMMUTABLE STRICT; SELECT concat_strings('A', 'B', NULL, 'C'); @@ -192,6 +192,19 @@ SELECT record_to_text('(1,a)'::rec); {"i":1,"t":"a"} (1 row) +CREATE FUNCTION return_record(i integer, t text) RETURNS record AS +$$ + return { "i": i, "t": t }; +$$ +LANGUAGE plv8; +SELECT * FROM return_record(1, 'a'); +ERROR: a column definition list is required for functions returning "record" +LINE 1: SELECT * FROM return_record(1, 'a'); + ^ +SELECT * FROM return_record(1, 'a') AS t(j integer, s text); +ERROR: input of anonymous composite types is not implemented +SELECT * FROM return_record(1, 'a') AS t(x text, y text); +ERROR: input of anonymous composite types is not implemented CREATE FUNCTION set_of_records() RETURNS SETOF rec AS $$ yield( { "i": 1, "t": "a" } ); @@ -227,7 +240,7 @@ LANGUAGE plv8; SELECT test_print('ABC'); NOTICE: args = ABC WARNING: warning -ERROR: invalid error level for print() +ERROR: Error: invalid error level for print() DETAIL: test_print() LINE 4: print(20, 'ERROR is not allowed'); -- executeSql() CREATE TABLE test_tbl (i integer, s text); @@ -280,7 +293,7 @@ SELECT * FROM return_sql(); CREATE FUNCTION test_sql_error() RETURNS void AS $$ executeSql("ERROR") $$ LANGUAGE plv8; SELECT test_sql_error(); -ERROR: syntax error at or near "ERROR" +ERROR: Error: syntax error at or near "ERROR" DETAIL: test_sql_error() LINE 1: executeSql("ERROR") -- REPLACE FUNCTION CREATE FUNCTION replace_test() RETURNS integer AS $$ return 1; $$ LANGUAGE plv8; diff --git a/plv8.cc b/plv8.cc index 56174fa..b8f55f0 100755 --- a/plv8.cc +++ b/plv8.cc @@ -675,6 +675,12 @@ plv8_fill_type(plv8_type *type, Oid typid) throw() } } +Handle +ThrowError(const char *message) throw() +{ + return ThrowException(Exception::Error(String::New(message))); +} + static Handle GetGlobalContext() throw() { diff --git a/plv8.h b/plv8.h old mode 100644 new mode 100755 index 2f2987c..0530584 --- a/plv8.h +++ b/plv8.h @@ -103,6 +103,8 @@ class Converter Converter& operator = (const Converter&); }; +extern v8::Handle ThrowError(const char *message) throw(); + // plv8_type.cc extern Datum ToDatum(v8::Handle value, bool *isnull, plv8_type *type); extern v8::Handle ToValue(Datum datum, bool isnull, plv8_type *type); diff --git a/plv8_func.cc b/plv8_func.cc old mode 100644 new mode 100755 index e89b51e..dd0bb1d --- a/plv8_func.cc +++ b/plv8_func.cc @@ -45,7 +45,7 @@ SafeCall(InvocationCallback fn, const Arguments& args) throw() } catch (js_error& e) { - return ThrowException(String::New("internal exception")); + return ThrowError("internal exception"); } catch (pg_error& e) { @@ -56,7 +56,7 @@ SafeCall(InvocationCallback fn, const Arguments& args) throw() FlushErrorState(); FreeErrorData(edata); - return ThrowException(message); + return ThrowException(Exception::Error(message)); } } @@ -70,7 +70,7 @@ static Handle PrintInternal(const Arguments& args) { if (args.Length() < 2) - return ThrowException(String::New("usage: print(elevel, ...)")); + return ThrowError("usage: print(elevel, ...)"); int elevel = args[0]->Int32Value(); switch (elevel) @@ -86,7 +86,7 @@ PrintInternal(const Arguments& args) case WARNING: break; default: - return ThrowException(String::New("invalid error level for print()")); + return ThrowError("invalid error level for print()"); } if (args.Length() == 2) @@ -114,7 +114,7 @@ Handle ExecuteSql(const Arguments& args) throw() { if (args.Length() != 1 && (args.Length() != 2 || !args[1]->IsArray())) - return ThrowException(String::New("usage: executeSql(sql [, args])")); + return ThrowError("usage: executeSql(sql [, args])"); Handle result; @@ -133,7 +133,7 @@ SPIResultToValue(int status) Handle result; if (status < 0) - return ThrowException(String::New("SPI failed")); + return ThrowError("SPI failed"); switch (status) { @@ -229,17 +229,17 @@ ExecuteSqlWithArgs(const Arguments& args) else if (value->IsDate()) { // TODO: Date - return ThrowException(String::New("Date is not supported as arguments for executeSql()")); + return ThrowError("Date is not supported as arguments for executeSql()"); } else if (value->IsObject()) { // TODO: Object - return ThrowException(String::New("Object is not supported as arguments for executeSql()")); + return ThrowError("Object is not supported as arguments for executeSql()"); } else if (value->IsArray()) { // TODO: Array - return ThrowException(String::New("Array is not supported as arguments for executeSql()")); + return ThrowError("Array is not supported as arguments for executeSql()"); } else { diff --git a/sql/plv8.sql b/sql/plv8.sql index 9667863..e9e8c55 100755 --- a/sql/plv8.sql +++ b/sql/plv8.sql @@ -29,11 +29,11 @@ SELECT unnamed_args(ARRAY['A', 'B'], ARRAY['C', 'D']); CREATE FUNCTION concat_strings(VARIADIC args text[]) RETURNS text AS $$ - var result = ""; - for (var i = 0; i < args.length; i++) - if (args[i] != null) - result += args[i]; - return result; + var result = ""; + for (var i = 0; i < args.length; i++) + if (args[i] != null) + result += args[i]; + return result; $$ LANGUAGE plv8 IMMUTABLE STRICT; SELECT concat_strings('A', 'B', NULL, 'C'); @@ -95,6 +95,15 @@ $$ LANGUAGE plv8; SELECT record_to_text('(1,a)'::rec); +CREATE FUNCTION return_record(i integer, t text) RETURNS record AS +$$ + return { "i": i, "t": t }; +$$ +LANGUAGE plv8; +SELECT * FROM return_record(1, 'a'); +SELECT * FROM return_record(1, 'a') AS t(j integer, s text); +SELECT * FROM return_record(1, 'a') AS t(x text, y text); + CREATE FUNCTION set_of_records() RETURNS SETOF rec AS $$ yield( { "i": 1, "t": "a" } );