FileDocCategorySizeDatePackage
SimpleTextConnection.javaAPI DocExample26569Sat Feb 03 11:43:40 GMT 2001jdbc.SimpleText

SimpleTextConnection

public class SimpleTextConnection extends SimpleTextObject implements SimpleTextIConnection

Fields Summary
protected SimpleTextIDriver
ownerDriver
protected boolean
connectionClosed
protected boolean
readOnly
protected boolean
canWrite
protected String
catalog
Constructors Summary
Methods Summary
public voidclearWarnings()

        // No-op
    
public voidclose()

        connectionClosed = true;
    
public voidcommit()

        // No-op for the SimpleText driver
    
public java.sql.StatementcreateStatement()

        if (traceOn()) {
            trace("Creating new SimpleTextStatement");
        }

        // Create a new Statement object

        SimpleTextStatement stmt = new SimpleTextStatement();

        // Initialize the statement

        stmt.initialize (this);

        return stmt;
    
public booleangetAutoCommit()

        // The SimpleText driver is always in auto-commit mode (it does
        // not support transactions)

        return true;
    
public java.lang.StringgetCatalog()

        return catalog;
    
public java.util.HashtablegetColumns(java.lang.String dir, java.lang.String table)

        Hashtable list = new Hashtable();

        // Create the full path to the table

        String fullPath = dir + "/" + table + SimpleTextDefine.DATA_FILE_EXT;

        File f = new File (fullPath);

        // If the file does not exist, return null

        if (!f.exists()) {
            if (traceOn()) {
                trace("File does not exist: " + fullPath);
            }
            return null;
        }

        String line = "";

        // Create a random access object and read the first line
        // Create the table

        try {
            RandomAccessFile raf = new RandomAccessFile(f, "r");

            // Read the first line, which is the column definitions

            line = raf.readLine();
			raf.close();

        }
        catch (IOException ex) {
            if (traceOn()) {
                trace("Unable to read file: " + fullPath);
            }
            return null;
        }

        // Now, parse the line.  First, check for the branding

        if (!line.startsWith(SimpleTextDefine.DATA_FILE_EXT)) {
            if (traceOn()) {
                trace("Invalid file format: " + fullPath);
            }
            return null;
        }

        line = line.substring(SimpleTextDefine.DATA_FILE_EXT.length());

        // Now we can use the StringTokenizer, since we know that the
        // column names can't contain data within quotes (this is why
        // we can't use the StringTokenizer with SQL statements)

        StringTokenizer st = new StringTokenizer(line, ",");

        String columnName;
        int columnType;
        int precision;
        SimpleTextColumn column;
        int count = 0;
        boolean searchable;
        int displaySize;
        String typeName;

        // Loop while more tokens exist

        while (st.hasMoreTokens()) {
            columnName = (st.nextToken()).trim();

            if (columnName.length() == 0) {
                continue;
            }

            if (columnName.startsWith(SimpleTextDefine.COL_TYPE_NUMBER)) {
                columnType = Types.INTEGER;
                precision = SimpleTextDefine.MAX_INTEGER_LEN;
                columnName = columnName.substring(
                            SimpleTextDefine.COL_TYPE_NUMBER.length());
                displaySize = precision;
                typeName = "VARCHAR";
                searchable = true;
            }
            else if (columnName.startsWith(SimpleTextDefine.COL_TYPE_BINARY)) {
                columnType = Types.VARBINARY;
                precision = SimpleTextDefine.MAX_VARBINARY_LEN;
                columnName = columnName.substring(
                            SimpleTextDefine.COL_TYPE_BINARY.length());
                displaySize = precision * 2;
                typeName = "BINARY";
                searchable = false;
            } else {
                columnType = Types.VARCHAR;
                precision = SimpleTextDefine.MAX_VARCHAR_LEN;
                searchable = true;
                displaySize = precision;
                typeName = "NUMBER";
            }

            // Create a new column object and add to the Hashtable

            column = new SimpleTextColumn(columnName, columnType, precision);
            column.searchable = searchable;
            column.displaySize = displaySize;
            column.typeName = typeName;

            // The column number will be 1-based

            count++;

            // Save the absolute column number

            column.colNo = count;

            list.put(new Integer(count), column);
        }

        return list;
    
public java.lang.StringgetDirectory(java.lang.String directory)

        String dir;

        if (directory == null) {
            dir = catalog;
        }
        else if (directory.length() == 0) {
            dir = catalog;
        }
        else {
            dir = directory;
            if (dir.endsWith("/") ||
                dir.endsWith("\\")) {
                dir = dir.substring(0, dir.length());
            }
        }

        return dir;
    
public java.sql.DatabaseMetaDatagetMetaData()

        SimpleTextDatabaseMetaData dbmd = new SimpleTextDatabaseMetaData ();

        dbmd.initialize(this);

        return dbmd;
    
public java.util.HashtablegetTables(java.lang.String dir, java.lang.String table)

        Hashtable list = new Hashtable();

        // Create a FilenameFilter object.  This object will only allow
        // files with the .SDF extension to be seen

        FilenameFilter filter = new SimpleTextEndsWith(
                    SimpleTextDefine.DATA_FILE_EXT);


        File file = new File(dir);

        if (file.isDirectory()) {

            // List all of the files in the directory with the .SDF extension

            String entries[] = file.list(filter);
            SimpleTextTable tableEntry;

            // Create a SimpleTextTable entry for each, and put in
            // the Hashtable

            for (int i = 0; i < entries.length; i++) {

                // A complete driver needs to further filter the table
                // name here

                tableEntry = new SimpleTextTable(dir, entries[i]);
                list.put(new Integer(i), tableEntry);
            }
        }

        return list;
    
public intgetTransactionIsolation()

        // The SimpleText driver does not support transactions

        return TRANSACTION_NONE;
    
public java.sql.SQLWarninggetWarnings()

        // No warnings exist for the SimpleText driver.  Always return
        // null

        return null;
    
public voidinitialize(SimpleTextIDriver driver, java.util.Properties info)


        // Save the owning driver object

        ownerDriver = driver;

        // Get the security manager and see if we can write to a file.
        // If no security manager is present, assume that we are a trusted
        // application and have read/write privileges.

        canWrite = false;

        SecurityManager securityManager = System.getSecurityManager ();

        if (securityManager != null) {
            try {
                // Use some arbitrary file to check for file write privileges

                securityManager.checkWrite ("SimpleText_Foo");

                // Flag is set if no exception is thrown

                canWrite = true;
            }

            // If we can't write, an exception is thrown.  We'll catch
            // it and do nothing

            catch (SecurityException ex) {
            }
        }
        else {
            canWrite = true;
        }

        // Set our initial read-only flag

        setReadOnly(!canWrite);

        // Get the directory.  It will either be supplied with the URL, in
        //  the property list, or we'll use our current default

        String s = ownerDriver.getSubname();
        int slen = 0;

        if (s != null) {
            slen = s.length();
        }

        if (slen == 0) {
            s = info.getProperty("Directory");
        }

        if (s == null) {
            s = System.getProperty("user.dir");
        }

        setCatalog(s);
    
public booleanisClosed()

        return connectionClosed;
    
public booleanisReadOnly()

        return readOnly;
    
public java.lang.StringnativeSQL(java.lang.String sql)


        // For the SimpleText driver, simply return the original
        // sql statement.  Other drivers will need to expand escape
        // sequences here.

        return sql;
    
public java.lang.String[]parseSQL(java.lang.String sql)

        String keywords[] = null;

        // Create a new Hashtable to keep our words in.  This way, we can
        // build the Hashtable as we go, then create a String array
        // once we know how may words are present

        java.util.Hashtable table = new java.util.Hashtable();
        int count = 0;

        // Current offset in the sql string

        int offset = 0;

        // Get the first word from the sql statement

        String word = parseWord(sql.substring(offset));

        // Loop while more words exist in the sql string

        while (word.length() > 0) {

            // Increment the offset pointer

            offset += word.length();

            // Trim all leading and trailing spaces

            word = word.trim();

            if (word.length() > 0) {

                // Put the word in our hashtable

                table.put(new Integer(count), word);
                count++;
            }

            // Get the next word

            word = parseWord(sql.substring(offset));
        }

        // Create our new String array with the proper number of elements

        keywords = new String[count];

        // Copy the words from the Hashtable to the String array

        for (int i = 0; i < count; i++) {
            keywords[i] = (String) table.get(new Integer(i));
        }
        return keywords;
    
public java.sql.CallableStatementprepareCall(java.lang.String sql)

        if (traceOn()) {
            trace("@prepareCall (sql=" + sql + ")");
        }

        // The SimpleText driver does not support callable statements

        throw new SQLException("Driver does not support this function");
    
public java.sql.PreparedStatementprepareStatement(java.lang.String sql)

        if (traceOn()) {
            trace("@prepareStatement (sql=" + sql + ")");
        }

        // Create a new PreparedStatement object

        SimpleTextPreparedStatement ps = new SimpleTextPreparedStatement();

        // Initialize the PreparedStatement

        ps.initialize(this, sql);

        return ps;
    
public voidrollback()

        // No-op for the SimpleText driver
    
public voidsetAutoCommit(boolean autoCommit)

        if (traceOn()) {
            trace("@setAutoCommit (autoCommit=" + autoCommit + ")");
        }

        // The SimpleText driver is always in auto-commit mode (it does
        // not support transactions).  Throw an exception if an attempt
        // is made to change the mode

        if (autoCommit == false) {
            throw DriverNotCapable();
        }
    
public voidsetCatalog(java.lang.String catalog)

        if (traceOn()) {
            trace("@setCatalog(" + catalog + ")");
        }

        // If the last character is a separator, remove it

        if (catalog.endsWith("/") ||
            catalog.endsWith("\\")) {
            catalog = catalog.substring(0, catalog.length());
        }

        // Make sure this is a directory

        File dir = new File(catalog);

        if (!dir.isDirectory()) {
            throw new SQLException("Invalid directory: " + catalog);
        }

        this.catalog = catalog;
    
public voidsetReadOnly(boolean readOnly)


        // If we are trying to set the connection not read only (allowing
        // writes), and this connection does not allow writes, throw
        // an exception

        if ((readOnly == false) &&
            (canWrite == false)) {
            throw DriverNotCapable();
        }

        // Set the readOnly attribute for the SimpleText driver.  If set,
        // the driver will not allow updates or deletes to any text file

        this.readOnly = readOnly;
    
public voidsetTransactionIsolation(int level)

        if (traceOn()) {
            trace("@setTransactionIsolation (level=" + level + ")");
        }

        // Throw an exception if the transaction isolation is being
        // changed to something different

        if (level != TRANSACTION_NONE) {
            throw DriverNotCapable();
        }