google-code-export / sqljet

Automatically exported from code.google.com/p/sqljet
0 stars 1 forks source link

bad string length on read when integer is written as string-typed value #154

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. write the string "10" into a null-typed record field
2. read field using ISqlJetCursor.getString(index);
3. test string length

What is the expected output? What do you see instead?

Expect to get the String "10".  Instead get a String of length 32, consisting
of the characters '1', '0', followed by 30 0 bytes.  This does not happen when
the string written isn't the printform of an integer, e.g., "X0".

See program below, which produces this error.

What version of the product are you using? On what operating system?

1.0.5, but seems to happen with 1.0.4 also.

Please provide any additional information below.

Java officially supports 0 as a String character value, so returning a String 
with trailing 0s is not the same as returning a String without the 0s.

Note also that there is an independent question of whether SqlJet supports 
strings containing 0s, which should probably be stated explicitly in 
documentation.

import java.io.File;

import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.SqlJetTransactionMode;
import org.tmatesoft.sqljet.core.table.ISqlJetCursor;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;

public class SqljetIndexTest {

    public static void main(String[] args) throws SqlJetException {
        String filename = args[0];
        File db_file = new File(filename);
        db_file.delete();
        SqlJetDb db = SqlJetDb.open(db_file, true);
        db.beginTransaction(SqlJetTransactionMode.WRITE);
        db.createTable("CREATE TABLE record (a NONE NOT NULL)");
        db.commit();
        ISqlJetTable table = db.getTable("record");
        db.beginTransaction(SqlJetTransactionMode.WRITE);
        table.insert("10");
        table.insert("X0");
        db.commit();
        db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
        ISqlJetCursor cursor = table.open();
        boolean more = !cursor.eof();
        while (more) {
            String s = cursor.getString(0);
            System.out.println(" a = " + s.length() + " '" + s + "'");
            for (int i = 0; i < s.length(); ++i)
                System.out.println("  " + i + " " + (int)s.charAt(i));
            more = cursor.next();
        }
        db.commit();
    }
}

generates output

 a = 32 '10
  0 49
  1 48
  2 0
  3 0
  4 0
  5 0
  6 0
  7 0
  8 0
  9 0
  10 0
  11 0
  12 0
  13 0
  14 0
  15 0
  16 0
  17 0
  18 0
  19 0
  20 0
  21 0
  22 0
  23 0
  24 0
  25 0
  26 0
  27 0
  28 0
  29 0
  30 0
  31 0
 a = 2 'X0'
  0 88
  1 48

Original issue reported on code.google.com by c...@google.com on 1 May 2011 at 10:31

GoogleCodeExporter commented 9 years ago

Original comment by kit...@gmail.com on 2 May 2011 at 5:31

GoogleCodeExporter commented 9 years ago
fixed in 'trunk'

Original comment by sergey.s...@gmail.com on 2 May 2011 at 10:51

GoogleCodeExporter commented 9 years ago

Original comment by kit...@gmail.com on 1 Jun 2011 at 9:40