/// <summary> /// consider that other databases may have differnt defauls for ondelete and onupdate... /// </summary> /// <param name="mode"></param> public XDataTableForeignKey(DsnConnection.ConnectionMode mode) { switch (mode) { case DsnConnection.ConnectionMode.MySqlNative: case DsnConnection.ConnectionMode.Odbc: break; } }
public static String GetColumnDef(DsnConnection.ConnectionMode mode, DsnConnection.ConnectionFlavor flavor, DataColumn col) { object o = col.ExtendedProperties["Extra Type"]; String extra_type_info = (o == null ? "" : o.ToString()); string ColTypeString; string default_val = (col.DefaultValue == DBNull.Value) ? null : col.DefaultValue.ToString(); bool timecol = false; switch (mode) { case DsnConnection.ConnectionMode.MySqlNative: case DsnConnection.ConnectionMode.Odbc: if ((col.DataType == typeof(DateTime) && extra_type_info == "createstamp") || (col.DataType == typeof(DateTime) && col.Namespace == "createstamp")) { timecol = true; default_val = "CURRENT_TIMESTAMP"; } ColTypeString = ((col.DataType == typeof(string) && extra_type_info == "text") ? "text" : (col.DataType == typeof(string) && col.Namespace == "text") ? "text" : (col.DataType == typeof(Guid)) ? "char(36)" : (col.DataType == typeof(Money)) ? "decimal(18,2)" : (col.DataType == typeof(decimal)) ? "decimal" : (col.DataType == typeof(string)) ? GetVarChar(col) : (col.DataType == typeof(Color)) ? "bigint" : (col.DataType == typeof(long)) ? "bigint" : (col.DataType == typeof(bool)) ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "bit" : "tinyint(1)") : (col.DataType == typeof(decimal)) ? "decimal(11,2)" : (col.DataType == typeof(double)) ? "double" : (col.DataType == typeof(TimeSpan)) ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "datetime" : "time default,") : (col.DataType == typeof(DateTime) && extra_type_info == "time") ? "timestamp" : (col.DataType == typeof(DateTime) && col.Namespace == "time") ? "timestamp" : (col.DataType == typeof(DateTime) && extra_type_info == "date") ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "datetime" : "date") : (col.DataType == typeof(DateTime) && col.Namespace == "date") ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "datetime" : "date") : (col.DataType == typeof(DateTime) && extra_type_info == "createstamp") ? "timestamp default CURRENT_TIMESTAMP" : (col.DataType == typeof(DateTime) && col.Namespace == "createstamp") ? "timestamp default CURRENT_TIMESTAMP" : (col.DataType == typeof(DateTime)) ? "datetime" : (col.DataType == typeof(byte[])) ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "varbinary(max) default" : "blob default NULL") : (col.AutoIncrement ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "int IDENTITY PRIMARY KEY" : "int(11) auto_increment") : (col.ColumnName.EndsWith("_id") ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "int" : "int(11)") : ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "int" : "int(11)")))) ; if (default_val == null) { ColTypeString += (col.AllowDBNull)?" NULL":" NOT NULL"; return(ColTypeString); } else { if (!timecol) { if (col.DataType == typeof(bool)) { return(ColTypeString + " default '" + Convert.ToInt32(col.DefaultValue).ToString() + "'"); } else { return(ColTypeString + " default '" + default_val + "'"); } } return(ColTypeString); } case DsnConnection.ConnectionMode.SQLServer: if ((col.DataType == typeof(DateTime) && extra_type_info == "createstamp") || (col.DataType == typeof(DateTime) && col.Namespace == "createstamp")) { timecol = true; default_val = "CURRENT_TIMESTAMP"; } ColTypeString = ((col.DataType == typeof(string) && extra_type_info == "text") ? "text" : (col.DataType == typeof(string) && col.Namespace == "text") ? "text" : (col.DataType == typeof(Guid)) ? "UNIQUEIDENTIFIER" : (col.DataType == typeof(Money)) ? "decimal(18,2)" : (col.DataType == typeof(decimal)) ? "decimal(18,4)" : (col.DataType == typeof(string)) ? GetVarChar(col) : (col.DataType == typeof(Color)) ? "bigint" : (col.DataType == typeof(long)) ? "bigint" : (col.DataType == typeof(bool)) ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "bit" : "tinyint(1)") : (col.DataType == typeof(decimal)) ? "decimal(11,2)" : (col.DataType == typeof(double)) ? "double" : (col.DataType == typeof(TimeSpan)) ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "datetime" : "time default,") : (col.DataType == typeof(DateTime) && extra_type_info == "time") ? "timestamp" : (col.DataType == typeof(DateTime) && col.Namespace == "time") ? "timestamp" : (col.DataType == typeof(DateTime) && extra_type_info == "date") ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "datetime" : "date") : (col.DataType == typeof(DateTime) && col.Namespace == "date") ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "datetime" : "date") : (col.DataType == typeof(DateTime) && extra_type_info == "createstamp") ? "timestamp default CURRENT_TIMESTAMP" : (col.DataType == typeof(DateTime) && col.Namespace == "createstamp") ? "timestamp default CURRENT_TIMESTAMP" : (col.DataType == typeof(DateTime)) ? "datetime" : (col.DataType == typeof(byte[])) ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "varbinary(max) default" : "blob default NULL") : (col.AutoIncrement ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "int IDENTITY PRIMARY KEY" : "int(11) auto_increment") : (col.ColumnName.EndsWith("_id") ? ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "int" : "int(11)") : ((flavor == DsnConnection.ConnectionFlavor.SQLServer) ? "int" : "int(11)")))) ; if (default_val == null) { ColTypeString += (col.AllowDBNull) ? " NULL" : " NOT NULL"; return(ColTypeString); } else { if (!timecol) { if (col.DataType == typeof(bool)) { return(ColTypeString + " default '" + Convert.ToInt32(col.DefaultValue).ToString() + "'"); } else { return(ColTypeString + " default '" + default_val + "'"); } } return(ColTypeString); } case DsnConnection.ConnectionMode.Sqlite: return ((col.DataType == typeof(string) && extra_type_info == "text") ? "text NOT NULL default ''" : (col.DataType == typeof(string) && col.Namespace == "text") ? "text NOT NULL default ''" : (col.DataType == typeof(Guid)) ? "char(36)" : (col.DataType == typeof(Money)) ? "decimal(18,2)" : (col.DataType == typeof(string)) ? GetVarChar(col) : (col.DataType == typeof(bool)) ? "tinyint(1) NOT NULL default 0" : (col.DataType == typeof(decimal)) ? "decimal(18,4) default 0.00" : (col.DataType == typeof(double)) ? "double not NULL default 0.0" : (col.DataType == typeof(TimeSpan)) ? "time default NULL," : (col.DataType == typeof(DateTime) && extra_type_info == "time") ? "timestamp default '00-00-00 00:00:00'" : (col.DataType == typeof(DateTime) && extra_type_info == "date") ? "date default '0000-00-00'" : (col.DataType == typeof(DateTime) && extra_type_info == "createstamp") ? "timestamp NOT NULL default CURRENT_TIMESTAMP" : (col.DataType == typeof(DateTime) && col.Namespace == "time") ? "timestamp default '00-00-00 00:00:00'" : (col.DataType == typeof(DateTime) && col.Namespace == "date") ? "date default '0000-00-00'" : (col.DataType == typeof(DateTime) && col.Namespace == "createstamp") ? "timestamp NOT NULL default CURRENT_TIMESTAMP" : (col.DataType == typeof(DateTime)) ? "datetime default '00-00-00 00:00:00'" : (col.DataType == typeof(byte[])) ? "blob default NULL" : (col.AutoIncrement ? "int(11) auto_increment" : (col.ColumnName.EndsWith("_id") ? "int(11)" : "int(11) NOT NULL default '0'"))); default: throw new Exception("unsupported get column def"); //return null; } }