Example #1
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;
            }
        }
Example #2
0
 public DataRow this[Guid name_id]
 {
     get
     {
         DataRow[] row = this.Select(XDataTable.ID(this) + "='" + name_id + "'");
         if (row.Length > 0)
         {
             return(row[0]);
         }
         return(null);
     }
 }
Example #3
0
 /// <summary>
 /// This is the thing that actually adds the default TableName_id and TableName_name
 /// </summary>
 /// <param name="trim_info">Option to trim _info and _description</param>
 public void AddDefaultColumns(bool trim_info, bool add_auto_id, bool add_auto_name)
 {
     if (add_auto_id)
     {
         DataColumn dc = this.Columns.Add(XDataTable.ID(this), typeof(int));
         dc.AutoIncrement = true;
         (this as DataTable).PrimaryKey = new DataColumn[] { dc };
     }
     if (add_auto_name)
     {
         this.Columns.Add(NameColumn, typeof(string)).Unique = true;
     }
 }
Example #4
0
 public object this[string name]
 {
     get
     {
         DataRow[] row = this.Select(NameColumn + "='" + name + "'");
         if (row.Length > 0)
         {
             return(row[0][XDataTable.ID(this)]);
         }
         DataRow dr;
         dr             = NewRow();
         dr[NameColumn] = name;
         Rows.Add(dr);
         CommitChanges();
         return(dr[XDataTable.ID(this)]);
     }
 }
Example #5
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);
            }
        }
Example #6
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;
            }
        }