I was recently working on some MySQL 5.5 performance testing. I had to generate a lot of SQL queries I would then use for my testing. To make my tests repeatable I needed to hardcode the values for IDs. That is, I couldn’t simply use:
because this query may generate a row with a different ID each time (depending on the value of auto_increment for the table_name). What I really needed is:
To accomplish that I needed to know a value of the next auto increment ID for my table. Here is how can you retrieve that value in MySQL:
This kind of “predicting” next ID will not work reliably and you should never use it in real application. It is safe in my case, as I’m only using it for generating data (queries) and I’m 100% that there are no concurrent connections to my database.
Below I will demonstrate how to use an excellent Dependency Injection component from Symfony 2, without using the whole Symfony framework. I will also use ClassLoader from Symfony and just for the sake of the demo I will integrate it with Zend Framework. The code requires PHP 5.3 (mostly for namespaces support).
Create a directory for your project. Inside create “lib” directory. Inside lib, create directory structure: Symfony/Component. Put ClassLoader code inside Component – the easiest thing to do is to clone if from Symfony github:
The same goes for DependencyInjection component:
Finally download Zend Framework and put the contents of Zend directory into lib/Zend (so for instance Log.php file will be available in lib/Zend/Log.php).
The actual source code will go into “src” directory, which is a sibling directory of “lib”.
Configuring the ClassLoader
DependencyInjection uses namespaces for managing classes, so it needs to be registered with registerNamespace method. Zend Framework follows PEAR naming convention for classes – registerPrefix will do the work for us. Finally, I will register our own code that will be stored in src directory. I will use namespaces as well. Create a new file (let’s call it main.php) in the top-level directory:
ClassLoader should now work just fine but we still need set_include_path so require functions inside Zend code will work correctly.
Dependency Injection container
Create a sample class that we’ll use for testing. I will call it Test and put it into Zabuchy\Techblog, which means it should be located at src/Zabuchy/Techblog/Test.php:
$logger should be injected using container – here is where we will use Zend_Log class. This one in turn requires a Writer, so we will create it as well. The rest of main.php will look like this:
Running the code should give an output like below:
Complete code
For reader’s convenience I’m attaching a full source code – including a copy of Zend library and Symfony 2 components.
This is part 5 of the Java Technologies Integration tutorial. The purpose of this part is to provide updates to Hibernate configuration in existing project to make the maintenance of the project more straight forward.
In order to do so the following files should be changed as follows:
Hibernate.xml
sessionFactory bean
Change sessionFactory bean from org.springframework.orm.hibernate3.LocalSessionFactoryBean to org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean. Such change allows using Hibernate annotations automatically.
packagesToScan property
Remove mappingResources property including all the mapping files reference. Instead add packagesToScan property. As the value enter package name, where your classes with hibernate annotations are located, e.g, net.opensesam or to be more specific net.opensesam.entity. Hibernate will scan your package and find all the entities (classes annotated with @Entity) automatically, so you do not have to waste your time on configuration of entity classes.
It is possible to define one package to be scanned:
as well as multiple locations of Hibernate annotated files:
annotatedClasses property
Instead of scanning you can also use annotatedClasses property, but then you have to list all the classes annotated as @Entity explicitly. For example:
Hibernate.xml file
Modified Hibernate.xml file of the project is presented below.
Mapping files
Having the changes introduced you no longer need Hibernate mapping files (*.hbm.xml). Remove them from src/main/resources/resources/hibernate/ directory.
Below is the fastest way to install the latest trunk version (currently 1.9.6) of Battle of Wesnoth on your Ubuntu Natty Narwhal. Note: it will probably install a bit too many packages than you really need.
That’s all – keep in mind that this may take a long time to execute… To play the game, you don’t need to install it, simply type:
I’d like to share a quick guide on how to set up master/slave replication for the MySQL 5.5 server. The procedure below should be used for development/testing only. If you want to create a production-ready setup, you should follow instructions from MySQL official documentation or use MySQL server packaged by your favorite Linux distribution.
2. Follow the installation instructions on http://dev.mysql.com/doc/refman/5.5/en/binary-installation.html or instructions below if you don’t care about the secure setup (e.g. you are only using this MySQL installation for testing). You should also follow my instructions if you want to avoid conflicts with the MySQL you may have installed from your Linux distribution package. Do not follow them for the production setup.
switch to root
on ubuntu/debian run sudo apt-get install libaio1
cd /opt
download mysql tarball
unpack it tar -zxf mysql-5.5.3-m3-linux2.6-x86_64-icc.tar.gz
You should be able to connect as a root without the password
To shut down your MySQL use
Now, repeat the procedure on the second machine and we can move on.
3. Set up the replication.
Master will already be configured for the replication, as the whole necessary setup (log-bin,server-id) is already included in support-files/my-medium.cnf. You will still need to create a replication user on the slave:
Go to your slave and set in the /opt/mysql/support-files/my-medium.cnf: server-id=2 (replace the default id 1 with 2)
Make sure you can connect from the machine where slave is installed to the master MySQL server, using ‘repl’ user.
That’s it! Let’s test it, on your master issue:
Corresponding table should be created on slave and should contain one record. Now, let’s test temporary disabling replication log on master:
You table on master should contain 5 rows but the one on the slave only 3 – the values 2 and 3 should be missing.
I wanted to change the way teasers are displayed – disable this information about the node submission: published by admin on Fri, 04/22/2011 – 17:25.
As it turns out, there is a variable that Drupal checks when generating submission information. The variable is set per each content type in a format node_submitted_<NODE TYPE>, e.g., node_submitted_page and should be simply set to TRUE or FALSE. Here is the code I’ve added to my micropt module, to micropt.install
You can change the value of the variable using drush or directly in the database:<p></p>
“b:1;” is a serialized value for TRUE, for FALSE use “b:0;”. If you do so, don’t forget to clear the cache!
Say you would like to transfer set of files from location A to B. The source directory is really big but you are only interested in transferring some files (say *.png only). rsync is a good tool for the job. There are two methods I know that can be utilized.
find + rsync
Use find to select the files and then send them over with rsync:
This will create the minimum directories (and sub-directories) required for your files.
rsync only
Use rsync command only with –include and –exclude options.
The difference from the previous command is that rsync will create all the directories from source in destination, and then copy *.png files. It will potentially leave your destination filled with empty directories – which may or may not be what you want.
to make perl properly handle utf-8 strings inserted and retrieved from ms sql database, freetds config must be used (the configuration with only odbc.ini is possible to make perl connect to a database, but i could not find any way to make it work with utf)
for this purpose, the entry in odbc.ini should contain “Servername”, which points to an entry in the freetds.conf. the sample configuration may look as following:
#/etc/odbc.ini
#/usr/local/etc/freetds.conf
#perl script connection string:
tips:
- use tds dump for debugging. for this, set the TDSDUMP env variable and try to connect; read debug info from the dump file:
- use tsql to debug connection. in another terminal, set the TDSDUMP variable, run
then exit, and compare information from the tsql’s dump with the script’s dump
- be careful with config files; freetds can read from /usr/local/etc/freetds or /etc/freetds.conf depending on it’s installation configuration, but will definitely use only $HOME/.freetds.conf if it exists
- and do not forget to enclose your utf-8 strings in N” in queries, otherwise ms sql server will not understand it’s utf:
Update on 2011.12.01: the script doesn’t work for my FF8 anymore, looks like checkboxmate-lefedor-edition is a way to go now.
Upon installing WYSIWYG module in Drupal 7, site builder is presented with rather annoying task of selecting all the buttons to be enabled. The corresponding section of the WYSIWYG form – “buttons and plugins” is rather intimidating:
Site builder is required to go through all the checkboxes and check them manually. There is some ongoing work to fix the problem on Drupal side but meanwhile there is a better way – Firefox plugin! Install greasemonkey, restart your browser and add this user script.
The usage is not very straightforward at the beginning but it’s very handy once you get used to it. Position your mouse cursor above the first checkbox, click and drag with the mouse. All the checkboxes inside the rectangle you’ll draw will flip their state.
Normally when you quit less, your screen will be redrawn and you will be back with the content you could see on your screen before using less. Sometimes though it’s very handy to see the file with less, scroll down to the fragment of your interest and have that fragment still visible on the screen after you quit less.
Although the results may vary depending on what kind of terminal you use, try -X option when invoking less:
This and more cool tricks with less can be found on the excellent less FAQ.