int main(int argc, char **argv) { AUrl url("mysql://*****:*****@localhost/aos/"); AMySQLServer server(url); AString strError; if (!server.init(strError)) { std::cout << strError << std::endl; return -1; } else { ARope rope; AXmlElement base("base"); server.emit(base); AResultSet rs; if (server.executeSQL("SELECT * FROM global", rs, strError)) { rs.emit(base); } else base.addElement("error", strError); base.emitXml(rope, 0); std::cout << rope << std::endl; } return 0; }
size_t AMySQLServer::executeSQL(const AString& sqlQuery, AResultSet& target, AString& error) { target.clear(); MYSQL_RES *pmyresult = executeSQL(sqlQuery, error); if (!pmyresult && !error.isEmpty()) return AConstant::npos; target.useSQL().assign(sqlQuery); return (pmyresult ? _processQueryAllRows(pmyresult, target) : 0); }
u4 AMySQLServer::_processQueryAllRows(MYSQL_RES *pmyresult, AResultSet& target) { u4 rowCount = 0; if (pmyresult) { MYSQL_ROW myrow; rowCount = (int)mysql_num_rows(pmyresult); if (rowCount == 0) return 0; //a_Get field info MYSQL_FIELD *fields = mysql_fetch_fields(pmyresult); u4 num_fields = mysql_num_fields(pmyresult); //a_Set up field data for (u4 u=0; u<num_fields; ++u) target.useFieldNames().push_back(fields[u].name); //a_Populate results AString str; myrow = mysql_fetch_row(pmyresult); while (myrow) { VECTOR_AString& row = target.insertRow(); row.reserve(num_fields); for(u4 field=0; field < num_fields; ++field) { row.push_back(myrow[field]); } myrow = mysql_fetch_row(pmyresult); } } mysql_free_result(pmyresult); return rowCount; }
void testSelect(ADatabase& db) { AString error; AResultSet rs; u4 rows = db.executeSQL("SELECT * from global", rs, error); if (AString::npos == rows) { std::cerr << error << std::endl; } else { std::cout << "Selected " << rows << " rows." << std::endl; AXmlElement root("root"); rs.emit(root); ARope rope; root.emitXml(rope, 0); std::cout << rope << std::endl; } u4 myNameCol = rs.getFieldIndex("name"); u4 myValueCol = rs.getFieldIndex("value"); for (u4 row=0; row<rs.getRowCount(); ++row) { const AString& name = rs.getData(row, myNameCol); if (name.equals("test_query")) { AQueryString qs(rs.getData(row, myValueCol)); AString str; qs.emit(str); std::cout << "Query string detected: " << str << std::endl; } else if (name.equals("flags")) { ABitArray flags(rs.getData(row, myValueCol)); AString str; flags.emit(str); std::cout << "Flags found: " << str << std::endl; str.clear(); flags.setOutputMode(ABitArray::Binary); flags.emit(str); std::cout << "Flags found: " << str << std::endl; } } }
int main(int argc, char **argv) { if (argc < 2) { std::cout << "Usage: this [SQLite database URL]" << std::endl; std::cout << "e.g. this sqlite://q:/mydata.db" << std::endl; return -1; } AUrl url(argv[1]); ASQLiteServer db(url); AString error; if (!db.init(error)) { std::cerr << error << std::endl; return -1; } AString input; AFile_IOStream iof; std::cout << "\r\nsqlite [?=help]>" << std::flush; while(AString::npos != iof.readLine(input)) { if (input.equals("createAOSTables")) { if (AString::npos == createAOSTables(db, error)) std::cerr << error << std::endl; else std::cout << "AOS tables created." << std::endl; } else if (input.equals("?")) { std::cout << "Enter SQL query or use one of the following built in functions:\r\n"; std::cout << " createAOSTables - create databases used by base AOS server modules\r\n"; std::cout << std::endl; } else if (input.equals("exit")) { return 0; } else { AResultSet rs; u4 rows = db.executeSQL(input, rs, error); if (AString::npos == rows) { std::cerr << error << std::endl; error.clear(); } else { std::cout << "Success, rows affected=" << rows << std::endl; ARope rope; rs.emit(rope); std::cout << rope << std::endl; } } input.clear(); std::cout << "\r\nsqlite>" << std::flush; } return 0; }