source: sqlite3_toppers/sample/sample1.c@ 165

Last change on this file since 165 was 58, checked in by ertl-honda, 11 years ago

追加.

  • Property svn:keywords set to Id
File size: 17.5 KB
Line 
1/*
2 * TOPPERS Software
3 * Toyohashi Open Platform for Embedded Real-Time Systems
4 *
5 * Copyright (C) 2013 by Kijineko Inc.
6 * Copyright (C) 2013 by Embedded and Real-Time Systems Laboratory
7 * Graduate School of Information Science, Nagoya Univ., JAPAN
8 *
9 * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
10 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
11 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
12 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
14 * スコード中に含まれていること.
15 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
18 * の無保証規定を掲載すること.
19 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
21 * と.
22 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
23 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
24 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
25 * 報告すること.
26 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
27 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
28 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
29 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
30 * 免責すること.
31 *
32 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
33 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
34 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
35 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
36 * の責任を負わない.
37 *
38 * $Id: sample1.c 58 2013-04-12 00:36:18Z ertl-honda $
39 */
40#include <kernel.h>
41#include <t_syslog.h>
42#include <t_stdlib.h>
43#include "syssvc/serial.h"
44#include "syssvc/syslog.h"
45#include "sample1.h"
46#include "sqlite3.h"
47
48#define GEN_TABLE_FROM_STATICDATA
49
50/*
51 * サービスコールのエラーのログ出力
52 */
53Inline void
54svc_perror(const char *file, int_t line, const char *expr, ER ercd)
55{
56 if (ercd < 0) {
57 t_perror(LOG_ERROR, file, line, expr, ercd);
58 }
59}
60
61#define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
62
63
64/*
65 * SQLの実行
66 */
67static void
68exec_sql(sqlite3 *db, const char *sql, sqlite3_callback cb, void *first_arg)
69{
70 char *pStr;
71
72 if (sqlite3_exec(db, sql, cb, first_arg, &pStr) != SQLITE_OK) {
73 syslog(LOG_NOTICE, "Error %s %d: %s\n%s\n", __FILE__, __LINE__, pStr, sql);
74 sqlite3_close(db);
75 ext_tsk();
76 }
77}
78
79#define TNUM_DATA 100
80
81#ifdef GEN_TABLE_FROM_STATICDATA
82/*
83 * テーブへのデータの格納のための文字列テーブル
84 */
85char init_data[TNUM_DATA][100] = {
86 "INSERT INTO sample (id, student, score, commit_date) values (1, 'Ey', 1, '1999-12-31 00:04:53')",
87 "INSERT INTO sample (id, student, score, commit_date) values (2, 'nJ', 20, '1999-12-31 00:57:41')",
88 "INSERT INTO sample (id, student, score, commit_date) values (3, 'um', 9, '1999-12-31 01:09:32')",
89 "INSERT INTO sample (id, student, score, commit_date) values (4, 'lN', 33, '1999-12-31 01:40:25')",
90 "INSERT INTO sample (id, student, score, commit_date) values (5, 'xV', 81, '1999-12-31 02:28:02')",
91 "INSERT INTO sample (id, student, score, commit_date) values (6, 'pC', 70, '1999-12-31 02:38:45')",
92 "INSERT INTO sample (id, student, score, commit_date) values (7, 'fE', 36, '1999-12-31 03:26:36')",
93 "INSERT INTO sample (id, student, score, commit_date) values (8, 'vX', 85, '1999-12-31 04:11:56')",
94 "INSERT INTO sample (id, student, score, commit_date) values (9, 'ZL', 77, '1999-12-31 04:33:59')",
95 "INSERT INTO sample (id, student, score, commit_date) values (10, 'kD', 92, '1999-12-31 04:43:50')",
96 "INSERT INTO sample (id, student, score, commit_date) values (11, 'uc', 30, '1999-12-31 05:08:40')",
97 "INSERT INTO sample (id, student, score, commit_date) values (12, 'Ey', 90, '1999-12-31 05:36:10')",
98 "INSERT INTO sample (id, student, score, commit_date) values (13, 'SO', 57, '1999-12-31 06:17:34')",
99 "INSERT INTO sample (id, student, score, commit_date) values (14, 'EZ', 10, '1999-12-31 07:10:06')",
100 "INSERT INTO sample (id, student, score, commit_date) values (15, 'ny', 28, '1999-12-31 07:56:05')",
101 "INSERT INTO sample (id, student, score, commit_date) values (16, 'SM', 50, '1999-12-31 08:14:24')",
102 "INSERT INTO sample (id, student, score, commit_date) values (17, 'HM', 8, '1999-12-31 08:59:20')",
103 "INSERT INTO sample (id, student, score, commit_date) values (18, 'EZ', 66, '1999-12-31 09:29:46')",
104 "INSERT INTO sample (id, student, score, commit_date) values (19, 'vO', 31, '1999-12-31 09:48:09')",
105 "INSERT INTO sample (id, student, score, commit_date) values (20, 'Aw', 12, '1999-12-31 10:44:46')",
106 "INSERT INTO sample (id, student, score, commit_date) values (21, 'DK', 13, '1999-12-31 11:07:13')",
107 "INSERT INTO sample (id, student, score, commit_date) values (22, 'iX', 72, '1999-12-31 11:58:34')",
108 "INSERT INTO sample (id, student, score, commit_date) values (23, 'MO', 90, '1999-12-31 12:13:25')",
109 "INSERT INTO sample (id, student, score, commit_date) values (24, 'UW', 88, '1999-12-31 12:32:10')",
110 "INSERT INTO sample (id, student, score, commit_date) values (25, 'fL', 96, '1999-12-31 12:40:01')",
111 "INSERT INTO sample (id, student, score, commit_date) values (26, 'LF', 35, '1999-12-31 13:31:31')",
112 "INSERT INTO sample (id, student, score, commit_date) values (27, 'xO', 41, '1999-12-31 14:19:44')",
113 "INSERT INTO sample (id, student, score, commit_date) values (28, 'vL', 98, '1999-12-31 15:18:41')",
114 "INSERT INTO sample (id, student, score, commit_date) values (29, 'Hw', 86, '1999-12-31 15:52:29')",
115 "INSERT INTO sample (id, student, score, commit_date) values (30, 'Fw', 47, '1999-12-31 16:41:02')",
116 "INSERT INTO sample (id, student, score, commit_date) values (31, 'hn', 69, '1999-12-31 17:32:29')",
117 "INSERT INTO sample (id, student, score, commit_date) values (32, 'iX', 6, '1999-12-31 18:26:32')",
118 "INSERT INTO sample (id, student, score, commit_date) values (33, 'EZ', 3, '1999-12-31 19:19:55')",
119 "INSERT INTO sample (id, student, score, commit_date) values (34, 'Sv', 97, '1999-12-31 20:05:05')",
120 "INSERT INTO sample (id, student, score, commit_date) values (35, 'Ie', 25, '1999-12-31 20:31:47')",
121 "INSERT INTO sample (id, student, score, commit_date) values (36, 'hH', 1, '1999-12-31 20:50:58')",
122 "INSERT INTO sample (id, student, score, commit_date) values (37, 'Gx', 62, '1999-12-31 21:06:53')",
123 "INSERT INTO sample (id, student, score, commit_date) values (38, 'tK', 71, '1999-12-31 21:29:19')",
124 "INSERT INTO sample (id, student, score, commit_date) values (39, 'VP', 20, '1999-12-31 21:33:57')",
125 "INSERT INTO sample (id, student, score, commit_date) values (40, 'RV', 75, '1999-12-31 22:29:47')",
126 "INSERT INTO sample (id, student, score, commit_date) values (41, 'PE', 31, '1999-12-31 22:40:22')",
127 "INSERT INTO sample (id, student, score, commit_date) values (42, 'ZR', 49, '1999-12-31 23:26:17')",
128 "INSERT INTO sample (id, student, score, commit_date) values (43, 'hH', 42, '2000-01-01 00:00:10')",
129 "INSERT INTO sample (id, student, score, commit_date) values (44, 'KR', 38, '2000-01-01 00:25:26')",
130 "INSERT INTO sample (id, student, score, commit_date) values (45, 'iX', 11, '2000-01-01 01:21:25')",
131 "INSERT INTO sample (id, student, score, commit_date) values (46, 'Nm', 74, '2000-01-01 02:19:52')",
132 "INSERT INTO sample (id, student, score, commit_date) values (47, 'WR', 96, '2000-01-01 02:25:58')",
133 "INSERT INTO sample (id, student, score, commit_date) values (48, 'ay', 56, '2000-01-01 02:39:31')",
134 "INSERT INTO sample (id, student, score, commit_date) values (49, 'ii', 100, '2000-01-01 03:16:08')",
135 "INSERT INTO sample (id, student, score, commit_date) values (50, 'hi', 56, '2000-01-01 03:25:22')",
136 "INSERT INTO sample (id, student, score, commit_date) values (51, 'hH', 31, '2000-01-01 04:02:52')",
137 "INSERT INTO sample (id, student, score, commit_date) values (52, 'RK', 8, '2000-01-01 04:07:52')",
138 "INSERT INTO sample (id, student, score, commit_date) values (53, 'TC', 54, '2000-01-01 05:07:26')",
139 "INSERT INTO sample (id, student, score, commit_date) values (54, 'NT', 18, '2000-01-01 05:24:18')",
140 "INSERT INTO sample (id, student, score, commit_date) values (55, 'xh', 17, '2000-01-01 05:48:45')",
141 "INSERT INTO sample (id, student, score, commit_date) values (56, 'hH', 8, '2000-01-01 06:08:31')",
142 "INSERT INTO sample (id, student, score, commit_date) values (57, 'KN', 55, '2000-01-01 06:49:27')",
143 "INSERT INTO sample (id, student, score, commit_date) values (58, 'hH', 5, '2000-01-01 07:07:57')",
144 "INSERT INTO sample (id, student, score, commit_date) values (59, 'fI', 99, '2000-01-01 07:58:31')",
145 "INSERT INTO sample (id, student, score, commit_date) values (60, 'QJ', 23, '2000-01-01 08:40:22')",
146 "INSERT INTO sample (id, student, score, commit_date) values (61, 'BF', 72, '2000-01-01 09:13:39')",
147 "INSERT INTO sample (id, student, score, commit_date) values (62, 'TD', 43, '2000-01-01 09:19:52')",
148 "INSERT INTO sample (id, student, score, commit_date) values (63, 'Hm', 5, '2000-01-01 10:08:09')",
149 "INSERT INTO sample (id, student, score, commit_date) values (64, 'CL', 37, '2000-01-01 10:44:52')",
150 "INSERT INTO sample (id, student, score, commit_date) values (65, 'Bq', 41, '2000-01-01 10:52:54')",
151 "INSERT INTO sample (id, student, score, commit_date) values (66, 'yX', 68, '2000-01-01 11:09:52')",
152 "INSERT INTO sample (id, student, score, commit_date) values (67, 'Vo', 31, '2000-01-01 11:28:29')",
153 "INSERT INTO sample (id, student, score, commit_date) values (68, 'dn', 10, '2000-01-01 11:45:09')",
154 "INSERT INTO sample (id, student, score, commit_date) values (69, 'bO', 44, '2000-01-01 12:44:02')",
155 "INSERT INTO sample (id, student, score, commit_date) values (70, 'pR', 2, '2000-01-01 12:53:11')",
156 "INSERT INTO sample (id, student, score, commit_date) values (71, 'HK', 50, '2000-01-01 13:28:05')",
157 "INSERT INTO sample (id, student, score, commit_date) values (72, 'OU', 49, '2000-01-01 13:46:50')",
158 "INSERT INTO sample (id, student, score, commit_date) values (73, 'ch', 10, '2000-01-01 14:16:53')",
159 "INSERT INTO sample (id, student, score, commit_date) values (74, 'NV', 72, '2000-01-01 14:51:01')",
160 "INSERT INTO sample (id, student, score, commit_date) values (75, 'wQ', 6, '2000-01-01 15:16:37')",
161 "INSERT INTO sample (id, student, score, commit_date) values (76, 'Ik', 24, '2000-01-01 15:29:08')",
162 "INSERT INTO sample (id, student, score, commit_date) values (77, 'hH', 3, '2000-01-01 16:14:29')",
163 "INSERT INTO sample (id, student, score, commit_date) values (78, 'ks', 76, '2000-01-01 16:58:13')",
164 "INSERT INTO sample (id, student, score, commit_date) values (79, 'Cw', 60, '2000-01-01 17:38:39')",
165 "INSERT INTO sample (id, student, score, commit_date) values (80, 'xq', 48, '2000-01-01 17:57:02')",
166 "INSERT INTO sample (id, student, score, commit_date) values (81, 'WY', 95, '2000-01-01 18:46:18')",
167 "INSERT INTO sample (id, student, score, commit_date) values (82, 'HK', 25, '2000-01-01 19:37:57')",
168 "INSERT INTO sample (id, student, score, commit_date) values (83, 'OO', 50, '2000-01-01 19:40:04')",
169 "INSERT INTO sample (id, student, score, commit_date) values (84, 'Dl', 59, '2000-01-01 20:29:56')",
170 "INSERT INTO sample (id, student, score, commit_date) values (85, 'SJ', 7, '2000-01-01 20:44:38')",
171 "INSERT INTO sample (id, student, score, commit_date) values (86, 'ks', 20, '2000-01-01 20:45:49')",
172 "INSERT INTO sample (id, student, score, commit_date) values (87, 'ql', 20, '2000-01-01 21:14:47')",
173 "INSERT INTO sample (id, student, score, commit_date) values (88, 'nM', 67, '2000-01-01 21:31:50')",
174 "INSERT INTO sample (id, student, score, commit_date) values (89, 'nF', 56, '2000-01-01 22:24:10')",
175 "INSERT INTO sample (id, student, score, commit_date) values (90, 'zQ', 1, '2000-01-01 22:36:01')",
176 "INSERT INTO sample (id, student, score, commit_date) values (91, 'Ce', 29, '2000-01-01 22:40:17')",
177 "INSERT INTO sample (id, student, score, commit_date) values (92, 'xg', 98, '2000-01-01 23:06:18')",
178 "INSERT INTO sample (id, student, score, commit_date) values (93, 'Qs', 55, '2000-01-01 23:44:24')",
179 "INSERT INTO sample (id, student, score, commit_date) values (94, 'bF', 42, '2000-01-02 00:42:50')",
180 "INSERT INTO sample (id, student, score, commit_date) values (95, 'Nz', 91, '2000-01-02 00:55:16')",
181 "INSERT INTO sample (id, student, score, commit_date) values (96, 'ln', 86, '2000-01-02 01:28:52')",
182 "INSERT INTO sample (id, student, score, commit_date) values (97, 'KW', 84, '2000-01-02 01:36:19')",
183 "INSERT INTO sample (id, student, score, commit_date) values (98, 'Dw', 72, '2000-01-02 02:19:29')",
184 "INSERT INTO sample (id, student, score, commit_date) values (99, 'FW', 80, '2000-01-02 02:45:21')",
185 "INSERT INTO sample (id, student, score, commit_date) values (100, 'Js', 76, '2000-01-02 03:07:38')"
186};
187
188/*
189 * データの格納(スタティックデータから)
190 */
191static void
192insert_data(sqlite3 *db)
193{
194 int i;
195
196 for(i = 0; i < TNUM_DATA; i++) {
197 exec_sql(db, init_data[i], NULL, NULL);
198 }
199}
200
201#else /* !GEN_TABLE_FROM_STATICDATA */
202
203#include <stdlib.h>
204#include <stdio.h>
205#include <string.h>
206#include <time.h>
207
208/*
209 * ランダムな英字を取得
210 */
211static int
212get_random_letter(void)
213{
214 static const char letters[] =
215 "abcdefghijklmnopqrstuvwxyz"
216 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
217 int i = rand() % sizeof(letters);
218 return letters[i];
219}
220
221/*
222 * データの格納(ランダムに生成)
223 */
224static void
225insert_data(sqlite3 *db)
226{
227 static char str[STRBUF_SIZE], data[4][STRBUF_SIZE];
228 int i;
229 struct tm t = { 0 };
230
231 t.tm_year = -1900 + 2000;
232 for(i = 0; i < NUM_OF_DATA; i++) {
233 t.tm_sec = (t.tm_sec + rand()) % 60;
234 t.tm_min += rand() % 60 + 1;
235 mktime(&t);
236
237 sprintf(data[0], "%d", i + 1);
238 sprintf(data[1], "%c%c", get_random_letter(), get_random_letter());
239 sprintf(data[2], "%d", rand() % NUM_OF_DATA + 1);
240 sprintf(data[3], "%04d-%02d-%02d %02d:%02d:%02d",
241 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
242 sprintf(str, "INSERT INTO sample (id, student, score, "
243 "commit_date) values (%s, '%s', %s, '%s')",
244 data[0], data[1], data[2], data[3]);
245 exec_sql(db, str, NULL, NULL);
246 }
247}
248
249#endif /* GEN_TABLE_FROM_STATICDATA */
250
251
252/*
253 * テーブルの作成
254 */
255static void
256make_table(sqlite3 *db)
257{
258 exec_sql(db,
259 "CREATE TABLE sample "
260 "(id INTEGER PRIMARY KEY, student TEXT NOT NULL, "
261 "score INTEGER NOT NULL, commit_date TEXT NOT NULL)",
262 NULL,
263 NULL);
264
265 insert_data(db);
266}
267
268
269/*
270 * コールバック関数用のデータ
271 */
272#define TNUM_COLUMN 4
273#define TNUM_RAW 11
274#define STRBUF_SIZE 256
275
276typedef struct {
277 char values[TNUM_COLUMN][STRBUF_SIZE];
278} RAW;
279
280typedef struct {
281 int raw_count;
282 RAW raw_name;
283 RAW raw_data[TNUM_RAW];
284}SQL_RESULT;
285
286/*
287 * SELECT文によって得られた結果行を1行ずつ取得するコールバック関数
288 */
289static int
290callback(void *option, int columnCount, char **columnValues, char **columnNames)
291{
292 int i,e;
293
294 SQL_RESULT *psql_result;
295
296 psql_result = (SQL_RESULT*)option;
297
298 for(i = 0; ((i < TNUM_COLUMN) && (i < columnCount)); i++) {
299 char *pstr;
300 if (psql_result->raw_count == 0) {
301 pstr = (psql_result->raw_name).values[i];
302 for(e = 0; (e <= (STRBUF_SIZE - 1)) && (columnNames[i][e] != '\0'); e++) {
303 pstr[e] = columnNames[i][e];
304 }
305 pstr[e] = '\0';
306 }
307 pstr = (psql_result->raw_data[psql_result->raw_count]).values[i];
308 for(e = 0; (e <= (STRBUF_SIZE - 1)) && (columnValues[i][e] != '\0'); e++) {
309 pstr[e] = columnValues[i][e];
310 }
311 pstr[e] = '\0';
312 }
313 psql_result->raw_count++;
314
315 return 0;
316}
317
318/*
319 * コールバック関数に渡すデータ
320 */
321SQL_RESULT sql_result;
322
323/*
324 * メインタスク
325 */
326void
327main_task(intptr_t exinf)
328{
329 ER_UINT ercd;
330 int rawCount;
331 sqlite3 *db;
332
333 SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_INFO), LOG_UPTO(LOG_EMERG)));
334 syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf);
335
336 /*
337 * シリアルポートの初期化
338 *
339 * システムログタスクと同じシリアルポートを使う場合など,シリアル
340 * ポートがオープン済みの場合にはここでE_OBJエラーになるが,支障は
341 * ない.
342 */
343 ercd = serial_opn_por(TASK_PORTID);
344 if (ercd < 0 && MERCD(ercd) != E_OBJ) {
345 syslog(LOG_ERROR, "%s (%d) reported by `serial_opn_por'.",
346 itron_strerror(ercd), SERCD(ercd));
347 }
348 SVC_PERROR(serial_ctl_por(TASK_PORTID,
349 (IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV)));
350
351
352 /* メモリ上にDBを構築 (":memory:"を指定) */
353 if (sqlite3_open(":memory:", &db) != SQLITE_OK) {
354 syslog(LOG_ERROR, "sqliet3 : open error!");
355 sqlite3_close(db);
356 return;
357 }
358
359 /* テーブルの作成 */
360 make_table(db);
361
362 /* DBの検索 */
363 syslog(LOG_NOTICE, "total_score top10");
364
365 /* socreの合計値のtop10の学生を取得 */
366 sql_result.raw_count = 0;
367 exec_sql(db,
368 "SELECT student, sum(score) "
369 "AS total_score FROM sample GROUP BY student "
370 "ORDER BY total_score DESC LIMIT 10",
371 callback,
372 (void*)(&sql_result));
373
374 /* Output Name */
375 syslog(LOG_NOTICE, "%s,%s",
376 sql_result.raw_name.values[0],
377 sql_result.raw_name.values[1]);
378
379 /* Output Data */
380 for(rawCount = 0; rawCount < sql_result.raw_count; rawCount++) {
381 syslog(LOG_NOTICE, "%s,%s",
382 sql_result.raw_data[rawCount].values[0],
383 sql_result.raw_data[rawCount].values[1]);
384 }
385
386 syslog(LOG_NOTICE, "total_commit_count top10");
387 sql_result.raw_count = 0;
388 exec_sql(db,
389 "SELECT student, count(id) AS total_commit_count "
390 "FROM sample GROUP BY student ORDER BY total_commit_count DESC LIMIT 10",
391 callback,
392 (void*)(&sql_result));
393
394 /* Output Name */
395 syslog(LOG_NOTICE, "%s,%s",
396 sql_result.raw_name.values[0],
397 sql_result.raw_name.values[1]);
398
399 /* Output Data */
400 for(rawCount = 0; rawCount < sql_result.raw_count; rawCount++) {
401 syslog(LOG_NOTICE, "%s,%s",
402 sql_result.raw_data[rawCount].values[0],
403 sql_result.raw_data[rawCount].values[1]);
404 }
405
406 sqlite3_close(db);
407}
408
Note: See TracBrowser for help on using the repository browser.