bool TestExtPdo::test_pdo_sqlite() { CreateSqliteTestTable(); try { string source = "sqlite:/tmp/foo.db"; sp_pdo dbh((NEW(c_pdo)())-> create(source.c_str(), TEST_USERNAME, TEST_PASSWORD, CREATE_MAP1(q_pdo_ATTR_PERSISTENT, false))); Variant vstmt = dbh->t_prepare("select * from foo"); c_pdostatement *stmt = vstmt.toObject().getTyped<c_pdostatement>(); VERIFY(stmt->t_execute()); Variant rs = stmt->t_fetch(q_pdo_FETCH_ASSOC); VS(rs, CREATE_MAP1("bar", "ABC")); rs = stmt->t_fetch(q_pdo_FETCH_ASSOC); VS(rs, CREATE_MAP1("bar", "DEF")); } catch (Object &e) { VS(e, null); } try { string source = "sqlite:/tmp/foo.db"; sp_pdo dbh((NEW(c_pdo)())-> create(source.c_str(), TEST_USERNAME, TEST_PASSWORD, CREATE_MAP1(q_pdo_ATTR_PERSISTENT, false))); Variant vstmt = dbh->t_query("select * from foo"); ArrayIterPtr iter = vstmt.begin(); VERIFY(!iter->end()); VS(iter->first(), 0); VS(iter->second(), CREATE_MAP2("bar", "ABC", 0, "ABC")); iter->next(); VERIFY(!iter->end()); VS(iter->first(), 1); VS(iter->second(), CREATE_MAP2("bar", "DEF", 0, "DEF")); iter->next(); VERIFY(iter->end()); } catch (Object &e) { VS(e, null); } try { string source = "sqlite:/tmp/foo.db"; sp_pdo dbh((NEW(c_pdo)())-> create(source.c_str(), TEST_USERNAME, TEST_PASSWORD, CREATE_MAP1(q_pdo_ATTR_PERSISTENT, false))); dbh->t_query("CREATE TABLE IF NOT EXISTS foobar (id INT)"); dbh->t_query("INSERT INTO foobar (id) VALUES (1)"); Variant res = dbh->t_query("SELECT id FROM foobar LIMIT 1"); c_pdostatement *stmt = res.toObject().getTyped<c_pdostatement>(); Variant ret = stmt->t_fetch(); VS(ret["id"], "1"); } catch (Object &e) { VS(e, null); } CleanupSqliteTestTable(); return Count(true); }
ProcessSharedVariant::ProcessSharedVariant(CVarRef source, ProcessSharedVariantLock* lock) : m_lock(putPtr(lock)) { switch (source.getType()) { case KindOfBoolean: { m_type = KindOfBoolean; m_data.num = source.toBoolean(); break; } case KindOfByte: case KindOfInt16: case KindOfInt32: case KindOfInt64: { m_type = KindOfInt64; m_data.num = source.toInt64(); break; } case KindOfDouble: { m_type = KindOfDouble; m_data.dbl = source.toDouble(); break; } case LiteralString: case KindOfStaticString: case KindOfString: { String s = source.toString(); m_type = KindOfString; if (lock) { m_data.str = putPtr(SharedMemoryManager::GetSegment() ->construct<SharedMemoryString> (boost::interprocess::anonymous_instance) (s.data(), s.size())); } else { // Just need this string to live long enough for the key lookup so // don't store in shared memory. m_data.str = putPtr(new SharedMemoryString(s.data(), s.size())); } break; } case KindOfArray: { ASSERT(lock); m_type = KindOfArray; uint i = 0; ProcessSharedVariantMapData* mapData = SharedMemoryManager::GetSegment() ->construct<ProcessSharedVariantMapData> (boost::interprocess::anonymous_instance)(); m_data.map = putPtr(mapData); ProcessSharedVariantToIntMap* map = SharedMemoryManager::GetSegment() ->construct<ProcessSharedVariantToIntMap> (boost::interprocess::anonymous_instance)(); mapData->map = putPtr(map); SharedMemoryVector<SharedVariant*>* keys = SharedMemoryManager::GetSegment() ->construct<SharedMemoryVector<SharedVariant*> > (boost::interprocess::anonymous_instance)(); mapData->keys = putPtr(keys); SharedMemoryVector<SharedVariant*>* vals = SharedMemoryManager::GetSegment() ->construct<SharedMemoryVector<SharedVariant*> > (boost::interprocess::anonymous_instance)(); mapData->vals = putPtr(vals); for (ArrayIterPtr it = source.begin(); !it->end(); it->next()) { ProcessSharedVariant* key = SharedMemoryManager::GetSegment() ->construct<ProcessSharedVariant> (boost::interprocess::anonymous_instance) (it->first(), getLock()); ProcessSharedVariant* val = SharedMemoryManager::GetSegment() ->construct<ProcessSharedVariant> (boost::interprocess::anonymous_instance) (it->second(), getLock()); (*map)[key] = i++; keys->push_back(putPtr(key)); vals->push_back(putPtr(val)); } break; } default: { m_type = KindOfObject; String s = f_serialize(source); m_data.str = putPtr(SharedMemoryManager::GetSegment() ->construct<SharedMemoryString> (boost::interprocess::anonymous_instance) (s.data(), s.size())); break; } } }