Class: PgQuery
- Inherits:
-
Object
- Object
- PgQuery
- Defined in:
- lib/pg_query/parse.rb,
lib/pg_query/version.rb,
lib/pg_query/parse_error.rb
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- VERSION =
'0.0.4'
Instance Attribute Summary collapse
-
#parsetree ⇒ Object
readonly
Returns the value of attribute parsetree.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(query, parsetree, warnings = []) ⇒ PgQuery
constructor
A new instance of PgQuery.
Constructor Details
#initialize(query, parsetree, warnings = []) ⇒ PgQuery
Returns a new instance of PgQuery.
26 27 28 29 30 |
# File 'lib/pg_query/parse.rb', line 26 def initialize(query, parsetree, warnings = []) @query = query @parsetree = parsetree @warnings = warnings end |
Instance Attribute Details
#parsetree ⇒ Object (readonly)
Returns the value of attribute parsetree.
24 25 26 |
# File 'lib/pg_query/parse.rb', line 24 def parsetree @parsetree end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
23 24 25 |
# File 'lib/pg_query/parse.rb', line 23 def query @query end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
25 26 27 |
# File 'lib/pg_query/parse.rb', line 25 def warnings @warnings end |
Class Method Details
._raw_parse(input) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'ext/pg_query/pg_query.c', line 31 static VALUE pg_query_raw_parse(VALUE self, VALUE input) { Check_Type(input, T_STRING); MemoryContext ctx = NULL; VALUE result; ErrorData* error = NULL; char stderr_buffer[STDERR_BUFFER_LEN + 1] = {0}; int stderr_global; int stderr_pipe[2]; ctx = AllocSetContextCreate(TopMemoryContext, "RootContext", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); MemoryContextSwitchTo(ctx); // Setup pipe for stderr redirection if (pipe(stderr_pipe) != 0) rb_raise(rb_eRuntimeError, "PgQuery._raw_parse: Could not allocate pipe for stderr redirection"); fcntl(stderr_pipe[0], F_SETFL, fcntl(stderr_pipe[0], F_GETFL) | O_NONBLOCK); // Redirect stderr to the pipe stderr_global = dup(STDERR_FILENO); dup2(stderr_pipe[1], STDERR_FILENO); close(stderr_pipe[1]); // Parse it! PG_TRY(); { List *tree; char *str; str = StringValueCStr(input); tree = raw_parser(str); str = nodeToString(tree); // Save stderr for result read(stderr_pipe[0], stderr_buffer, STDERR_BUFFER_LEN); result = rb_ary_new(); rb_ary_push(result, rb_tainted_str_new_cstr(str)); rb_ary_push(result, rb_str_new2(stderr_buffer)); pfree(str); } PG_CATCH(); { error = CopyErrorData(); FlushErrorState(); } PG_END_TRY(); // Restore stderr & return to previous PostgreSQL memory context dup2(stderr_global, STDERR_FILENO); MemoryContextSwitchTo(TopMemoryContext); MemoryContextDelete(ctx); // If we got an error, throw a ParseError exception if (error) raise_parse_error(error); return result; } |
.parse(query) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pg_query/parse.rb', line 4 def self.parse(query) parsetree, stderr = _raw_parse(query) parsetree = [] if parsetree == '<>' if !parsetree.nil? && !parsetree.empty? parsetree = parsetree_to_json(parsetree) parsetree = JSON.parse(parsetree, max_nesting: 1000) end warnings = [] stderr.each_line do |line| next unless line[/^WARNING/] warnings << line.strip end PgQuery.new(query, parsetree, warnings) end |