Sunday, July 7, 2019

Uncommon Database Queries

Update from Select


MySQL - Update only a single column

UPDATE b T 
  SET v1 = (SELECT v1 FROM a S WHERE T.id = S.id);

MySQL - Update multiple columns in a single query

UPDATE b T
  LEFT OUTER JOIN a S
 ON ((T.id = S.id) AND (S.v1 IN ('a', 'b', 'c')))
SET
  T.v1 = S.v1,
  T.v2 = IF(S.v2 > 100, 'High', 'Low');

INNER JOIN will also work.


Delete with Joins

MySQL - From Single Table

DELETE t2
  FROM t2 INNER JOIN t1 ON (t2.ref = t1.id)
WHERE t1.type = 100;

Ref:

Sunday, June 8, 2014

HTML5

Audio Play

In HTML5, audio (and video as well) can easily be played even with the player using audio/video elements.

If you add multiple audio/video elements to HTML, they'll start playing at once. And to play them one after the other, the next clip should be started after the end of the previous using ended event.

This JavaScript example shows how to play an array of audio clips one after the other:

function playAll(playList, current) {
  current = current || 0;

  playList[current].volume = 0.75;
  playList[current].play();

  if (playList[1 + current]) {
    playList[current].addEventListener('ended', function () {
      playAll(playList, 1 + current)
    });
  }
}

var playList = [
  new Audio('1.mp3'),
  new Audio('2.mp3'),
  new Audio('3.mp3'),
  new Audio('4.mp3'),
  new Audio('5.mp3')
];

playAll(playList);

Wednesday, February 26, 2014

Yii and non-Yii

Blending Yii and non-Yii applications together

If you want to link Yii application with non-Yii application, the best option is to re-write the existing application in Yii.

Definitely you can initiate HTTP requests to invoke Yii actions, but it creates TCP/IP connections which are costly. If both applications reside in the same web server, you can use below method.


Create a bootstrap (this code segment can be placed inside anywhere in current non-Yii application):
<?php
// change the following paths if necessary
$yii='yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/application.php';

// remove the following lines when in production mode
//defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);

// `Application.php` must exist in the same level of THIS php file
$yiiapp = Yii::createApplication('Application', $config);

// Call methods inside `Application.php`
$yiiapp->doAction1();
$yiiapp->doAction2();

Create the wrapper `Application.php` in the same dir-level as the above bootstrap code segment is (any name can be used; but need to alter Yii::createApplication(...) above)
<?php
/**
 * NOTE:
 *   - `Yii::app()` is available from here.
 */
class Application extends CApplication
{
  public function init()
  {
  }

  /**
   * Yii::createApplication(...)->run() executes this method
   */
  public function processRequest()
  {
  }


  public function doAction1()
  {
  }

  public function doAction2()
  {
  }
}

Friday, January 3, 2014

Yii and Oracle

Getting Table Structure

This is how to get name, type and size of all the columns of table, owner_name.mytable.
// dbOracle must have been defined in configurations
$remote_db = Yii::app()->dbOracle;

$stru = $remote_db->createCommand()
  ->from('ALL_TAB_COLS')
  ->where("TABLE_NAME = 'mytable' AND OWNER = 'owner_name'")
  ->queryAll();

$col_info = array();

foreach ($stru as $_col_meta)
{
  $_info = new stdClass();

  $_info->name = $_col_meta['column_name'];
  $_info->type = $_col_meta['data_type'];
  $_info->size = $_col_meta['data_length'];

  $col_info[] = $_info;
}

print_r($col_info);

Thursday, August 8, 2013

Ubuntu Tweaks

Ubuntu ISO images


Ubuntu lists all popular downloads at Ubuntu Releases. And they list almost all the CD-Images here.

Ubuntu for older PCs

It's difficult to use older Ubuntu Operating Systems (those without updates) for PCs running with low memory and CPU power.

The solution is to install using the Alternate CDs provided at Ubuntu Releases (for example, Ubuntu-12.04.2 Alternate i386).

If you still running out of resources, you can use Gnome Classic panel instead of Unity, by installing gnome-panel (using sudo apt-get install gnome-panel).

After installing, restart the PC. Then you can switch from default Unity to Gnome Classic by clicking small Ubuntu icon alongside your user name.


Ubuntu Post-installation Steps

Making Pidgin icon stay on system tray

  1. Go to Pidgin Preferences and set Show System Tray Icon to Always
  2. Open up a Linux shell and execute gsettings set com.canonical.Unity.Panel systray-whitelist "['all']"
  3. Reload Pidgin
  4. Right-click Pidgin icon on system tray and tick Blink on New Messages to alert yourself on new messages (better disable Libnotify Popups from Plugins menu)

Disable Global Menus

All applications have used one global menu by default, instead of each application having its own menu bar and for Firefox before version 22.
  1. Open up super-user Linux shell and execute sudo apt-get autoremove --purge appmenu-gtk appmenu-gtk3 appmenu-qt firefox-globalmenu
  2. For Firefox version 23 and later; enter about:config in the address bar, search for use_unity_menubar setting, double-click on the item to set the value to false, close Firefox
  3. Restart PC

PHP and MySQL

PHP-My-Admin Configuration on Ubuntu (12.04)

  1. Install PHP-My-Admin using sudo apt-get install phpmyadmin and the installation location is /usr/share/phpmyadmin.
  2. The general configuration steps mentioned in PMA Quick Installation Wiki will not work 100% as Ubuntu expects the required file at /var/lib/phpmyadmin/config.inc.php.
  3. Copy a sample using cp -p /usr/share/phpmyadmin/config.sample.inc.php /var/lib/phpmyadmin/config.inc.php
  4. Change the permissions and ownership using sudo chgrp www-data /var/lib/phpmyadmin/config.inc.php and sudo chmod 640 /var/lib/phpmyadmin/config.inc.php
  5. If you need your development environment with empty root password for MySQL, change the value of $cfg['Servers'][$i]['AllowNoPassword'] to true inside newly copied /var/lib/phpmyadmin/config.inc.php
  6. Enjoy