Thursday, March 23, 2017

Postgres - Generate Series of dates

Below SQL generate rows of first day of months between 2010 and 2012

select (yr || ''-'' || mnt || ''-'' || 1) ::date as dt from(
(SELECT t.z  AS yr FROM generate_series(2010,2015,1) AS t(z)) z
cross join
(SELECT u.y  AS mnt FROM generate_series(1,12,1) AS u(y)) y
)p

Tuesday, March 14, 2017

QueryDSL

What is QueryDSL?

Querydsl is an extensive Java framework, which allows for the generation of type-safe queries in a syntax similar to SQL

Why QueryDSL?

  1. Code Completion
  2. Type Safety
  3. No Strings Involved
  4. Adapts to Refactoring

Example QueryDSLs

query.from(employee).where(employee.name.eq("auhuman"));
query.from(employee).where(employee.age.gt(35)).and(employee.gender.eq(“Male”);

Maven Dependencies

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>3.7.4</version>
</dependency>
<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>3.7.4</version>
</dependency>

Maven Build Plugin

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources/java</outputDirectory>
                <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

Entity

@Entity
public class Employee {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName, lastName, description;

    private Long count;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }
}

Sample Code

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBodyList<Company> queryDSL(@RequestParam(value = "name", required = false) String name) {
    JPAQuery query = new JPAQuery(entityManager);    
    QCompany company = QCompany.company;    
   if (name == null) {
        return query.from(company).list(company);    
   } else {
        return query.from(company).where(company.name.eq(name)).list(company);    
   }
}

@RequestMapping(value = "/queryDSLGroupByCount", method = RequestMethod.GET)
@ResponseBody
List<Employee> queryDSLGroupByCount() {
    JPAQuery jpaQuery = new JPAQuery(entityManager);
    QEmployee employee = QEmployee.employee;
    return jpaQuery.from(employee).groupBy(employee.lastName).list(Projections.constructor(
            Employee.class,
            employee.lastName,
            employee.count()
    ));
}


Friday, May 30, 2014

jpeg to pdf in mac

iPubSoft PDF Creator - Free for 5 jpegs. If you have more than 5 then create multiple pdfs and combine them using below software.
Combine PDFs - Free for first 1000 pages.

Thursday, April 17, 2014

Sublime : Open CSV in Open Office Excel

Create File Default/open_in_open_office
place below contents

import sublime, sublime_plugin, subprocess, os
class OpenInOpenOfficeCommand(sublime_plugin.TextCommand):

def run(self, edit):
if self.view.file_name():
os.chdir("/Applications/OpenOffice.app/Contents/program")
subprocess.Popen(["./soffice", self.view.file_name()])

def is_visible(self):
return self.view.file_name() and (self.view.file_name()[-4:] == ".csv")


Update File Default->Context.sublime-menu
{ "command": "open_in_open_office", "caption": "Open in Open Office" }
references
http://stackoverflow.com/questions/16147073/adding-custom-menus-in-sublime-text http://stackoverflow.com/questions/636561/how-can-i-run-an-external-command-asynchronously-from-python http://stackoverflow.com/questions/431684/how-do-i-cd-in-python https://docs.python.org/2/library/subprocess.html

Monday, January 13, 2014

Print sqls executed on MySQL server to log

To print sqls executed on MySQL server to a log, set the command line option while starting the MySQL server.
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" --general-log=1 --general-log-file="c:\mysql.log"

Thursday, August 23, 2012

Find duplicate lines in 1 and more files

Step 1: In Windows machine collect all files in one folder (temp)and execute the command in the command prompt
type *.*> main.txt
Step 2: Copy this file to a unix machine and execute the following command
sort main.txt | uniq --count > result.txt
In result.txt, all those lines which has count more than 1 are duplicates.

Tuesday, August 14, 2012

Java Generic Merge Sort - Top Down Approach

Reference : http://en.wikipedia.org/wiki/Merge_sort
package sorting; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; import java.util.Queue; public class MergeSort { private Object[] data = null; private Comparator<Object> comparator = null; public MergeSort(Object[] data, Comparator<Object> comparator){ this.data = data; this.comparator = comparator; } public Object[] sort(){ return sort(data); } private Object[] sort(Object[] data) { if(data==null || data.length<2) return data; //divide into two sublist till it has only one element int middle = data.length/2; Object[] first = Arrays.copyOfRange(data, 0, middle); Object[] second = Arrays.copyOfRange(data, middle, data.length); //merge two list into one and do this recursively till you get one list. return merge(sort(first), sort(second)); } private Object[] merge(Object[] first, Object[] second) { ArrayList<Object> result = new ArrayList<Object>(); Queue<Object> firstQueue = new LinkedList<Object>(); for(Object o:first) firstQueue.add(o); Queue<Object> secondQueue = new LinkedList<Object>(); for(Object o:second) secondQueue.add(o); while(firstQueue.size()>0 || secondQueue.size()>0){ if(firstQueue.size()>0 && secondQueue.size()>0){ if(comparator.compare(firstQueue.peek(),secondQueue.peek())<=0){ result.add(firstQueue.remove()); }else{ result.add(secondQueue.remove()); } }else if(firstQueue.size()>0){ result.add(firstQueue.remove()); }else{ result.add(secondQueue.remove()); } } return result.toArray(); } }