Back to index

lightning-sunbird  0.9+nobinonly
storage1.cpp
Go to the documentation of this file.
00001 
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 
00005 #include "nsIComponentManager.h"
00006 #include "nsISimpleEnumerator.h"
00007 #include "nsIServiceManager.h"
00008 #include "nsCOMPtr.h"
00009 #include "nsString.h"
00010 #include "nsILocalFile.h"
00011 
00012 #include "mozIStorageService.h"
00013 #include "mozIStorageConnection.h"
00014 #include "mozIStorageValueArray.h"
00015 #include "mozIStorageStatement.h"
00016 #include "mozIStorageFunction.h"
00017 
00018 #include "mozStorageCID.h"
00019 
00020 static NS_DEFINE_CID(kmozStorageServiceCID, MOZ_STORAGE_SERVICE_CID);
00021 static NS_DEFINE_CID(kmozStorageConnectionCID, MOZ_STORAGE_CONNECTION_CID);
00022 
00023 #define TEST_CHECK_ERROR(rv) \
00024         do { if (NS_FAILED(rv)) {              \
00025             dbConn->GetLastError(&gerr); \
00026             dbConn->GetLastErrorString(gerrstr); \
00027             fprintf (stderr, "Error: %d 0x%08x %s\n", gerr, gerr, gerrstr.get()); \
00028             return 0; \
00029             } } while (0)
00030 
00031 #ifdef XP_UNIX
00032 #define TEST_DB NS_LITERAL_CSTRING("/tmp/foo.sdb")
00033 #else
00034 #define TEST_DB NS_LITERAL_CSTRING("foo.sdb")
00035 #endif
00036 
00037 int gerr;
00038 nsCString gerrstr;
00039 
00040 class TestFunc : public mozIStorageFunction {
00041 public:
00042     TestFunc() { }
00043     NS_DECL_ISUPPORTS
00044     NS_DECL_MOZISTORAGEFUNCTION
00045 };
00046 
00047 NS_IMPL_ISUPPORTS1(TestFunc, mozIStorageFunction)
00048 
00049 NS_IMETHODIMP
00050 TestFunc::OnFunctionCall (mozIStorageValueArray *sva)
00051 {
00052     fprintf (stderr, "* function call!\n");
00053     return NS_OK;
00054 }
00055 
00056 int
00057 main (int argc, char **argv)
00058 {
00059     nsresult rv;
00060 
00061     NS_InitXPCOM2(nsnull, nsnull, nsnull);
00062 
00063     nsCOMPtr<mozIStorageService> dbSrv;
00064     dbSrv = do_GetService(kmozStorageServiceCID, &rv);
00065     NS_ENSURE_SUCCESS(rv, rv);
00066 
00067     nsCOMPtr<nsILocalFile> f;
00068     rv = NS_NewNativeLocalFile (TEST_DB, PR_FALSE, getter_AddRefs(f));
00069     NS_ENSURE_SUCCESS(rv, rv);
00070 
00071     nsCOMPtr<mozIStorageConnection> dbConn;
00072     rv = dbSrv->OpenDatabase(f, getter_AddRefs(dbConn));
00073     NS_ENSURE_SUCCESS(rv, rv);
00074 
00075     rv = dbConn->CreateFunction("x_test", -1, new TestFunc());
00076     NS_ENSURE_SUCCESS(rv, rv);
00077 
00078     rv = dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("SELECT x_test(1)"));
00079     NS_ENSURE_SUCCESS(rv, rv);
00080 
00081     rv = dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DROP TABLE foo"));
00082     // TEST_CHECK_ERROR(rv);
00083 
00084     rv = dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("CREATE TABLE foo (i INTEGER)"));
00085     TEST_CHECK_ERROR(rv);
00086 
00087     nsCOMPtr<mozIStorageStatement> dbFooInsertStatement;
00088     rv = dbConn->CreateStatement (NS_LITERAL_CSTRING("INSERT INTO foo VALUES ( ?1 )"), getter_AddRefs(dbFooInsertStatement));
00089     TEST_CHECK_ERROR(rv);
00090 
00091     nsCOMPtr<mozIStorageStatement> dbFooSelectStatement;
00092     rv = dbConn->CreateStatement (NS_LITERAL_CSTRING("SELECT i FROM foo"), getter_AddRefs(dbFooSelectStatement));
00093     TEST_CHECK_ERROR(rv);
00094 
00095     for (int i = 0; i < 10; i++) {
00096         rv = dbFooInsertStatement->BindInt32Parameter (0, i);
00097         TEST_CHECK_ERROR(rv);
00098 
00099         rv = dbFooInsertStatement->Execute ();
00100         TEST_CHECK_ERROR(rv);
00101     }
00102 
00103     fprintf (stderr, "10 values written to foo...\n");
00104 
00105     nsCOMPtr<mozIStorageValueArray> dbRow = do_QueryInterface(dbFooSelectStatement);
00106     PRBool hasMore = PR_FALSE;
00107 
00108     while ((dbFooSelectStatement->ExecuteStep(&hasMore) == NS_OK) && hasMore)
00109     {
00110         PRUint32 len;
00111         
00112         dbRow->GetNumEntries (&len);
00113         fprintf (stderr, "Row[length %d]: %d '%s'\n", len, dbRow->AsInt32(0), dbRow->AsSharedUTF8String(0, 0));
00114     }
00115 
00116     TEST_CHECK_ERROR(rv);
00117     fprintf (stderr, "Done. %d 0x%08x %p\n", rv, rv, dbRow.get());
00118 }