Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameterized queries #32

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -121,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):
Expand Down
18 changes: 15 additions & 3 deletions pyorient/messages/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )

Expand Down Expand Up @@ -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 = [
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pyorient/orient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions tests/test_graph_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down