line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include "state.h" |
2
|
|
|
|
|
|
|
#include "context.h" |
3
|
|
|
|
|
|
|
#include "debug.h" |
4
|
|
|
|
|
|
|
#include "xsutil.h" |
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
|
|
|
HV* newState (pTHX) { |
7
|
4
|
|
|
|
|
|
return initState(aTHX_ newHV_mortal()); |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
|
|
|
HV* initState (pTHX_ HV* state) { |
11
|
8
|
|
|
|
|
|
XSUTIL_HV_STORE_REF(state, "schema", (SV*)newHV_mortal()); |
12
|
4
|
|
|
|
|
|
XSUTIL_HV_STORE_NOINC(state, "table", &PL_sv_undef); |
13
|
8
|
|
|
|
|
|
XSUTIL_HV_STORE(state, "context", XSUTIL_NEW_SVIV_MORTAL(CONTEXT_GLOBAL)); |
14
|
8
|
|
|
|
|
|
XSUTIL_HV_STORE(state, "_context", XSUTIL_NEW_SVIV_MORTAL(CONTEXT_GLOBAL)); |
15
|
8
|
|
|
|
|
|
XSUTIL_HV_STORE(state, "_nest", XSUTIL_NEW_SVIV_MORTAL(0)); |
16
|
8
|
|
|
|
|
|
XSUTIL_HV_STORE(state, "nest", XSUTIL_NEW_SVIV_MORTAL(0)); |
17
|
4
|
|
|
|
|
|
return state; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
985
|
|
|
|
|
|
SV* _get_parser_context (pTHX_ HV* state) { |
21
|
985
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(state, "context"); |
22
|
985
|
50
|
|
|
|
|
if (ssv) { |
23
|
985
|
|
|
|
|
|
return *ssv; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else { |
26
|
0
|
|
|
|
|
|
sv_dump((SV*)state); |
27
|
0
|
|
|
|
|
|
croak("Cannot get context."); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
207
|
|
|
|
|
|
SV* _get_parser_recent_context (pTHX_ HV* state) { |
32
|
207
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(state, "_context"); |
33
|
207
|
50
|
|
|
|
|
if (ssv) { |
34
|
207
|
|
|
|
|
|
return *ssv; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
else { |
37
|
0
|
|
|
|
|
|
sv_dump((SV*)state); |
38
|
0
|
|
|
|
|
|
croak("Cannot get recent context."); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
1675
|
|
|
|
|
|
SV* _get_nest (pTHX_ HV* state) { |
43
|
1675
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(state, "nest"); |
44
|
1675
|
50
|
|
|
|
|
if (ssv) { |
45
|
1675
|
|
|
|
|
|
return *ssv; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
0
|
|
|
|
|
|
sv_dump((SV*)state); |
49
|
0
|
|
|
|
|
|
croak("Cannot get nest."); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
207
|
|
|
|
|
|
SV* _get_recent_nest (pTHX_ HV* state) { |
54
|
207
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(state, "nest"); |
55
|
207
|
50
|
|
|
|
|
if (ssv) { |
56
|
207
|
|
|
|
|
|
return *ssv; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
0
|
|
|
|
|
|
sv_dump((SV*)state); |
60
|
0
|
|
|
|
|
|
croak("Cannot get nest."); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
139
|
|
|
|
|
|
void set_parser_context (pTHX_ HV* state, const IV context) { |
65
|
|
|
|
|
|
|
{ |
66
|
139
|
|
|
|
|
|
SV* context_sv = _get_parser_context(aTHX_ state); |
67
|
139
|
|
|
|
|
|
SV* _context_sv = _get_parser_recent_context(aTHX_ state); |
68
|
139
|
50
|
|
|
|
|
sv_setiv(_context_sv, SvIV(context_sv)); |
69
|
139
|
|
|
|
|
|
sv_setiv(context_sv, context); |
70
|
|
|
|
|
|
|
DEBUG_OUT("context: %d\n", (int)SvIV(context_sv)); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
{ |
73
|
139
|
|
|
|
|
|
SV* nest_sv = _get_nest(aTHX_ state); |
74
|
139
|
|
|
|
|
|
SV* _nest_sv = _get_recent_nest(aTHX_ state); |
75
|
139
|
50
|
|
|
|
|
sv_setiv(_nest_sv, SvIV(nest_sv)); |
76
|
139
|
|
|
|
|
|
sv_setiv(nest_sv, 0); |
77
|
|
|
|
|
|
|
DEBUG_OUT("nest: %d\n", (int)SvIV(nest_sv)); |
78
|
|
|
|
|
|
|
} |
79
|
139
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
68
|
|
|
|
|
|
void restore_context (pTHX_ HV* state) { |
82
|
|
|
|
|
|
|
{ |
83
|
68
|
|
|
|
|
|
SV* context_sv = _get_parser_context(aTHX_ state); |
84
|
68
|
|
|
|
|
|
SV* _context_sv = _get_parser_recent_context(aTHX_ state); |
85
|
68
|
50
|
|
|
|
|
sv_setiv(context_sv, SvIV(_context_sv)); |
86
|
68
|
|
|
|
|
|
sv_setiv(_context_sv, 0); |
87
|
|
|
|
|
|
|
DEBUG_OUT("context: %d\n", (int)SvIV(context_sv)); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
{ |
90
|
68
|
|
|
|
|
|
SV* nest_sv = _get_nest(aTHX_ state); |
91
|
68
|
|
|
|
|
|
SV* _nest_sv = _get_recent_nest(aTHX_ state); |
92
|
68
|
50
|
|
|
|
|
sv_setiv(nest_sv, SvIV(_nest_sv)); |
93
|
68
|
|
|
|
|
|
sv_setiv(_nest_sv, 0); |
94
|
|
|
|
|
|
|
DEBUG_OUT("nest: %d\n", (int)SvIV(nest_sv)); |
95
|
|
|
|
|
|
|
} |
96
|
68
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
389
|
|
|
|
|
|
IV get_parser_context (pTHX_ HV* state) { |
99
|
389
|
50
|
|
|
|
|
return SvIV(_get_parser_context(aTHX_ state)); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
708
|
|
|
|
|
|
IV get_nest (pTHX_ HV* state) { |
103
|
708
|
50
|
|
|
|
|
return SvIV(_get_nest(aTHX_ state)); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
36
|
|
|
|
|
|
void incr_nest (pTHX_ HV* state) { |
107
|
36
|
|
|
|
|
|
SV* nest = _get_nest(aTHX_ state); |
108
|
36
|
50
|
|
|
|
|
sv_setiv(nest, SvIV(nest) + 1); |
109
|
36
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
16
|
|
|
|
|
|
void decr_nest (pTHX_ HV* state) { |
112
|
16
|
|
|
|
|
|
SV* nest = _get_nest(aTHX_ state); |
113
|
16
|
50
|
|
|
|
|
sv_setiv(nest, SvIV(nest) - 1); |
114
|
16
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
11
|
|
|
|
|
|
void set_table (pTHX_ HV* state, const char* name, const size_t length) { |
117
|
11
|
|
|
|
|
|
SV* table = get_table(aTHX_ state); |
118
|
|
|
|
|
|
|
DEBUG_OUT("table name: %.*s\n", (int)length, name); |
119
|
11
|
100
|
|
|
|
|
if (SvOK(table)) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
120
|
7
|
|
|
|
|
|
sv_setpvn(table, name, length); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else { |
123
|
4
|
|
|
|
|
|
table = sv_2mortal(newSVpvn(name, length)); |
124
|
4
|
|
|
|
|
|
XSUTIL_HV_STORE(state, "table", table); |
125
|
|
|
|
|
|
|
} |
126
|
11
|
|
|
|
|
|
get_or_create_schema(aTHX_ state, table); |
127
|
11
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
75
|
|
|
|
|
|
SV* get_table (pTHX_ HV* state) { |
130
|
75
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(state, "table"); |
131
|
75
|
50
|
|
|
|
|
if (ssv) { |
132
|
|
|
|
|
|
|
DEBUG_OUT("table name: %s\n", SvPV_nolen(*ssv)); |
133
|
75
|
|
|
|
|
|
return *ssv; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else { |
136
|
|
|
|
|
|
|
return &PL_sv_undef; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
61
|
|
|
|
|
|
HV* get_current_schema (pTHX_ HV* state) { |
141
|
61
|
|
|
|
|
|
SV* table = get_table(aTHX_ state); |
142
|
61
|
|
|
|
|
|
return get_or_create_schema(aTHX_ state, table); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
75
|
|
|
|
|
|
HV* get_or_create_schema (pTHX_ HV* state, SV* key) { |
146
|
75
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(state, "schema"); |
147
|
75
|
50
|
|
|
|
|
if (!ssv) { |
148
|
0
|
|
|
|
|
|
sv_dump((SV*)state); |
149
|
0
|
|
|
|
|
|
croak("Cannot get schema."); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
75
|
|
|
|
|
|
HV* schema = (HV*)SvRV(*ssv); |
153
|
75
|
|
|
|
|
|
HE* entry = XSUTIL_HV_FETCH_ENT(schema, key); |
154
|
75
|
100
|
|
|
|
|
if (entry) { |
155
|
|
|
|
|
|
|
DEBUG_OUT("get table: %s\n", SvPV_nolen(key)); |
156
|
69
|
|
|
|
|
|
return (HV*) SvRV(HeVAL(entry)); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
else { |
159
|
|
|
|
|
|
|
DEBUG_OUT("create table: %s\n", SvPV_nolen(key)); |
160
|
6
|
|
|
|
|
|
HV* table = newHV_mortal(); |
161
|
12
|
|
|
|
|
|
XSUTIL_HV_STORE_ENT_REF(schema, key, (SV*)table); |
162
|
6
|
|
|
|
|
|
return table; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
64
|
|
|
|
|
|
AV* get_or_create_columns (pTHX_ HV* table) { |
167
|
64
|
|
|
|
|
|
SV** ssv = XSUTIL_HV_FETCH(table, "columns"); |
168
|
64
|
100
|
|
|
|
|
if (ssv) { |
169
|
|
|
|
|
|
|
DEBUG_OUT("get columns\n"); |
170
|
58
|
|
|
|
|
|
return (AV*)SvRV(*ssv); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
else { |
173
|
|
|
|
|
|
|
DEBUG_OUT("create columns\n"); |
174
|
6
|
|
|
|
|
|
AV* columns = newAV_mortal(); |
175
|
12
|
|
|
|
|
|
XSUTIL_HV_STORE_REF(table, "columns", (SV*)columns); |
176
|
6
|
|
|
|
|
|
return columns; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |