blob: 66f4e7d1c9f0099b4bf04c726a3ee0aa4fa48589 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001CONCEPT
Zesstra7ea4a032019-11-26 20:11:40 +01002 mysql - mySQL support
MG Mud User88f12472016-06-24 23:31:02 +02003
4DESCRIPTION
5 On hosts with the mySQL package installed, the driver can be
6 configured to interface with the mySQL database. If that is done,
7 the driver defines the macro __MYSQL__ for LPC programs and
8 activates a number of efuns.
9
10 -- Configuration --
11
12 Create a dedicated user in the mySQL database for the driver.
13 Enter this username and password in the file pkg-mysql.c, function
14 mysql_real_connect(), and compile the driver (the username and
Zesstra7ea4a032019-11-26 20:11:40 +010015 password are built into the driver for security reasons). If you
16 choose to not create either a username and/or a password, leave the
17 corresponding entry at 0.
MG Mud User88f12472016-06-24 23:31:02 +020018
19 Use mysqladmin to create any databases you want to provide - the
20 names are later used in the efun db_connect() to connect to
21 the databases.
22
23
24 -- Usage --
25
26 The idea behind SQL-support is that you can swap large amounts of
27 data into a database where it can be accessed very easily.
28 As mySQL "limits" the number of connections to 100 and as every
29 connection to the mySQL-server takes time, you should use
30 database serverobjects in your MUD which constantly keep the
31 connection to the mySQL-server.
32
33 To connect to your mySQL-server, use the efun db_connect(). It
34 takes only one argument which is the name of the database (which
Zesstra7ea4a032019-11-26 20:11:40 +010035 must exist). The return-value of db_connect() is an integer
MG Mud User88f12472016-06-24 23:31:02 +020036 representing the unique handle to the database with which you will
37 identify your connection later.
38
39 To send or retrieve data from this connection, use db_exec(). The
40 first parameter for all efuns dealing with an open connection is
41 always the handle and so is the first argument the handle and the
42 second one the command you want to issue. The return-value is
43 either 0 if there was an error in your command (this can have
44 various reasons), otherwise your handle is returned again. A typical
45 SQL-statement to retrieve data would be like this:
46
47 select aliases.command from aliases where (name = 'mario' AND
48 alias regexp 'l.*')
49
50 As you know, mySQL accepts either " or ' to classify strings for
51 parameters. Most likely, you will pass variables and don't know
52 whether they contain one or more of these key-chars (or even other
53 chars that need to be converted). mySQL provides a function for
54 converting just any string into an acceptable argument and this is
55 implemented in db_conv_string().
56
57 So the above example with variables looks like this:
58
59 select aliases.command from aliases where (name ='"+
60 db_conv_string(name)+"' AND alias regexp '"+
61 db_conv_string(mask)+"')
62
63 I left out the db_exec()-stuff, more complete examples will follow.
64
65 After you initiated a statement that should return rows from the
66 database, use db_fetch() to retrieve the data. db_fetch() returns
67 the data row by row and not all at once. You need to call it until
68 it returns 0. THIS IS IMPORTANT! If stop calling db_fetch() before
69 it reaches the end of data, serious inconsistencies can happen.
70
71 If you used a DELETE- or UPDATE-statement, you cannot call db_fetch(),
72 but you might be interested in the number of deleted/changed rows
73 which can be queried with db_affected_rows().
74
75 After all operations are done in the database, you should use
76 db_close() to close the connection again. If you are using a
77 database-server-concept, place it in the remove()-function.
78
79 The SQL-efuns have some built-in optimization-features to speed up
80 often used connections. To get a list of all open connections to the
81 mySQL-server, use db_handles() which returns an array of integers
82 with all open handles.
83
84
85 -- Security --
86
87 Most SQL efuns (unless execute by the master or the simul-efun object)
88 trigger a privilege_violation ("mysql", "<efun_name>"). If a more
89 finegrained control is desired, overload the individual efuns with a
90 nomask simul-efun.
91
92 The unprivileged efuns are:
93
94 db_conv_string()
95
96
Zesstra7ea4a032019-11-26 20:11:40 +010097 -- Caveats --
98
99 The driver enables automatic reconnects on the database connections.
100 This means that if a connection is lost - the most common case for
101 this is timeouts, which by default happen after 28800 seconds of
102 inactivity - an attempt will be made to establish a new connection to
103 the database server. When that happens, all session state (temprary
104 tables and state changes from SET statements) will be lost. It's best
105 not to rely on such state.
106
MG Mud User88f12472016-06-24 23:31:02 +0200107EXAMPLE
108 A simple server to store aliases could be implemented like this:
109
110 /*
111 ** CREATION:
112 **
113 ** create table aliases (
114 ** name varchar(15) not NULL,
115 ** alias varchar(20) not NULL,
116 ** command varchar(255) not NULL,
117 ** primary key (name, alias));
118 */
119
120 #define DATABASE "mud"
121
122 private int handle;
123
124 public void create()
125 {
126 handle = db_connect(DATABASE);
127 }
128
129 public int remove()
130 {
131 if ( handle )
132 db_close(handle);
133 destruct(ME);
134 return !ME;
135 }
136
137 public int AddAlias(string alias, string command, object ob)
138 {
139 if ( !handle )
140 handle = db_connect(DATABASE);
141 if ( !db_exec(handle,
142 "insert into aliases (name, alias, command) values "
143 "('" + getuid(ob) + "','" + db_conv_string(alias)
144 + "','"+
145 db_conv_string(command) + "')") )
146 return -1;
147 return 1;
148 }
149
150 public int RemoveAlias(string alias, object ob)
151 {
152 int res;
153
154 if ( !handle )
155 handle = db_connect(DATABASE);
156 res = db_exec(handle,
157 "delete from aliases where (name = '"+
158 getuid(ob) + "' AND alias = '"
159 + db_conv_string(alias)+
160 "')");
161 if ( !res )
162 return 0;
163 res = db_affected_rows(handle);
164 return (res > 0)?1:-1;
165 }
166
167 public mixed *QueryAliases(string mask, object ob)
168 {
169 mixed *result;
170 string *tmp;
171
172 if ( !handle )
173 handle = db_connect(DATABASE);
174 if ( !db_exec(handle,
175 "select aliases.alias, aliases.command from aliases where "
176 "(name = '" + getuid(ob)+
177 "' AND alias regexp '" + db_conv_string(mask) + "')") )
178 return ({ });
179 result = ({ });
180 while ( sizeof(tmp = db_fetch(handle)) )
181 result += ({ tmp });
182 return result;
183 }
184
185 public string QueryAlias(string alias, object ob)
186 {
187 mixed *result;
188 string *tmp;
189
190 if ( !handle )
191 handle = db_connect(DATABASE);
192 if ( !db_exec(handle,
193 "select aliases.command from aliases where "
194 "(name = '" + getuid(ob)+
195 "' AND alias = '" + db_conv_string(alias) + "')") )
196 return 0;
197 result = ({ });
198 while ( sizeof(tmp = db_fetch(handle)) )
199 result += tmp;
200 return sizeof(result)?result[0]:0;
201 }
202
203
204AUTHOR
205 Mark Daniel Reidel and others.
206
207HISTORY
208 mySQL support was added as a package in 3.2.8 and became and
209 integral driver part in 3.2.9.
210 LDMud 3.2.11 added a privilege_violation() call for each efun.
211
212SEE ALSO
213 pgsql(C), db_affected_rows(E), db_conv_string(E), db_close(E),
214 db_connect(E), db_exec(E), db_fetch(E), db_handles(E),
215 db_insert_id(E), db_coldefs(E), db_error(E), privilege_violation(A)