Skip to content

Instantly share code, notes, and snippets.

@josegomezr
Created October 10, 2015 17:35
Show Gist options
  • Save josegomezr/9bb195e816af3a8e2112 to your computer and use it in GitHub Desktop.
Save josegomezr/9bb195e816af3a8e2112 to your computer and use it in GitHub Desktop.
PyOrientDB Snippet
import pyorient
from my_api import exceptions, config
class Binary(CDBCI):
client = None
_connected = False
def __init__(self, autoConnect = True, settings={}):
self._settings = config.db['binary'].copy()
self._settings.update(settings)
if autoConnect:
self.connect()
def connect(self):
if self._connected:
return
try:
client = pyorient.OrientDB(self._settings.get('host'), self._settings.get('port'))
client.db_open(self._settings.get('name'), self._settings.get('username'), self._settings.get('password'))
except pyorient.exceptions.PyOrientConnectionException, e:
raise exceptions.ConnectionException("Couldn't Connect to OrientDB Server", e)
except pyorient.exceptions.PyOrientBadMethodCallException, e:
raise exceptions.ConnectionException("Couldn't Connect to OrientDB Server", e)
except pyorient.exceptions.PyOrientDatabaseException, e:
raise exceptions.ConnectionException("Couldn't Connect to OrientDB Server", e)
self.client = client
self._connected = True
def query(self, *args):
if not self._connected:
self.connect()
if not self._connected:
raise exceptions.ConnectionException("Not Connected To OrientDB")
try:
return self.client.query(*args);
except pyorient.exceptions.PyOrientException, e:
self._connected = False
raise exceptions.SQLException("SQL Error", e)
def disconnect(self):
if self._connected:
self.client.db_close()
class Vertex(object):
def __init__(self, connection):
self.connection = connection
# ... omitted for brevity
def search(self, typeClass, query):
SQL = "SELECT * FROM " + typeClass + " WHERE @class = 'V' AND "
if isinstance(query, basestring):
SQL += "any().toLowerCase() LIKE '%" + query.lower() + "%'"
else:
first = True
for word in query:
if not first:
SQL += " AND "
else:
first = False
SQL += "any().toLowerCase() LIKE '%" + word.lower() + "%'"
result = self.connection.query(SQL)
return NodeSet(self.connection, result)
db = Binary(True)
# here comes the breaking ball *badumtsss*
Vertex(db).search('criteria')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment