Search in sources :

Example 26 with Connection

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();
    }
}
Also used : Query(org.sql2o.Query) Connection(org.sql2o.Connection)

Example 27 with Connection

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;
    }
}
Also used : Container(net.runelite.cache.fs.Container) ArchiveFiles(net.runelite.cache.fs.ArchiveFiles) Connection(org.sql2o.Connection) FileEntry(net.runelite.http.service.cache.beans.FileEntry) FSFile(net.runelite.cache.fs.FSFile)

Example 28 with Connection

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;
    }
}
Also used : Connection(org.sql2o.Connection) ArrayList(java.util.ArrayList) ArchiveEntry(net.runelite.http.service.cache.beans.ArchiveEntry)

Example 29 with Connection

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 "";
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) LoginResponse(net.runelite.http.api.ws.messages.LoginResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthResponse(net.runelite.http.api.account.OAuthResponse) Response(com.github.scribejava.core.model.Response) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Connection(org.sql2o.Connection) UserEntry(net.runelite.http.service.account.beans.UserEntry) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 30 with Connection

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;
    }
}
Also used : Instant(java.time.Instant) Connection(org.sql2o.Connection) HiscoreEndpoint(net.runelite.http.api.hiscore.HiscoreEndpoint) PlayerEntity(net.runelite.http.service.xp.beans.PlayerEntity)

Aggregations

Connection (org.sql2o.Connection)31 Test (org.junit.Test)16 Query (org.sql2o.Query)7 UUID (java.util.UUID)5 Table (org.sql2o.data.Table)5 Instant (java.time.Instant)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Sql2oException (org.sql2o.Sql2oException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SessionEntry (net.runelite.http.service.account.beans.SessionEntry)2 PlayerEntity (net.runelite.http.service.xp.beans.PlayerEntity)2 Sql2o (org.sql2o.Sql2o)2 Row (org.sql2o.data.Row)2 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 SQLException (java.sql.SQLException)1