Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. - ratbox-services database hooks -
  2. ------------------------------------------------------
  3.  
  4. This document describes the database hook system contained in
  5. ratbox-services, which provides a method for external programs to
  6. modify parts of the database that ratbox-services keeps in RAM.
  7.  
  8.  
  9. - Database tables -
  10. -------------------
  11.  
  12. All tables used for database hooks must follow the same schema, the only
  13. thing that is variable is the name of the table. The table contains three
  14. columns, an automatically generated ID, the name of a hook and the data.
  15.  
  16. sqlite schema:
  17. CREATE TABLE <name> (id INTEGER PRIMARY KEY, hook TEXT, data TEXT);
  18.  
  19. mysql schema:
  20. CREATE TABLE <name> (id INTEGER AUTO_INCREMENT, hook VARCHAR(50) NOT NULL,
  21. data TEXT, PRIMARY KEY(id));
  22.  
  23. pgsql schema:
  24. CREATE TABLE <name> (id SERIAL, hook VARCHAR(50) NOT NULL,
  25. data TEXT, PRIMARY KEY(id));
  26.  
  27.  
  28. - Inserting data -
  29. ------------------
  30.  
  31. The id column should always be entered as NULL (and not 'NULL') so that an
  32. id is generated automatically.
  33.  
  34. The hook column should be the name of the hook, this name correlates with
  35. the hook in ratbox-services so the right callback is used. This is case
  36. sensitive.
  37.  
  38. The data column is a character delimeted text field of the arguments
  39. ratbox-services needs to deal with the request. The delimeter can be any
  40. character the database will store, there are no limits on the number of
  41. arguments.
  42.  
  43. Examples:
  44. INSERT INTO sync_table (id, hook, data) VALUES(NULL, 'HOOKNAME', 'arg1|arg2|arg3');
  45. INSERT INTO sync_table (id, hook, data) VALUES(NULL, 'HOOKMK2', 'arg1 arg2');
  46. INSERT INTO sync_table (id, hook, data) VALUES(NULL, 'RBHOOK', '');
  47.  
  48.  
  49. - Adding the hook -
  50. -------------------
  51.  
  52. A function of type dbh_callback should be added to the code, which takes
  53. two arguments, struct rsdb_hook *dbh, const char *data:
  54. dbh_callback sync_func(struct rsdb_hook *dbh, const char *data);
  55.  
  56. This function MUST NOT perform any database queries directly.
  57.  
  58. This function should then be added as a hook through rsdb_hook_add(), which
  59. takes four parameters:
  60. - (const char *) The name of the table to search
  61. - (const char *) The name of the hook
  62. - (int) How often to search for entries
  63. - (dbh_callback) The callback function
  64.  
  65. Eg:
  66. rsdb_hook_add("sync_table", "HOOKNAME", 900, sync_func);
  67.  
  68.  
  69. - Called hooks -
  70. ----------------
  71.  
  72. The hook function will be called when there is a row in the given table,
  73. with the given hook. The hook function should "return 1;" when the row
  74. needs to be deleted, or "return 0;" when the row should be left in the
  75. database.
  76.  
  77. string_to_array_delim() is provided to convert the data from its delimited
  78. form into a (char **) array. This function takes the following parameters:
  79. - (char *) string to parse
  80. - (char **) array to store values in
  81. - (char) delimiter
  82. - (int) maximum number of parameters. Always add 1 to the number of
  83. parameters you need.
  84.  
  85. string_to_array_delim() will convert the string into the given array and set
  86. the last field of the array to NULL. To achieve this always, it will parse
  87. upto maxpara-1 parameters -- hence you always need to add 1 to the number of
  88. parameters you need. string_to_array_delim() returns the number of fields
  89. now in the given array (not including the terminating NULL field).
  90.  
  91. To use string_to_array_delim() on the text in the callback, you should first
  92. make a copy of it.
  93.  
  94. Eg:
  95. static int
  96. sync_func(struct rsdb_hook *dbh, const char *c_data)
  97. {
  98. char *argv[3+1];
  99. char *data;
  100. int argc;
  101.  
  102. data = LOCAL_COPY(c_data);
  103. argc = string_to_array_delim(data, argv, '|', 4);
  104.  
  105. ...
  106.  
  107. return 1;
  108. }
  109.  
  110.  
  111. - Performing database queries -
  112. -------------------------------
  113.  
  114. If you need to perform database queries you should schedule these using
  115. rsdb_hook_schedule(). Simply pass the query you wish to execute to this
  116. function, and once ratbox-services has exhausted all rows in the result set,
  117. the queries will be executed.
  118.  
  119. Eg:
  120. rsdb_hook_schedule("DELETE FROM table WHERE x='%Q'", argv[0]);
  121.  
  122.  
  123. - Copyright (C) 2006 Lee Hardy <lee -at- leeh.co.uk> -
  124. - Copyright (C) 2006 ircd-ratbox development team -
  125. - $Id: dbhook.txt 22820 2006-06-21 18:01:00Z leeh $ -
  126. ------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement