source: ogServer-Git/src/dbi.c @ 3cb98c7

Last change on this file since 3cb98c7 was 3cb98c7, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

#941 move og_dbi_get_computer_info() to dbi

Move this function to the dbi.{h,c} files.

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 * Copyright (C) 2020 Soleta Networks <info@soleta.eu>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Affero General Public License as published by the
6 * Free Software Foundation, version 3.
7 */
8
9#include <syslog.h>
10#include <netinet/in.h>
11#include <arpa/inet.h>
12#include <string.h>
13#include "dbi.h"
14
15struct og_dbi *og_dbi_open(struct og_dbi_config *config)
16{
17        struct og_dbi *dbi;
18
19        dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi));
20        if (!dbi)
21                return NULL;
22
23        dbi_initialize_r(NULL, &dbi->inst);
24        dbi->conn = dbi_conn_new_r("mysql", dbi->inst);
25        if (!dbi->conn) {
26                free(dbi);
27                return NULL;
28        }
29
30        dbi_conn_set_option(dbi->conn, "host", config->host);
31        dbi_conn_set_option(dbi->conn, "port", config->port);
32        dbi_conn_set_option(dbi->conn, "username", config->user);
33        dbi_conn_set_option(dbi->conn, "password", config->passwd);
34        dbi_conn_set_option(dbi->conn, "dbname", config->database);
35        dbi_conn_set_option(dbi->conn, "encoding", "UTF-8");
36
37        if (dbi_conn_connect(dbi->conn) < 0) {
38                free(dbi);
39                return NULL;
40        }
41
42        return dbi;
43}
44
45void og_dbi_close(struct og_dbi *dbi)
46{
47        dbi_conn_close(dbi->conn);
48        dbi_shutdown_r(dbi->inst);
49        free(dbi);
50}
51
52int og_dbi_get_computer_info(struct og_computer *computer, struct in_addr addr)
53{
54        const char *msglog;
55        struct og_dbi *dbi;
56        dbi_result result;
57
58        dbi = og_dbi_open(&dbi_config);
59        if (!dbi) {
60                syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
61                       __func__, __LINE__);
62                return -1;
63        }
64        result = dbi_conn_queryf(dbi->conn,
65                                 "SELECT ordenadores.idordenador,"
66                                 "       ordenadores.nombreordenador,"
67                                 "       ordenadores.idaula,"
68                                 "       ordenadores.idproautoexec,"
69                                 "       centros.idcentro FROM ordenadores "
70                                 "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula "
71                                 "INNER JOIN centros ON centros.idcentro=aulas.idcentro "
72                                 "WHERE ordenadores.ip='%s'", inet_ntoa(addr));
73        if (!result) {
74                dbi_conn_error(dbi->conn, &msglog);
75                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
76                       __func__, __LINE__, msglog);
77                og_dbi_close(dbi);
78                return -1;
79        }
80        if (!dbi_result_next_row(result)) {
81                syslog(LOG_ERR, "client does not exist in database (%s:%d)\n",
82                       __func__, __LINE__);
83                dbi_result_free(result);
84                og_dbi_close(dbi);
85                return -1;
86        }
87
88        computer->id = dbi_result_get_uint(result, "idordenador");
89        computer->center = dbi_result_get_uint(result, "idcentro");
90        computer->room = dbi_result_get_uint(result, "idaula");
91        computer->procedure_id = dbi_result_get_uint(result, "idproautoexec");
92        strncpy(computer->name,
93                dbi_result_get_string(result, "nombreordenador"),
94                OG_DB_COMPUTER_NAME_MAXLEN);
95
96        dbi_result_free(result);
97        og_dbi_close(dbi);
98
99        return 0;
100}
Note: See TracBrowser for help on using the repository browser.