All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tarantool.SqlProtoUtils Maven / Gradle / Ivy

There is a newer version: 1.9.4
Show newest version
package org.tarantool;

import org.tarantool.protocol.TarantoolPacket;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public abstract class SqlProtoUtils {
    public static List> readSqlResult(TarantoolPacket pack) {
        List> data = (List>) pack.getBody().get(Key.DATA.getId());

        List> values = new ArrayList<>(data.size());
        List metaData = getSQLMetadata(pack);
        for (List row : data) {
            LinkedHashMap value = new LinkedHashMap<>();
            for (int i = 0; i < row.size(); i++) {
                value.put(metaData.get(i).getName(), row.get(i));
            }
            values.add(value);
        }
        return values;
    }

    public static List> getSQLData(TarantoolPacket pack) {
        return (List>) pack.getBody().get(Key.DATA.getId());
    }

    public static List getSQLMetadata(TarantoolPacket pack) {
        List> meta = (List>) pack.getBody().get(Key.SQL_METADATA.getId());
        List values = new ArrayList<>(meta.size());
        for (Map c : meta) {
            values.add(new SQLMetaData((String) c.get(Key.SQL_FIELD_NAME.getId())));
        }
        return values;
    }

    public static Long getSqlRowCount(TarantoolPacket pack) {
        Map info = (Map) pack.getBody().get(Key.SQL_INFO.getId());
        Number rowCount;
        if (info != null && (rowCount = ((Number) info.get(Key.SQL_ROW_COUNT.getId()))) != null) {
            return rowCount.longValue();
        }
        return null;
    }

    public static class SQLMetaData {
        protected String name;

        public SQLMetaData(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return "SQLMetaData{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy