Ejemplo n.º 1
0
        void dt_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            if (filling)
            {
                return;
            }

            // going to blow away this row anyway.
            if (moving)
            {
                return;
            }
            IXDataTable table = e.Row.Table as IXDataTable;

            if (e.Column.ColumnName == table.NameColumn)
            {
                filling = true;
                DataRow[] groups = relation_data_table.Select(e.Row.Table.PrimaryKey[0] + "='" + e.Row[e.Row.Table.PrimaryKey[0].Ordinal] + "'");
                //e.Row.GetChildRows( relation.ChildrenOfParent );
                foreach (DataRow group in groups)
                {
                    DataRow[] currents = group.GetChildRows(relation.TableName);
                    foreach (DataRow current in currents)
                    {
                        current[NameColumn] = GetDisplayMember(group);
                    }
                }
                filling = false;
            }
            else if (e.Column.ColumnName == relation.NumberColumn)
            {
            }
        }
Ejemplo n.º 2
0
        //DataColumn auto_id;
        public static void AddDefaultColumns(DataTable table, bool trim_info, bool add_auto_id, bool add_auto_name)
        {
            if (add_auto_id)
            {
                IXDataTable ixtable = (IXDataTable)table;
                DataColumn  dc;

                if (ixtable != null)
                {
                    if (ixtable.AutoKeyType == typeof(Guid))
                    {
                        table.TableNewRow += new DataTableNewRowEventHandler(table_TableNewRow);
                    }
                    dc = table.Columns.Add(XDataTable.ID(table), ixtable.AutoKeyType);
                    if (ixtable.AutoKeyType == typeof(int))
                    {
                        dc.AutoIncrement     = true;
                        dc.AllowDBNull       = false;
                        dc.AutoIncrementStep = 1;
                        dc.AutoIncrementSeed = 1;
                    }
                    ixtable.auto_id = dc;
                }
                else
                {
                    dc = table.Columns.Add(XDataTable.ID(table), typeof(int));
                }

                table.PrimaryKey = new DataColumn[] { dc };
            }
            if (add_auto_name)
            {
                table.Columns.Add(XDataTable.Name(table), typeof(string)).Unique = true;
            }
        }
Ejemplo n.º 3
0
        public DataRow Clone(DataRow clone)
        {
            DataRow row = NewRow();

            foreach (DataColumn dc in clone.Table.Columns)
            {
                try
                {
                    if (dc.AutoIncrement)
                    {
                        continue;
                    }
                    row[dc.ColumnName] = clone[dc];
                }
                catch
                {
                    // the column night not exist in the source table...
                }
            }
            {
                IXDataTable xdatatable = clone.Table as IXDataTable;
                if (xdatatable != null)
                {
                    if (xdatatable.AutoKeyType == typeof(Guid))
                    {
                        row[xdatatable.PrimaryKeyName] = Guid.NewGuid();                        // DsnConnection.GetGUID();
                    }
                }
            }
            return(row);
        }
Ejemplo n.º 4
0
        public DataRow AddClonedRow(DataRow new_parent, DataRow clone)
        {
            String      primary_key        = null;
            String      parent_primary_key = null;
            IXDataTable source             = clone.Table as IXDataTable;
            IXDataTable parent_source      = new_parent.Table as IXDataTable;

            if (parent_source != null)
            {
                parent_primary_key = parent_source.PrimaryKeyName;
            }

            if (source != null)
            {
                primary_key = source.PrimaryKeyName;
                // first, check if the row already exists in the target...
                // if so, result with null instead of a row.
                DataRow[] rows = Select(source.PrimaryKeyName + "=" + clone[source.PrimaryKeyName]);
                //if( rows.Length > 0 )
                {
                    //	return rows[0];
                }
            }

            DataRow row = NewRow();

            foreach (DataColumn dc in clone.Table.Columns)
            {
                try
                {
                    // skip setting my own primary key
                    if (primary_key != null && dc.ColumnName == primary_key)
                    {
                        continue;
                    }
                    // use new parent primary key
                    else if (parent_primary_key != null && dc.ColumnName == parent_primary_key)
                    {
                        row[dc.ColumnName] = new_parent[dc.ColumnName];
                    }
                    else
                    {
                        row[dc.ColumnName] = clone[dc];
                    }
                }
                catch
                {
                    // the column night not exist in the source table...
                }
            }
            this.Rows.Add(row);
            return(row);
        }
Ejemplo n.º 5
0
        //public static readonly String NameColumn = XDataTable.Name( TableName );

        void InitCurrentObject(DataTable table1, DataTable table2, DataTable[] tables)
        {
            IXDataTable[] tmp = null;
            if (tables != null)
            {
                tmp = new IXDataTable[tables.Length];
                for (int n = 0; n < tables.Length; n++)
                {
                    tmp[n] = tables[n] as IXDataTable;
                }
            }
            InitCurrentObject(table1 as IXDataTable, table2 as IXDataTable, tmp);
        }
Ejemplo n.º 6
0
        public DataRow AddClonedRow(DataRow clone, bool allow_duplicate_key)
        {
            IXDataTable xsource = clone.Table as IXDataTable;
            DataTable   source  = clone.Table as DataTable;

            if (!allow_duplicate_key && xsource != null)
            {
                // first, check if the row already exists in the target...
                // if so, result with null instead of a row.
                DataRow[] rows;
                rows = Select(xsource.PrimaryKeyName + "='" + clone[xsource.PrimaryKeyName].ToString() + "'");
                if (rows.Length > 0)
                {
                    return(null);
                }
            }

            DataRow row = NewRow();

            foreach (DataColumn dc in source.Columns)
            {
                try
                {
                    // leave this NULL and auto-increment
                    if (dc.AutoIncrement)
                    {
                        continue;
                    }
                    if (row.Table.Columns.Contains(dc.ColumnName))
                    {
                        row[dc.ColumnName] = clone[dc.ColumnName];
                    }
                }
                catch
                {
                    // the column night not exist in the source table...
                }
            }
            this.Rows.Add(row);
            return(row);
        }
Ejemplo n.º 7
0
        public DataRow AddClonedRow(DataRow clone)
        {
            String      primary_key = null;
            IXDataTable source      = clone.Table as IXDataTable;

            if (source != null)
            {
                primary_key = source.PrimaryKeyName;
                // first, check if the row already exists in the target...
                // if so, result with null instead of a row.
                DataRow[] rows = Select(source.PrimaryKeyName + "=" + clone[source.PrimaryKeyName]);
                if (rows.Length > 0)
                {
                    return(rows[0]);
                }
            }

            DataRow row = NewRow();

            foreach (DataColumn dc in clone.Table.Columns)
            {
                try
                {
                    if (primary_key != null && dc.ColumnName == primary_key)
                    {
                        continue;
                    }
                    row[dc.ColumnName] = clone[dc];
                }
                catch
                {
                    // the column night not exist in the source table...
                }
            }
            this.Rows.Add(row);
            return(row);
        }
Ejemplo n.º 8
0
        static void table_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            MySQLDataTable     that   = e.Row.Table as MySQLDataTable;
            MySQLDataTable <T> that_t = e.Row.Table as MySQLDataTable <T>;
            IXDataTable        xthat  = e.Row.Table as IXDataTable;
            DsnConnection      conn   = null;

            if (that != null)
            {
                conn = that.connection;
            }
            else if (that_t != null)
            {
                conn = that_t.connection;
            }
            if (xthat != null)
            {
                e.Row[xthat.PrimaryKeyName] = DsnConnection.GetGUID(conn);
            }
            else
            {
                e.Row[XDataTable.ID(e.Row.Table)] = DsnConnection.GetGUID(conn);
            }
        }
Ejemplo n.º 9
0
        static public void AppendToDatabase(DsnConnection connection, DataTable table)
        {
            if (connection == null)
            {
                return;
            }
            String      PrimaryKeyName = XDataTable.ID(table);
            IXDataTable xdataTable     = table as IXDataTable;
            Type        key_type       = table.Columns[PrimaryKeyName].DataType;

            if (xdataTable != null)
            {
                xdataTable.filling = true;
            }
            Log.log("AppendToDatabase should check primary key type - if it's Guid, then min/max are irrelavent");
            object min_local_id = table.Compute("min(" + PrimaryKeyName + ")", null);
            object max_local_id = table.Compute("max(" + PrimaryKeyName + ")", null);

            // no rows to commit.
            if (min_local_id.GetType() == typeof(DBNull))
            {
                return;
            }

            if (key_type == typeof(Int32))
            {
                object max_real_id = connection.ExecuteScalar("select max(" + PrimaryKeyName + ") from "
                                                              + connection.sql_quote_open + table.Prefix + table.TableName + connection.sql_quote_close);
                if ((max_real_id == null) || (max_real_id.GetType() == typeof(DBNull)))
                {
                    //
                    if (Convert.ToInt32(min_local_id) != 1)
                    {
                        int n = 1;
                        foreach (DataRow row in table.Rows)
                        {
                            row[PrimaryKeyName] = n;
                            n++;
                        }
                    }
                }
                else
                {
                    if (Convert.ToInt32(max_real_id) >= Convert.ToInt32(min_local_id) && Convert.ToInt32(max_real_id) <= Convert.ToInt32(max_local_id))
                    {
                        int final_id = Convert.ToInt32(max_real_id) + table.Rows.Count + 1;
                        if (final_id < Convert.ToInt32(max_local_id))
                        {
                            int n = Convert.ToInt32(max_real_id) + 1;
                            // while moving row ID's we may duplicate, so... look ahead...
                            foreach (DataRow row in table.Rows)
                            {
                                DataRow[] conflict = table.Select(PrimaryKeyName + "=" + n);
                                if (conflict.Length > 0)
                                {
                                    int old_id = Convert.ToInt32(row[PrimaryKeyName]);
                                    conflict[0][PrimaryKeyName] = 0;
                                    row[PrimaryKeyName]         = n;
                                    conflict[0][PrimaryKeyName] = old_id;
                                }
                                else
                                {
                                    row[PrimaryKeyName] = n;
                                }
                                n++;
                            }


                            //object o = null;
                            //o.GetType();
                        }
                        else
                        {
                            int r;
                            for (r = table.Rows.Count - 1; r >= 0; r--)
                            {
                                table.Rows[r][PrimaryKeyName] = final_id;
                                final_id--;
                            }
                        }
                    }
                    else
                    {
                        int n = Convert.ToInt32(max_real_id) + 1;
                        foreach (DataRow row in table.Rows)
                        {
                            row[PrimaryKeyName] = n;
                            n++;
                        }
                    }
                }
            }
            else if (key_type == typeof(Guid))
            {
                foreach (DataRow row in table.Rows)
                {
                    row[PrimaryKeyName] = connection.GetGUID();
                }
            }
            DsnSQLUtil.InsertAllRows(connection, table);
            if (xdataTable != null)
            {
                xdataTable.filling = false;
            }
        }
Ejemplo n.º 10
0
        void InitCurrentObject(IXDataTable table1, IXDataTable table2, IXDataTable[] tables)
        {
            parents = table1;
            if (parents != null)
            {
                parent_keyname = XDataTable.ID(parents as DataTable);
            }

            children = table2;
            if (children != null)
            {
                child_keyname = XDataTable.ID(children as DataTable);
            }

            all_parents = tables;
            NameColumn  = relation.NameColumn;
            //session_game_groups = table1.DataSet.Tables[MySQLRelationTable.RelationName( table1, table2 )] as MySQLRelationTable;

            if (children == null && all_parents == null)
            {
                throw new Exception("Table is not an XDataTable with a DisplayMemberName");
            }

            base.TableName = "current_" + relation.TableName;
            DataColumn mykeycol = Columns.Add(relation_keyname = relation.PrimaryKeyName, XDataTable.DefaultAutoKeyType);

            Columns.Add(NameColumn, typeof(String));

            if (relation_data_table.DataSet.Tables.Contains(this.ToString()) == false)
            {
                relation_data_table.DataSet.Tables.Add(this);

                DataRelation r = new DataRelation(relation.TableName
                                                  , relation_data_table.Columns[relation.PrimaryKeyName]
                                                  , mykeycol);
                //r.ChildKeyConstraint.ExtendedProperties.
                relation_data_table.DataSet.Relations.Add(r);
            }

            // these are to handle updating the current display name...
            // they should resolve to a common routine in the derived class.
            if (children != null)
            {
                children.ColumnChanged += new DataColumnChangeEventHandler(child_ColumnChanged);
            }
            if (parents != null)
            {
                parents.ColumnChanged += new DataColumnChangeEventHandler(parents_ColumnChanged);
            }
            if (all_parents != null)
            {
                foreach (IXDataTable dt in all_parents)
                {
                    dt.ColumnChanged += new DataColumnChangeEventHandler(dt_ColumnChanged);
                }
            }
            base.PrimaryKey           = new DataColumn[] { mykeycol };
            relation.RowChanged      += new DataRowChangeEventHandler(parent_children_RowChanged);
            relation.ColumnChanged   += new DataColumnChangeEventHandler(parent_child_ColumnChanged);
            relation.RowOrderChanged += new MySQLRelationTable.OnRowOrderChanged(relation_RowOrderChanged);
            Fill();
        }
Ejemplo n.º 11
0
        public void Fill(DsnConnection odbc)
        {
            active_dataset.Clear();
            foreach (DataTable f in TableFiller)
            {
                IXDataTable xtable = f as IXDataTable;
                if (xtable != null)
                {
                    xtable.filling = true;
                }
                f.BeginLoadData();
            }
            // use a slightly more complex fill method, 1, if a table has a self filler, use it
            // else use default fill all
            // and then if there is a default fill proc, call it - it will add default data if rows=0
            foreach (TableDefaultFiller f in TableFillers)
            {
                Type type = f.table.GetType();
                if (f.real_fill_method != null)
                {
                    type.InvokeMember(f.real_fill_method,
                                      BindingFlags.Default | BindingFlags.InvokeMethod,
                                      null,
                                      f.table,
                                      null);
                }
                else
                {
                    DsnSQLUtil.FillDataTable(odbc, f.table, null, null);
                }
                bool fill_ok = false;
                if (f.method_name == "DefaultFill")
                {
                    if (f.table.Rows.Count == 0)
                    {
                        fill_ok = true;
                    }
                }
                else
                {
                    fill_ok = true;
                }

                if (fill_ok)
                {
                    if (f.method_name != null)
                    {
                        type.InvokeMember(f.method_name,
                                          BindingFlags.Default | BindingFlags.InvokeMethod,
                                          null,
                                          f.table,
                                          null);
                    }
                }
            }
            foreach (DataTable f in TableFiller)
            {
                try
                {
                    IXDataTable xtable = f as IXDataTable;
                    if (xtable != null)
                    {
                        xtable.filling = false;
                    }
                    f.EndLoadData();
                }
                catch (Exception e)
                {
                    Log.log("Failed to load: " + e.Message);
                    DumpTableErrors();
                }
            }
        }