use of org.sql2o.Connection in project runelite by runelite.
the class ItemService method batchInsertItems.
private void batchInsertItems(RSSearch search) {
try (Connection con = sql2o.beginTransaction()) {
Query q = con.createQuery("insert into items (id, name, description, type) values (:id," + " :name, :description, :type) ON DUPLICATE KEY UPDATE name = :name," + " description = :description, type = :type");
for (RSItem rsItem : search.getItems()) {
q.addParameter("id", rsItem.getId()).addParameter("name", rsItem.getName()).addParameter("description", rsItem.getDescription()).addParameter("type", rsItem.getType()).addToBatch();
}
q.executeBatch();
con.commit();
}
}
use of org.sql2o.Connection in project runelite by runelite.
the class CacheService method getArchiveFiles.
public ArchiveFiles getArchiveFiles(ArchiveEntry archiveEntry) throws IOException {
CacheDAO cacheDao = new CacheDAO();
try (Connection con = sql2o.open();
ResultSetIterable<FileEntry> files = cacheDao.findFilesForArchive(con, archiveEntry)) {
byte[] archiveData = getArchive(archiveEntry);
if (archiveData == null) {
return null;
}
Container result = Container.decompress(archiveData, null);
if (result == null) {
return null;
}
byte[] decompressedData = result.data;
ArchiveFiles archiveFiles = new ArchiveFiles();
for (FileEntry fileEntry : files) {
FSFile file = new FSFile(fileEntry.getFileId());
archiveFiles.addFile(file);
file.setNameHash(fileEntry.getNameHash());
}
archiveFiles.loadContents(decompressedData);
return archiveFiles;
}
}
use of org.sql2o.Connection in project runelite by runelite.
the class CacheService method findArchivesForIndex.
public List<ArchiveEntry> findArchivesForIndex(IndexEntry indexEntry) {
try (Connection con = sql2o.open()) {
CacheDAO cacheDao = new CacheDAO();
ResultSetIterable<ArchiveEntry> archiveEntries = cacheDao.findArchivesForIndex(con, indexEntry);
List<ArchiveEntry> archives = new ArrayList<>();
Iterables.addAll(archives, archiveEntries);
return archives;
}
}
use of org.sql2o.Connection in project runelite by runelite.
the class AccountService method callback.
@RequestMapping("/callback")
public Object callback(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String error, @RequestParam String code, @RequestParam("state") String stateStr) throws InterruptedException, ExecutionException, IOException {
if (error != null) {
logger.info("Error in oauth callback: {}", error);
return null;
}
State state = gson.fromJson(stateStr, State.class);
logger.info("Got authorization code {} for uuid {}", code, state.getUuid());
OAuth20Service service = new ServiceBuilder().apiKey(oauthClientId).apiSecret(oauthClientSecret).scope(SCOPE).callback(RL_OAUTH_URL).state(gson.toJson(state)).build(GoogleApi20.instance());
OAuth2AccessToken accessToken = service.getAccessToken(code);
// Access user info
OAuthRequest orequest = new OAuthRequest(Verb.GET, USERINFO);
service.signRequest(accessToken, orequest);
Response oresponse = service.execute(orequest);
if (oresponse.getCode() / 100 != 2) {
// Could be a forged result
return null;
}
UserInfo userInfo = gson.fromJson(oresponse.getBody(), UserInfo.class);
logger.info("Got user info: {}", userInfo);
try (Connection con = sql2o.open()) {
con.createQuery("insert ignore into users (username) values (:username)").addParameter("username", userInfo.getEmail()).executeUpdate();
UserEntry user = con.createQuery("select id from users where username = :username").addParameter("username", userInfo.getEmail()).executeAndFetchFirst(UserEntry.class);
if (user == null) {
logger.warn("Unable to find newly created user session");
// that's weird
return null;
}
// insert session
con.createQuery("insert ignore into sessions (user, uuid) values (:user, :uuid)").addParameter("user", user.getId()).addParameter("uuid", state.getUuid().toString()).executeUpdate();
logger.info("Created session for user {}", userInfo.getEmail());
}
response.sendRedirect(RL_REDIR);
notifySession(state.getUuid(), userInfo.getEmail());
return "";
}
use of org.sql2o.Connection in project runelite by runelite.
the class XpTrackerService method findOrCreatePlayer.
private synchronized PlayerEntity findOrCreatePlayer(String username) {
try (Connection con = sql2o.open()) {
PlayerEntity playerEntity = con.createQuery("select * from player where name = :name").addParameter("name", username).executeAndFetchFirst(PlayerEntity.class);
if (playerEntity != null) {
return playerEntity;
}
Instant now = Instant.now();
int id = con.createQuery("insert into player (name, tracked_since) values (:name, :tracked_since)").addParameter("name", username).addParameter("tracked_since", now).executeUpdate().getKey(int.class);
playerEntity = new PlayerEntity();
playerEntity.setId(id);
playerEntity.setName(username);
playerEntity.setTracked_since(now);
return playerEntity;
}
}
Aggregations