1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
<title>Reference</title>
6
<link rel="stylesheet" href="../luadoc.css" type="text/css" />
7
<!--meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/-->
14
<div id="product_logo"></div>
15
<div id="product_name"><big><b></b></big></div>
16
<div id="product_description"></div>
17
</div> <!-- id="product" -->
27
<li><a href="../index.html">Index</a></li>
42
<a href="../files/CoreMudlet.html">CoreMudlet.lua</a>
45
<li><strong>DB.lua</strong></li>
48
<a href="../files/DebugTools.html">DebugTools.lua</a>
52
<a href="../files/GUIUtils.html">GUIUtils.lua</a>
56
<a href="../files/Logging.html">Logging.lua</a>
60
<a href="../files/LuaGlobal.html">LuaGlobal.lua</a>
64
<a href="../files/Other.html">Other.lua</a>
68
<a href="../files/StringUtils.html">StringUtils.lua</a>
72
<a href="../files/TableUtils.html">TableUtils.lua</a>
76
<a href="../files/geyser/Geyser.html">geyser/Geyser.lua</a>
80
<a href="../files/geyser/GeyserColor.html">geyser/GeyserColor.lua</a>
84
<a href="../files/geyser/GeyserContainer.html">geyser/GeyserContainer.lua</a>
88
<a href="../files/geyser/GeyserGauge.html">geyser/GeyserGauge.lua</a>
92
<a href="../files/geyser/GeyserGeyser.html">geyser/GeyserGeyser.lua</a>
96
<a href="../files/geyser/GeyserLabel.html">geyser/GeyserLabel.lua</a>
100
<a href="../files/geyser/GeyserMiniConsole.html">geyser/GeyserMiniConsole.lua</a>
104
<a href="../files/geyser/GeyserReposition.html">geyser/GeyserReposition.lua</a>
108
<a href="../files/geyser/GeyserSetConstraints.html">geyser/GeyserSetConstraints.lua</a>
112
<a href="../files/geyser/GeyserTests.html">geyser/GeyserTests.lua</a>
116
<a href="../files/geyser/GeyserUtil.html">geyser/GeyserUtil.lua</a>
120
<a href="../files/geyser/GeyserWindow.html">geyser/GeyserWindow.lua</a>
129
</div> <!-- id="navigation" -->
133
<h1>File <code>DB.lua</code></h1>
142
<table class="function_list">
145
<td class="name" nowrap><a href="#datetime:parse">datetime:parse</a> (source, format, as_epoch)</td>
146
<td class="summary">Parses the specified source string, according to the format if given, to return a representation of the date/time.</td>
150
<td class="name" nowrap><a href="#db:AND">db:AND</a> (...)</td>
151
<td class="summary">Returns a compound database expression that combines all of the simple expressions passed into it.</td>
155
<td class="name" nowrap><a href="#db:OR">db:OR</a> (left, right)</td>
156
<td class="summary">Returns a compound database expression that combines both of the simple expressions passed into it.</td>
160
<td class="name" nowrap><a href="#db:Timestamp">db:Timestamp</a> (ts, fmt)</td>
161
<td class="summary"><b><u>TODO</u></b> </td>
165
<td class="name" nowrap><a href="#db:add">db:add</a> (sheet, ...)</td>
166
<td class="summary">Adds one or more new rows to the specified sheet.</td>
170
<td class="name" nowrap><a href="#db:aggregate">db:aggregate</a> (field, fn, query)</td>
171
<td class="summary">Returns the result of calling the specified aggregate function on the field and its sheet.</td>
175
<td class="name" nowrap><a href="#db:between">db:between</a> (field, left_bound, right_bound)</td>
176
<td class="summary">Returns a database expression to test if the field in the sheet is a value between lower_bound and upper_bound.</td>
180
<td class="name" nowrap><a href="#db:close">db:close</a> ()</td>
181
<td class="summary"><b><u>TODO</u></b> </td>
185
<td class="name" nowrap><a href="#db:create">db:create</a> (db_name, sheets)</td>
186
<td class="summary">Creates and/or modifies an existing database.</td>
190
<td class="name" nowrap><a href="#db:delete">db:delete</a> (sheet, query)</td>
191
<td class="summary">Deletes rows from the specified sheet.</td>
195
<td class="name" nowrap><a href="#db:echo_sql">db:echo_sql</a> (sql)</td>
196
<td class="summary">This is a debugging function, which echos any SQL commands if db.debug_sql is true.</td>
200
<td class="name" nowrap><a href="#db:eq">db:eq</a> (field, value, case_insensitive)</td>
201
<td class="summary">Returns a database expression to test if the field in the sheet is equal to the value.</td>
205
<td class="name" nowrap><a href="#db:exp">db:exp</a> (text)</td>
206
<td class="summary">Returns the string as-is to the database.</td>
210
<td class="name" nowrap><a href="#db:fetch">db:fetch</a> (sheet, query, order_by, descending)</td>
211
<td class="summary">Returns a table array containing a table for each matching row in the specified sheet.</td>
215
<td class="name" nowrap><a href="#db:fetch_sql">db:fetch_sql</a> (sheet, sql)</td>
216
<td class="summary">Execute SQL select query against database.</td>
220
<td class="name" nowrap><a href="#db:get_database">db:get_database</a> (db_name)</td>
221
<td class="summary">Returns a reference of an already existing database.</td>
225
<td class="name" nowrap><a href="#db:gt">db:gt</a> (field, value)</td>
226
<td class="summary">Returns a database expression to test if the field in the sheet is greater than to the value.</td>
230
<td class="name" nowrap><a href="#db:gte">db:gte</a> (field, value)</td>
231
<td class="summary">Returns a database expression to test if the field in the sheet is greater than or equal to the value.</td>
235
<td class="name" nowrap><a href="#db:in_">db:in_</a> (field, tbl)</td>
236
<td class="summary">Returns a database expression to test if the field in the sheet is one of the values in the table array.</td>
240
<td class="name" nowrap><a href="#db:is_nil">db:is_nil</a> (field)</td>
241
<td class="summary">Returns a database expression to test if the field in the sheet is nil.</td>
245
<td class="name" nowrap><a href="#db:is_not_nil">db:is_not_nil</a> (field)</td>
246
<td class="summary">Returns a database expression to test if the field in the sheet is not nil.</td>
250
<td class="name" nowrap><a href="#db:like">db:like</a> (field, value)</td>
251
<td class="summary">Returns a database expression to test if the field in the sheet matches the specified pattern.</td>
255
<td class="name" nowrap><a href="#db:lt">db:lt</a> (field, value)</td>
256
<td class="summary">Returns a database expression to test if the field in the sheet is less than the value.</td>
260
<td class="name" nowrap><a href="#db:lte">db:lte</a> (field, value)</td>
261
<td class="summary">Returns a database expression to test if the field in the sheet is less than or equal to the value.</td>
265
<td class="name" nowrap><a href="#db:merge_unique">db:merge_unique</a> (sheet, tables)</td>
266
<td class="summary">Merges the specified table array into the sheet, modifying any existing rows and adding any that don't exist.</td>
270
<td class="name" nowrap><a href="#db:not_between">db:not_between</a> (field, left_bound, right_bound)</td>
271
<td class="summary">Returns a database expression to test if the field in the sheet is NOT a value between lower_bound and upper_bound.</td>
275
<td class="name" nowrap><a href="#db:not_eq">db:not_eq</a> (field, value, case_insensitive)</td>
276
<td class="summary">Returns a database expression to test if the field in the sheet is NOT equal to the value.</td>
280
<td class="name" nowrap><a href="#db:not_in">db:not_in</a> (field, tbl)</td>
281
<td class="summary">Returns a database expression to test if the field in the sheet is not one of the values in the table array.</td>
285
<td class="name" nowrap><a href="#db:not_like">db:not_like</a> (field, value)</td>
286
<td class="summary">Returns a database expression to test if the field in the sheet does not match the specified pattern.</td>
290
<td class="name" nowrap><a href="#db:safe_name">db:safe_name</a> (name)</td>
291
<td class="summary"><b><u>TODO</u></b> db:safe_name(name) On a filesystem level, names are restricted to being alphanumeric only.</td>
295
<td class="name" nowrap><a href="#db:set">db:set</a> (field, value, query)</td>
296
<td class="summary">The db:set function allows you to set a certain field to a certain value across an entire sheet.</td>
300
<td class="name" nowrap><a href="#db:update">db:update</a> (sheet, tbl)</td>
301
<td class="summary">This function updates a row in the specified sheet, but only accepts a row which has been previously obtained by db:fetch.</td>
317
<h2><a name="functions"></a>Functions</h2>
318
<dl class="function">
322
<dt><a name="datetime:parse"></a>
323
<b><big>datetime:parse </big></b>(source, format, as_epoch)</dt>
326
Parses the specified source string, according to the format if given, to return a representation of the date/time. The default format if not specified is: "^%Y-%m-%d %H:%M:%S$" <br/><br/> If as_epoch is provided and true, the return value will be a Unix epoch -- the number of seconds since 1970. This is a useful format for exchanging date/times with other systems. If as_epoch is false, then a Lua time table will be returned. Details of the time tables are provided in the http://www.lua.org/pil/22.1.html. <br/><br/> Supported Format Codes </pre> %b Abbreviated Month Name %B Full Month Name %d Day of Month %H Hour (24-hour format) %I Hour (12-hour format, requires %p as well) %p AM or PM %m 2-digit month (01-12) %M 2-digit minutes (00-59) %S 2-digit seconds (00-59) %y 2-digit year (00-99), will automatically prepend 20 so 10 becomes 2010 and not 1910. %Y 4-digit year. </pre>
362
<dt><a name="db:AND"></a>
363
<b><big>db:AND </big></b>(...)</dt>
366
Returns a compound database expression that combines all of the simple expressions passed into it. These expressions should be generated with other db: functions such as db:eq, db:like, db:lt and the like. <br/><br/> This compound expression will only find items in the sheet if all sub-expressions match.
391
<li><a href='DB.html#db:fetch'>db:fetch</a>
401
<dt><a name="db:OR"></a>
402
<b><big>db:OR </big></b>(left, right)</dt>
405
Returns a compound database expression that combines both of the simple expressions passed into it. These expressions should be generated with other db: functions such as db:eq, db:like, db:lt and the like. <br/><br/> This compound expression will find any item that matches either the first or the second sub-expression.
434
<li><a href='DB.html#db:fetch'>db:fetch</a>
444
<dt><a name="db:Timestamp"></a>
445
<b><big>db:Timestamp </big></b>(ts, fmt)</dt>
480
<dt><a name="db:add"></a>
481
<b><big>db:add </big></b>(sheet, ...)</dt>
484
Adds one or more new rows to the specified sheet. If any of these rows would violate a UNIQUE index, a lua error will be thrown and execution will cancel. As such it is advisable that if you use a UNIQUE index, you test those values before you attempt to insert a new row. <br/><br/> Each table is a series of key-value pairs to set the values of the sheet, but if any keys do not exist then they will be set to nil or the default value. As you can see, all fields are optional.
509
<li>Adding one record. <pre> db:add(mydb.enemies, {name="Bob Smith", city="San Francisco"})
512
<li>Adding multiple records. <pre> db:add(mydb.enemies,
513
{name="John Smith", city="San Francisco"},
514
{name="Jane Smith", city="San Francisco"},
515
{name="Richard Clark"}
531
<dt><a name="db:aggregate"></a>
532
<b><big>db:aggregate </big></b>(field, fn, query)</dt>
535
Returns the result of calling the specified aggregate function on the field and its sheet. <br/><br/> The supported aggregate functions are: <pre> COUNT - Returns the total number of records that are in the sheet or match the query.
536
AVG - Returns the average of all the numbers in the specified field.
537
MAX - Returns the highest number in the specified field.
538
MIN - Returns the lowest number in the specified field.
539
TOTAL - Returns the value of adding all the contents of the specified field.
568
<li>Example: <pre> local mydb = db:get_database("my database")
569
echo(db:aggregate(mydb.enemies.name, "count"))
583
<dt><a name="db:between"></a>
584
<b><big>db:between </big></b>(field, left_bound, right_bound)</dt>
587
Returns a database expression to test if the field in the sheet is a value between lower_bound and upper_bound. This only really makes sense for numbers and Timestamps.
620
<li><a href='DB.html#db:not_between'>db:not_between</a>
622
<li><a href='DB.html#db:fetch'>db:fetch</a>
632
<dt><a name="db:close"></a>
633
<b><big>db:close </big></b>()</dt>
655
<dt><a name="db:create"></a>
656
<b><big>db:create </big></b>(db_name, sheets)</dt>
659
Creates and/or modifies an existing database. This function is safe to define at a top-level of a Mudlet script: in fact it is reccommended you run this function at a top-level without any kind of guards. If the named database does not exist it will create it. If the database does exist then it will add any columns or indexes which didn't exist before to that database. If the database already has all the specified columns and indexes, it will do nothing. <br/><br/> The database will be called Database_<sanitized database name>.db and will be stored in the Mudlet configuration directory. <br/><br/> Database 'tables' are called 'sheets' consistently throughout this documentation, to avoid confusion with Lua tables. <br/><br/> The schema table must be a Lua table array containing table dictionaries that define the structure and layout of each sheet. <br/><br/> For sheets with unique indexes, you may specify a _violations key to indicate how the db layer handle cases where the unique index is violated. The options you may use are: <pre> FAIL - the default. A hard error is thrown, cancelling the script.
660
IGNORE - The command that would add a record that violates uniqueness just fails silently.
661
REPLACE - The old record which matched the unique index is dropped, and the new one is added to replace it.
686
<li>Example bellow will create a database with two sheets; the first is kills and is used to track every successful kill, with both where and when the kill happened. It has one index, a compound index tracking the combination of name and area. The second sheet has two indexes, but one is unique: it isn't possible to add two items to the enemies sheet with the same name. <pre> local mydb = db:create("combat_log",
691
killed = db:Timestamp("CURRENT_TIMESTAMP"),
692
_index = {{"name", "area"}}
698
enemied = db:Timestamp("CURRENT_TIMESTAMP"),
700
_unique = { "name" },
701
_violations = "IGNORE"
705
</pre> Note that you have to use double {{ }} if you have composite index/unique constrain.
718
<dt><a name="db:delete"></a>
719
<b><big>db:delete </big></b>(sheet, query)</dt>
722
Deletes rows from the specified sheet. The argument for query tries to be intelligent: <br/> * if it is a simple number, it deletes a specific row by _row_id <br/> * if it is a table that contains a _row_id (e.g., a table returned by db:get) it deletes just that record. <br/> * Otherwise, it deletes every record which matches the query pattern which is specified as with db:get. <br/> * If the query is simply true, then it will truncate the entire contents of the sheet. <br/>
747
<li>When passed an actual result table that was obtained from db:fetch, it will delete the record for that table. <pre> enemies = db:fetch(mydb.enemies)
748
db:delete(mydb.enemies, enemies[1])
751
<li>When passed a number, will delete the record for that _row_id. This example shows getting the row id from a table. <pre> enemies = db:fetch(mydb.enemies)
752
db:delete(mydb.enemies, enemies[1]._row_id)
755
<li>As above, but this example just passes in the row id directly. <pre> db:delete(mydb.enemies, 5)
758
<li>Here, we will delete anything which matches the same kind of query as db:fetch uses - namely, anyone who is in the city of San Francisco. <pre> db:delete(mydb.enemies, db:eq(mydb.enemies.city, "San Francisco"))
761
<li>And finally, we will delete the entire contents of the enemies table. <pre> db:delete(mydb.enemies, true)
776
<dt><a name="db:echo_sql"></a>
777
<b><big>db:echo_sql </big></b>(sql)</dt>
780
This is a debugging function, which echos any SQL commands if db.debug_sql is true. You should not call this function directly from Mudlet.
800
<li>Set following lua variable to enable SQL echos. <pre> db.debug_sql=true
814
<dt><a name="db:eq"></a>
815
<b><big>db:eq </big></b>(field, value, case_insensitive)</dt>
818
Returns a database expression to test if the field in the sheet is equal to the value.
851
<li><a href='DB.html#db:fetch'>db:fetch</a>
861
<dt><a name="db:exp"></a>
862
<b><big>db:exp </big></b>(text)</dt>
865
Returns the string as-is to the database. <br/><br/> Use this function with caution, but it is very useful in some circumstances. One of the most common of such is incrementing an existing field in a db:set() operation, as so: <pre> db:set(mydb.enemies, db:exp("kills + 1"), db:eq(mydb.enemies.name, "Ixokai"))
866
</pre> This will increment the value of the kills field for the row identified by the name Ixokai. <br/><br/> But there are other uses, as the underlining database layer provides many functions you can call to do certain things. If you want to get a list of all your enemies who have a name longer then 10 characters, you may do: <pre> db:fetch(mydb.enemies, db:exp("length(name) > 10"))
867
</pre> Again, take special care with this, as you are doing SQL syntax directly and the library can't help you get things right.
892
<li><a href='DB.html#db:fetch'>db:fetch</a>
902
<dt><a name="db:fetch"></a>
903
<b><big>db:fetch </big></b>(sheet, query, order_by, descending)</dt>
906
Returns a table array containing a table for each matching row in the specified sheet. All arguments but sheet are optional. If query is nil, the entire contents of the sheet will be returned. <br/><br/> Query is a string which should be built by calling the various db: expression functions, such as db:eq, db:AND, and such. You may pass a SQL WHERE clause here if you wish, but doing so is very dangerous. If you don't know SQL well, its best to build the expression.<br/><br/> Query may also be a table array of such expressions, if so they will be AND'd together implicitly.<br/><br/> The results that are returned are not in any guaranteed order, though they are usually the same order as the records were inserted. If you want to rely on the order in any way, you must pass a value to the order_by field. This must be a table array listing the columns you want to sort by. It can be { "column1" }, or { "column1", "column2" } <br/><br/> The results are returned in ascending (smallest to largest) order; to reverse this pass true into the final field.
939
<li>The first will fetch all of your enemies, sorted first by the city they reside in and then by their name. <pre> db:fetch(mydb.enemies, nil, {"city", "name"})
942
<li>The second will fetch only the enemies which are in San Francisco. <pre> db:fetch(mydb.enemies, db:eq(mydb.enemies.city, "San Francisco"))
945
<li>The third will fetch all the things you've killed in Undervault which have Drow in their name. <pre> db:fetch(mydb.kills,
947
db:eq(mydb.kills.area, "Undervault"),
948
db:like(mydb.kills.name, "%Drow%")
962
<li><a href='DB.html#db:fetch_sql'>db:fetch_sql</a>
972
<dt><a name="db:fetch_sql"></a>
973
<b><big>db:fetch_sql </big></b>(sheet, sql)</dt>
976
Execute SQL select query against database. This only useful for some very specific cases. <br/> Use db:fetch if possible instead - this function should not be normally used!
979
<br/><b>Release:</b> post Mudlet 1.1.1 (<b><u>TODO update before release</u></b>)
1002
<li>Following will select all distinct area from my kills DB. <pre> db:fetch_sql(mydb.kills, "SELECT distinct area FROM kills")
1013
<li><a href='DB.html#db:fetch'>db:fetch</a>
1023
<dt><a name="db:get_database"></a>
1024
<b><big>db:get_database </big></b>(db_name)</dt>
1027
Returns a reference of an already existing database. This instance can be used to get references to the sheets (and from there, fields) that are defined within the database. You use these references to construct queries. <br/><br/> These references do not contain any actual data, they only point to parts of the database structure.
1047
<li>If a database has a sheet named enemies, you can obtain a reference to that sheet by simply doing: <pre> local mydb = db:get_database("my database")
1048
local enemies_ref = mydb.enemies
1049
local name_ref = mydb.enemies.name
1063
<dt><a name="db:gt"></a>
1064
<b><big>db:gt </big></b>(field, value)</dt>
1067
Returns a database expression to test if the field in the sheet is greater than to the value.
1096
<li><a href='DB.html#db:fetch'>db:fetch</a>
1106
<dt><a name="db:gte"></a>
1107
<b><big>db:gte </big></b>(field, value)</dt>
1110
Returns a database expression to test if the field in the sheet is greater than or equal to the value.
1139
<li><a href='DB.html#db:fetch'>db:fetch</a>
1149
<dt><a name="db:in_"></a>
1150
<b><big>db:in_ </big></b>(field, tbl)</dt>
1153
Returns a database expression to test if the field in the sheet is one of the values in the table array. <br/><br/> First, note the trailing underscore carefully! It is required.
1177
<li>The following example illustrates the use of <b>in_</b>: This will obtain all of your kills which happened in the Undervault, Hell or Purgatory. Every db:in_ expression can be written as a db:OR, but that quite often gets very complex. <pre> local mydb = db:get_database("my database")
1178
local areas = {"Undervault", "Hell", "Purgatory"}
1179
db:fetch(mydb.kills, db:in_(mydb.kills.area, areas))
1190
<li><a href='DB.html#db:fetch'>db:fetch</a>
1200
<dt><a name="db:is_nil"></a>
1201
<b><big>db:is_nil </big></b>(field)</dt>
1204
Returns a database expression to test if the field in the sheet is nil.
1229
<li><a href='DB.html#db:fetch'>db:fetch</a>
1239
<dt><a name="db:is_not_nil"></a>
1240
<b><big>db:is_not_nil </big></b>(field)</dt>
1243
Returns a database expression to test if the field in the sheet is not nil.
1268
<li><a href='DB.html#db:fetch'>db:fetch</a>
1278
<dt><a name="db:like"></a>
1279
<b><big>db:like </big></b>(field, value)</dt>
1282
Returns a database expression to test if the field in the sheet matches the specified pattern. <br/><br/> LIKE patterns are not case-sensitive, and allow two wild cards. The first is an underscore which matches any single one character. The second is a percent symbol which matches zero or more of any character. <pre> LIKE with "_" is therefore the same as the "." regular expression.
1283
LIKE with "%" is therefore the same as ".*" regular expression.
1313
<li><a href='DB.html#db:not_like'>db:not_like</a>
1315
<li><a href='DB.html#db:fetch'>db:fetch</a>
1325
<dt><a name="db:lt"></a>
1326
<b><big>db:lt </big></b>(field, value)</dt>
1329
Returns a database expression to test if the field in the sheet is less than the value.
1358
<li><a href='DB.html#db:fetch'>db:fetch</a>
1368
<dt><a name="db:lte"></a>
1369
<b><big>db:lte </big></b>(field, value)</dt>
1372
Returns a database expression to test if the field in the sheet is less than or equal to the value.
1401
<li><a href='DB.html#db:fetch'>db:fetch</a>
1411
<dt><a name="db:merge_unique"></a>
1412
<b><big>db:merge_unique </big></b>(sheet, tables)</dt>
1415
Merges the specified table array into the sheet, modifying any existing rows and adding any that don't exist. This function is a convenience utility that allows you to quickly modify a sheet, changing existing rows and add new ones as appropriate. It ONLY works on sheets which have a unique index, and only when that unique index is only on a single field. For more complex situations you'll have to do the logic yourself. The table array may contain tables that were either returned previously by db:fetch, or new tables that you've constructed with the correct fields, or any mix of both. Each table must have a value for the unique key that has been set on this sheet.
1439
<li>For example, consider this database: <pre> local mydb = db:create("peopledb",
1446
_index = { "city" },
1447
_unique = { "name" }
1451
</pre> Here you have a database with one sheet, which contains your friends, their race, level, and what city they live in. Let's say you want to fetch everyone who lives in San Francisco, you could do: <pre> local results = db:fetch(mydb.friends, db:eq(mydb.friends.city, "San Francisco"))
1452
</pre> The tables in results are static, any changes to them are not saved back to the database. But after a major radioactive cataclysm rendered everyone in San Francisco a mutant, you could make changes to the tables as so: <pre> for _, friend in ipairs(results) do
1453
friend.race = "Mutant"
1455
</pre> If you are also now aware of a new arrival in San Francisco, you could add them to that existing table array: <pre> results[#results+1] = {name="Bobette", race="Mutant", city="San Francisco"}
1456
</pre> And commit all of these changes back to the database at once with: <pre> db:merge_unique(mydb.friends, results)
1457
</pre> The db:merge_unique function will change the 'city' values for all the people who we previously fetched, but then add a new record as well.
1470
<dt><a name="db:not_between"></a>
1471
<b><big>db:not_between </big></b>(field, left_bound, right_bound)</dt>
1474
Returns a database expression to test if the field in the sheet is NOT a value between lower_bound and upper_bound. This only really makes sense for numbers and Timestamps.
1507
<li><a href='DB.html#db:between'>db:between</a>
1509
<li><a href='DB.html#db:fetch'>db:fetch</a>
1519
<dt><a name="db:not_eq"></a>
1520
<b><big>db:not_eq </big></b>(field, value, case_insensitive)</dt>
1523
Returns a database expression to test if the field in the sheet is NOT equal to the value.
1556
<li><a href='DB.html#db:fetch'>db:fetch</a>
1566
<dt><a name="db:not_in"></a>
1567
<b><big>db:not_in </big></b>(field, tbl)</dt>
1570
Returns a database expression to test if the field in the sheet is not one of the values in the table array.
1599
<li><a href='DB.html#db:in_'>db:in_</a>
1601
<li><a href='DB.html#db:fetch'>db:fetch</a>
1611
<dt><a name="db:not_like"></a>
1612
<b><big>db:not_like </big></b>(field, value)</dt>
1615
Returns a database expression to test if the field in the sheet does not match the specified pattern. LIKE patterns are not case-sensitive, and allow two wild cards. The first is an underscore which matches any single one character. The second is a percent symbol which matches zero or more of any character. <pre> LIKE with "_" is therefore the same as the "." regular expression.
1616
LIKE with "%" is therefore the same as ".*" regular expression.
1646
<li><a href='DB.html#db:like'>db:like</a>
1648
<li><a href='DB.html#db:fetch'>db:fetch</a>
1658
<dt><a name="db:safe_name"></a>
1659
<b><big>db:safe_name </big></b>(name)</dt>
1662
<b><u>TODO</u></b> db:safe_name(name) On a filesystem level, names are restricted to being alphanumeric only. So, "my_database" becomes "mydatabase", and "../../../../etc/passwd" becomes "etcpasswd". This prevents any possible security issues with database names.
1690
<dt><a name="db:set"></a>
1691
<b><big>db:set </big></b>(field, value, query)</dt>
1694
The db:set function allows you to set a certain field to a certain value across an entire sheet. Meaning, you can change all of the last_read fields in the sheet to a certain value, or possibly only the last_read fields which are in a certain city. The query argument can be any value which is appropriate for db:fetch, even nil which will change the value for the specified column for EVERY row in the sheet. For example, consider a situation in which you are tracking how many times you find a certain type of egg during Easter. You start by setting up your database and adding an Eggs sheet, and then adding a record for each type of egg. <pre> local mydb = db:create("egg database", {eggs = {color = "", last_found = db.Timestamp(false), found = 0}})
1702
</pre> Now, you have three columns. One is a string, one a timestamp (that ends up as nil in the database), and one is a number. <br/><br/> You can then set up a trigger to capture from the mud the string, "You pick up a (.*) egg!", and you end up arranging to store the value of that expression in a variable called "myegg". <br/><br/> To increment how many we found, we will do this: <pre> myegg = "Red" -- We will pretend a trigger set this.
1703
db:set(mydb.eggs.found, db:exp("found + 1"), db:eq(mydb.eggs.color, myegg))
1704
db:set(mydb.eggs.last_found, db.Timestamp("CURRENT_TIMESTAMP"), db:eq(mydb.eggs.color, myegg))
1705
</pre> This will go out and set two fields in the Red egg sheet; the first is the found field, which will increment the value of that field (using the special db:exp function). The second will update the last_found field with the current time. <br/><br/> Once this contest is over, you may wish to reset this data but keep the database around. To do that, you may use a more broad use of db:set as such: <pre> db:set(mydb.eggs.found, 0)
1706
db:set(mydb.eggs.last_found, nil)
1743
<dt><a name="db:update"></a>
1744
<b><big>db:update </big></b>(sheet, tbl)</dt>
1747
This function updates a row in the specified sheet, but only accepts a row which has been previously obtained by db:fetch. Its primary purpose is that if you do a db:fetch, then change the value of a field or tow, you can save back that table.
1771
<li>This obtains a database reference, and queries the friends sheet for someone named Bob. As this returns a table array containing only one item, it assigns that one item to the local variable named bob. We then change the notes on Bob, and pass it into db:update() to save the changes back. <pre> local mydb = db:get_database("my database")
1772
local bob = db:fetch(mydb.friends, db:eq(mydb.friends.name, "Bob"))[1]
1773
bob.notes = "He's a really awesome guy."
1774
db:update(mydb.friends, bob)
1794
</div> <!-- id="content" -->
1796
</div> <!-- id="main" -->
1799
<p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
1800
</div> <!-- id="about" -->
1802
</div> <!-- id="container" -->
b'\\ No newline at end of file'