Sunday, July 8, 2012

MySQL IF NULL return 0 - IFNULL() function

Test if expression is NULL; return NULL if true, return expression if false.

Definition:
IFNULL(expr1, expr2)

Example:
IFNULL(SELECT MAX(field) FROM table, 0)

Saturday, July 7, 2012

MySQL get date tomorrow

MySQL function to get tomorrow's date:

DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY)

MySQL: Delete / Reset Query Cache

Delete the cache results for MySQL queries:

RESET QUERY CACHE;

OR

FLUSH TABLES;

PS: "FLUSH QUERY CACHE" also exists, but the command only defragments the query cache to better utilize the memory; it does not delete/empty the query cache.

Friday, July 6, 2012

PHP: Generate random string, random date

PHP Function: Generate random string

/**
 * PHP Generate random string
 * @param int $length
 * @param array $chars - optional
 * @return string
 */
function generateRandString($length, $chars = array()) {
    if(empty($chars)) {
        $chars = array_merge(
            range('a', 'z'), range('A', 'Z'),
            range('0', '9'), array(' ', '.', ',', '-', ';')
        );
    }
   
    $string = '';
   
    for($i = 0; $i < $length; $i++) {
        $string .= $chars[mt_rand(0, count($chars) - 1)];
    }
   
    return $string;
}

PHP Function: Generate random date

/**
 * PHP Generate random date
 * Compute a random date between 2 given dates
 * @param string $date1 - YYYY-MM-DD format
 * @param string $date2 - YYYY-MM-DD format
 * @return string date
 */
function generateRandDate($date1, $date2) {
    list($y, $m, $d) = explode('-', $date1);
    $time1 = mktime(0, 0, 0, $m, $d, $y);
    list($y, $m, $d) = explode('-', $date2);
    $time2 = mktime(0, 0, 0, $m, $d, $y);
    return date('Y-m-d', mt_rand($time1, $time2));
}

Thursday, July 5, 2012

Linux: SVN subversion client certificate config (.p12)

Console SVN subversion client configuration. Using .p12 SSL certificate.

Edit: ~/.subversion/servers

[groups]
group1=*.hostname1.com
group2=*.hostname2.com

[group1]
ssl-client-cert-file=/home/user/cert/cert-file.p12
ssl-client-cert-password=abcde

[group2]
ssl-client-cert-file=/home/user/cert/cert-file.p12
ssl-client-cert-password=abcde

Setting the password is optional. This can be skipped, but the password will be required at every svn action.

OOP recommended practices

Inheritance

class Customer {
    protected $discount = 0;
   
    public function getDiscount() {
        return $this->discount;
    }
}

class CustomerPremium extends Customer {
    protected $discount = 5;
}


Global Constants

Recommended: no use of global constants inside class methods.

class MyClass {
    protected $path_server;
   
    public function setPathServer($path_server) {
        $this->path_server = $path_server;
    }
   
    public function getFileContents($file) {
        return file_get_contents($this->path_server . '/' . $file);
    }
}


Abstract & Final

abstract class MyClassAbstract {
    abstract protected function computeData();
    abstract protected function sendMail();
   
    final public function run() {
        $this->computeData();
        $this->sendMail();
    }
}

class MyClass extends MyClassAbstract {
   
    protected function computeData() {
        ...
    }
   
    protected function sendMail() {
        ...
    }
}