1 /*****************************************************************************\
2  *  mysql_common.h - common functions for the mysql storage plugin.
3  *****************************************************************************
4  *
5  *  Copyright (C) 2004-2007 The Regents of the University of California.
6  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
7  *  Written by Danny Auble <da@llnl.gov>
8  *
9  *  This file is part of Slurm, a resource management program.
10  *  For details, see <https://slurm.schedmd.com/>.
11  *  Please also read the included file: DISCLAIMER.
12  *
13  *  Slurm is free software; you can redistribute it and/or modify it under
14  *  the terms of the GNU General Public License as published by the Free
15  *  Software Foundation; either version 2 of the License, or (at your option)
16  *  any later version.
17  *
18  *  In addition, as a special exception, the copyright holders give permission
19  *  to link the code of portions of this program with the OpenSSL library under
20  *  certain conditions as described in each individual source file, and
21  *  distribute linked combinations including the two. You must obey the GNU
22  *  General Public License in all respects for all of the code used other than
23  *  OpenSSL. If you modify file(s) with this exception, you may extend this
24  *  exception to your version of the file(s), but you are not obligated to do
25  *  so. If you do not wish to do so, delete this exception statement from your
26  *  version.  If you delete this exception statement from all source files in
27  *  the program, then also delete it here.
28  *
29  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
30  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
32  *  details.
33  *
34  *  You should have received a copy of the GNU General Public License along
35  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
36  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
37  *
38  *  This file is patterned after jobcomp_linux.c, written by Morris Jette and
39  *  Copyright (C) 2002 The Regents of the University of California.
40 \*****************************************************************************/
41 #ifndef _MYSQL_COMMON_H
42 #define _MYSQL_COMMON_H
43 
44 #include <inttypes.h>
45 #include <pthread.h>
46 #include <stdio.h>
47 
48 #include "slurm/slurm_errno.h"
49 #include "src/common/list.h"
50 #include "src/common/xstring.h"
51 
52 #include <mysql.h>
53 #include <mysqld_error.h>
54 
55 typedef enum {
56 	SLURM_MYSQL_PLUGIN_NOTSET,
57 	SLURM_MYSQL_PLUGIN_AS, /* accounting_storage */
58 	SLURM_MYSQL_PLUGIN_JC, /* jobcomp */
59 } slurm_mysql_plugin_type_t;
60 
61 typedef struct {
62 	bool cluster_deleted;
63 	char *cluster_name;
64 	MYSQL *db_conn;
65 	pthread_mutex_t lock;
66 	char *pre_commit_query;
67 	bool rollback;
68 	List update_list;
69 	int conn;
70 } mysql_conn_t;
71 
72 typedef struct {
73 	char *backup;
74 	uint32_t port;
75 	char *host;
76 	char *user;
77 	char *pass;
78 } mysql_db_info_t;
79 
80 typedef struct {
81 	char *name;
82 	char *options;
83 } storage_field_t;
84 
85 extern mysql_conn_t *create_mysql_conn(int conn_num, bool rollback,
86 				       char *cluster_name);
87 extern int destroy_mysql_conn(mysql_conn_t *mysql_conn);
88 extern mysql_db_info_t *create_mysql_db_info(slurm_mysql_plugin_type_t type);
89 extern int destroy_mysql_db_info(mysql_db_info_t *db_info);
90 
91 extern int mysql_db_get_db_connection(mysql_conn_t *mysql_conn, char *db_name,
92 				   mysql_db_info_t *db_info);
93 extern int mysql_db_close_db_connection(mysql_conn_t *mysql_conn);
94 extern int mysql_db_cleanup();
95 extern int mysql_db_query(mysql_conn_t *mysql_conn, char *query);
96 extern int mysql_db_delete_affected_rows(mysql_conn_t *mysql_conn, char *query);
97 extern int mysql_db_ping(mysql_conn_t *mysql_conn);
98 extern int mysql_db_commit(mysql_conn_t *mysql_conn);
99 extern int mysql_db_rollback(mysql_conn_t *mysql_conn);
100 
101 extern MYSQL_RES *mysql_db_query_ret(mysql_conn_t *mysql_conn,
102 				     char *query, bool last);
103 extern int mysql_db_query_check_after(mysql_conn_t *mysql_conn, char *query);
104 
105 extern uint64_t mysql_db_insert_ret_id(mysql_conn_t *mysql_conn, char *query);
106 
107 extern int mysql_db_create_table(mysql_conn_t *mysql_conn, char *table_name,
108 				 storage_field_t *fields, char *ending);
109 
110 #endif
111