public CreateTableResult CreateDaoTable(DaoObject daoObject, DatabaseAgent agent) { DaoTable tableAttribute = (DaoTable)daoObject.GetType().GetCustomAttributes(typeof(DaoTable), true)[0]; Expect.IsNotNull(tableAttribute); string tableName = tableAttribute.TableName; string idColumnName = this.GetIdColumnName(daoObject); Dictionary <PropertyInfo, DaoColumn> columns = this.GetColumns(daoObject); StringBuilder sqlColumns = new StringBuilder(); bool first = true; foreach (PropertyInfo key in columns.Keys) { string dataType = this.TranslateToDataType(key); string special = ""; bool primaryKey = false; if (key.HasCustomAttributeOfType <DaoIdColumn>(true, true))// -- old -> key.Name.Equals(idColumnName)) { special += this.GetIdentitySpec(); } // -- added 2/1/11 else if (key.HasCustomAttributeOfType <DaoPrimaryKeyColumn>(true, true)) { special += this.GetPrimaryKeySpec(); primaryKey = true; } // -- end added if (!columns[key].AllowNulls && !primaryKey) { special += " NOT NULL"; } if (!first) { sqlColumns.Append(", "); } sqlColumns.AppendFormat("\"{0}\" {1} {2}", columns[key].ColumnName, dataType, special); first = false; } try { agent.ExecuteSql(string.Format(DatabaseAgent.CREATETABLEFORMAT, tableName, sqlColumns.ToString())); if (agent.DbType == DaoDbType.Firebird) { this.CreateFirebirdIdTable(tableName, idColumnName, agent); } } catch (Exception ex) { return(CatchException(ex, tableName));//CreateDaoTableResult.UnknownError; } return(CreateTableResult.Success); }
private AddForeignKeyResult AddForeignKey(DaoForeignKeyInfo foreignKey, DatabaseAgent agent) { if (agent.DbType == DaoDbType.SQLite) { return(AddForeignKeyResult.Success); // foreign key constraints aren't supported in SQLite } string foreignKeyName = string.Empty; try { foreignKeyName = foreignKey.DaoForeignKeyColumn.ForeignKeyName; if (agent.DbType == DaoDbType.Firebird) { foreignKeyName = StringExtensions.RandomString(31, false, false); } // table1Name, fkName, column1Name, table2Name, column2Name agent.ExecuteSql(string.Format( DatabaseAgent.ADDFOREIGNKEYFORMAT, foreignKey.TableName, foreignKeyName, foreignKey.DaoForeignKeyColumn.ColumnName, foreignKey.DaoForeignKeyColumn.ReferencedTable, foreignKey.DaoForeignKeyColumn.ReferencedKey)); } catch (FbException ex) { if (ex.ErrorCode == -2147467259)// foreign key already exists { return(AddForeignKeyResult.Success); } } catch (SqlException sqlEx) { if (sqlEx.Message.ToLower().Contains(string.Format("already an object named '{0}'", foreignKeyName).ToLower())) { return(AddForeignKeyResult.Success); } else { return(AddForeignKeyResult.Error); } } catch (Exception ex) { Console.WriteLine(string.Format("An error occurred adding foreign key: {0}\r\n{1}", ex.Message, ex.StackTrace)); return(AddForeignKeyResult.Error); } return(AddForeignKeyResult.Success); }
public void AddColumn(string table, string columnName, string dataType, DatabaseAgent agent) { agent.ExecuteSql(string.Format(DatabaseAgent.ADDCOLUMNFORMAT, table, columnName + " " + dataType)); }