Example #1
0
void SqliteConnection::Open(const path& file, FileAccess fileAccess, FileShare share) {
	int flags = 0;
#if UCFG_USE_SQLITE==3
	if (share == FileShare::None)
		flags = SQLITE_OPEN_EXCLUSIVE;
#else
	Blob utf = Encoding::UTF8.GetBytes(String(file));
#endif
	switch (fileAccess) {
	case FileAccess::ReadWrite: flags |= SQLITE_OPEN_READWRITE; break;
	case FileAccess::Read: flags |= SQLITE_OPEN_READONLY; break;
	default:
		Throw(E_INVALIDARG);
	}
	sqlite_db *pdb;
#if UCFG_USE_SQLITE==3
	SqliteCheck(m_db, ::sqlite3_open_v2(String(file), &pdb, flags, 0));
	m_db.reset(pdb);
	::sqlite3_extended_result_codes(m_db, true);
#else
	SqliteCheck(m_db, ::sqlite4_open(0, (const char*)utf.constData(), &pdb));
	m_db.reset(pdb);
#endif
	ExecuteNonQuery("PRAGMA foreign_keys = ON");
}
Example #2
0
void SqliteConnection::ExecuteNonQuery(RCString sql) {
    String s = sql.TrimEnd();
    if (s.Length > 1 && s[s.Length-1] != ';')
        s += ";";

#if UCFG_USE_SQLITE==3
    for (const void *tail=(const Char16*)s; SqliteIsComplete16(tail);) {
        SqliteCommand cmd(_self);
        SqliteCheck(_self, ::sqlite3_prepare16_v2(_self, tail, -1, &cmd.m_stmt, &tail));
#else
    Blob utf = Encoding::UTF8.GetBytes(s);
    for (const char *tail=(const char*)utf.constData(); SqliteIsComplete(tail);) {
        SqliteCommand cmd(_self);
        SqliteCheck(_self, ::sqlite_prepare(_self, tail, -1, &cmd.m_stmt, &tail));
#endif
        cmd.ExecuteNonQuery();
    }
}

Int64 SqliteConnection::get_LastInsertRowId() {
    return sqlite_last_insert_rowid(m_db);
}

pair<int, int> SqliteConnection::Checkpoint(int eMode) {
#if UCFG_USE_SQLITE==3
    int log, ckpt;
    SqliteCheck(_self, ::sqlite3_wal_checkpoint_v2(_self, 0, eMode, &log, &ckpt));
    return make_pair(log, ckpt);
#else
    ExecuteNonQuery("PRAGMA lsm_checkpoint");
    return make_pair(0, 0);							//!!!?
#endif
}
Example #3
0
String ConvertToBase58(const ConstBuf& cbuf) {
	HashValue hash = HasherEng::GetCurrent()->HashForAddress(cbuf);
	Blob v = cbuf + Blob(hash.data(), 4);
	vector<char> r;

	vector<byte> tmp(v.Size+1, 0);
	std::reverse_copy(v.begin(), v.end(), tmp.begin());
	for (BigInteger n(&tmp[0], tmp.size()); Sign(n);) {
		pair<BigInteger, BigInteger> pp = div(n, 58);
		n = pp.first;
		r.insert(r.begin(), s_pszBase58[explicit_cast<int>(pp.second)]);
	}

	for (int i=0; i<v.Size && !v.constData()[i]; ++i)
		r.insert(r.begin(), s_pszBase58[0]);
	return String(&r[0], r.size());
}
Example #4
0
void SqliteConnection::Create(RCString file) {
#if UCFG_USE_SQLITE==3
    SqliteCheck(m_db, ::sqlite3_open16((const String::Char*)file, &m_db));
    ::sqlite_extended_result_codes(m_db, true);	//!!!? only Sqlite3?
#else
    Blob utf = Encoding::UTF8.GetBytes(file);
    SqliteCheck(m_db, ::sqlite4_open(0, (const char*)utf.constData(), &m_db));
#endif
    ExecuteNonQuery("PRAGMA encoding = \"UTF-8\"");
    ExecuteNonQuery("PRAGMA foreign_keys = ON");

    /*!!!
    #ifndef SQLITE_OMIT_TRACE
    	sqlite3_trace(m_con.db, DbTraceHandler, this);
    #endif
    	*/
}
Example #5
0
String ConvertToBase58ShaSquare(const ConstBuf& cbuf) {
	SHA256 sha;
	HashValue hash = HashValue(sha.ComputeHash(sha.ComputeHash(cbuf)));
	Blob v = cbuf + Blob(hash.data(), 4);
	vector<char> r;

	vector<byte> tmp(v.Size+1, 0);
	std::reverse_copy(v.begin(), v.end(), tmp.begin());
	for (BigInteger n(&tmp[0], tmp.size()); Sign(n);) {
		pair<BigInteger, BigInteger> pp = div(n, 58);
		n = pp.first;
		r.insert(r.begin(), s_pszBase58[explicit_cast<int>(pp.second)]);
	}

	for (int i=0; i<v.Size && !v.constData()[i]; ++i)
		r.insert(r.begin(), s_pszBase58[0]);
	return String(&r[0], r.size());
}
Example #6
0
void XmlTextWriter::WriteAttributeString(RCString name, RCString value) {
	Blob blob = Encoding->GetBytes(name);
	WriteAttributeString((const char*)blob.constData(), value);
}
Example #7
0
void XmlTextWriter::WriteQuotedString(RCString value) {
	Blob blob = Encoding->GetBytes(value);
	m_os << quote(string((char*)blob.constData(), blob.Size));
}
void XptMessage::WriteString16(BinaryWriter& wr, RCString s) {
	Blob blob = Encoding::UTF8.GetBytes(s);
	uint16_t len = (uint16_t)blob.Size;
	(wr << len).BaseStream.WriteBuffer(blob.constData(), len);
}