m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java
blob: f0496034f25ca94294fb1db14028c9aeb5cbf2ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package pl.edu.mimuw.cloudatlas.model;

import java.io.ByteArrayInputStream;

import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program;
import pl.edu.mimuw.cloudatlas.interpreter.query.parser;
import pl.edu.mimuw.cloudatlas.interpreter.query.Yylex;
import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.querysigner.QueryData;

/**
 * A class that holds a CloudAtlas query.
 */
public class ValueQuery extends Value {
    // Original source code
    private String code;
    // Parsed query
    private Program query;
    // Query signature
    private byte[] signature;
    // Query signing timestamp
    private long timestamp;

    /**
     * Constructs a new <code>ValueQuery</code> object.
     *
     * @param query the code of the query
     */
    public ValueQuery(String query) throws Exception {
        this.code = query;
        if (!query.isEmpty()) {
            Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes()));
            this.query = (new parser(lex)).pProgram();
        }
        this.signature = null;
        this.timestamp = System.currentTimeMillis();
    }

    public ValueQuery(String query, byte[] querySignature) throws Exception {
        this.code = query;
        if (!query.isEmpty()) {
            Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes()));
            this.query = (new parser(lex)).pProgram();
        }
        this.signature = querySignature;
        this.timestamp = System.currentTimeMillis();
    }

    public ValueQuery(QueryData queryData) throws Exception {
        this.code = queryData.getCode();
        if (!queryData.getCode().isEmpty()) {
            Yylex lex = new Yylex(new ByteArrayInputStream(queryData.getCode().getBytes()));
            this.query = (new parser(lex)).pProgram();
        }
        this.signature = queryData.getSignature();
        this.timestamp = System.currentTimeMillis();
    }

    public ValueQuery(String query, long timestamp) throws Exception {
        this.code = query;
        if (!query.isEmpty()) {
            Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes()));
            this.query = (new parser(lex)).pProgram();
        }
        this.signature = null;
        this.timestamp = timestamp;
    }

    private ValueQuery() {
        this.code = null;
        this.query = null;
        this.signature = null;
        this.timestamp = System.currentTimeMillis();
    }

    public String getCode() { return code; }

    public Program getQuery() {
        return query;
    }

    public byte[] getSignature() { return signature; }

    public long getTimestamp() { return timestamp; }

    public void setTimestamp(long timestamp) { this.timestamp = timestamp; }

    @Override
    public Type getType() {
        return TypePrimitive.QUERY;
    }

    @Override
    public boolean isNull() {
        return query == null || code == null;
    }

    public Value isEqual(Value value) {
        sameTypesOrThrow(value, Operation.EQUAL);
        if(isNull() && value.isNull())
            return new ValueBoolean(true);
        else if(isNull() || value.isNull())
            return new ValueBoolean(false);
        return new ValueBoolean(code.equals(((ValueQuery)value).code));
    }

    @Override
    public Value getDefaultValue() {
        return new ValueQuery();
    }

    @Override
    public Value convertTo(Type type) {
        switch(type.getPrimaryType()) {
            case QUERY:
                return this;
            case STRING:
                return isNull() ? ValueString.NULL_STRING : new ValueString(code);
            default:
                throw new UnsupportedConversionException(getType(), type);
        }
    }
}