-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconnect.c
More file actions
122 lines (108 loc) · 2.69 KB
/
Copy pathconnect.c
File metadata and controls
122 lines (108 loc) · 2.69 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* 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"
/* Establish a connection to the database identified by a URI string */
SQL *
sql_connect(const char *uristring)
{
URI *uri;
SQL *conn;
uri = uri_create_str(uristring, NULL);
if(!uri)
{
sql_set_error_("08000", "Failed to parse connection URI");
return NULL;
}
conn = sql_connect_uri(uri);
uri_destroy(uri);
return conn;
}
/* Establish a connection to the database identified by a URI */
SQL *
sql_connect_uri(URI *uri)
{
SQL_ENGINE *engine;
SQL *conn;
engine = sql_engine_(uri);
if(!engine)
{
return NULL;
}
conn = engine->api->create(engine);
if(!conn)
{
sql_set_error_("53000", "The client engine failed to create a connection object");
return NULL;
}
if(conn->api->connect(conn, uri))
{
/* Save error state */
sql_set_error_(conn->api->sqlstate(conn), conn->api->error(conn));
conn->api->release(conn);
return NULL;
}
return conn;
}
/* Disconnect from a server */
int
sql_disconnect(SQL *sql)
{
return sql->api->release(sql);
}
/* Lock a connection, waiting if needed */
int
sql_lock(SQL *sql)
{
return sql->api->lock(sql);
}
/* Unlock a locked connection */
int
sql_unlock(SQL *sql)
{
return sql->api->unlock(sql);
}
/* Attempt to lock a connection, fail immediately if already locked */
int
sql_trylock(SQL *sql)
{
return sql->api->trylock(sql);
}
/* Escape a string */
size_t
sql_escape(SQL *restrict sql, const unsigned char *restrict from, size_t length, char *restrict buf, size_t buflen)
{
/* Note that:
* 1. The return value is always the number of bytes required, including the NULL terminator.
* 2. If the return value is greater than buflen, the number of bytes actually written is
* unspecified, except that it will be less than buflen.
* 3. It is entirely valid to specify buf and buflen as zero.
*/
return sql->api->escape(sql, from, length, buf, buflen);
}
/* Set the query-logging function */
int
sql_set_querylog(SQL *sql, SQL_LOG_QUERY fn)
{
return sql->api->set_querylog(sql, fn);
}
int
sql_set_errorlog(SQL *sql, SQL_LOG_ERROR fn)
{
return sql->api->set_errorlog(sql, fn);
}