public Tables LoadTables() { writer.WriteLine("// This file was automatically generated by the PetaPoco T4 Template"); writer.WriteLine("// Do not make changes directly to this file - edit the template instead"); writer.WriteLine("// "); writer.WriteLine("// The following connection settings were used to generate this file"); writer.WriteLine("// "); writer.WriteLine("// Provider: `{0}`", this.providerName); writer.WriteLine("// Connection String: `{0}`", ZapPassword(this.connectionString)); writer.WriteLine("// Schema: `{0}`", this.SchemaName); writer.WriteLine("// Include Views: `{0}`", this.includeViews); writer.WriteLine(""); DbProviderFactory _factory; try { _factory = DbProviderFactories.GetFactory(this.providerName); } catch (Exception x) { var error = x.Message.Replace("\r\n", "\n").Replace("\n", " "); warning(string.Format("Failed to load provider `{0}` - {1}", this.providerName, error)); writer.WriteLine(""); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine("// Failed to load provider `{0}` - {1}", this.providerName, error); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine(""); return new Tables(); } writer.WriteLine("// Factory Name: `{0}`", _factory.GetType().Name); try { using (var conn = _factory.CreateConnection()) { conn.ConnectionString = this.connectionString; conn.Open(); SchemaReader reader; if (_factory.GetType().Name == "MySqlClientFactory") { // MySql reader = new MySqlSchemaReader(); } else if (_factory.GetType().Name == "SqlCeProviderFactory") { // SQL CE reader = new SqlServerCeSchemaReader(); } else if (_factory.GetType().Name == "NpgsqlFactory") { // PostgreSQL reader = new PostGreSqlSchemaReader(); } else if (_factory.GetType().Name == "OracleClientFactory") { // Oracle reader = new OracleSchemaReader(); } else if (_factory.GetType().Name == "SQLiteFactory") { // Sqlite reader = new SqliteSchemaReader(); } else { // Assume SQL Server reader = new SqlServerSchemaReader(); } reader.outer = writer; Tables result = reader.ReadSchema(conn, _factory); // Remove unrequired tables/views for (int i = result.Count - 1; i >= 0; i--) { if (this.SchemaName != null && string.Compare(result[i].Schema, this.SchemaName, true) != 0) { result.RemoveAt(i); continue; } if (!this.includeViews && result[i].IsView) { result.RemoveAt(i); } } conn.Close(); var rxClean = new Regex("^(Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$"); foreach (var t in result) { t.ClassName = this.classPrefix + t.ClassName + this.classSuffix; foreach (var c in t.Columns) { c.PropertyName = rxClean.Replace(c.PropertyName, "_$1"); // Make sure property name doesn't clash with class name if (c.PropertyName == t.ClassName) { c.PropertyName = "_" + c.PropertyName; } } } return result; } } catch (Exception x) { var error = x.Message.Replace("\r\n", "\n").Replace("\n", " "); warning(string.Format("Failed to read database schema - {0}", error)); writer.WriteLine(""); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine("// Failed to read database schema - {0}", error); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine(""); return new Tables(); } }
public Tables LoadTables() { writer.WriteLine("// This file was automatically generated by the PetaPoco T4 Template"); writer.WriteLine("// Do not make changes directly to this file - edit the template instead"); writer.WriteLine("// "); writer.WriteLine("// The following connection settings were used to generate this file"); writer.WriteLine("// "); writer.WriteLine("// Provider: `{0}`", this.providerName); writer.WriteLine("// Connection String: `{0}`", ZapPassword(this.connectionString)); writer.WriteLine("// Schema: `{0}`", this.SchemaName); writer.WriteLine("// Include Views: `{0}`", this.includeViews); writer.WriteLine(""); DbProviderFactory _factory; try { _factory = DbProviderFactories.GetFactory(this.providerName); } catch (Exception x) { var error = x.Message.Replace("\r\n", "\n").Replace("\n", " "); warning(string.Format("Failed to load provider `{0}` - {1}", this.providerName, error)); writer.WriteLine(""); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine("// Failed to load provider `{0}` - {1}", this.providerName, error); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine(""); return(new Tables()); } writer.WriteLine("// Factory Name: `{0}`", _factory.GetType().Name); try { using (var conn = _factory.CreateConnection()) { conn.ConnectionString = this.connectionString; conn.Open(); SchemaReader reader; if (_factory.GetType().Name == "MySqlClientFactory") { // MySql reader = new MySqlSchemaReader(); } else if (_factory.GetType().Name == "SqlCeProviderFactory") { // SQL CE reader = new SqlServerCeSchemaReader(); } else if (_factory.GetType().Name == "NpgsqlFactory") { // PostgreSQL reader = new PostGreSqlSchemaReader(); } else if (_factory.GetType().Name == "OracleClientFactory") { // Oracle reader = new OracleSchemaReader(); } else if (_factory.GetType().Name == "SQLiteFactory") { // Sqlite reader = new SqliteSchemaReader(); } else { // Assume SQL Server reader = new SqlServerSchemaReader(); } reader.outer = writer; Tables result = reader.ReadSchema(conn, _factory); // Remove unrequired tables/views for (int i = result.Count - 1; i >= 0; i--) { if (this.SchemaName != null && string.Compare(result[i].Schema, this.SchemaName, true) != 0) { result.RemoveAt(i); continue; } if (!this.includeViews && result[i].IsView) { result.RemoveAt(i); } } conn.Close(); var rxClean = new Regex("^(Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$"); foreach (var t in result) { t.ClassName = this.classPrefix + t.ClassName + this.classSuffix; foreach (var c in t.Columns) { c.PropertyName = rxClean.Replace(c.PropertyName, "_$1"); // Make sure property name doesn't clash with class name if (c.PropertyName == t.ClassName) { c.PropertyName = "_" + c.PropertyName; } } } return(result); } } catch (Exception x) { var error = x.Message.Replace("\r\n", "\n").Replace("\n", " "); warning(string.Format("Failed to read database schema - {0}", error)); writer.WriteLine(""); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine("// Failed to read database schema - {0}", error); writer.WriteLine("// -----------------------------------------------------------------------------------------"); writer.WriteLine(""); return(new Tables()); } }