forked from nevali/libsql
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.c
More file actions
95 lines (83 loc) · 1.92 KB
/
error.c
File metadata and controls
95 lines (83 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright 2012-2013 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_libsql.h"
static pthread_once_t error_once = PTHREAD_ONCE_INIT;
static pthread_key_t error_key;
static void error_init(void);
struct error_struct
{
char sqlstate[6];
char message[512];
};
/* Set the per-thread connectionless SQLSTATE code */
void
sql_set_error_(const char *sqlstate, const char *msg)
{
struct error_struct *p;
pthread_once(&error_once, error_init);
p = pthread_getspecific(error_key);
if(!p)
{
p = (struct error_struct *) calloc(1, sizeof(struct error_struct));
if(!p)
{
return;
}
pthread_setspecific(error_key, p);
}
strncpy(p->sqlstate, sqlstate, sizeof(p->sqlstate) - 1);
strncpy(p->message, msg, sizeof(p->message) - 1);
}
const char *
sql_sqlstate(SQL *connection)
{
struct error_struct *p;
if(connection)
{
return connection->api->sqlstate(connection);
}
pthread_once(&error_once, error_init);
p = pthread_getspecific(error_key);
if(!p)
{
return "0000";
}
return p->sqlstate;
}
const char *
sql_error(SQL *connection)
{
struct error_struct *p;
if(connection)
{
return connection->api->error(connection);
}
pthread_once(&error_once, error_init);
p = pthread_getspecific(error_key);
if(!p)
{
return "No error";
}
return p->message;
}
static void
error_init(void)
{
pthread_key_create(&error_key, NULL);
}