1#!/usr/bin/env lua
2
3
4do
5
6
7local _parse_sql_actions = { [0] =
8  0, 1, 0, 1, 1, 2, 0, 2, 2, 0, 9, 2, 0, 10, 2, 0, 11, 2, 0, 13,
9  2, 1, 2, 2, 1, 6, 3, 0, 3, 4, 3, 0, 3, 5, 3, 0, 3, 7, 3, 0,
10  3, 8, 3, 0, 3, 12, 4, 0, 2, 3, 7, 4, 0, 3, 8, 11
11};
12
13local _parse_sql_trans_keys = { [0] =
14  0, 0, 45, 45, 10, 10, 42, 42, 10, 42, 10, 47, 82, 82,
15  69, 69, 65, 65, 84, 84, 69, 69, 32, 32, 68, 84, 65,
16  65, 84, 84, 65, 65, 66, 66, 65, 65, 83, 83, 69, 69,
17  9, 47, 9, 96, 45, 45, 10, 10, 42, 42, 10, 42, 10, 47,
18  10, 96, 10, 96, 9, 47, 9, 59, 45, 45, 10, 10, 42,
19  42, 10, 42, 10, 47, 65, 65, 66, 66, 76, 76, 69, 69,
20  32, 32, 73, 96, 70, 70, 32, 32, 78, 78, 79, 79, 84, 84,
21  32, 32, 69, 69, 88, 88, 73, 73, 83, 83, 84, 84, 83,
22  83, 32, 32, 96, 96, 10, 96, 10, 96, 32, 32, 40, 40,
23  10, 10, 32, 41, 32, 32, 75, 96, 69, 69, 89, 89, 32, 32,
24  96, 96, 10, 96, 10, 96, 10, 10, 82, 82, 73, 73, 77,
25  77, 65, 65, 82, 82, 89, 89, 32, 32, 75, 75, 69, 69,
26  89, 89, 32, 32, 78, 78, 73, 73, 81, 81, 85, 85, 69, 69,
27  32, 32, 75, 75, 10, 96, 10, 96, 10, 10, 10, 59, 10,
28  59, 82, 82, 79, 79, 80, 80, 32, 32, 84, 84, 65, 65,
29  66, 66, 76, 76, 69, 69, 32, 32, 73, 73, 70, 70, 32, 32,
30  69, 69, 88, 88, 73, 73, 83, 83, 84, 84, 83, 83, 32,
31  32, 96, 96, 10, 96, 10, 96, 59, 59, 78, 78, 83, 83,
32  69, 69, 82, 82, 84, 84, 32, 32, 73, 73, 78, 78, 84, 84,
33  79, 79, 32, 32, 96, 96, 10, 96, 10, 96, 32, 32, 40,
34  86, 10, 41, 32, 32, 86, 86, 65, 65, 76, 76, 85, 85,
35  69, 69, 83, 83, 32, 32, 40, 40, 39, 78, 10, 92, 10, 92,
36  41, 44, 44, 59, 32, 78, 48, 57, 41, 57, 48, 57, 41,
37  57, 85, 85, 76, 76, 76, 76, 34, 116, 79, 79, 67, 67,
38  75, 75, 32, 32, 84, 84, 65, 65, 66, 66, 76, 76, 69, 69,
39  83, 83, 32, 32, 96, 96, 10, 96, 10, 96, 32, 32, 87,
40  87, 82, 82, 73, 73, 84, 84, 69, 69, 69, 69, 84, 84,
41  32, 32, 10, 59, 10, 59, 78, 83, 76, 76, 79, 79, 67, 67,
42  75, 75, 32, 32, 84, 84, 65, 65, 66, 66, 76, 76, 69,
43  69, 83, 83, 69, 69, 9, 85, 0
44};
45
46local _parse_sql_key_spans = { [0] =
47  0, 1, 1, 1, 33, 38, 1, 1, 1, 1, 1, 1, 17, 1, 1, 1, 1, 1, 1, 1,
48  39, 88, 1, 1, 1, 33, 38, 87, 87, 39, 51, 1, 1, 1, 33, 38, 1, 1, 1, 1,
49  1, 24, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 87, 87, 1, 1,
50  1, 10, 1, 22, 1, 1, 1, 1, 87, 87, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51  1, 1, 1, 1, 1, 1, 1, 1, 1, 87, 87, 1, 50, 50, 1, 1, 1, 1, 1, 1,
52  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 87, 87, 1, 1, 1,
53  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 87, 87, 1, 47, 32, 1, 1, 1, 1, 1,
54  1, 1, 1, 1, 40, 83, 83, 4, 16, 47, 10, 17, 10, 17, 1, 1, 1, 83, 1, 1,
55  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 87, 87, 1, 1, 1, 1, 1, 1, 1, 1,
56  1, 50, 50, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 77
57};
58
59local _parse_sql_index_offsets = { [0] =
60  0, 0, 2, 4, 6, 40, 79, 81, 83, 85, 87, 89, 91, 109, 111, 113, 115, 117, 119, 121,
61  123, 163, 252, 254, 256, 258, 292, 331, 419, 507, 547, 599, 601, 603, 605, 639, 678, 680, 682, 684,
62  686, 688, 713, 715, 717, 719, 721, 723, 725, 727, 729, 731, 733, 735, 737, 739, 741, 829, 917, 919,
63  921, 923, 934, 936, 959, 961, 963, 965, 967, 1055, 1143, 1145, 1147, 1149, 1151, 1153, 1155, 1157, 1159, 1161,
64  1163, 1165, 1167, 1169, 1171, 1173, 1175, 1177, 1179, 1181, 1269, 1357, 1359, 1410, 1461, 1463, 1465, 1467, 1469, 1471,
65  1473, 1475, 1477, 1479, 1481, 1483, 1485, 1487, 1489, 1491, 1493, 1495, 1497, 1499, 1501, 1503, 1591, 1679, 1681, 1683,
66  1685, 1687, 1689, 1691, 1693, 1695, 1697, 1699, 1701, 1703, 1705, 1793, 1881, 1883, 1931, 1964, 1966, 1968, 1970, 1972,
67  1974, 1976, 1978, 1980, 1982, 2023, 2107, 2191, 2196, 2213, 2261, 2272, 2290, 2301, 2319, 2321, 2323, 2325, 2409, 2411,
68  2413, 2415, 2417, 2419, 2421, 2423, 2425, 2427, 2429, 2431, 2433, 2521, 2609, 2611, 2613, 2615, 2617, 2619, 2621, 2623,
69  2625, 2627, 2678, 2729, 2736, 2738, 2740, 2742, 2744, 2746, 2748, 2750, 2752, 2754, 2756, 2758, 2760
70};
71
72local _parse_sql_indicies = { [0] =
73  0, 1, 2, 0, 3, 1, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
74  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3,
75  4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
76  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 6, 3, 7,
77  1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 1, 1, 1, 1, 1, 1, 1,
78  1, 1, 1, 1, 1, 1, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20,
79  1, 21, 1, 22, 23, 22, 22, 22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80  1, 1, 1, 1, 1, 1, 22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 24,
81  1, 25, 1, 22, 23, 22, 22, 22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
82  1, 1, 1, 1, 1, 1, 22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 24,
83  1, 25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 26, 1, 27, 1, 23, 27, 28, 1, 29, 28,
86  28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
87  28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 30, 28, 29, 28, 28, 28, 28, 28, 28, 28,
88  28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
89  28, 28, 28, 28, 30, 28, 28, 28, 28, 22, 28, 32, 31, 31, 31, 31, 31, 31, 31, 31,
90  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
91  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
92  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
93  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 1, 31, 32,
94  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
95  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
96  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
97  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
98  31, 31, 31, 31, 31, 33, 31, 34, 35, 34, 34, 34, 1, 1, 1, 1, 1, 1, 1, 1,
99  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 34, 1, 1, 1, 1, 1, 1, 1, 1, 1,
100  1, 1, 1, 36, 1, 37, 1, 34, 35, 34, 34, 34, 1, 1, 1, 1, 1, 1, 1, 1,
101  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 34, 1, 1, 1, 1, 1, 1, 1, 1, 1,
102  1, 1, 1, 36, 1, 37, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 38,
103  1, 35, 38, 39, 1, 40, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39,
104  39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 41, 39, 40,
105  39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39,
106  39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 41, 39, 39, 39, 39, 34, 39, 42, 1,
107  43, 1, 44, 1, 45, 1, 46, 1, 47, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 48, 1, 49, 1, 50, 1, 51, 1, 52,
109  1, 53, 1, 54, 1, 55, 1, 56, 1, 57, 1, 58, 1, 59, 1, 60, 1, 61, 1, 48,
110  1, 63, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
111  62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
112  62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
113  62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
114  62, 62, 62, 62, 62, 62, 62, 1, 62, 65, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
115  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
116  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
117  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
118  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 66, 64, 67, 1, 68,
119  1, 69, 1, 70, 1, 1, 1, 1, 1, 1, 1, 1, 71, 1, 72, 1, 73, 1, 1, 1,
120  1, 74, 1, 1, 1, 1, 75, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 76, 1, 77,
121  1, 78, 1, 79, 1, 80, 1, 82, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
122  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
123  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
124  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
125  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 1, 81, 82, 81, 81, 81, 81,
126  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
127  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
128  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
129  81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
130  81, 83, 81, 69, 83, 84, 1, 85, 1, 86, 1, 87, 1, 88, 1, 89, 1, 90, 1, 91,
131  1, 92, 1, 93, 1, 83, 1, 94, 1, 95, 1, 96, 1, 97, 1, 98, 1, 99, 1, 73,
132  1, 101, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
133  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
134  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
135  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
136  100, 100, 100, 100, 100, 100, 100, 1, 100, 103, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102,
137  102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102,
138  102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102,
139  102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102,
140  102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 104, 102, 105, 83, 106,
141  71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
142  71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
143  71, 71, 71, 71, 71, 71, 71, 71, 107, 71, 108, 71, 71, 71, 71, 71, 71, 71, 71, 71,
144  71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
145  71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 107,
146  71, 109, 1, 110, 1, 111, 1, 112, 1, 113, 1, 114, 1, 115, 1, 116, 1, 117, 1, 118,
147  1, 119, 1, 120, 1, 121, 1, 122, 1, 123, 1, 124, 1, 125, 1, 126, 1, 127, 1, 128,
148  1, 129, 1, 131, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
149  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
150  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
151  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
152  130, 130, 130, 130, 130, 130, 130, 130, 130, 1, 130, 131, 130, 130, 130, 130, 130, 130, 130, 130,
153  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
154  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
155  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
156  130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 132, 130, 6,
157  1, 133, 1, 134, 1, 135, 1, 136, 1, 137, 1, 138, 1, 139, 1, 140, 1, 141, 1, 142,
158  1, 143, 1, 144, 1, 146, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
159  145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
160  145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
161  145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
162  145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 1, 145, 148, 147, 147, 147, 147, 147, 147,
163  147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147,
164  147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147,
165  147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147,
166  147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 149,
167  147, 150, 1, 151, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
168  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
169  1, 1, 1, 1, 1, 1, 1, 1, 1, 152, 1, 153, 151, 151, 151, 151, 151, 151, 151, 151,
170  151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151,
171  151, 151, 154, 151, 155, 1, 152, 1, 156, 1, 157, 1, 158, 1, 159, 1, 160, 1, 161, 1,
172  162, 1, 163, 1, 1, 1, 1, 1, 164, 1, 1, 165, 165, 165, 165, 165, 165, 165, 165, 165,
173  165, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174  1, 166, 1, 168, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
175  167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 169, 167, 167, 167, 167, 167, 167, 167,
176  167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
177  167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
178  167, 167, 167, 167, 167, 170, 167, 172, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171,
179  171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 173, 171, 171, 171,
180  171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171,
181  171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171,
182  171, 171, 171, 171, 171, 171, 171, 171, 171, 174, 171, 175, 1, 1, 176, 1, 161, 1, 1, 1,
183  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 177, 1, 178, 1, 1, 1, 1, 1, 1,
184  163, 1, 1, 1, 1, 1, 164, 1, 1, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 1,
185  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 166,
186  1, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 1, 180, 1, 1, 181, 1, 182, 1, 179,
187  179, 179, 179, 179, 179, 179, 179, 179, 179, 1, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183,
188  1, 180, 1, 1, 181, 1, 1, 1, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 1, 184,
189  1, 185, 1, 186, 1, 171, 1, 1, 171, 1, 171, 1, 1, 1, 1, 1, 1, 1, 1, 171,
190  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
191  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
192  1, 171, 1, 171, 1, 1, 171, 1, 1, 171, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
193  1, 171, 1, 1, 1, 171, 1, 171, 1, 187, 1, 188, 1, 189, 1, 190, 1, 191, 1, 192,
194  1, 193, 1, 194, 1, 195, 1, 196, 1, 197, 1, 198, 1, 200, 199, 199, 199, 199, 199, 199,
195  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
196  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
197  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
198  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 1,
199  199, 200, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
200  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
201  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
202  199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
203  199, 199, 199, 199, 199, 199, 199, 201, 199, 202, 1, 203, 1, 204, 1, 205, 1, 206, 1, 132,
204  1, 207, 1, 208, 1, 209, 1, 210, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209,
205  209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209,
206  209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 211, 209, 2, 209,
207  209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209,
208  209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209, 209,
209  209, 209, 209, 209, 209, 209, 209, 211, 209, 212, 1, 1, 1, 1, 213, 1, 214, 1, 215, 1,
210  216, 1, 217, 1, 218, 1, 219, 1, 220, 1, 221, 1, 222, 1, 223, 1, 132, 1, 127, 1,
211  6, 2, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
212  1, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 224, 1, 225, 1,
213  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 226, 227,
214  1, 1, 1, 1, 228, 1, 1, 229, 1, 1, 1, 1, 1, 1, 230, 1, 231, 1, 0
215};
216
217local _parse_sql_trans_targs = { [0] =
218  2, 0, 196, 4, 4, 5, 196, 7, 8, 9, 10, 11, 12, 13, 36, 14, 15, 16, 17, 18,
219  19, 20, 21, 21, 22, 24, 27, 23, 25, 25, 26, 28, 28, 29, 30, 30, 31, 33, 32, 34,
220  34, 35, 37, 38, 39, 40, 41, 42, 56, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
221  54, 55, 57, 57, 57, 57, 58, 59, 60, 61, 62, 92, 63, 64, 71, 82, 89, 65, 66, 67,
222  68, 69, 69, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88,
223  90, 90, 90, 90, 91, 70, 92, 93, 196, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
224  106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 116, 117, 119, 120, 121, 122, 123, 124, 125,
225  126, 127, 128, 129, 130, 131, 131, 131, 131, 132, 133, 134, 137, 134, 135, 136, 138, 139, 140, 141,
226  142, 143, 144, 145, 150, 151, 154, 146, 146, 147, 157, 146, 146, 147, 157, 148, 149, 196, 144, 151,
227  148, 149, 152, 153, 155, 156, 147, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
228  171, 172, 173, 174, 175, 176, 177, 179, 180, 181, 181, 182, 184, 195, 185, 186, 187, 188, 189, 190,
229  191, 192, 193, 194, 1, 3, 6, 94, 118, 158, 178, 183
230};
231
232local _parse_sql_trans_actions = { [0] =
233  1, 0, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
234  1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 3, 1, 1, 1, 1,
235  3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
236  1, 1, 5, 20, 1, 3, 30, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
237  1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
238  5, 20, 1, 3, 26, 3, 3, 1, 23, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
239  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1,
240  1, 1, 1, 1, 1, 5, 20, 1, 3, 42, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1,
241  1, 1, 11, 1, 5, 5, 1, 5, 20, 46, 5, 1, 3, 34, 1, 14, 1, 17, 1, 1,
242  51, 38, 1, 1, 1, 1, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
243  3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1,
244  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
245};
246
247local parse_sql_start = 196;
248local parse_sql_first_final = 196;
249local parse_sql_error = 0;
250
251local parse_sql_en_main = 196;
252
253
254
255
256local _sql_unescapes = setmetatable({
257	["\\0"] = "\0";
258	["\\'"] = "'";
259	["\\\""] = "\"";
260	["\\b"] = "\b";
261	["\\n"] = "\n";
262	["\\r"] = "\r";
263	["\\t"] = "\t";
264	["\\Z"] = "\26";
265	["\\\\"] = "\\";
266	["\\%"] = "%";
267	["\\_"] = "_";
268},{ __index = function(t, s) assert(false, "Unknown escape sequences: "..s); end });
269
270function parse_sql(data, h)
271	local p = 1;
272	local pe = #data + 1;
273	local cs;
274
275	local pos_char, pos_line = 1, 1;
276
277	local mark, token;
278	local table_name, columns, value_lists, value_list, value_count;
279
280
281  cs = parse_sql_start;
282
283--  ragel flat exec
284
285  local testEof = false;
286  local _slen = 0;
287  local _trans = 0;
288  local _keys = 0;
289  local _inds = 0;
290  local _acts = 0;
291  local _nacts = 0;
292  local _tempval = 0;
293  local _goto_level = 0;
294  local _resume = 10;
295  local _eof_trans = 15;
296  local _again = 20;
297  local _test_eof = 30;
298  local _out = 40;
299
300  while true do -- goto loop
301  local _continue = false;
302  repeat
303    local _trigger_goto = false;
304    if _goto_level <= 0 then
305
306-- noEnd
307      if p == pe then
308        _goto_level = _test_eof;
309        _continue = true; break;
310      end
311
312
313-- errState != 0
314      if cs == 0 then
315        _goto_level = _out;
316       _continue = true; break;
317      end
318    end -- _goto_level <= 0
319
320     if _goto_level <= _resume then
321      _keys = cs * 2; -- LOCATE_TRANS
322      _inds = _parse_sql_index_offsets[cs];
323      _slen = _parse_sql_key_spans[cs];
324
325      if   _slen > 0 and
326         _parse_sql_trans_keys[_keys] <= data:byte(p) and
327         data:byte(p) <= _parse_sql_trans_keys[_keys + 1] then
328        _trans = _parse_sql_indicies[ _inds + data:byte(p) - _parse_sql_trans_keys[_keys] ];
329      else _trans =_parse_sql_indicies[ _inds + _slen ]; end
330
331    cs = _parse_sql_trans_targs[_trans];
332
333    if _parse_sql_trans_actions[_trans] ~= 0 then
334      _acts = _parse_sql_trans_actions[_trans];
335      _nacts = _parse_sql_actions[_acts];
336      _acts = _acts + 1;
337
338      while _nacts > 0 do
339        _nacts = _nacts - 1;
340        _acts = _acts + 1;
341        _tempval = _parse_sql_actions[_acts - 1];
342
343     -- start action switch
344        if _tempval  == 0 then --4 FROM_STATE_ACTION_SWITCH
345-- line 34 "sql.rl" -- end of line directive
346       pos_char = pos_char + 1;       -- ACTION
347        elseif _tempval  == 1 then --4 FROM_STATE_ACTION_SWITCH
348-- line 35 "sql.rl" -- end of line directive
349       pos_line = pos_line + 1; pos_char = 1;       -- ACTION
350        elseif _tempval  == 2 then --4 FROM_STATE_ACTION_SWITCH
351-- line 38 "sql.rl" -- end of line directive
352       mark = p;       -- ACTION
353        elseif _tempval  == 3 then --4 FROM_STATE_ACTION_SWITCH
354-- line 39 "sql.rl" -- end of line directive
355       token = data:sub(mark, p-1);       -- ACTION
356        elseif _tempval  == 4 then --4 FROM_STATE_ACTION_SWITCH
357-- line 52 "sql.rl" -- end of line directive
358       table.insert(columns, token); columns[#columns] = token;       -- ACTION
359        elseif _tempval  == 5 then --4 FROM_STATE_ACTION_SWITCH
360-- line 58 "sql.rl" -- end of line directive
361       table_name,columns = token,{};       -- ACTION
362        elseif _tempval  == 6 then --4 FROM_STATE_ACTION_SWITCH
363-- line 59 "sql.rl" -- end of line directive
364       h.create(table_name, columns);       -- ACTION
365        elseif _tempval  == 7 then --4 FROM_STATE_ACTION_SWITCH
366-- line 65 "sql.rl" -- end of line directive
367
368			value_count = value_count + 1; value_list[value_count] = token:gsub("\\.", _sql_unescapes);
369		      -- ACTION
370        elseif _tempval  == 8 then --4 FROM_STATE_ACTION_SWITCH
371-- line 68 "sql.rl" -- end of line directive
372       value_count = value_count + 1; value_list[value_count] = tonumber(token);       -- ACTION
373        elseif _tempval  == 9 then --4 FROM_STATE_ACTION_SWITCH
374-- line 69 "sql.rl" -- end of line directive
375       value_count = value_count + 1;       -- ACTION
376        elseif _tempval  == 10 then --4 FROM_STATE_ACTION_SWITCH
377-- line 71 "sql.rl" -- end of line directive
378       value_list,value_count = {},0;       -- ACTION
379        elseif _tempval  == 11 then --4 FROM_STATE_ACTION_SWITCH
380-- line 71 "sql.rl" -- end of line directive
381       table.insert(value_lists, value_list);       -- ACTION
382        elseif _tempval  == 12 then --4 FROM_STATE_ACTION_SWITCH
383-- line 74 "sql.rl" -- end of line directive
384       table_name,value_lists = token,{};       -- ACTION
385        elseif _tempval  == 13 then --4 FROM_STATE_ACTION_SWITCH
386-- line 75 "sql.rl" -- end of line directive
387       h.insert(table_name, value_lists);       -- ACTION
388        end
389-- line 355 "sql.lua" -- end of line directive
390    -- end action switch
391      end -- while _nacts
392    end
393
394    if _trigger_goto then _continue = true; break; end
395    end -- endif
396
397    if _goto_level <= _again then
398      if cs == 0 then
399        _goto_level = _out;
400        _continue = true; break;
401      end
402      p = p + 1;
403      if p ~= pe then
404        _goto_level = _resume;
405        _continue = true; break;
406      end
407    end -- _goto_level <= _again
408
409    if _goto_level <= _test_eof then
410    end -- _goto_level <= _test_eof
411
412    if _goto_level <= _out then break; end
413  _continue = true;
414  until true;
415  if not _continue then break; end
416  end -- endif _goto_level <= out
417
418  -- end of execute block
419
420
421	if cs < parse_sql_first_final then
422		print("parse_sql: there was an error, line "..pos_line.." column "..pos_char);
423	else
424		print("Success. EOF at line "..pos_line.." column "..pos_char)
425	end
426end
427
428end
429
430-- import modules
431package.path = package.path..";../?.lua;";
432
433local my_name = arg[0];
434if my_name:match("[/\\]") then
435	package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua");
436	package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
437end
438
439
440-- ugly workaround for getting datamanager to work outside of prosody :(
441prosody = { };
442prosody.platform = "unknown";
443if os.getenv("WINDIR") then
444	prosody.platform = "windows";
445elseif package.config:sub(1,1) == "/" then
446	prosody.platform = "_posix";
447end
448package.loaded["util.logger"] = {init = function() return function() end; end}
449
450local dm = require "util.datamanager";
451dm.set_data_path("data");
452
453local datetime = require "util.datetime";
454
455local st = require "util.stanza";
456local parse_xml = require "util.xml".parse;
457
458function store_password(username, host, password)
459	-- create or update account for username@host
460	local ret, err = dm.store(username, host, "accounts", {password = password});
461	print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password);
462end
463
464function store_vcard(username, host, stanza)
465	-- create or update vCard for username@host
466	local ret, err = dm.store(username, host, "vcard", st.preserialize(stanza));
467	print("["..(err or "success").."] stored vCard: "..username.."@"..host);
468end
469
470function store_roster(username, host, roster_items)
471	-- fetch current roster-table for username@host if he already has one
472	local roster = dm.load(username, host, "roster") or {};
473	-- merge imported roster-items with loaded roster
474	for item_tag in roster_items:childtags() do
475		-- jid for this roster-item
476		local item_jid = item_tag.attr.jid
477		-- validate item stanzas
478		if (item_tag.name == "item") and (item_jid ~= "") then
479			-- prepare roster item
480			-- TODO: is the subscription attribute optional?
481			local item = {subscription = item_tag.attr.subscription, groups = {}};
482			-- optional: give roster item a real name
483			if item_tag.attr.name then
484				item.name = item_tag.attr.name;
485			end
486			-- optional: iterate over group stanzas inside item stanza
487			for group_tag in item_tag:childtags() do
488				local group_name = group_tag:get_text();
489				if (group_tag.name == "group") and (group_name ~= "") then
490					item.groups[group_name] = true;
491				else
492					print("[error] invalid group stanza: "..group_tag:pretty_print());
493				end
494			end
495			-- store item in roster
496			roster[item_jid] = item;
497			print("[success] roster entry: " ..username.."@"..host.." - "..item_jid);
498		else
499			print("[error] invalid roster stanza: " ..item_tag:pretty_print());
500		end
501
502	end
503	-- store merged roster-table
504	local ret, err = dm.store(username, host, "roster", roster);
505	print("["..(err or "success").."] stored roster: " ..username.."@"..host);
506end
507
508function store_subscription_request(username, host, presence_stanza)
509	local from_bare = presence_stanza.attr.from;
510
511	-- fetch current roster-table for username@host if he already has one
512	local roster = dm.load(username, host, "roster") or {};
513
514	local item = roster[from_bare];
515	if item and (item.subscription == "from" or item.subscription == "both") then
516		return; -- already subscribed, do nothing
517	end
518
519	-- add to table of pending subscriptions
520	if not roster.pending then roster.pending = {}; end
521	roster.pending[from_bare] = true;
522
523	-- store updated roster-table
524	local ret, err = dm.store(username, host, "roster", roster);
525	print("["..(err or "success").."] stored subscription request: " ..username.."@"..host.." - "..from_bare);
526end
527
528local os_date = os.date;
529local os_time = os.time;
530local os_difftime = os.difftime;
531function datetime_parse(s)
532	if s then
533		local year, month, day, hour, min, sec, tzd;
534		year, month, day, hour, min, sec, tzd = s:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d)%.?%d*([Z+%-]?.*)$");
535		if year then
536			local time_offset = os_difftime(os_time(os_date("*t")), os_time(os_date("!*t"))); -- to deal with local timezone
537			local tzd_offset = 0;
538			if tzd ~= "" and tzd ~= "Z" then
539				local sign, h, m = tzd:match("([+%-])(%d%d):?(%d*)");
540				if not sign then return; end
541				if #m ~= 2 then m = "0"; end
542				h, m = tonumber(h), tonumber(m);
543				tzd_offset = h * 60 * 60 + m * 60;
544				if sign == "-" then tzd_offset = -tzd_offset; end
545			end
546			sec = (sec + time_offset) - tzd_offset;
547			return os_time({year=year, month=month, day=day, hour=hour, min=min, sec=sec, isdst=false});
548		end
549	end
550end
551
552function store_offline_messages(username, host, stanza)
553	-- TODO: maybe use list_load(), append and list_store() instead
554	--       of constantly reopening the file with list_append()?
555	--for ch in offline_messages:childtags() do
556		--print("message :"..ch:pretty_print());
557		stanza.attr.node = nil;
558
559		local stamp = stanza:get_child("x", "jabber:x:delay");
560		if not stamp or not stamp.attr.stamp then print(2) return; end
561
562		for i=1,#stanza do if stanza[i] == stamp then table.remove(stanza, i); break; end end
563		for i=1,#stanza.tags do if stanza.tags[i] == stamp then table.remove(stanza.tags, i); break; end end
564
565		local parsed_stamp = datetime_parse(stamp.attr.stamp);
566		if not parsed_stamp then print(1, stamp.attr.stamp) return; end
567
568		stanza.attr.stamp, stanza.attr.stamp_legacy = datetime.datetime(parsed_stamp), datetime.legacy(parsed_stamp);
569		local ret, err = dm.list_append(username, host, "offline", st.preserialize(stanza));
570		print("["..(err or "success").."] stored offline message: " ..username.."@"..host.." - "..stanza.attr.from);
571	--end
572end
573
574-- load data
575local arg = ...;
576local help = "/? -? ? /h -h /help -help --help";
577if not arg or help:find(arg, 1, true) then
578	print([[XEP-227 importer for Prosody
579
580  Usage: jabberd14sql2prosody.lua filename.sql
581]]);
582	os.exit(1);
583end
584local f = io.open(arg);
585local s = f:read("*a");
586f:close();
587
588local table_count = 0;
589local insert_count = 0;
590local row_count = 0;
591-- parse
592parse_sql(s, {
593	create = function(table_name, columns)
594		--[[print(table_name);]]
595		table_count = table_count + 1;
596	end;
597	insert = function(table_name, value_lists)
598		--[[print(table_name, #value_lists);]]
599		insert_count = insert_count + 1;
600		row_count = row_count + #value_lists;
601
602		for _,value_list in ipairs(value_lists) do
603			if table_name == "users" then
604				local user, realm, password = unpack(value_list);
605				store_password(user, realm, password);
606			elseif table_name == "roster" then
607				local user, realm, xml = unpack(value_list);
608				local stanza,err = parse_xml(xml);
609				if stanza then
610					store_roster(user, realm, stanza);
611				else
612					print("[error] roster: XML parsing failed for "..user.."@"..realm..": "..err);
613				end
614			elseif table_name == "vcard" then
615				local user, realm, name, email, nickname, birthday, photo, xml = unpack(value_list);
616				if xml then
617					local stanza,err = parse_xml(xml);
618					if stanza then
619						store_vcard(user, realm, stanza);
620					else
621						print("[error] vcard: XML parsing failed for "..user.."@"..realm..": "..err);
622					end
623				else
624					--print("[warn] vcard: NULL vCard for "..user.."@"..realm..": "..err);
625				end
626			elseif table_name == "storedsubscriptionrequests" then
627				local user, realm, fromjid, xml = unpack(value_list);
628				local stanza,err = parse_xml(xml);
629				if stanza then
630					store_subscription_request(user, realm, stanza);
631				else
632					print("[error] storedsubscriptionrequests: XML parsing failed for "..user.."@"..realm..": "..err);
633				end
634			elseif table_name == "messages" then
635				--local user, realm, node, correspondent, type, storetime, delivertime, subject, body, xml = unpack(value_list);
636				local user, realm, type, xml = value_list[1], value_list[2], value_list[5], value_list[10];
637				if type == "offline" and xml ~= "" then
638					local stanza,err = parse_xml(xml);
639					if stanza then
640						store_offline_messages(user, realm, stanza);
641					else
642						print("[error] offline messages: XML parsing failed for "..user.."@"..realm..": "..err);
643						print(unpack(value_list));
644					end
645				end
646			end
647		end
648	end;
649});
650
651print("table_count", table_count);
652print("insert_count", insert_count);
653print("row_count", row_count);
654
655