Category Archives: Programming and Tech Stuff

Useful Postgres SQL Example Codes

1. create basic demo table in postgres sql with an ENUM column and an auto-increment one


-- we create an enum type
CREATE TYPE demo_enum AS ENUM ('item1', 'item2', 'item3');

-- we create a sequence
CREATE SEQUENCE demotable_id_seq;

-- we create the actual table and we'll use the sequence for autoincrement column
-- and the type for an enum-type column
CREATE TABLE demo_table
("id" integer NOT NULL DEFAULT nextval('demotable_id_seq'),
start_date timestamp,
end_date timestamp,
demo_choice demo_enum,
demo_extern integer,
status smallint DEFAULT 1,
PRIMARY KEY ("id"));

-- we alter the sequence
ALTER SEQUENCE demotable_id_seq OWNED BY demo_table.id;

-- we add a foreign key constraint
ALTER TABLE demo_table ADD FOREIGN KEY (demo_extern) REFERENCES demo_other_table(demo_extern_column);

How to make NetBeans 8 for PHP look more like Notepad++

Writing on the same subject here as the last 2 articles (line-spacing and python support) on NetBeans.

Little back-story, for the past 7 years I worked and I’m still working all my web-related projects in Notepad++. I’ve tried all the IDEs out there, didn’t liked any of them for reasons more or less logical (Yes, sometimes I listen my feelings/emotions when making choices, for example “I like this editor or not” :p )

Recently I decided to give another try at NetBeans (following my colleagues persistence 😀 ) and first thing I wanted to do was to make it look more like my notepad++. Especially when it comes to color highlight and syntax.

I found the same question being asked here. One of the answers gave a full solution to the case: a download offering all the color-configs for making the NetBeans IDE show PHP codes colored like Notepad++ does.

You can download it from the following link: Notepad++ Theme for NetBeans 6.7.x

1. Unzip file
2. From menu go Tools/Options then click on Import button in the shown dialogue browse the unzipped folder
3. From the shown tree check “Fonts & Colors” and click on OK button.
4. After restarting IDE again from menu goto Tools/Options select Fonts & Colors tab and from Profile combo box select “Notepad_plusplus”
have a nice time.

Since I have no idea if the download link will work forever (probably not :p) or if the original poster will take it down, etc, etc, I am uploading the source on my blog as well and provide it for download from the link below. Again, all the credits go to the original author and to the user who posted the solution on the askubuntu.com forum: Saeed Zarinfam (I don’t know if the author of the config files is the same person with the author of the post, whomever they are, many thanks!)

Download directly: Notepad_plusplus_Theme_for_NetBeans_6.7.x_(php5-html-css)

How to increase line-height (line spacing) in NetBeans 8

Being a Notepad++ veteran user, the differences in code coloring, highlight and editor display in NetBeans IDE kinda put me off.

So first thing I wanted was to increase the line-height of the text (code, to be precise :p ) which is shown in NetBeans’ editor.

Luckily for me, I was not alone, other people asked the same question on google and some even answered it :))

The best answer by far is the one given by user pengemizt from stackoverflow.com on the question you can find here. That question was asking about NetBeans 7 but I just tried it on NetBeans 8 and it works ok.

I’m gonna copy/paste the answer here (all the credit going, like I’ve said above, to user pengemizt)

1. Find org-netbeans-modules-editor-settings-CustomPreferences.xml in ~/.netbeans/x.x/config/Editors/Preferences where x.x is your netbeans version.
In my case, this was C:\Users\Andrei\AppData\Roaming\NetBeans\8.0\config\Editors\Preferences\org-netbeans-modules-editor-settings-CustomPreferences.xml

NOTE.: By trial and error I found that this file is generated only after you first run NetBeans first time. I’ve just installed it on my home computer and decided to do all the changes first, and this file was nowhere to be found on my pc, so I had to run first netbeans and then edit the xml config file.

2. Add this xml markup just above </editor-preferences> or anywhere if you know what you doing.

    <entry javaType="java.lang.Float" name="line-height-correction" xml:space="preserve">

        <value><![CDATA[1.4]]></value>

    </entry>

 

And I may add, restart NetBeans. The solution above gave as example setting the line-height to 1.4 but in my tests I found that a value of 1.2 works better (for me).

How to set up Netbeans with Python support

Recently I had to work with some python-powered codes and one of my colleagues pointed me to a nice tutorial on how to enable support for Python on Netbeans IDE.

Since I’m a 7-years-old Notepad++ veteran user :p I was glad to get some help on my new “still in tests” IDE of use 😀

So here’s the tutorial’s link. Enjoy!

BTW, although the tutorial is for Netbeans 7.1, I can confirm it works on NetBeans 8 as well.

https://blogs.oracle.com/geertjan/entry/python_in_netbeans_ide_71

Basic PHP PDO Extension Usage & Syntax

PDO Extension defines a consistent interface for accessing databases in PHP.

What does that mean? Well, it means programmers can write PHP codes which can be easily ported from one database type to another; let’s say you write your codes for MS SQL but then suddenly the project takes a turn and you need to adapt your codes for MySQL … would be great to be able to make the transition easy, wouldn’t be?

Now, you can read all about it on the Official PHP Manual and also you can read tutorials about it, but I’m about to give you below a simple example of the basic PDO Syntax.

<?php
$db = new PDO(
	'mysql:host = localhost; dbname=someDbName; charset=utf8', 
	'username', 
	'password',
	array(
		PDO::ATTR_EMULATE_PREPARES => false,
		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
	)
);

try{
	$res = $db->query('err');
}
catch(PDOException $ex){
	echo "error";
	callSomeLogFunction($ex->getMessage());
}

//other code examples:
$stmt = $db->query('select * from table');
$results = $stmt-?fetchAll(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();

//here's how to get the ID of the last inserted row
//when you're doing an insert before
$lastId = $db->lastInsertId();

//preparing queries
$stmt = $db->prepare("select * from table where id=:id and name=:name");
$stmt->bindValue(':id',$id,PDO::PARAM_INT);
$stmt->bindValue(':name',$name,PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

Setup new Centos VPS with LAMP, Git and gitolite

Recently I had to install and maintain several Centos servers and I had to repeat the same setup instructions multiple times.

I thought it’d be nice to have the following list of to-do steps which needed to be repeated on every server install, especially so other employees could take on this job after me.

The scenario I had to work on required the setup of a new Centos 6 server with LAMP (php 5.5 at least) and configuring Git and gitolite for git user-managing access.

So the first thing was to install a Centos 6.5 minimal distribution and then all the work was done through Putty and heavy-usage of yum.

To avoid conflicts later-on because of repository packages, one of my colleagues recommended me to use yum-plugin-priorities. (thanks, Madalin 😉 )

Now, although this plugin is a bit controversial, we found that during our exploitation it was really helpful in preventing our servers to become a packages-mess in no-time. So I’m gonna go along and recommend it further. You should go however to the link and read the documentation for yum-plugin-priorities to see if it suits your needs and to learn how to proper configure it.

So here’s the list. It’s not something new or original content, it’s more of a compilation list to have all the info in one place. I have included links to the sites from where the info was used.

  1. Install yum-plugin-priorities (say yes if you’re asked about a key, it’s because you’re using the repo’s for the first time
    yum install yum-plugin-priorities
    
  1. install wget:
    yum install wget
    
  2. cd to user’s home:
    cd ~
    
  3. install remi repo:
    wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmwget
    http://rpms.famillecollet.com/enterprise/remi-release-6.rpmsudo
    rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
    

    You can read more about the remi repo here.

  4. install webtatic repo:
    rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
    

    You can read more about the webtatic repo here.

  5. Important! edit repo files located in /etc/yum.repos.d/ and enable the ones you want and add priority=N, according to yum-priorities plugin required settings (http://wiki.centos.org/PackageManagement/Yum/Priorities)
    .
  6. update your system
    yum update
    
  7. install sort-of lamp, but don’t install php, since we need php 5.5 at least which we’ll get from the webtatic repo one step later:
    Install Apache and start it:

    yum install httpd
    service httpd start
    

    Install MySQL and start it:

    yum install mysql, mysql-server
    service mysql start
    

    Secure your fresh MySQL installation by running this script:

    /usr/bin/mysql_secure_installation
    

    You can read more about the default configuration for securing your mysql on this tutorial from digitalocean.com

  8. Install PHP 5.5 and the modules you want (we installed them all)
    yum install php55w  php55w-bcmath  php55w-cli  php55w-common  php55w-dba  php55w-devel  php55w-embedded  php55w-enchant  php55w-fpm  php55w-gd  php55w-imap  php55w-interbase  php55w-intl  php55w-ldap  php55w-mbstring  php55w-mcrypt  php55w-mssql  php55w-mysqlnd  php55w-odbc  php55w-opcache  php55w-pdo  php55w-pear.noarch  php55w-pecl-apcu  php55w-pecl-apcu-devel  php55w-pecl-memcache  php55w-pecl-xdebug  php55w-pgsql  php55w-process  php55w-pspell  php55w-recode  php55w-snmp  php55w-soap  php55w-tidy  php55w-xml  php55w-xmlrpc
    
  9. don’t forget to add chkconfig setting so the services would start automatically
    chkconfig --levels 235 httpd on
    chkconfig --levels 235 mysqld on
    
  10. Optionally, install nodejs, if you need it (we did)
    yum install nodejs
    
  11. Install the dev-tools
    yum groupinstall "Development tools"
    
  12. Install Git and create user/group
    yum install git
    useradd git
    usermod -u 600 git
    groupmod -g 600 git
    passwd git
    
  13. Install gitolite on the server by loging in as git user and cloning the gitolite repo
    git clone https://github.com/sitaramc/gitolite
    
  14. Since gitolite administration is based on keys and we used windows machines to connect to the linux servers, we used putty to generate keys for every user. Copy-paste the text from puttygen as USERNAME.pub and save the private key as USERNAME.ppkThe .pub key will have to be uploaded by gitolite admin to the gitolite repos which the USERNAME will have access.

    We used this tutorial for our first install, what’s really important to keep in mind is that there are two machines in use: one is the server (linux server) and the other is the client (in the tutorial I’ve linked the client is also a linux box; in our scenario the clients were windows machines).

    It’s important to note that for the first use of gitolite, you have to create the .pub/.ppk pair of keys for the client user which will be the gitolite admin (so you’ll create this keys on the client machine, may that be linux, windows, w/e) and copy the .pub key into the git folder from the Server machine and give it proper permissions. Let’s say it will be git-admin.pub and git-admin.ppk like in the tutorial link above.
    Then, after you’ll install gitolite by logging in as user git onto the server machine, you’ll use the git-admin.pub key as argument to the initial setup of gitolite, like this:

    gitolite/install -ln
    gitolite setup -pk Git-Admin.pub
    
  15. Since we used windows machines to develop on, we installed git client on them and gitextensions also.For the git installer setup  we used the following options:
    – advanced context menu, associate .git
    – use git bash only
    – use plink
    – checkout windows style, commit linux style

    And for the gitextensions setup we used the following options:
    – install kdiff only
    – use putty
    – (dumb observation: sometimes the kdiff installer windows pops under the main gitextensions installer window and at first sight it seems the main installer hanged; it didn’t, it just awaits for your action on the windows behind)

  16. We also had an interesting setup where our git server would automatically deploy upon receive, on the httpd test server installed on the same machine. We used a script placed in hooks/post-receive folder with the following content:
    #!/bin/sh
    GIT_WORK_TREE=/home/path/to/your/www
    export GIT_WORK_TREE
    umask 002
    git checkout -f
    

    The script has to have proper permissions.
    Also, the www folder should be owned by apache user and have the group set to apache group and the git user should be added to the apache group.

    chmod +x hooks/post-receive
    chown apache:apache /home/path/to/your/www
    usermod --groups apache git
    

    Also, you have to init a new empty repo into the www folder

    cd /home/path/to/your/www
    mkdir newfolder
    cd newfolder
    git init
    
  17. After this, you can create new repos on the git server machine, from the client machine, by using the gitolite-admin repo.You must clone first the admin repo (git clone gitolite-admin.git) on your client machine and then use this repo as a setup tool for the repos you wanna create and manage.You can create new git repos, add users to those repos by putting their pub keys into the keys folder and setting up the gitolite.conf file.

    Every time you push the changes for the gitolite-admin repo, the new keys will be uploaded to server and gitolite.conf changes taken into consideration and this will basically be your tool to manage the git repos on the (test) server machine.

    A very good tutorial which we initially used when setting gitolite can be found here: http://sachinsharm.wordpress.com/2013/10/04/installsetup-and-configure-git-server-with-gitolite-and-gitweb-on-centosrhel-6-4/ 

 


 

External links for reference, used in this article:

remi repo: http://www.rackspace.com/knowledge_center/article/installing-rhel-epel-repo-on-centos-5x-or-6x
webtatic repo: http://webtatic.com/projects/yum-repository/
yum-priorities: http://wiki.centos.org/PackageManagement/Yum/Priorities
simple tutorial on lamp install: https://www.digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-6
about gitolite: http://gitolite.com/gitolite/index.html
large tutorial on git and gitolite: http://sachinsharm.wordpress.com/2013/10/04/installsetup-and-configure-git-server-with-gitolite-and-gitweb-on-centosrhel-6-4/
git clients: http://git-scm.com/downloads
gitextensions for windows: https://code.google.com/p/gitextensions/
good info on how to auto-deploy from git server: http://stackoverflow.com/questions/9132144/how-can-i-automatically-deploy-my-app-after-a-git-push-github-and-node-js
if you want to install phpmyadmin via yum: http://tecadmin.net/how-to-install-phpmyadmin-on-centos-using-yum/
in case you get httpd dead but subsys locked error: http://sandzoctanium.com/upgrade-from-php-5-3-to-php-5-5-and-get-error-httpd-dead-but-subsys-locked/

 

Useful git commands

1. Clear all unstaged files

git checkout -- .

2. Revert (reset) a single uncommitted file

git checkout -- filename-to-revert

3. Add all untracked files to stage

git add .

4. Add to stage and commit

git commit -m "message" -a

5. Ignore Self-Signed SSL Certificates

git config http.sslVerify false

or

git clone https://url-to-your-git-repo/ -c http.sslVerify=false

10 Basic SVN Commands

1. svn checkout
– downloads sources from repository to working copy
example.:

svn checkout URL PATH

2. svn commit

– saves changes from working copy to repository
example.:

svn commit -m "descriptive message"

3. svn list

– show (lists) directory entries on the repository
example.:

svn list --verbose

4. svn add
– ads a new file to the repository
example.:

svn add <filename.ext>
svn commit -m "added new file"

5. svn delete
– removes an item from the repository

svn delete <filename.ext>
svn commit -m "removed a file"

6. svn diff

– shows differences between working copy and repository
examples.:

svn diff <filename.ext>

another example comparing file@R1 to file@R2

svn -r R1:R2 diff <filename.ext>

7. svn status

– shows the status of a file in the working copy
example.:

svn status path/to/file

8. svn log
– shows the changes to tge repository
example.:

svn log path

9. svn move

– moves or renames a file
example.:

svn move src dest

10. svn update
– brings changes from the remote repository to local
example.:

svn update path

9 Basic Git Commands

Here’s a list with 9 basic Git commands you’ll use most often:

1. git init
– creates a new local repo

2. git clone
– checks out a repository
example.:

git clone username@host:/path/to/repo

3. git add
– ads one file to the index
example.:

git add <filename.ext>

4. git commit
– commits changes to head
example.:

git commit -m "descriptive message"

5. git push
– sends changes to master branch of remote repo
example.:

git push origin master

6. git status
– lists the changed files and those needing a commit or push

7. git remote add origin <server>
– if you haven’t connected your local repo to a remote server, add the server to be able to push to it

8. git pull
– fetch and merge changes from the remote server to your working directory

9. git diff
– view all possible merge conflicts

Bonus: git push -u origin branch_name
– push a local branch to remote git server

AJAX Classic Syntax

I’ve posted the jQuery Basic Syntax for making an AJAX call, but what about the classic javascript version?

So here it is, for beginners, the classic Javascript code for making an AJAX request; in the classic version you have to supply slightly different code for instantiate the request object, depending on if you’re on Internet Explorer or the rest of browsers.

Enjoy! 🙂


var xmlhttp;
if(window.XMLHttpRequest){
	// code for FF
	xmlhttp = new XMLHttpRequest();
}
else{
	// code for IE
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("POST", "some.php", true);
xmlhttp.setRequestHeader("Cotnent-type", "application/x-www-form-urlenconded");
xmlhttp.send("name=John&location=Boston");
xmlhttp.onreadystatechange = function(){
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
		alert(xmlhttp.responseText);
	}
}