* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Data.OracleClient / Test / testblob.cs
1 // testblob.cs - tests loading a binary file into an oracle blob and vice-versa
2 using System;
3 using System.Data;
4 using System.Data.OracleClient;
5 using System.Text;
6 using System.IO;
7
8 class TestBlob 
9 {
10         static string infilename = @"mono-win32-setup-dark.bmp";
11         static string outfilename = @"mono-win32-setup-dark2.bmp";
12         static string connectionString = "Data Source=palis;User ID=scott;Password=tiger";
13
14         public static void Main (string[] args) 
15         {
16                 OracleConnection con = new OracleConnection();
17                 con.ConnectionString = connectionString;
18                 con.Open();
19
20                 BLOBTest (con);
21                 ReadBlob (con);
22                 
23                 con.Close();
24                 con = null;
25         }
26
27         // read the BLOB into file "cs-parser2.cs"
28         public static void ReadBlob (OracleConnection connection) 
29         {
30                 if (File.Exists(outfilename) == true) {
31                         Console.WriteLine("Filename already exists: " + outfilename);
32                         return;
33                 }
34
35                 OracleCommand rcmd = connection.CreateCommand ();
36                 rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
37                 OracleDataReader reader2 = rcmd.ExecuteReader ();
38                 if (!reader2.Read ())
39                         Console.WriteLine ("ERROR: RECORD NOT FOUND");
40
41                 Console.WriteLine ("  TESTING OracleLob OBJECT 2...");
42                 OracleLob lob2 = reader2.GetOracleLob (0);
43                 Console.WriteLine ("  LENGTH: {0}", lob2.Length);
44                 Console.WriteLine ("  CHUNK SIZE: {0}", lob2.ChunkSize);
45
46                 byte[] lobvalue = (byte[]) lob2.Value;
47
48                 FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
49                 BinaryWriter w = new BinaryWriter(fs);
50                 w.Write(lobvalue);
51                 w.Close();
52                 fs.Close();
53
54                 lob2.Close ();
55                 reader2.Close ();
56         }
57
58         public static void BLOBTest (OracleConnection connection) 
59         {               
60                 Console.WriteLine ("  BEGIN TRANSACTION ...");
61
62                 OracleTransaction transaction = connection.BeginTransaction ();
63
64                 Console.WriteLine ("  Drop table BLOBTEST ...");
65                 try {
66                         OracleCommand cmd2 = connection.CreateCommand ();
67                         cmd2.Transaction = transaction;
68                         cmd2.CommandText = "DROP TABLE BLOBTEST";
69                         cmd2.ExecuteNonQuery ();
70                 }
71                 catch (OracleException oe1) {
72                         // ignore if table already exists
73                 }
74
75                 Console.WriteLine ("  CREATE TABLE ...");
76
77                 OracleCommand create = connection.CreateCommand ();
78                 create.Transaction = transaction;
79                 create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
80                 create.ExecuteNonQuery ();
81
82                 Console.WriteLine ("  INSERT RECORD ...");
83
84                 OracleCommand insert = connection.CreateCommand ();
85                 insert.Transaction = transaction;
86                 insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
87                 insert.ExecuteNonQuery ();
88
89                 OracleCommand select = connection.CreateCommand ();
90                 select.Transaction = transaction;
91                 select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
92                 Console.WriteLine ("  SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
93
94                 OracleDataReader reader = select.ExecuteReader ();
95                 if (!reader.Read ())
96                         Console.WriteLine ("ERROR: RECORD NOT FOUND");
97
98                 Console.WriteLine ("  TESTING OracleLob OBJECT ...");
99                 OracleLob lob = reader.GetOracleLob (0);
100                 Console.WriteLine ("  LENGTH: {0}", lob.Length);
101                 Console.WriteLine ("  CHUNK SIZE: {0}", lob.ChunkSize);
102
103                 try {
104                         if (File.Exists(infilename) == false) {
105                                 Console.WriteLine("Filename does not exist: " + infilename);
106                                 return;
107                         }
108
109                         FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
110                         BinaryReader r = new BinaryReader(fs);
111                         
112                         byte[] bytes = null;
113                         int bufferLen = 8192;
114                         bytes = r.ReadBytes (bufferLen);
115
116                         while(bytes.Length > 0) {
117                                 Console.WriteLine("byte count: " + bytes.Length.ToString());
118                                 lob.Write (bytes, 0, bytes.Length);
119                                 if (bytes.Length < bufferLen)
120                                         break;
121                                 bytes = r.ReadBytes (bufferLen);
122                         }
123
124                         r.Close();
125                         fs.Close ();    
126                 }
127                 catch (Exception e) {
128                         Console.WriteLine("The file could not be read:");
129                         Console.WriteLine(e.Message);
130                 }
131
132                 lob.Close ();
133
134                 Console.WriteLine ("  CLOSING READER...");
135                         
136                 reader.Close ();
137                 transaction.Commit ();
138         }
139 }