All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclarion.clarion.jdbc.PgSchema Maven / Gradle / Ivy

/**
 * Copyright 2010, by Andrew Barnham
 *
 * The contents of this file are subject to
 * GNU Lesser General Public License (LGPL), v.3
 * http://www.gnu.org/licenses/lgpl.txt
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 */
package org.jclarion.clarion.jdbc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PgSchema 
{
    private static PgSchema sInstance;
    
    public static PgSchema get()
    {
        if (sInstance==null) {
            sInstance=new PgSchema();
            sInstance.load();
        }
        return sInstance;
    }
    
    private List schema=new ArrayList();

    public PgSchema()
    {
        schema=new ArrayList();
    }

    public PgSchema(List schema)
    {
        this.schema=schema;
    }
    
    public PgSchema[] split(String regex)
    {
        Pattern p = Pattern.compile(regex);
        PgSchema[] split = new PgSchema[2];

        int midpoint=0;
        while (midpoint0) {
                    schema.add(result.toString());
                }
                break;
            }
            
            if (next.startsWith("SET client_encoding")) continue;
                
            if (next.length()==0) continue;
            if (next.charAt(0)=='-') {
                if (result.length()>0) {
                    schema.add(result.toString());
                }
                result.setLength(0);
                continue;
            }
            
            if (result.length()>0) result.append('\n');
            result.append(next);
        }
    }
 
    public int getCount()
    {
        return schema.size();
    }
    
    public String getTask(int ofs)
    {
        return schema.get(ofs);
    }
    
    public List getTasks()
    {
        return schema;
    }

    public Set getTables()
    {
        HashSet set=new HashSet();
        Pattern p = Pattern.compile("^create table (\\S+)");
        for (String line : schema ) {
            line=line.toLowerCase();
            Matcher m = p.matcher(line);
            if (m.find()) {
                set.add(m.group(1));
            }
        }
        return set;
    }

    public Set getColumns(String name)
    {
        HashSet set=new HashSet();
        Pattern p = Pattern.compile("^create table "+(name.toLowerCase())+" ");
        Pattern f = Pattern.compile("^\\s+(\\S+)\\s(\\S+)");
        
        
        for (String line : schema ) {
            line=line.toLowerCase();
            
            Matcher m = p.matcher(line);
            if (m.find()) {
                
                StringTokenizer lines = new StringTokenizer(line,"\n");
                while (lines.hasMoreTokens()) {
                    m = f.matcher(lines.nextToken());
                    if (m.find()) {
                        String col = m.group(1);
                        col=col.replaceAll("\"","");
                        set.add(col);
                    }
                }
            }
        }
        return set;
    }
    
    public void write(Writer w) throws IOException
    {
        for (String line : schema ) {
            w.write("--\n");
            w.write(line);
            w.write("\n");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy