BeBot - An Anarchy Online and Age Of Conan chat automaton

Development => Coding and development discussion => Topic started by: Alreadythere on January 26, 2006, 11:27:14 am

Title: Thoughts Prefix/Suffix for tablenames
Post by: Alreadythere on January 26, 2006, 11:27:14 am
I've just done a revision of the function I use to add prefix and suffix as I wish to tablenames, so I thought I'd post it again :)

I don't see any other way then to modify Bot.php for this function, as I can't make sure otherwise that it is loaded when needed. As I don't have any influence on the order in which modules are loaded.

I added
Code: [Select]
$master_table = strtolower($bot_name) . '_tablenames'; to my Bot.conf, added $master_table to the constructor of the Bot class, and added two vars to the Bot class in the constructor: $master_tablename and $tablenames.
Code: [Select]
$this -> master_tablename = $master_table;
$this -> tablenames = array();


To create the table with all the names and the default entries for prefix and suffix I've added the following lines into main.php somewhere behind the line creating the $db object:
Code: [Select]
$db -> query("CREATE TABLE IF NOT EXISTS " . $master_table . "(internal_name VARCHAR(255) NOT NULL PRIMARY KEY, table_name VARCHAR(100) NOT NULL, "
. " use_prefix VARCHAR(10) NOT NULL DEFAULT 'false', use_suffix VARCHAR(10) NOT NULL DEFAULT 'false')");

  // add the default settings, if not existing
  $db -> query("INSERT IGNORE INTO " . $master_table . " (internal_name, table_name, use_prefix, use_suffix) VALUES ('prefix', '" . strtolower($bot_name) . "', 'false', 'false')");
  $db -> query("INSERT IGNORE INTO " . $master_table . " (internal_name, table_name, use_prefix, use_suffix) VALUES ('suffix', '" . strtolower($bot_name) . "', 'false', 'false')");


The function that finally handles all requests is in the Bot class. On first query for a tablename $table it checks if an entry for the table exists. If it does, it creats the tablename out of the information there, saves it in $tablenames for caching and returns it. If it doesn't exist, it creates a entry in the form $botname_$table and caches this name. On all further requests for $table the function returns the cached value. This saves database queries, and it doesn't make sense to change tablenames during the bot runtime anyways.
Code: [Select]
        /*
        Returns the name for the selected table:
        If the tablename isn't in the database, it creates a new one defaulting to botname_$table.
        To reduce total number of database selects, the tablenames are cached on first query.
        It is assumed that they don't change during the bot runtime
        */
        function get_tablename($table)
        {
                // get name out of cached entries if possible:
                if (isset($this -> tablenames[$table]))
                {
                        return $this -> tablenames[$table];
                }

                // check the database for the name, default prefix and default suffix:
                $name = $this -> db -> select("SELECT * FROM " . $this -> master_tablename . " WHERE internal_name = '" . $table . "'");
                $prefix = $this -> db -> select("SELECT * FROM " . $this -> master_tablename . " WHERE internal_name = 'prefix'");
                $suffix = $this -> db -> select("SELECT * FROM " . $this -> master_tablename . " WHERE internal_name = 'suffix'");

                if (empty($name))
                {
                        // no entry existing, create one:
                        $tablename = $prefix[0][1] . "_" . $table;
                        $this -> db -> query("INSERT INTO " . $this -> master_tablename . " (internal_name, table_name, use_prefix,"
                                . " use_suffix) VALUES ('" . $table . "', '" . $table . "', 'true', 'false')");
                }
                else
                {
                        // entry exists, create the correct tablename:
                        $tablename = $name[0][1];

                        if ($name[0][2] == 'true')
                        {
                                $tablename = $prefix[0][1] . "_" . $tablename;
                        }

                        if ($name[0][3] == 'true')
                        {
                                $tablename = $tablename . "_" . $suffix[0][1];
                        }
                }

                // cache the entry and return it:
                $this -> tablenames[$table] = $tablename;

                return $tablename;
        }

Actually writing this function is the easy part, the hard part is modifying all database queries. Every single one needs to be modified to use this function.

But then, any way of adding prefix/suffix to the names imposes those modifications anyways.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Khalem on January 28, 2006, 03:35:56 am
Some thoughts on this.

First, any examples on where a suffix would be usefull in addition to a prefix?

Second, why hold the suffix/prefix in the database at all. It'd not something you are likely to change, and as such i would much more prefer it to be a config file setting, or at the very least a setting that is read from database on startup and kept in memory eliminating imho uneccecary calls to the database, no matter how insignificant they may seem, things do add up over time, and i do believe things should be as efficient as possible wherever it can be done as a matter of good coding practises.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Alreadythere on January 28, 2006, 11:18:56 am
No clue if anyone even uses a suffix, for me prefix alone always was enough. On the other hand, adding it doesn't add any real overhead, and there may be people that want to use it.

I should at least go and read the prefix/suffix in only once, true. Seeing that table names won't change during runtime, adding them as variables in the Bot.conf would prolly be best.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Khalem on January 29, 2006, 12:26:46 am
Okies.

This is on my ToDo list for getting into SVN when i find time now along with the color configuration.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: buff on March 24, 2006, 09:54:33 am
alreadythere

can you post ur bot.php file?
i had it worked for a while and yesterday i was modifying stuff
forgot to back up now it's all messed up :(
i can't get it to work for some reason
would appreciate if u can post ur bot.php here ;)
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Alreadythere on March 24, 2006, 12:02:47 pm
Sure, here (http://www.m8y.de/ao/bebot/Bot.phps) it is.

But be warned, it's heavily modified.
The buddy list is modified to handle a notify list for non-members.
The is_member() function is extented.
I guess some more things are changed too, can't remember.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Alreadythere on April 13, 2006, 06:40:19 pm
Here (http://www.m8y.de/ao/bebot/corefiles.tgz) is an adaption of the current 0.3.2 core files to prefix and suffix.

Basically I added some parameters inside Bot.conf for default values.
The main.php passes those values to the Bot class.

The Bot class has two new functions:
- the already mentioned get_tablename($table), which gives back the internal name for $table.
- a function define_tablename($table, $useprefix, $usesuffix), which should be used to define the tablename on first calling, usually in the CREATE call - $useprefix and $usesuffix are booleans.

Both functions won't overwrite already existing tablenames in the master table.

And I modified all database calls inside Bot.php and core/* to the new scheme. Haven't found the time to adapt the module calls yet.

If someone is bored, basically it's just nest every tablename inside $this -> bot -> get_tablename(" ... "), and use define_tablename() only inside the create entries.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Alreadythere on May 15, 2006, 07:23:09 pm
Here (http://www.m8y.de/ao/bebot/corefiles.tgz) is an adaption of the current 0.3.2 core files to prefix and suffix.
Have to update the version (as sson as I find time), as it's slightly buggy in the posted state.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Alreadythere on May 30, 2006, 03:28:37 pm
Finally got around to update and test the implementation.

All files with db access are modified, I hope I haven't missed to adapt some lines.
I tested everything as far as startup - which means it doesn't crash there in any of the files. I haven't tried to run tests for every single command, mainly because I'm not using that many.

This (http://www.m8y.de/ao/bebot/tablenames/bebot0.3.2_tablenames.diff) is a diff -Nur of the original vs the modified directory tree.
This (http://www.m8y.de/ao/bebot/tablenames/bebot0.3.2_tablenames.zip) is a zip with all the files including the modifications.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Pharexys on June 07, 2006, 01:46:33 pm
wow i missed this thread :D
I'm gonna test it and maybe even do that to all modules i use :P
so this is the way of doing the prefix/sufix, and to run all bots on 1 db :D
Well gonna give it a try when im home and post here how it works.
Title: Re: Thoughts Prefix/Suffix for tablenames
Post by: Khalem on July 25, 2006, 12:29:28 am
Table prefix support has now made it into SVN.

I did end up making some changes.
Notably i discarded suffixes, i keep coming back to the fact that the likelyhood of people using it is extremely low, it serves no real purpose.
Instead of calling a function in every single query for tablenames, tablenames should be prefixed with #___ in the queries which in turn will be parsed into the proper prefix in  MySQL.php.
SimplePortal 2.3.7 © 2008-2024, SimplePortal