From f7ca1f9beb5019635036631f3f57444538fdb3ca Mon Sep 17 00:00:00 2001 From: Luca Garulli Date: Wed, 17 Aug 2016 23:58:30 -0500 Subject: [PATCH 1/2] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b32a8c3e..cca35e86 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,10 @@ Pyorient works with orientdb version 1.7 and later. pip install pyorient +## Documentation + + [OrientDB PyOrient Python Driver](http://orientdb.com/docs/last/PyOrient.html) + ## How to contribute - Fork the project From c62a309f160a7349d7fbd0441b1cd2a09d665683 Mon Sep 17 00:00:00 2001 From: Serg Date: Thu, 14 Mar 2019 17:14:15 -0700 Subject: [PATCH 2/2] add parameterized query --- README.md | 7 ++++++ pyorient/messages/commands.py | 18 ++++++++++++--- pyorient/orient.py | 5 ++++ tests/test_graph_usage.py | 43 +++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cca35e86..95d898b1 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,13 @@ client.record_load( rec_position._rid, "*:-1", _my_callback ) result = client.query("select from my_class", 10, '*:0') ``` +### Make a parameterized query +```python +sql = "select * from my_class where id = ? or id = ? " +params = [1,2] +result = client.query_parameterized(sql, params, 10, '*:0') +``` + ### Make an Async query ```python def _my_callback(for_every_record): diff --git a/pyorient/messages/commands.py b/pyorient/messages/commands.py index cd1240ab..6e22e4ca 100644 --- a/pyorient/messages/commands.py +++ b/pyorient/messages/commands.py @@ -70,6 +70,7 @@ def __init__(self, _orient_socket): self._fetch_plan = '*:0' self._command_type = QUERY_SYNC self._mod_byte = 's' + self._prepared_params = None self._append( ( FIELD_BYTE, COMMAND_OP ) ) @@ -99,8 +100,7 @@ def prepare(self, params=None ): self._mod_byte = 's' else: if self._callback is None: - raise PyOrientBadMethodCallException( "No callback was " - "provided.",[]) + raise PyOrientBadMethodCallException( "No callback was provided.", []) self._mod_byte = 'a' _payload_definition = [ @@ -124,7 +124,10 @@ def prepare(self, params=None ): if self._command_type == QUERY_SCRIPT: _payload_definition.insert( 1, ( FIELD_STRING, 'sql' ) ) - _payload_definition.append( ( FIELD_INT, 0 ) ) + if self._prepared_params: + _payload_definition.append((FIELD_STRING, self._prepared_params)) + + _payload_definition.append((FIELD_INT, 0)) payload = b''.join( self._encode_field( x ) for x in _payload_definition @@ -135,6 +138,15 @@ def prepare(self, params=None ): return super( CommandMessage, self ).prepare() + def prepare_parametric_query(self, params): + sql_params = params[2] + params_list = [] + for i in range(len(sql_params)): + params_list.append(str(i) + ':' + str(sql_params[i]) + ',') + prepared_params_value = ''.join(params_list)[:-1] + self._prepared_params = 'params:{' + prepared_params_value + '}' + return self.prepare(params[0:2] + params[3:]) + def fetch_response(self): # skip execution in case of transaction diff --git a/pyorient/orient.py b/pyorient/orient.py index 746df43a..7324a495 100644 --- a/pyorient/orient.py +++ b/pyorient/orient.py @@ -422,6 +422,11 @@ def query_async(self, *args): return self.get_message("CommandMessage") \ .prepare(( QUERY_ASYNC, ) + args).send().fetch_response() + def query_parameterized(self, *args): + return self.get_message("CommandMessage") \ + .prepare_parametric_query(( QUERY_SYNC, ) + args).send()\ + .fetch_response() + def data_cluster_add(self, *args): return self.get_message("DataClusterAddMessage") \ .prepare(args).send().fetch_response() diff --git a/tests/test_graph_usage.py b/tests/test_graph_usage.py index 7c0ac06d..38008b75 100644 --- a/tests/test_graph_usage.py +++ b/tests/test_graph_usage.py @@ -47,6 +47,49 @@ def testGraph(self): assert 'specie' in animal assert 'name' in animal + # add data for tests + + self.client.command("insert into Animal set name = 'capybara', specie = 'rodent'") + self.client.command("insert into Animal set name = 'chipmunk', specie = 'rodent'") + self.client.command("insert into Animal set name = 'wolf', specie = 'canis'") + self.client.command("insert into Animal set name = 'coyote', specie = 'canis'") + + # parameterized query + + sql = "select * from animal where name = ? or name = ?" + + params = ['rat', 'chipmunk'] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 2 + params = ['capybara', 'chipmunk'] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 2 + params = ['squirrel', 'hamster'] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 0 + params = ['capybara'] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 1 + params = [] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 0 + + sql = "select * from animal where specie = ?" + + params = ['canis'] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 2 + + animals = self.client.query_parameterized(sql, params, 1) + assert len(animals) == 1 + + sql = "select * from animal where name = ? and specie = ?" + + params = ['wolf', 'canis'] + animals = self.client.query_parameterized(sql, params) + assert len(animals) == 1 + + # Create the vertex and insert the food values self.client.command('create class Food extends V')