Below is a quick sketch of vertical arrays
constructed from DataReader.

--- Raul Miller <[EMAIL PROTECTED]> wrote:

> On 2/26/07, Oleg Kobchenko <[EMAIL PROTECTED]> wrote:
> > Are there unit tests of samples for this class?
> 
> I don't know how to write portable instances of
> DataTable objects.  (DataTable does not have
> a public constructor.)  There's probably some way
> to do this (perhaps involving loading a DataSet from
> an xml file), but I don't know all the details.

Attached is an example of CSV DataReader.

> That said, some of the simpler conversions could be
> tested by pushing data from J through JConvert and
> comparing that result with 3!:1 inside J.  (But I've
> not tried that, yet.)
> 
> > If the data intended for J comes from the database,
> > wouldn't be logical to use DataReader instead of
> > DataTable, which created a second cache of the whole
> > result in memory?
> 
> [1] An extra temporary in-memory copy is not necessarily
> anything worth agonizing over.
> 
> [2] I imagine constructing portable unit tests involving
> DataReader would be even more problematic than
> constructing unit tests involving DataTable.

Hmm... No. See example.

> [3] I think this would wind up being more complex to
> implement.  (If nothing else, I think you wind up having
> to use GetSchemaTable to determine the basic
> structure of the table, so you wind up having to work
> with both an empty DataTable and the DataReader).

It doesn't look complex. I think it is simpler.
(DynamicMethod is not needed and replaced with
bulk switches).

> > However, the logic will have to
> > be regrouped to be able to fill separate columns
> > in parallel.
> 
> Yes, that also.
> 
> > Boxed result is interesting and J boxed arrays construction
> > in .NET too. But it might be more efficient to use native
> > homogenous .NET vectors for columns and pass them natively
> > into J unboxed with fixing and SetM.
> 
> How do you make that work for strings (varchar
> columns)?

Strings are joined and then cut in J.

> Meanwhile, here's my current concerns:
...


============================================================[Program.cs]==
using System;
using System.Data.OleDb;
using System.Data;
using System.Text;
using System.IO;
using System.Collections.Generic;

public class MyClass {

    public static void Main() {

        string connectionString = 
            string.Format(
                "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source={0};" +
                    "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"",
                Path.GetFullPath(Directory.GetCurrentDirectory() + 
"\\..\\..\\test"));

        using (OleDbConnection conn = new OleDbConnection(connectionString)) {

            try {
                conn.Open();
                OleDbCommand myCommand = conn.CreateCommand();

                myCommand.CommandType = CommandType.Text; 
                myCommand.CommandText = 
                    string.Format("select * from test.csv where firstname <> 
'{0}'", "Test");

                System.Collections.IList[] cols;
                int totalCount = 0;

                using (OleDbDataReader dataReader = myCommand.ExecuteReader()) {
                    cols = new System.Collections.IList[dataReader.FieldCount];
                    System.Collections.IList list;
                    for (int i = 0; i < dataReader.FieldCount; i++) { 
                        switch (Type.GetTypeCode(dataReader.GetFieldType(i))) {
                        case TypeCode.Int32: list = new List<int>(); break;
                        case TypeCode.Double: list = new List<double>(); break;
                        default: list = new List<string>(); break;
                        }
                        cols[i] = list;
                    }
                    while (dataReader.Read()) {
                        for (int i = 0; i < dataReader.FieldCount; i++) {
                            cols[i].Add(dataReader.GetValue(i));
                        }
                        totalCount++;
                    }
                }
                for (int i = 0; i < totalCount; i++) {
                    Console.WriteLine("{0}, {1}, {2}, {3}",
                        cols[0][i], cols[1][i], cols[2][i], cols[3][i]);
                }
                int[] AgeArray = new int[totalCount];
                cols[2].CopyTo(AgeArray, 0);
                Console.WriteLine("Array Type: {0}, Length: {1}",
                    AgeArray.GetType(), AgeArray.Length);

                string[] NameArray = new string[totalCount];
                cols[0].CopyTo(NameArray, 0);
                Console.WriteLine("String Array: {0}", string.Join("|", 
NameArray));

            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        Console.WriteLine("all done.");
    }

}
===========================================================[test.csv]==

FirstName,LastName
John,Doe,25,1.2
James,Fox,35,100
Adam,Smith,45,0.345

==========================================================[schema.ini]==

[test.csv]
ColNameHeader=True
Format=CSVDelimited
MaxScanRows=25
CharacterSet=ANSI
Col1=FirstName Char Width 20
Col2=LastName Char Width 20
Col3=Age Integer
Col4=Value Double

========================================================================

Sample output:

John, Doe, 25, 1.2
James, Fox, 35, 100
Adam, Smith, 45, 0.345
Array Type: System.Int32[], Length: 3
String Array: John|James|Adam
all done.



 
____________________________________________________________________________________
Finding fabulous fares is fun.  
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel 
bargains.
http://farechase.yahoo.com/promo-generic-14795097
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to