Esempio n. 1
0
        /// <summary>
        /// Test DataTable implicit DELETE
        /// </summary>
        private static void Test_DataTable_DeleteImplicit()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                //Insert a new row
                ExecuteSQL("insert into nation values('ZZZZ', 'Z', 'Z', 'Z')", conn);
                Debug.Assert(GetTableRowsCount("nation", conn) == 216);

                String sql = "select * from nation order by `code` desc";
                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter(sql, conn))
                {
                    CUBRIDDataAdapter    daCmd      = new CUBRIDDataAdapter(sql, conn);
                    CUBRIDCommandBuilder cmdBuilder = new CUBRIDCommandBuilder(daCmd);
                    da.DeleteCommand = cmdBuilder.GetDeleteCommand();

                    DataTable dt = new DataTable("nation");
                    da.Fill(dt);

                    Debug.Assert(dt.Rows[0]["capital"].ToString() == "Z");

                    dt.Rows[0].Delete();
                    da.Update(dt);

                    Debug.Assert(dt.Rows.Count == 215);
                }

                Debug.Assert(GetTableRowsCount("nation", conn) == 215);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Test basic SQL statements execution, using DataSet
        /// </summary>
        private static void Test_DataSet_Basic()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String            sql = "select * from nation order by `code` asc";
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter();
                da.SelectCommand = new CUBRIDCommand(sql, conn);
                DataSet ds = new DataSet("nation");
                da.Fill(ds);

                DataTable dt0 = ds.Tables["Table"];
                Debug.Assert(dt0 != null);

                dt0 = ds.Tables[0];

                Debug.Assert(dt0.Columns.Count == 4);
                Debug.Assert(dt0.DefaultView.Count == 215);
                Debug.Assert(dt0.DefaultView.AllowEdit == true);
                Debug.Assert(dt0.DefaultView.AllowDelete == true);
                Debug.Assert(dt0.DefaultView.AllowNew == true);
                Debug.Assert(dt0.DataSet.DataSetName == "nation");

                DataRow[] dataRow = dt0.Select("continent = 'Africa'");

                Debug.Assert(dataRow.Length == 54);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Test exporting XML from DataSet
        /// </summary>
        private static void Test_DataSet_ExportXML()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String            sql = "select * from nation order by `code` asc";
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter();
                da.SelectCommand = new CUBRIDCommand(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "nation");

                string filename = @".\Test_DataSet_ExportXML.xml";
                ds.WriteXml(filename);

                if (!System.IO.File.Exists(filename))
                {
                    throw new Exception("XML output file not found!");
                }
                else
                {
                    System.IO.File.Delete(filename);
                }
            }
        }
Esempio n. 4
0
        public void CUBRIDDataAdapter_ConstructorWithSqlAndConn_Test()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = DBHelper.connString;
                conn.Open();

                DBHelper.ExecuteSQL("drop table if exists t", conn);
                DBHelper.ExecuteSQL("create table t (id int, name varchar(100))", conn);
                DBHelper.ExecuteSQL("insert into t values (1, 'Nancy')", conn);
                DBHelper.ExecuteSQL("insert into t values (2, 'Peter')", conn);

                string            selectCommandText = "select * from t";
                CUBRIDDataAdapter adapter           = new CUBRIDDataAdapter(selectCommandText, conn);
                DataTable         dt = new DataTable("student");
                adapter.Fill(dt);

                //verify data

                Assert.AreEqual(1, (int)dt.Rows[0]["id"]);
                Assert.AreEqual("Nancy", dt.Rows[0]["name"].ToString());

                Assert.AreEqual(2, (int)dt.Rows[1]["id"]);
                Assert.AreEqual("Peter", dt.Rows[1]["name"].ToString());

                //revert test db
                DBHelper.ExecuteSQL("drop table if exists t", conn);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Test SQL statements execution, using DataView
        /// </summary>
        private static void Test_DataView_Basic()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String            sql = "select * from nation order by `code` asc";
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter();
                da.SelectCommand = new CUBRIDCommand(sql, conn);
                DataTable dt = new DataTable("nation");
                da.Fill(dt);

                DataView dataView = new DataView(dt);

                Debug.Assert(dataView.Count == 215);
                Debug.Assert(dataView.Table.TableName == "nation");

                foreach (DataRowView view in dataView)
                {
                    Debug.Assert(dataView[0][0].ToString() == "AFG");
                    break; //retrieve just one row
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Test batch update, using DataAdapter
        /// </summary>
        private static void Test_DataAdapter_BatchUpdate()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter())
                {
                    // Set the INSERT command and parameter.
                    da.InsertCommand = new CUBRIDCommand("insert into nation values ('A', 'B', 'C', 'D')");
                    da.InsertCommand.UpdatedRowSource = UpdateRowSource.None;

                    // Set the UPDATE command and parameters.
                    da.UpdateCommand = new CUBRIDCommand("update nation set capital = 'X' where `code` = 'A'");
                    da.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;

                    // Set the DELETE command and parameter.
                    da.DeleteCommand = new CUBRIDCommand("delete from nation where `code` = 'A'");
                    da.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;

                    // Set the batch size.
                    da.UpdateBatchSize = 3;

                    // Execute the update.
                    DataTable dt = new DataTable("nation");
                    da.Update(dt);
                }

                Debug.Assert(GetTableRowsCount("nation", conn) == 215);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Test BLOB SELECT, using CUBRIDDataAdapter and DataTable
        /// </summary>
        private static void Test_Blob_SelectDataAdapter()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                CreateTestTableLOB(conn);

                string        sql1  = "insert into t (b) values(?)";
                CUBRIDCommand cmd1  = new CUBRIDCommand(sql1, conn);
                CUBRIDBlob    Blob1 = new CUBRIDBlob(conn);
                CUBRIDBlob    Blob2 = new CUBRIDBlob(conn);

                byte[] bytes1 = new byte[256];
                bytes1[0]   = 69;
                bytes1[1]   = 98;
                bytes1[2]   = 99;
                bytes1[255] = 122;

                Blob1.SetBytes(1, bytes1);

                CUBRIDParameter param = new CUBRIDParameter();
                param.ParameterName  = "?";
                param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
                param.Value          = Blob1;
                cmd1.Parameters.Add(param);
                cmd1.Parameters[0].DbType = DbType.Binary;
                cmd1.ExecuteNonQuery();
                cmd1.Close();

                string            sql = "SELECT b from t";
                DataTable         dt  = new DataTable("t");
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter();
                da.SelectCommand = new CUBRIDCommand(sql, conn);
                da.Fill(dt);

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    CUBRIDBlob bImage = (CUBRIDBlob)dt.Rows[j]["b"];
                    byte[]     bytes  = new byte[(int)bImage.BlobLength];
                    bytes = bImage.GetBytes(1, (int)bImage.BlobLength);

                    Debug.Assert(bytes1.Length == bytes.Length, "The selected length is not valid!");
                    bool ok = true;
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        if (bytes1[i] != bytes[i])
                        {
                            ok = false;
                        }
                    }

                    Debug.Assert(ok == true, "The BLOB was not selected correctly!");
                }

                CleanupTestTableLOB(conn);
            }
        }
Esempio n. 8
0
        public void CUBRIDCommand_Prepare_Basic_Test()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection(DBHelper.connString))
            {
                conn.Open();
                DBHelper.ExecuteSQL("drop table if exists t", conn);
                DBHelper.ExecuteSQL("create table t (id int, name varchar(50))", conn);

                CUBRIDCommand cmd = new CUBRIDCommand(null, conn);

                LogTestStep("Test Prepare");
                cmd.CommandText =
                    "insert into t (id, name) values (?, ?)";

                CUBRIDParameter idParam   = new CUBRIDParameter("?", CUBRIDDataType.CCI_U_TYPE_INT, 8);
                CUBRIDParameter nameParam = new CUBRIDParameter("?", CUBRIDDataType.CCI_U_TYPE_STRING, 20);
                idParam.Value   = 2;
                nameParam.Value = "Rachel Green";
                cmd.Parameters.Add(idParam);
                cmd.Parameters.Add(nameParam);


                Log("Call Prepare after setting the Commandtext and Parameters.");
                cmd.Prepare();
                cmd.ExecuteNonQuery();

                Log("Change parameter values and call ExecuteNonQuery.");
                cmd.Parameters[0].Value = 5;
                cmd.Parameters[1].Value = "Bill Gates";
                cmd.ExecuteNonQuery();

                Log("Verify the date are inserted by querying them from db");
                cmd = new CUBRIDCommand("select * from t", conn);
                CUBRIDDataAdapter adapter = new CUBRIDDataAdapter(cmd);
                DataTable         dt      = new DataTable();
                adapter.Fill(dt);

                Assert.AreEqual(2, dt.Rows.Count);
                Assert.AreEqual(2, (int)dt.Rows[0][0]);
                Assert.AreEqual("Rachel Green", dt.Rows[0][1].ToString());
                Assert.AreEqual(5, (int)dt.Rows[1][0]);
                Assert.AreEqual("Bill Gates", dt.Rows[1][1].ToString());
                LogStepPass();

                cmd.Close();

                Log("delete test table");
                DBHelper.ExecuteSQL("drop table if exists t", conn);
                LogTestResult();
            }
        }
Esempio n. 9
0
        //http://msdn.microsoft.com/en-us/library/bbw6zyha%28v=vs.80%29.aspx
        /// <summary>
        /// Test DataTable explicit UPDATE
        /// </summary>
        private static void Test_DataTable_UpdateExplicit()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String sql = "select * from nation order by `code` asc LIMIT 10";
                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter(sql, conn))
                {
                    //Initialize the command object that will be used as the UpdateCommand for the DataAdapter.
                    CUBRIDCommand daUpdate = new CUBRIDCommand("update nation set capital = ? where code = ?", conn);

                    //Parameter: capital
                    daUpdate.Parameters.Add(new CUBRIDParameter("?p1", DbType.String));
                    daUpdate.Parameters[0].SourceVersion           = DataRowVersion.Current;
                    daUpdate.Parameters[0].SourceColumn            = "capital";
                    daUpdate.Parameters[0].SourceColumnNullMapping = false;

                    //Parameter: code
                    daUpdate.Parameters.Add(new CUBRIDParameter("?p2", DbType.String));
                    daUpdate.Parameters[1].SourceVersion           = DataRowVersion.Original;
                    daUpdate.Parameters[1].SourceColumn            = "code";
                    daUpdate.Parameters[1].SourceColumnNullMapping = false;

                    daUpdate.UpdatedRowSource = UpdateRowSource.None;
                    //Assign the command to the UpdateCommand property of the DataAdapter.
                    da.UpdateCommand = daUpdate;

                    DataTable dt = new DataTable("nation");
                    da.Fill(dt);
                    DataRow workRow = dt.Rows[0];
                    Debug.Assert(workRow["capital"].ToString() == "Kabul");
                    workRow.BeginEdit();
                    workRow["capital"] = "MyKabul";
                    workRow.EndEdit();
                    da.Update(dt);
                    dt.AcceptChanges();

                    Debug.Assert(workRow["capital"].ToString() == "MyKabul");
                    Debug.Assert(workRow.RowState.ToString() != "New");
                }

                Debug.Assert((string)GetSingleValue("select capital from nation where `code` = 'AFG'", conn) == "MyKabul");
                //Revert changes
                ExecuteSQL("update nation set capital = 'Kabul' where capital = 'MyKabul'", conn);
                Debug.Assert((string)GetSingleValue("select capital from nation where `code` = 'AFG'", conn) == "Kabul");
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Test DataTableReader GetSchemaTable() method
        /// </summary>
        private static void Test_GetSchemaTable()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                string            sql     = "select * from athlete order by `code`";
                CUBRIDDataAdapter adapter = new CUBRIDDataAdapter(sql, conn);
                DataTable         table   = new DataTable();

                //To retrieve the AlolowDBNull, IsUnique, IsKey, IsAutoIncrement and BaseTableName values from the Database Server
                //you must use the FillSchema() method.
                adapter.FillSchema(table, SchemaType.Source);

                using (DataTableReader reader = new DataTableReader(table))
                {
                    DataTable schemaTable = reader.GetSchemaTable();
                    DataRow   row         = schemaTable.Rows[0];

                    Debug.Assert(row["ColumnName"].ToString() == "code");
                    Debug.Assert(row["ColumnOrdinal"].ToString() == "0");
                    Debug.Assert(row["ColumnSize"].ToString() == "-1");
                    Debug.Assert(row["NumericPrecision"].ToString() == "");
                    Debug.Assert(row["NumericScale"].ToString() == "");
                    Debug.Assert(row["IsUnique"].ToString() == "True");
                    Debug.Assert(row["IsKey"].ToString() == "True");
                    Debug.Assert(row["BaseTableNamespace"].ToString() == "");
                    Debug.Assert(row["BaseColumnNamespace"].ToString() == "");
                    Debug.Assert(row["BaseCatalogName"].ToString() == "");
                    Debug.Assert(row["BaseColumnName"].ToString() == "code");
                    Debug.Assert(row["BaseSchemaName"].ToString() == "");
                    Debug.Assert(row["BaseTableName"].ToString() == "athlete");
                    Debug.Assert(row["DataType"].ToString() == "System.Int32");
                    Debug.Assert(row["AllowDBNull"].ToString() == "False");
                    Debug.Assert(row["ProviderType"].ToString() == "");
                    Debug.Assert(row["Expression"].ToString() == "");
                    Debug.Assert(row["AutoIncrementSeed"].ToString() == "0");
                    Debug.Assert(row["AutoincrementStep"].ToString() == "1");
                    Debug.Assert(row["IsAutoIncrement"].ToString() == "True");
                    Debug.Assert(row["IsRowVersion"].ToString() == "False");
                    Debug.Assert(row["IsLong"].ToString() == "False");
                    Debug.Assert(row["IsReadOnly"].ToString() == "False");
                    Debug.Assert(row["ColumnMapping"].ToString() == "1");
                    Debug.Assert(row["DefaultValue"].ToString() == "");
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Test CLOB SELECT, using CUBRIDDataAdapter and DataSet
        /// </summary>
        private static void Test_Clob_SelectDataAdapter2()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                CreateTestTableLOB(conn);

                string        sql1 = "insert into t (c) values(?)";
                CUBRIDCommand cmd1 = new CUBRIDCommand(sql1, conn);

                CUBRIDClob Clob1 = new CUBRIDClob(conn);

                String str1 = conn.ConnectionString; //Use ConnectionString content for testing
                Clob1.SetString(1, str1);

                CUBRIDParameter param = new CUBRIDParameter();
                param.ParameterName  = "?";
                param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB;
                param.Value          = Clob1;
                cmd1.Parameters.Add(param);
                cmd1.ExecuteNonQuery();
                cmd1.Close();

                string sql = "SELECT c from t";

                DataSet           ds = new DataSet();
                CUBRIDDataAdapter da = new CUBRIDDataAdapter();
                da.SelectCommand = new CUBRIDCommand(sql, conn);
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    CUBRIDClob cImage = (CUBRIDClob)dt.Rows[j]["c"];
                    string     str    = cImage.GetString(1, (int)cImage.ClobLength);

                    Debug.Assert(str.Length == str1.Length, "The selected CLOB length is not valid!");
                    Debug.Assert(str.Equals(str1), "The CLOB was not selected correctly!");
                }

                CleanupTestTableLOB(conn);
            }
        }
Esempio n. 12
0
        public void conn_dataAdapter_update()
        {
            string           conn_string = "server=test-db-server;database=demodb;port=33000;user=dba;password="******"drop table if exists tbl";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "create table tbl (id int, name varchar(100))";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into tbl values (1, 'Nancy')";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into tbl values (2, 'Peter')";
            cmd.ExecuteNonQuery();

            CUBRIDDataAdapter adapter = new CUBRIDDataAdapter();

            //SelectCommand
            string        sql  = "select * from tbl";
            CUBRIDCommand cmd2 = new CUBRIDCommand(sql, conn);

            adapter.SelectCommand = cmd2;

            sql  = "insert into tbl values (3, 'Kitty')";
            cmd2 = new CUBRIDCommand(sql, conn);
            adapter.InsertCommand = cmd2;
            adapter.InsertCommand.ExecuteNonQuery();

            sql  = "update tbl set name='Mandy' where id=1";
            cmd2 = new CUBRIDCommand(sql, conn);
            adapter.UpdateCommand = cmd2;
            adapter.UpdateCommand.ExecuteNonQuery();

            sql  = "delete from tbl where name='Mandy'";
            cmd2 = new CUBRIDCommand(sql, conn);
            adapter.DeleteCommand = cmd2;
            adapter.DeleteCommand.ExecuteNonQuery();

            conn.Close();
        }
Esempio n. 13
0
        //http://msdn.microsoft.com/en-us/library/tf579hcz%28v=vs.80%29.aspx
        /// <summary>
        /// Test CUBRIDCommandBuilder class, and methods used to automatically get SQL commands
        /// </summary>
        private static void Test_CommandBuilder_GetCommands()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String            sql = "select * from nation order by `code` asc";
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter(sql, conn);

                CUBRIDCommandBuilder cmdBuilder = new CUBRIDCommandBuilder(da);
                da.UpdateCommand = cmdBuilder.GetUpdateCommand();
                Debug.Assert(da.UpdateCommand.CommandText == "UPDATE `nation` SET `code` = ?, `name` = ?, `continent` = ?, `capital` = ? WHERE ((`code` = ?) AND (`name` = ?) AND ((? = 1 AND `continent` IS NULL) OR (`continent` = ?)) AND ((? = 1 AND `capital` IS NULL) OR (`capital` = ?)))");
                da.InsertCommand = cmdBuilder.GetInsertCommand();
                Debug.Assert(da.InsertCommand.CommandText == "INSERT INTO `nation` (`code`, `name`, `continent`, `capital`) VALUES (?, ?, ?, ?)");
                da.DeleteCommand = cmdBuilder.GetDeleteCommand();
                Debug.Assert(da.DeleteCommand.CommandText == "DELETE FROM `nation` WHERE ((`code` = ?) AND (`name` = ?) AND ((? = 1 AND `continent` IS NULL) OR (`continent` = ?)) AND ((? = 1 AND `capital` IS NULL) OR (`capital` = ?)))");
            }
        }
        private static void Test_CommandBuilder()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String            sql = "select * from nation order by `code` asc";
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter(sql, conn);

                CUBRIDCommandBuilder cmdBuilder = new CUBRIDCommandBuilder(da);
                string sql_format = string.Format("select {0},{1} from {2}",
                                                  cmdBuilder.QuoteIdentifier("name"),
                                                  cmdBuilder.QuoteIdentifier("age"),
                                                  cmdBuilder.QuoteIdentifier("user"));

                Console.WriteLine(sql_format);
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Test CUBRIDCommand column properties
        /// </summary>
        private static void Test_Command_ColumnProperties()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String        sql  = "select * from nation";
                CUBRIDCommand cmd  = new CUBRIDCommand(sql, conn);
                CUBRIDCommand cmd2 = cmd.Clone();

                try
                {
                    cmd.Cancel();
                }
                catch (Exception e)
                {
                    string r = "System.NotSupportedException: Specified method is not supported";
                    Debug.Assert(e.Message.Substring(0, r.Length) == r);
                }

                Debug.Assert(cmd.CommandType == cmd2.CommandType);
                CUBRIDDataAdapter da = new CUBRIDDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable("");
                da.FillSchema(dt, SchemaType.Source);//To retrieve all the column properties you have to use the FillSchema() method

                Debug.Assert(cmd.ColumnInfos[0].Name == "code");
                Debug.Assert(cmd.ColumnInfos[0].IsPrimaryKey == true);
                Debug.Assert(cmd.ColumnInfos[0].IsForeignKey == false);
                Debug.Assert(cmd.ColumnInfos[0].IsNullable == false);
                Debug.Assert(cmd.ColumnInfos[0].RealName == "");
                Debug.Assert(cmd.ColumnInfos[0].Precision == 3);
                Debug.Assert(cmd.ColumnInfos[0].Scale == 0);
                Debug.Assert(cmd.ColumnInfos[0].IsAutoIncrement == false);
                Debug.Assert(cmd.ColumnInfos[0].IsReverseIndex == false);
                Debug.Assert(cmd.ColumnInfos[0].IsReverseUnique == false);
                Debug.Assert(cmd.ColumnInfos[0].IsShared == false);
                Debug.Assert(cmd.ColumnInfos[0].Type == CUBRIDDataType.CCI_U_TYPE_CHAR);
                Debug.Assert(cmd.ColumnInfos[0].Table == "nation");
            }
        }
Esempio n. 16
0
        //http://msdn.microsoft.com/en-us/library/bbw6zyha%28v=vs.80%29.aspx
        /// <summary>
        /// Test DataTable explicit DELETE
        /// </summary>
        private static void Test_DataTable_DeleteExplicit()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                //Insert a new row
                ExecuteSQL("insert into nation values('ZZZZ', 'Z', 'Z', 'Z')", conn);
                Debug.Assert(GetTableRowsCount("nation", conn) == 216);

                String sql = "select * from nation order by `code` desc";
                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter(sql, conn))
                {
                    //Initialize the command object that will be used as the DeleteCommand for the DataAdapter.
                    CUBRIDCommand daDelete = new CUBRIDCommand("delete from nation where code = ?", conn);

                    //Parameter: code
                    daDelete.Parameters.Add(new CUBRIDParameter("?p1", DbType.String));
                    daDelete.Parameters["?p1"].SourceVersion           = DataRowVersion.Original;
                    daDelete.Parameters["?p1"].SourceColumn            = "code";
                    daDelete.Parameters["?p1"].SourceColumnNullMapping = false;

                    daDelete.UpdatedRowSource = UpdateRowSource.OutputParameters;

                    //Assign the command to the DeleteCommand property of the DataAdapter.
                    da.DeleteCommand = daDelete;

                    DataTable dt = new DataTable("nation");
                    da.Fill(dt);

                    Debug.Assert(dt.Rows[0]["capital"].ToString() == "Z");

                    dt.Rows[0].Delete();
                    da.Update(dt);

                    Debug.Assert(dt.Rows.Count == 215);
                }

                Debug.Assert(GetTableRowsCount("nation", conn) == 215);
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Test basic SQL Statements execution, using DataTable
        /// </summary>
        private static void Test_DataTable_Basic()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String sql = "select * from nation order by `code` asc";
                using (DataTable dt = new DataTable("nation"))
                {
                    CUBRIDDataAdapter da = new CUBRIDDataAdapter();
                    da.SelectCommand = new CUBRIDCommand(sql, conn);
                    da.Fill(dt);

                    Debug.Assert(dt.Columns.Count == 4);
                    Debug.Assert(dt.Rows.Count == 215);
                    Debug.Assert(dt.Rows[1][1].ToString() == "Netherlands Antilles");
                    Debug.Assert(dt.Rows[3][2].ToString() == "Africa");
                }
            }
        }
Esempio n. 18
0
        //http://msdn.microsoft.com/en-us/library/bbw6zyha%28v=vs.80%29.aspx
        /// <summary>
        /// Test DataTable implicit UPDATE
        /// </summary>
        private static void Test_DataTable_UpdateImplicit()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String sql = "select * from nation order by `code` asc";
                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter(sql, conn))
                {
                    using (CUBRIDDataAdapter daCmd = new CUBRIDDataAdapter(sql, conn))
                    {
                        CUBRIDCommandBuilder cmdBuilder = new CUBRIDCommandBuilder(daCmd);
                        da.UpdateCommand = cmdBuilder.GetUpdateCommand();
                    }

                    DataTable dt = new DataTable("nation");
                    da.Fill(dt);

                    //Update data
                    DataRow workRow = dt.Rows[0];

                    Debug.Assert(workRow["code"].ToString() == "AFG");
                    Debug.Assert(workRow["capital"].ToString() == "Kabul");

                    workRow.BeginEdit();
                    workRow["capital"] = "MyKabul";
                    workRow.EndEdit();
                    da.Update(dt);

                    Debug.Assert(workRow["capital"].ToString() == "MyKabul");
                    Debug.Assert(workRow.RowState.ToString() != "New");
                }

                Debug.Assert((string)GetSingleValue("select capital from nation where `code` = 'AFG'", conn) == "MyKabul");
                //Revert changes
                ExecuteSQL("update nation set capital = 'Kabul' where capital = 'MyKabul'", conn);
                Debug.Assert((string)GetSingleValue("select capital from nation where `code` = 'AFG'", conn) == "Kabul");
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Test DataTable column properties
        /// </summary>
        private static void Test_DataTable_ColumnProperties()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String            sql = "select * from nation";
                CUBRIDDataAdapter da  = new CUBRIDDataAdapter();
                da.SelectCommand = new CUBRIDCommand(sql, conn);
                DataTable dt = new DataTable("nation");
                da.FillSchema(dt, SchemaType.Source);//To retrieve all the column properties you have to use the FillSchema() method

                Debug.Assert(dt.Columns[0].ColumnName == "code");
                Debug.Assert(dt.Columns[0].AllowDBNull == false);
                Debug.Assert(dt.Columns[0].DefaultValue.ToString() == "");
                Debug.Assert(dt.Columns[0].Unique == true);
                Debug.Assert(dt.Columns[0].DataType == typeof(System.String));
                Debug.Assert(dt.Columns[0].Ordinal == 0);
                Debug.Assert(dt.Columns[0].Table == dt);
            }
        }
Esempio n. 20
0
        //http://msdn.microsoft.com/en-us/library/bbw6zyha%28v=vs.80%29.aspx
        /// <summary>
        /// Test DataTable implicit INSERT
        /// </summary>
        private static void Test_DataTable_InsertImplicit()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String sql = "select * from nation order by `code` asc";
                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter(sql, conn))
                {
                    using (CUBRIDDataAdapter daCmd = new CUBRIDDataAdapter(sql, conn))
                    {
                        CUBRIDCommandBuilder cmdBuilder = new CUBRIDCommandBuilder(daCmd);
                        da.InsertCommand = cmdBuilder.GetInsertCommand();
                    }

                    DataTable dt = new DataTable("nation");
                    da.Fill(dt);

                    DataRow newRow = dt.NewRow();
                    newRow["code"]      = "ZZZ";
                    newRow["name"]      = "ABCDEF";
                    newRow["capital"]   = "MyXYZ";
                    newRow["continent"] = "QWERTY";
                    dt.Rows.Add(newRow);

                    da.Update(dt);

                    Debug.Assert(dt.Rows.Count == 216);
                }

                Debug.Assert(GetTableRowsCount("nation", conn) == 216);
                //Revert changes
                ExecuteSQL("delete from nation where `code` = 'ZZZ'", conn);
                Debug.Assert(GetTableRowsCount("nation", conn) == 215);
            }
        }
        private static void TestGetBytes()
        {
            string sql = "drop table if exists TestTable;";

            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }

            sql = "CREATE TABLE TestTable (clsid BLOB);";
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                CUBRIDDataAdapter ad1 = new CUBRIDDataAdapter(cmd);
                CUBRIDDataAdapter ad2 = new CUBRIDDataAdapter("Select * from TestTable", connString);
                cmd.ExecuteNonQuery();
            }
            byte[] bytes = new byte[36] {
                55, 56, 50, 69, 55, 57, 67, 69, 45, 50, 70, 68, 68, 45, 52, 68, 50, 55, 45, 65, 51, 48, 48, 45, 69, 48, 56, 56, 70, 56, 68, 68, 55, 54, 66, 69
            };
            sql = "INSERT INTO TestTable VALUES(?);";
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                CUBRIDBlob Blob = new CUBRIDBlob(conn);
                Blob.SetBytes(1, bytes);
                CUBRIDParameter param = new CUBRIDParameter();
                param.ParameterName      = "?p";
                param.Value              = Blob; cmd.Parameters.Add(param);
                cmd.Parameters[0].DbType = DbType.Binary;
                cmd.ExecuteNonQuery();
            }
            using (CUBRIDCommand cmd = new CUBRIDCommand("Select * from TestTable", conn))
            {
                using (CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader())
                {
                    reader.Read(); byte[] buffer = new byte[36];
                    long          len            = reader.GetBytes(0, 0, buffer, 0, 36);
                    ASCIIEncoding encoding       = new ASCIIEncoding();
                    string        clsid          = encoding.GetString(buffer);
                    Debug.Assert(clsid == "782E79CE-2FDD-4D27-A300-E088F8DD76BE");
                }
            }


            sql = "drop table if exists TestTable;";
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }

            sql = "CREATE TABLE TestTable (clsid CLOB);";
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
            sql = "INSERT INTO TestTable VALUES('1234567890');";
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
            using (CUBRIDCommand cmd = new CUBRIDCommand("Select * from TestTable", conn))
            {
                using (CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader())
                {
                    reader.Read();
                    byte[] buffer = new byte[36];
                    long   len    = reader.GetBytes(0, 0, buffer, 0, 8);

                    try
                    {
                        len = reader.GetBytes(0, 0, buffer, 0, 36);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
        }
Esempio n. 22
0
        public void CUBRIDDataAdapter_Command_Test()
        {
            CUBRIDConnection conn = new CUBRIDConnection();

            conn.ConnectionString = DBHelper.connString;
            conn.Open();

            DBHelper.ExecuteSQL("drop table if exists t", conn);
            DBHelper.ExecuteSQL("create table t (id int, name varchar(100))", conn);
            DBHelper.ExecuteSQL("insert into t values (1, 'Nancy')", conn);
            DBHelper.ExecuteSQL("insert into t values (2, 'Peter')", conn);

            CUBRIDDataAdapter adapter = new CUBRIDDataAdapter();

            //SelectCommand
            string        sql = "select * from t";
            CUBRIDCommand cmd = new CUBRIDCommand(sql, conn);

            adapter.SelectCommand = cmd;

            DataTable dt = new DataTable("student");

            adapter.Fill(dt);

            //verify data
            Assert.AreEqual(1, (int)dt.Rows[0]["id"]);
            Assert.AreEqual("Nancy", dt.Rows[0]["name"].ToString());
            Assert.AreEqual(2, (int)dt.Rows[1]["id"]);
            Assert.AreEqual("Peter", dt.Rows[1]["name"].ToString());
            Assert.AreEqual(sql, adapter.SelectCommand.CommandText);

            //UpdateCommand
            sql = "update t set name='Mandy' where id=1";
            cmd = new CUBRIDCommand(sql, conn);
            adapter.UpdateCommand = cmd;
            adapter.UpdateCommand.ExecuteNonQuery();
            dt.AcceptChanges();
            adapter.Update(dt);

            Console.WriteLine(dt.Rows[0]["name"]);

            //dt.AcceptChanges();
            //Assert.AreEqual(1, (int)dt.Rows[0]["id"]);
            //Assert.AreEqual("Mandy", dt.Rows[0]["name"].ToString());
            //Assert.AreEqual(2, (int)dt.Rows[1]["id"]);
            //Assert.AreEqual("Peter", dt.Rows[1]["name"].ToString());
            //Assert.AreEqual(sql, adapter.UpdateCommand.CommandText);

            //DeleteCommand
            sql = "delete from t where name='Mandy'";
            cmd = new CUBRIDCommand(sql, conn);
            adapter.DeleteCommand = cmd;
            adapter.DeleteCommand.ExecuteNonQuery();
            dt.Rows[0].Delete();

            //TODO: Verifcation: might need to 'SqlCommandBuilder'?
            dt.AcceptChanges();
            adapter.Update(dt);
            Assert.AreEqual(1, dt.Rows.Count);
            Assert.AreEqual(2, (int)dt.Rows[0]["id"]);
            Assert.AreEqual("Peter", dt.Rows[0]["name"].ToString());
            Assert.AreEqual(sql, adapter.DeleteCommand.CommandText);
            // --
            //InsertCommand


            //revert test db
            DBHelper.ExecuteSQL("drop table if exists t", conn);

            conn.Close();
        }
Esempio n. 23
0
        public void CUBRIDCommand_Clone_Test()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection(DBHelper.connString))
            {
                conn.Open();
                DBHelper.ExecuteSQL("drop table if exists t", conn);
                DBHelper.ExecuteSQL("create table t (id int primary key, name varchar(50))", conn);
                DBHelper.ExecuteSQL("insert into t (id, name) values (2, 'Rachel Green')", conn);
                DBHelper.ExecuteSQL("insert into t (id, name) values (3, 'Rachel Green')", conn);
                DBHelper.ExecuteSQL("insert into t (id, name) values (5, 'Bill Gates')", conn);

                LogTestStep("Clone a CUBRIDCommand which has parameters");
                CUBRIDCommand cmd = new CUBRIDCommand(null, conn);
                cmd.CommandText = "select * from t where id = ?myId and name = ?myName";

                CUBRIDParameter idParam   = new CUBRIDParameter("?myId", CUBRIDDataType.CCI_U_TYPE_INT, 8);
                CUBRIDParameter nameParam = new CUBRIDParameter("?myName", CUBRIDDataType.CCI_U_TYPE_STRING, 20);
                idParam.Value   = 2;
                nameParam.Value = "Rachel Green";
                cmd.Parameters.Add(idParam);
                cmd.Parameters.Add(nameParam);

                CUBRIDCommand cmdClone = cmd.Clone();

                CUBRIDDataAdapter adapter = new CUBRIDDataAdapter();

                adapter.SelectCommand = cmdClone;

                Log("Verify the cloned command");
                DataTable dt = new DataTable("");
                adapter.Fill(dt);

                Assert.AreEqual(1, dt.Rows.Count);
                Assert.AreEqual(2, (int)dt.Rows[0][0]);
                Assert.AreEqual("Rachel Green", dt.Rows[0][1].ToString());

                adapter.FillSchema(dt, SchemaType.Source);//To retrieve all the column properties you have to use the FillSchema() method

                Assert.AreEqual(cmdClone.ColumnInfos[0].Name, "id");
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsPrimaryKey, true);
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsForeignKey, false);
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsNullable, false);
                Assert.AreEqual(cmdClone.ColumnInfos[0].RealName, "t");
                Assert.AreEqual(cmdClone.ColumnInfos[0].Precision, 10);
                Assert.AreEqual(cmdClone.ColumnInfos[0].Scale, 0);
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsAutoIncrement, false);
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsReverseIndex, false);
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsReverseUnique, false);
                Assert.AreEqual(cmdClone.ColumnInfos[0].IsShared, false);
                Assert.AreEqual(cmdClone.ColumnInfos[0].Type, CUBRIDDataType.CCI_U_TYPE_INT);
                Assert.AreEqual(cmdClone.ColumnInfos[0].Table, "t");
                LogStepPass();
                adapter.Dispose();
                cmd.Close();

                Log("delete test db");
                //DBHelper.ExecuteSQL("drop table if exists t", conn);
            }

            LogTestResult();
        }
Esempio n. 24
0
        /// <summary>
        /// Test DataTable explicit INSERT
        /// </summary>
        private static void Test_DataTable_InsertExplicit()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                String sql = "select * from nation order by `code` DESC LIMIT 10";
                using (CUBRIDDataAdapter da = new CUBRIDDataAdapter(sql, conn))
                {
                    //Initialize the command object that will be used as the UpdateCommand for the DataAdapter.
                    CUBRIDCommand daInsert = new CUBRIDCommand("insert into nation values(?,?,?,?)", conn);
                    daInsert.CommandType = CommandType.Text;

                    //Parameter: code
                    daInsert.Parameters.Add(new CUBRIDParameter("?p1", DbType.String));
                    daInsert.Parameters["?p1"].SourceVersion           = DataRowVersion.Current;
                    daInsert.Parameters["?p1"].SourceColumn            = "code";
                    daInsert.Parameters["?p1"].SourceColumnNullMapping = false;

                    //Parameter: name
                    daInsert.Parameters.Add(new CUBRIDParameter("?p2", DbType.String));
                    daInsert.Parameters["?p2"].SourceVersion           = DataRowVersion.Original;
                    daInsert.Parameters["?p2"].SourceColumn            = "name";
                    daInsert.Parameters["?p2"].SourceColumnNullMapping = false;

                    //Parameter: continent
                    daInsert.Parameters.Add(new CUBRIDParameter("?p3", DbType.String));
                    daInsert.Parameters["?p3"].SourceVersion           = DataRowVersion.Current;
                    daInsert.Parameters["?p3"].SourceColumn            = "continent";
                    daInsert.Parameters["?p3"].SourceColumnNullMapping = false;

                    //Parameter: capital
                    daInsert.Parameters.Add(new CUBRIDParameter("?p4", DbType.String));
                    daInsert.Parameters["?p4"].SourceVersion           = DataRowVersion.Original;
                    daInsert.Parameters["?p4"].SourceColumn            = "capital";
                    daInsert.Parameters["?p4"].SourceColumnNullMapping = false;

                    daInsert.UpdatedRowSource = UpdateRowSource.None;

                    //Assign the command to the InsertCommand property of the DataAdapter.
                    da.InsertCommand = daInsert;

                    DataTable dt = new DataTable("nation");
                    da.Fill(dt);
                    DataRow newRow = dt.NewRow();
                    newRow["code"]      = "ZZZ";
                    newRow["name"]      = "ABCDEF";
                    newRow["capital"]   = "MyXYZ";
                    newRow["continent"] = "QWERTY";
                    dt.Rows.InsertAt(newRow, 0);
                    da.Update(dt);
                    dt.AcceptChanges();

                    Debug.Assert(dt.Rows[0]["capital"].ToString() == "MyXYZ");
                    Debug.Assert(newRow.RowState.ToString() != "New");
                }

                Debug.Assert(GetTableRowsCount("nation", conn) == 216);
                //Revert changes
                ExecuteSQL("delete from nation where `code` = 'ZZZ'", conn);
                Debug.Assert(GetTableRowsCount("nation", conn) == 215);
            }
        }
Esempio n. 25
0
        public Task <IList <Column> > GetTableDetails(Table table, string owner)
        {
            IList <Column> columns = new List <Column>();
            var            conn    = new CUBRIDConnection(connectionStr);

            conn.Open();
            try
            {
                using (conn)
                {
                    var       schema = new CUBRIDSchemaProvider(conn);
                    DataTable dt_fk  = schema.GetForeignKeys(new[] { table.Name.ToLower() });

                    string sqlInfo   = String.Format("select * from [{0}] limit 1", table.Name.ToLower());
                    var    adapter   = new CUBRIDDataAdapter(sqlInfo, conn);
                    var    tableInfo = new DataTable();
                    adapter.FillSchema(tableInfo, SchemaType.Source);

                    using (var reader = new DataTableReader(tableInfo))
                    {
                        DataTable schemaTable = reader.GetSchemaTable();
                        for (var k = 0; k < schemaTable.Rows.Count; k++)
                        {
                            string columnName    = schemaTable.Rows[k]["ColumnName"].ToString().ToLower();
                            var    isUnique      = (bool)schemaTable.Rows[k]["IsUnique"];
                            var    isNullable    = (bool)schemaTable.Rows[k]["AllowDBNull"];
                            var    isPrimaryKey  = (bool)schemaTable.Rows[k]["IsKey"];
                            var    isIdentity    = (bool)schemaTable.Rows[k]["IsAutoIncrement"];
                            var    dataLength    = (int)schemaTable.Rows[k]["ColumnSize"];
                            int    dataPrecision = 0;
                            if (schemaTable.Rows[k]["NumericPrecision"].ToString() != String.Empty)
                            {
                                dataPrecision = (int)schemaTable.Rows[k]["NumericPrecision"];
                            }
                            int dataScale = 0;
                            if (schemaTable.Rows[k]["NumericScale"].ToString() != String.Empty)
                            {
                                dataScale = (int)schemaTable.Rows[k]["NumericScale"];
                            }
                            bool   isForeignKey   = false;
                            string fkTableName    = "";
                            string constraintName = "";
                            for (var i_fk = 0; i_fk < dt_fk.Rows.Count; i_fk++)
                            {
                                if (dt_fk.Rows[i_fk]["FKCOLUMN_NAME"].ToString().ToLower() == columnName)
                                {
                                    isForeignKey   = true;
                                    fkTableName    = dt_fk.Rows[i_fk]["PKTABLE_NAME"].ToString();
                                    constraintName = dt_fk.Rows[i_fk]["FK_NAME"].ToString();
                                    break;
                                }
                            }
                            string dataType;
                            using (var cmd = new CUBRIDCommand(sqlInfo, conn))
                            {
                                using (var CUBRIDReader = (CUBRIDDataReader)cmd.ExecuteReader())
                                {
                                    CUBRIDReader.Read();
                                    dataType = CUBRIDReader.GetColumnTypeName(k);
                                }
                            }
                            var m = new DataTypeMapper();
                            columns.Add(new Column
                            {
                                Name                = columnName,
                                DataType            = dataType,
                                IsNullable          = isNullable,
                                IsUnique            = isUnique,
                                IsPrimaryKey        = isPrimaryKey,
                                IsForeignKey        = isForeignKey,
                                IsIdentity          = isIdentity,
                                DataLength          = dataLength,
                                DataPrecision       = dataPrecision,
                                DataScale           = dataScale,
                                ForeignKeyTableName = fkTableName,
                                ConstraintName      = constraintName,
                                MappedDataType      = m.MapFromDBType(ServerType.CUBRID, dataType, null, null, null),
                            });
                        }
                    }
                }

                table.Columns = columns;

                table.Owner      = owner;
                table.PrimaryKey = DeterminePrimaryKeys(table);

                table.HasManyRelationships = DetermineHasManyRelationships(table);
            }
            finally
            {
                conn.Close();
            }

            return(Task.FromResult(columns));
        }