Skip to content

Commit

Permalink
Updated Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
snej committed Mar 11, 2019
1 parent 5e6b105 commit ee5b9ef
Show file tree
Hide file tree
Showing 6 changed files with 517 additions and 594 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -123,9 +123,9 @@ This only works on Mac so far. And it assumes that Xcode puts build output in a
1. Build the native library. (If using Xcode, build the "CBL_C Dylib" target.)
2. `pip install cffi`
3. `cd python/CouchbaseLite`
4. `python BuildPyCBL.py`
4. `python3 BuildPyCBL.py`
5. `cd ..`
6. `python test.py` -- some tests. Should run without throwing exceptions.
6. `python3 test.py` -- some tests. Should run without throwing exceptions.

## Using It

Expand Down
29 changes: 12 additions & 17 deletions python/CouchbaseLite/BuildPyCBL.py
Expand Up @@ -124,11 +124,11 @@
//////// CBLDatabase.h
bool cbl_databaseExists(const char* name, const char *inDirectory);
bool cbl_copyDB(const char* fromPath,
bool cbl_copyDatabase(const char* fromPath,
const char* toName,
const CBLDatabaseConfiguration* config,
CBLError*);
bool cbl_deleteDB(const char *name,
bool cbl_deleteDatabase(const char *name,
const char *inDirectory,
CBLError*);
CBLDatabase* cbl_db_open(const char *name,
Expand All @@ -142,17 +142,16 @@
const char* cbl_db_name(const CBLDatabase*);
const char* cbl_db_path(const CBLDatabase*);
uint64_t cbl_db_count(const CBLDatabase*);
uint64_t cbl_db_lastSequence(const CBLDatabase*);
CBLDatabaseConfiguration cbl_db_config(const CBLDatabase*);
typedef void (*CBLDatabaseListener)(void *context,
typedef void (*CBLDatabaseChangeListener)(void *context,
const CBLDatabase* db,
unsigned numDocs,
const char **docIDs);
extern "Python" void databaseListenerCallback(void *context, const CBLDatabase* db,
unsigned numDocs, const char **docIDs);
CBLListenerToken* cbl_db_addListener(const CBLDatabase* db,
CBLDatabaseListener listener,
CBLListenerToken* cbl_db_addChangeListener(const CBLDatabase* db,
CBLDatabaseChangeListener listener,
void *context);
//////// CBLDocument.h
Expand All @@ -166,12 +165,9 @@
bool cbl_doc_delete(const CBLDocument* document,
CBLConcurrencyControl concurrency,
CBLError* error);
bool cbl_db_deleteDocument(CBLDatabase* database,
const char* docID,
CBLError* error);
bool cbl_doc_purge(const CBLDocument* document,
CBLError* error);
bool cbl_db_purgeDocument(CBLDatabase* database,
bool cbl_db_purgeDocumentByID(CBLDatabase* database,
const char* docID,
CBLError* error);
CBLDocument* cbl_db_getMutableDocument(CBLDatabase* database,
Expand All @@ -189,21 +185,20 @@
CBLQuery* cbl_query_new(const CBLDatabase* db,
const char *jsonQuery,
CBLError* error);
FLDict cbl_query_parameters(CBLQuery* query);
void cbl_query_setParameters(CBLQuery* query,
FLDict parameters);
void cbl_query_setParametersFromJSON(CBLQuery* query,
void cbl_query_setParametersAsJSON(CBLQuery* query,
const char* json);
CBLResultSet* cbl_query_execute(CBLQuery*, CBLError*);
FLSliceResult cbl_query_explain(CBLQuery*);
unsigned cbl_query_columnCount(CBLQuery*);
FLSlice cbl_query_columnName(CBLQuery*,
unsigned columnIndex);
bool cbl_results_next(CBLResultSet*);
FLValue cbl_results_column(CBLResultSet*,
unsigned column);
FLValue cbl_results_property(CBLResultSet*,
const char* property);
//CBLListenerToken* cbl_query_addListener(CBLQuery* query,
bool cbl_resultset_next(CBLResultSet*);
FLValue cbl_resultset_valueAtIndex(CBLResultSet*, unsigned index);
FLValue cbl_resultset_valueForKey(CBLResultSet*, const char* key);
//CBLListenerToken* cbl_query_addChangeListener(CBLQuery* query,
// CBLQueryListener* listener,
// void *context);
""")
Expand Down
8 changes: 2 additions & 6 deletions python/CouchbaseLite/Database.py
Expand Up @@ -36,7 +36,7 @@ def delete(self):

@staticmethod
def deleteFile(name, dir):
if lib.cbl_deleteDB(cstr(name), cstr(dir), gError):
if lib.cbl_deleteDatabase(cstr(name), cstr(dir), gError):
return True
elif gError.code == 0:
return False
Expand All @@ -63,10 +63,6 @@ def config(self):
def count(self):
return lib.cbl_db_count(self._ref)

@property
def lastSequence(self):
return lib.cbl_db_lastSequence(self._ref)

# Documents:

def getDocument(self, id):
Expand Down Expand Up @@ -119,7 +115,7 @@ def __exit__(self, exc_type, exc_value, traceback):
def addListener(self, listener):
handle = ffi.new_handle(listener)
self.listeners.add(handle)
c_token = lib.cbl_db_addListener(self._ref, lib.databaseListenerCallback, handle)
c_token = lib.cbl_db_addChangeListener(self._ref, lib.databaseListenerCallback, handle)
return ListenerToken(self, handle, c_token)

def removeListener(self, token):
Expand Down
8 changes: 4 additions & 4 deletions python/CouchbaseLite/Query.py
Expand Up @@ -42,7 +42,7 @@ def execute(self):
raise CBLException("Query failed", gError)
try:
lastResult = None
while lib.cbl_results_next(results):
while lib.cbl_resultset_next(results):
if lastResult:
lastResult.invalidate()
lastResult = QueryResult(self, results)
Expand Down Expand Up @@ -76,9 +76,9 @@ def __getitem__(self, key):
if isinstance(key, int):
if key < 0 or key >= self.query.columnCount:
raise IndexError("Column index out of range")
item = lib.cbl_results_column(self._ref, key)
item = lib.cbl_resultset_valueAtIndex(self._ref, key)
elif isinstance(key, str):
item = lib.cbl_results_property(self._ref, key)
item = lib.cbl_resultset_valueForKey(self._ref, key)
if item == None:
raise KeyError("No such column in Query")
else:
Expand Down Expand Up @@ -108,7 +108,7 @@ def asDictionary(self):
result = {}
keys = self.query.columnNames
for i in range(0, self.query.columnCount):
item = lib.cbl_results_column(self._ref, i)
item = lib.cbl_resultset_valueAtIndex(self._ref, i)
if lib.FLValue_GetType(item) != lib.kFLUndefined:
result[keys[i]] = decodeFleece(item)
return result

0 comments on commit ee5b9ef

Please sign in to comment.