source: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_sql.c @ 875

Last change on this file since 875 was 617, checked in by david, 10 years ago

commit of partial async queue process management

File size: 3.3 KB
Line 
1/**
2 * Author : David Saggiorato
3 *
4 *  Copyright 2008-2009 GeoLabs SARL. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
26
27#include <string.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <mysql.h>
31
32MYSQL *con;
33char * host;
34char *user;
35char *passwd;
36char *bdd;
37int port;
38
39
40void init_sql(const char* h,const char *u, const char *pas, const char * b,int p){
41    host = strdup(h);
42    user = strdup(u);
43    passwd = strdup(pas);
44    bdd = strdup(b);
45    port=p;
46}
47
48
49int init_connection(){
50    con = mysql_init(NULL);
51    if (con == NULL){
52        /* erreur allocation et initialisation mysql */
53        return -1;
54    }
55    if (mysql_real_connect(con,host,user,passwd,bdd,port,NULL,0) == NULL){
56        fprintf(stderr, "%s\n", mysql_error(con));
57        mysql_close(con);
58        return -2;
59    }
60    return 0;
61}
62
63
64char * get_uuid(){
65    init_connection();
66    char * query = "Select uuid();";
67    if (mysql_query(con, query) != 0){
68        fprintf(stderr,"%s\n", mysql_error(con));
69        return NULL;
70    }
71    MYSQL_RES *result = mysql_store_result(con);
72    if (result == NULL){
73        fprintf(stderr,"get_uuid: no uuid\n");
74        return NULL;
75    }
76    int num_fields = mysql_num_fields(result);
77    if (num_fields != 1)
78        return NULL;
79    MYSQL_ROW row = mysql_fetch_row(result);
80    char * tmp = strdup(row[0]);
81    mysql_free_result(result);
82    mysql_close(con);
83    return tmp;
84 }
85
86int add_job(const char * uuid) {
87    init_connection();
88    const char * query = "insert into status (uuid,status,created_time) values ('%s','queue',now())";
89    char * query_f = (char*) malloc(strlen(query) + strlen(uuid) + 1);
90    sprintf(query_f,query,uuid);
91    if (mysql_query(con, query_f) != 0){
92        fprintf(stderr, "%s\n", mysql_error(con));
93        free(query_f);
94        return -1;
95    }
96    free(query_f);
97    mysql_close(con);
98    return 0;
99}
100
101int start_job(const char *uuid){
102    init_connection();
103    const char * query = "update status set start_date=now(), status='running', progress=0 where uuid='%s';";
104    char * query_f = (char*) malloc(strlen(query) + strlen(uuid) + 1);
105    sprintf(query_f,query,uuid);
106    if (mysql_query(con, query_f) != 0){
107        fprintf(stderr, "%s\n", mysql_error(con));
108        free(query_f);
109        return -1;
110    }
111    free(query_f);
112    mysql_close(con);
113    return 0;
114}
115
116
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png