vals = []
for k, v in values.iteritems():
keys.append(k)
- if type(v) in (str, unicode):
+ if type(v) is str:
vals.append("'%s'" % v.replace("'", "''"))
else:
vals.append(str(v))
#
##############################################################################
-import cPickle
+import msgpack
from cursor import Cursor
import uuid
class Collection(object):
def __init__(self, database, table_name):
self.database = database
- self.table_name = unicode(table_name)
+ self.table_name = str(table_name)
def __repr__(self):
return "Collection(%r, %r)" % (self.database, self.table_name)
def _get_fields(self):
tables = self.database.connection._get_tables(self.database.db_name)
- return [unicode(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)]
+ return [str(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)]
def count(self):
return self.database.connection._count(self.database.db_name, self.table_name)
else:
docs = doc_or_docs
for doc in docs:
- if not u'_id' in doc:
- doc[u'_id'] = uuid.uuid4().hex
+ if not '_id' in doc:
+ doc['_id'] = uuid.uuid4().hex
self._insert_document(doc)
if type(doc_or_docs) in (list, tuple):
- return [d[u'_id'] for d in docs]
+ return [d['_id'] for d in docs]
else:
- return docs[0][u'_id']
+ return docs[0]['_id']
def _insert_document(self, doc):
table_id = '%s$_id' % self.table_name
fields = self._get_fields()
- coded_id = cPickle.dumps(doc['_id'])
+ coded_id = msgpack.dumps(doc['_id'])
self.database.connection._insert(self.database.db_name, table_id, {'id': coded_id})
for f in doc:
if f == '_id':
table_f = '%s$%s' % (self.table_name, f)
values = {
'id': coded_id,
- 'value': cPickle.dumps(doc[f]),
+ 'value': msgpack.dumps(doc[f]),
}
if type(doc[f]) in (int, float):
values['number'] = doc[f]
def database_names(self):
try:
- return [unicode(x) for x in self._get_databases()]
+ return [str(x) for x in self._get_databases()]
except:
return []
def collection_names(self, db_name):
try:
- return list(set([unicode(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
+ return list(set([str(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
except:
return []
#
##############################################################################
-import cPickle
+import msgpack
class Cursor(object):
def __init__(self, collection, spec=None, fields=None, **kwargs):
query['where'] = []
for k, v in self.spec.iteritems():
table_f = '%s$%s' % (self.collection.table_name, k)
- if k == u'_id':
- query['where'].append(((table_id, 'id'), '=', cPickle.dumps(v)))
+ if k == '_id':
+ query['where'].append(((table_id, 'id'), '=', msgpack.dumps(v)))
elif type(v) in (int, float):
field_q = self._get_cursor_field(table_id, table_f, field_name='number')
query['where'].append((field_q, '=', v))
else:
field_q = self._get_cursor_field(table_id, table_f)
- query['where'].append((field_q, '=', cPickle.dumps(v)))
+ query['where'].append((field_q, '=', msgpack.dumps(v)))
return self.collection.database.connection._get_cursor(self.collection.database.db_name, query)
raise StopIteration
else:
document = {}
- if u'_id' in self.fields:
- document[u'_id'] = cPickle.loads(res[0])
- fields_without_id = filter(lambda x: x != u'_id', self.fields)
+ if '_id' in self.fields:
+ document['_id'] = msgpack.loads(res[0])
+ fields_without_id = filter(lambda x: x != '_id', self.fields)
for i in xrange(len(fields_without_id)):
if not res[i + 1] is None:
- document[fields_without_id[i]] = cPickle.loads(res[i + 1])
+ document[fields_without_id[i]] = msgpack.loads(res[i + 1])
return document
else:
return None
class Database(object):
def __init__(self, connection, db_name):
self.connection = connection
- self.db_name = unicode(db_name)
+ self.db_name = str(db_name)
def __getattr__(self, table_name):
return Collection(self, table_name)