MySQL Triggers with Logging

By Michael McLaughlin, Planet MySQLApril 26, 2013 at 01:01AM

Somebody asked why you can’t implement MySQL triggers that write information when you want to stop the DML statement, like autonomous procedures in Oracle. The question was a surprise but I didn’t find anything on it, so here’s how you can do it. This is more or less like an autonomous process by leveraging both the InnoDB and MyISAM engine’s behaviors. This post leverages an earlier explanation of MySQL Triggers.

  1. First you create a MyISAM table, which is a persistent store that auto commits when you’re other InnoDB tables can be transactionally dependent. Here’s a simple MyISAM logger table.
CREATE TABLE logger
( logger_id         INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
, logger_event      VARCHAR(50)
, logger_table      VARCHAR(50)
, logger_instring   VARCHAR(100)
, logger_outstring  VARCHAR(100)
, created_by        INT UNSIGNED
, creation_date     DATE
, last_updated_by   INT UNSIGNED
, last_update_date  DATE) ENGINE=MyISAM;
  1. Next, you create an on-insert trigger that changes an input but doesn’t stop the transaction. It also writes to the logger MyISAM table in the scope of the transaction.
CREATE TRIGGER contact_insert
BEFORE INSERT ON contact
FOR EACH ROW
BEGIN
 
  /* Check if last name contains a white space. */
  IF new.last_name REGEXP '^.* .*$' THEN
 
    /* Insert into an MyISAM table, which auto commits in the scope
       of a transaction. */
    INSERT INTO logger
    VALUES ( null
           ,'insert'
           ,'contact'
           , new.last_name
           , REPLACE(new.last_name,' ','-')
           , new.created_by
           , new.creation_date
           , new.last_updated_by
           , new.last_update_date );
 
    /* Replace the name for the INSERT INTO the CONTACT table. */
    SET new.last_name := REPLACE(new.last_name,' ','-');
  END IF;
END;
$$
  1. Next, you create an on-update trigger that changes an update while aborting the transaction. It also writes to the logger MyISAM table because its outside the InnoDB scope of a transaction and auto committed on insert.
CREATE TRIGGER contact_update
BEFORE UPDATE ON contact
FOR EACH ROW
BEGIN
 
  /* Check if last name contains a white space. */
  IF new.last_name REGEXP '^.* .*$' THEN
 
    /* Insert into an MyISAM table, which auto commits in the scope
       of a transaction. */
    INSERT INTO logger
    VALUES ( null
           ,'update'
           ,'contact'
           , new.last_name
           , null           
           , old.created_by
           , old.creation_date
           , new.last_updated_by
           , new.last_update_date );
 
    /* Throw an exception to force the business user to see they
       can't update a last name with a white space. */
    SIGNAL SQLSTATE '42000';
  END IF;
END;
$$
  1. Next, you create a test case with an INSERT and UPDATE statement that meets the condition of the triggers.
/* Insert a row meeting the trigger condition. */
INSERT INTO contact VALUES
( null, 1001, 1003,'Catherine', null,'Zeta Jones', 1001, UTC_DATE(), 1001, UTC_DATE());
 
/* Update a row meeting the trigger condition. */
UPDATE contact
SET    last_name = 'Zeta Jones'
,      last_updated_by = 1003
,      last_update_date = UTC_DATE()
WHERE  last_name = 'Zeta-Jones';
  1. Last, query the logger table. You have a record inserted for both the allowed behavior and the aborted behavior. This means you have the ability to capture material that should never be inserted or updated into a table and who did it by leveraging the who-audit columns of the table.
SELECT * FROM logger;

It returns:

+-----------+--------------+--------------+-----------------+------------------+------------+---------------+-----------------+------------------+
| logger_id | logger_event | logger_table | logger_instring | logger_outstring | created_by | creation_date | last_updated_by | last_update_date |
+-----------+--------------+--------------+-----------------+------------------+------------+---------------+-----------------+------------------+
|         1 | insert       | contact      | Zeta Jones      | Zeta-Jones       |       1001 | 2013-04-26    |            1001 | 2013-04-26       |
|         2 | update       | contact      | Zeta Jones      | NULL             |       1001 | 2013-04-26    |            1003 | 2013-04-26       |
+-----------+--------------+--------------+-----------------+------------------+------------+---------------+-----------------+------------------+
2 rows in set (0.00 sec)

This effectively delivers in MySQL the equivalent of an autonomous transaction in Oracle. The result from the non-critical trigger records the before and after value, while the results from the critical update trigger only record the before values because the event is aborted by raising an error in the trigger. As always, I hope this helps somebody looking for a solution.

PlanetMySQL Voting:
Vote UP /
Vote DOWN

10 Productivity Apps For Your Mac-Based Home Office

By Bakari Chavanu, MakeUseOfApril 26, 2013 at 08:31PM

If you work at homebased office like I do, you no doubt spend a significant amount of time getting things done on your Mac. While I have already written about the advantages of using a standup desk, there are also several important and general productivity apps for almost any type of workflow you engage in.

Though there is no robot application (yet!) that can do all the work for me, the following are 10 of the most useful free or low-cost productivity apps that I use on a daily or regular basis. These applications not only save me time but in many cases help me work more efficiently.

Fantastical

It’s difficult to work in any home-based office and not need a useful calendar. Though OS X comes installed with a default Calendar application, the third-party option, Fantastical ($19.99) is hands down the best calendar you should run on your Mac.

With Fantastical, you can input and check calendar events and schedules right from your Mac’s menu bar. You can create a new event simply by opening Fantastical’s drop-down window and entering the data for the event. Instead of clicking numbers and times, you simply write the event as if you were writing it on a scratch sheet of paper.

For example, to schedule a lunch meeting for next Wednesday, simply type: “Lunch with Bakari next wed, 11:30am, at Fresh Choice.” As you type, you watch Fantastical fill in all the data for you.

Fantasical app

You can preset the type of reminders you want for calendar events. Fantastical syncs with Apple’s Calendar application, but there’s also an iPhone version ($8.99) of Fantastical that works the same way. By having Fantastical in your menu bar, you don’t have to open the Calendar app just to add or review a few events.

Super Memory Cleaner

If you find that your Mac slows down throughout the day or when you have several applications open, you should download Super Memory Cleaner (free). It does a great job of cleaning up hundreds of megabytes, or even gigabytes, of memory with one simple click. You can select to have it auto-clean, or clean at startup.

Super Memory Cleaner

Desktop Wallpapers

If you meet clients in your home office, and/or you like your office to have professional décor, your desktop wallpaper should be just as classy as your iMac or Macbook computer. My wallpaper of choice is a collection produced by Vlad Studio.

Vlad Studio

These free wallpapers are designed by digital artist, Vlad Gerasimov, and you can download them for nearly any size desktop monitor. I recommend selecting and downloading a few dozens of your favorites and simply have your Mac change the pictures everyday, or each time you wake up your Mac. I find that these unique wallpapers add a little inspiration to my daily workflow.

Caffeine

There are occasions when you need to keep your Mac desktop or laptop awake when you’re doing a presentation, or playing a video while multitasking in another part of your office. This is where Caffeine (free) comes in. When you enable it, it keeps your Mac from going to sleep until you cut if off. Jackson reviewed this application when it was first released back in 2008.

Caffeine

Wunderlist

There are no shortage of to-do applications for the Mac, but if you’re still looking for one, you should give Wunderlist (free) a try. Dave reviewed the iOS mobile version of Wunderlist, but the desktop version contains a similar user interface.

Wunderlist

Wunderlist is a clean, well designed cross-platform program where you can mange and sync all of your to-do lists. You can also share and collaborate lists with your colleagues. If you don’t need a task manager with lots of bells and whistles, Wunderlist can be very useful in your workflow.

FunctionFlip

FunctionFlip (free/donation) is a Preferences utility that allows you to customize those Fn keys at the top of your keyboard that you might rarely use. For instance, while I constantly use the assigned volume keys, I hardly ever use the brightness, iTunes, Exposé and Launchpad Fn keys on my iMac.

So with FunctionFlip, I can turn off those functions and assign them another purpose using applications like Keyboard Maestro or QuickSilver.

FunctionFlip

Trello

Another useful task and project manager is an online and mobile application called Trello. It’s sort of like a whiteboard for sorting ideas, lists of tasks, and project workflows. You can share your “whiteboards” with others, and view them in any web browser or the iOS version (free) of of the application and service. Erez reviewed Trello in more detail here.

Trello5

Launchpad Manager

If you have amassed a lot of applications on your Mac, you have probably experienced how difficult it is to use Launchpad to access all of your applications. The Launchpad feature in Lion and Mountain Lion is not very useful if you don’t have your applications organized alphabetically or in folders. This is where Launchpad Manager ($7.99) comes in.

Launchpadmanager

Launchpad Manager includes over a dozen features, including the ability to alphabetize applications, delete icons from Launchpad without uninstalling the applications themselves, and move selected applications to another Launchpad page. You can easily move applications into groups, rename icons and groups, and quickly cut and paste applications from one folder to another. You can use custom layouts of your Launchpad for different purposes.

You can download a free version of Launchpad Manager, but many of its advanced features are only available in the paid pro version.

Dropzone

Dropzone ($9.99) is a nifty little application that enables you to perform various tasks from the menu bar or from the left or right side of your desktop screen. For example, say you download a new application that is delivered to you in a DMG file. You can drag that DMG file to Dropzone and drop it on the Install Application action, and it will proceed to automatically open and install that application, and then delete the DMG file for you.

Dropzone

You can create another action that sends files to a pre-selected folder. There’s actions for quickly printing a file or converting a long URL to a bit.ly short URL. You can download a 15-day trial of the application, which I recommend. Spend some time with it, and check out the user contributed actions for Dropzone.

If you find more than five actions that will enable you to be more productive, then it may be worth paying for the application, which you should download from the Mac App Store.

Time Out

One of the ways to be more productive in your Mac-based home office is to actually take breaks from your Mac. Time Out (free) will remind you to take “normal”, say 10 minute breaks, and “micro” breaks, like 10 seconds every 30 minutes, based on the time intervals you set.

Overview

When I don’t use this application, I end up working at my computer for hours before I take a break. Not taking a break causes a strain on my eyes, and by the afternoon I’m less productive. You can postpone or skip breaks, but doing so too often will defeat the purpose.

That’s it for my Mac-based home office applications. Let us know which applications you find most useful in your workflow.

The post 10 Productivity Apps For Your Mac-Based Home Office appeared first on MakeUseOf.

The Right Way to Care for Your Pocket Knife

By Alan Henry, LifehackerApril 26, 2013 at 08:00AM

You may know how to care for a kitchen knife, but when’s the last time you paid your simple pocket knife the attention it deserves? Instructables user kennethisme has a great tutorial on how to care for your average folding pocket knife, from cleaning the blade to lubricating the body.

It’s something that a lot of people overlook, especially if your primary use for a pocket knife is opening packages, mail, and occasionally cutting zip ties and other tough plastic bits. Just like a kitchen knife, the dirtier it gets the more dangerous it is, so keeping it in good condition is important. Kennethisme scrubs the blade down with warm, soapy water and an old toothbrush, and then moves on to picking the right lubricant to keep the hinge, lock, and other moving parts well oiled. He suggests a petroleum-based wet lubricant, as opposed to a spray-on dry lubricant that will attract lint or dust. Similarly, he points out that you should use a food-grade lubricant if you plan to use your pocket knife in any food preparation.

Hit the link below for the whole guide and a few more tips, including specific lubricant suggestions. He doesn’t get into keeping a pocket knife sharp, but the principles there are similar to other, previously mentioned kitchen skills. Unfortunately taking proper care of a pocket knife is something many of us often forget, and the guide makes it easy.

Pocket Knife Maintenance: Cleaning and Lubricating | Instructables

Force Windows to Use Your Wired Connection Instead of Wi-Fi

By Melanie Pinola, LifehackerApril 26, 2013 at 01:30PM

If you have a wireless connection and then plug into your wired network, Windows might continue to use your wireless connection for your network usage. Here’s how to change it so Windows uses your wired connection by default.

As explained on Microsoft, when more than one network connection is available, Windows will choose the one with the lowest metric value (automatically assigned based on the network connection’s rated speed). Previously, to change the default priority of each interface you would change the metric value for each connection. NirmalTV suggests a newer, simpler method:

  • Go to Network Connections under the Control Panel
  • Under the file menu, go to Advanced > Advanced Settings
  • In the Adapters and Bindings tab, click on the connection you want prioritized (e.g., the ethernet connection) and use the up arrow to move it to the top of the list

That’s it! Hit OK and now your wired connection will be the default (when you’re plugged in, that is).

How to Make Windows Select Wired Connection Instead of Wireless Connection | NirmalTV

Pull Dents Out of Your Car with a Hot Glue Gun

By Eric Ravenscraft, LifehackerApril 25, 2013 at 03:00PM

You may be able to pull out small dents in your car using a hot glue gun and some wooden dowels with a DIY version of what’s known as a “glue pull.”

In professional shops, a glue pull involves attaching handles to a dent with hot glue and then tugging them out with specialized tools (as demonstrated in this video). For small dents, however, YouTube user Tom demonstrates how you can perform a similar technique at home with a hot glue gun and some makeshift handles.

This method requires a bit of finesse, some trial and error, and may not be for everyone. Glue shouldn’t harm your paint job, but when you’re dealing with bending metal, fixes can get very subjective. We haven’t seen too many DIY videos for this technique, so it’s not as thoroughly tested as some other tips we’ve featured—and we’d actually recommend against using a metal hammer directly on your car as Tom does in this video—but if you’re up for venturing into semi-uncharted territory, give it a shot. Just be sure to do your research before you start and exercise caution.

Fixing a dent with hot glue | via WonderHowTo

Get Creative With WordPress – 5 Interactive Ways To Use The Platform

By Nancy Messieh, MakeUseOfApril 23, 2013 at 11:31PM

There’s quite a lot of ways you can use WordPress beyond simply using it as a blogging platform. We’ve already taken a look at a list of 5 things you might not have known you could do with WordPress.  The platform lends itself to versatility and there’s a huge variety of websites out there that you can’t even tell are created using WordPress, and there are five ways you can put the platform to use that goes beyond simply content management.

WordPress can be used to create an extremely interactive experience on your website, which can translated into a social network, a project management tool, a community board and more.

Create a Private Social Network

While there are options out there for private social networks such as Yammer, using WordPress to create that social network gives you so much more control over the experience. There are several ways to go about turning WordPress into a private social network – whether it’s using a theme or a plugin.

One of the easiest ways to get it done is to use the theme P2, which creates a Twitter-like experience, allowing you to create and add users, post updates and more.

P2 also comes with threaded comments, real-time notifications for new comments, keyboard shortcuts and more. It can be used to create a private social network, or as a live-streaming tool of your own.

You can see the theme in action on WordPress’ very own blog for its core development team.

If you’re looking for something even more elaborate, you can give BuddyPress a try. We’ve taken an in-depth look at what it has to offer, so check out our review of BuddyPress here. It allows you to create user profiles, add friends, send private messages, create user groups, discussion forums and more.

Create a Help Desk

If you want to use WordPress to incorporate a more heavy-duty customer service experience on your site, there are a few themes that will get the job done. You can create a help desk, allowing customers or users to submit questions, support tickets and more, with complete functionality to manage these submissions.

One great example of this is Woo Themes’ SupportPress.  With the paid theme, users can submit tickets, while on the backend, you can assign the tickets to members  of your team. SupportPress also goes one step further by allowing you to create a knowledge base as well so that users don’t submit duplicate tickets on questions that have already been addressed.

Support Desk is another option available to users. The responsive theme, which will set you back $50, also allows you to create a community forum and knowledge base, as well as FAQ pages. You can see the theme in action here.

If you’d rather not spend a little less in order to get a Q&A site up and running, you can opt for a plugin instead.  Q&A will set you back $19, while ClickDesk is free. The latter allows you to place a live chat feature on your site, and if you aren’t online to answer questions straight away, users can leave a message.

Create a Wiki

Another way you can get more out of WordPress is to use the CMS to create your own personal Wiki. The WordPress Wiki Theme, which costs $40, allows you to create user accounts, post content, categories and more. The theme is also completely searchable. See the theme in action here.

Woo Themes also provides users with the $50 option, Wikeasi, but if you don’t want to spend a penny, you can opt for the WordPress plugin WordPress Wiki Lite, but with the caveat of your Wiki sitting on one WordPress page within your website.

Use for GTD

WordPress can also be used as a pretty impressive productivity tool. You can use it as your own personal GTD tool or as a collaborative tool for project management.

With the plugin, Project Tasks, you can turn your WordPress website into a project management tool where you can create and assign tasks, keep track of progress and keep a log of your completed work.

If you’re looking for something even more robust, CollabPress allows you to create projects, create and assign tasks, upload files, and view tasks in a calendar.

Create a Directory, Classifieds, Jobs Board, etc.

WordPress also lends itself to creating any kind of information board or directory. You can use it to create a business directory, a jobs board or a classifieds board. No matter what kind of information you want to collate into a directory on your WordPress website, there’s likely a plugin or theme that will get the job done.

There’s nothing to stop you from creating your own Yellow Pages-like service that could become a useful resource, and with the Business Directory plugin you can even monetize the service by enabling users to submit and post to your directory for a fee.

Can you think of any other creative uses for WordPress? Let us know about them in the comments.

The post Get Creative With WordPress – 5 Interactive Ways To Use The Platform appeared first on MakeUseOf.