/* * bool db_reset_record_cursor(string $col_name) * Reset the record ID cursor. * Parameter * col_name: Collection name * Return * TRUE on success. FALSE on failure. */ static int unqliteBuiltin_db_reset_record_cursor(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; int nByte; /* Extract collection name */ if( argc < 1 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ unqliteCollectionResetRecordCursor(pCol); jx9_result_bool(pCtx,1); }else{ /* No such collection */ jx9_result_bool(pCtx,0); } return JX9_OK; }
/* * value db_fetch_by_id(string $col_name,int64 $record_id) * value db_get_by_id(string $col_name,int64 $record_id) * Fetch a record using its unique ID from a given collection. * Parameter * col_name: Collection name * record_id: Record number (__id field of a JSON object) * Return * Record content success. NULL on failure (No such record). */ static int unqliteBuiltin_db_fetch_by_id(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; jx9_int64 nId; int nByte; int rc; /* Extract collection name */ if( argc < 2 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or record ID"); /* Return NULL */ jx9_result_null(pCtx); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return NULL */ jx9_result_null(pCtx); return JX9_OK; } /* Extract the record ID */ nId = jx9_value_to_int(argv[1]); SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ /* Fetch the desired record */ jx9_value *pValue; pValue = jx9_context_new_scalar(pCtx); if( pValue == 0 ){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Jx9 is running out of memory"); jx9_result_null(pCtx); return JX9_OK; }else{ rc = unqliteCollectionFetchRecordById(pCol,nId,pValue); if( rc == UNQLITE_OK ){ jx9_result_value(pCtx,pValue); /* pValue will be automatically released as soon we return from this function */ }else{ /* No such record, return null */ jx9_result_null(pCtx); } } }else{ /* No such collection, return null */ jx9_result_null(pCtx); } return JX9_OK; }
/* * bool db_store(string $col_name,...) * bool db_put(string $col_name,...) * Store one or more JSON values in a given collection. * Parameter * col_name: Collection name * Return * TRUE on success. FALSE on failure. */ static int unqliteBuiltin_db_store(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; int nByte; int rc; int i; /* Extract collection name */ if( argc < 2 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or records"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol == 0 ){ jx9_context_throw_error_format(pCtx,JX9_CTX_ERR,"No such collection '%z'",&sName); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } /* Store the given values */ for( i = 1 ; i < argc ; ++i ){ rc = unqliteCollectionPut(pCol,argv[i],0); if( rc != UNQLITE_OK){ jx9_context_throw_error_format(pCtx,JX9_CTX_ERR, "Error while storing record %d in collection '%z'",i,&sName ); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } } /* All done, return TRUE */ jx9_result_bool(pCtx,1); return JX9_OK; }
/* * Create a new collection. */ UNQLITE_PRIVATE int unqliteCreateCollection( unqlite_vm *pVm, /* Target VM */ SyString *pName /* Collection name */ ) { unqlite_col *pCol; int rc; /* Perform a lookup first */ pCol = unqliteCollectionFetch(pVm,pName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ return UNQLITE_EXISTS; } /* Now, safely create the collection */ rc = unqliteVmLoadCollection(pVm,pName->zString,pName->nByte,UNQLITE_VM_COLLECTION_CREATE,0); return rc; }
/* * bool db_set_schema(string $col_name, object $json_object) * Set a schema for a given collection. * Parameter * col_name: Collection name. * json_object: Collection schema (Must be a JSON object). * Return * TRUE on success. FALSE on failure. */ static int unqliteBuiltin_db_set_schema(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; int nByte; int rc; /* Extract collection name */ if( argc < 2 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or db scheme"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } if( !jx9_value_is_json_object(argv[1]) ){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection scheme"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ rc = UNQLITE_NOOP; pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ /* Set the collection scheme */ rc = unqliteCollectionSetSchema(pCol,argv[1]); }else{ jx9_context_throw_error_format(pCtx,JX9_CTX_WARNING, "No such collection '%z'", &sName ); } /* Processing result */ jx9_result_bool(pCtx,rc == UNQLITE_OK); return JX9_OK; }
/* * bool db_drop_record(string $col_name,int64 record_id) * Remove a given record from a collection. * Parameter * col_name: Collection name. * record_id: ID of the record. * Return * TRUE on success. FALSE on failure. */ static int unqliteBuiltin_db_drop_record(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; jx9_int64 nId; int nByte; int rc; /* Extract collection name */ if( argc < 2 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or records"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol == 0 ){ jx9_context_throw_error_format(pCtx,JX9_CTX_ERR,"No such collection '%z'",&sName); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } /* Extract the record ID */ nId = jx9_value_to_int64(argv[1]); /* Drop the record */ rc = unqliteCollectionDropRecord(pCol,nId,1,1); /* Processing result */ jx9_result_bool(pCtx,rc == UNQLITE_OK); return JX9_OK; }
/* * object db_get_schema(string $col_name) * Return the schema associated with a given collection. * Parameter * col_name: Collection name * Return * Collection schema on success. null otherwise. */ static int unqliteBuiltin_db_get_schema(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; int nByte; /* Extract collection name */ if( argc < 1 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or db scheme"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ /* Return the collection schema */ jx9_result_value(pCtx,&pCol->sSchema); }else{ jx9_context_throw_error_format(pCtx,JX9_CTX_WARNING, "No such collection '%z'", &sName ); jx9_result_null(pCtx); } return JX9_OK; }
/* * string db_creation_date(string $col_name) * Return the creation date of the given collection. * Parameter * col_name: Collection name * Return * Creation date on success. FALSE on failure. */ static int unqliteBuiltin_db_creation_date(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; int nByte; /* Extract collection name */ if( argc < 1 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return false */ jx9_result_bool(pCtx,0); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ Sytm *pTm = &pCol->sCreation; jx9_result_string_format(pCtx,"%d-%d-%d %02d:%02d:%02d", pTm->tm_year,pTm->tm_mon,pTm->tm_mday, pTm->tm_hour,pTm->tm_min,pTm->tm_sec ); }else{ /* No such collection */ jx9_result_bool(pCtx,0); } return JX9_OK; }
/* * array db_fetch_all(string $col_name,[callback filter_callback]) * array db_get_all(string $col_name,[callback filter_callback]) * Retrieve all records of a given collection and apply the given * callback if available to filter records. * Parameter * col_name: Collection name * Return * Contents of the collection (JSON array) on success. NULL on failure. */ static int unqliteBuiltin_db_fetch_all(jx9_context *pCtx,int argc,jx9_value **argv) { unqlite_col *pCol; const char *zName; unqlite_vm *pVm; SyString sName; int nByte; int rc; /* Extract collection name */ if( argc < 1 ){ /* Missing arguments */ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name"); /* Return NULL */ jx9_result_null(pCtx); return JX9_OK; } zName = jx9_value_to_string(argv[0],&nByte); if( nByte < 1){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name"); /* Return NULL */ jx9_result_null(pCtx); return JX9_OK; } SyStringInitFromBuf(&sName,zName,nByte); pVm = (unqlite_vm *)jx9_context_user_data(pCtx); /* Fetch the collection */ pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD); if( pCol ){ jx9_value *pValue,*pArray,*pCallback = 0; jx9_value sResult; /* Callback result */ /* Allocate an empty scalar value and an empty JSON array */ pArray = jx9_context_new_array(pCtx); pValue = jx9_context_new_scalar(pCtx); jx9MemObjInit(pCtx->pVm,&sResult); if( pValue == 0 || pArray == 0 ){ jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Jx9 is running out of memory"); jx9_result_null(pCtx); return JX9_OK; } if( argc > 1 && jx9_value_is_callable(argv[1]) ){ pCallback = argv[1]; } unqliteCollectionResetRecordCursor(pCol); /* Fetch collection records one after one */ while( UNQLITE_OK == unqliteCollectionFetchNextRecord(pCol,pValue) ){ if( pCallback ){ jx9_value *apArg[2]; /* Invoke the filter callback */ apArg[0] = pValue; rc = jx9VmCallUserFunction(pCtx->pVm,pCallback,1,apArg,&sResult); if( rc == JX9_OK ){ int iResult; /* Callback result */ /* Extract callback result */ iResult = jx9_value_to_bool(&sResult); if( !iResult ){ /* Discard the result */ unqliteCollectionCacheRemoveRecord(pCol,unqliteCollectionCurrentRecordId(pCol) - 1); continue; } } } /* Put the value in the JSON array */ jx9_array_add_elem(pArray,0,pValue); /* Release the value */ jx9_value_null(pValue); } jx9MemObjRelease(&sResult); /* Finally, return our array */ jx9_result_value(pCtx,pArray); /* pValue will be automatically released as soon we return from * this foreign function. */ }else{ /* No such collection, return null */ jx9_result_null(pCtx); } return JX9_OK; }