[617] | 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 | |
---|
[603] | 27 | #include <string.h> |
---|
| 28 | #include <stdio.h> |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <mysql.h> |
---|
| 31 | |
---|
| 32 | MYSQL *con; |
---|
| 33 | char * host; |
---|
| 34 | char *user; |
---|
| 35 | char *passwd; |
---|
| 36 | char *bdd; |
---|
| 37 | int port; |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | void 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 | |
---|
| 49 | int 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 | |
---|
| 64 | char * 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 | |
---|
[617] | 86 | int add_job(const char * uuid) { |
---|
[603] | 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 | } |
---|
[617] | 100 | |
---|
| 101 | int 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 | |
---|