// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.Common; namespace Spludlow.Data { public static class DAL { public class CommandInfo { public CommandInfo() { } public bool Identity; public string[] ColumnNames; public string[] ParameterNames; public int[] ParameterTypes; public int[] ParameterMaxLengths; public string CommandText; } public static IDAL Create(string connectionString) { if (connectionString.StartsWith("@") == true) connectionString = Spludlow.Config.ConnectionString(connectionString.Substring(1)); string databaseType = "sql"; if (connectionString.StartsWith("[") == true) { int index = connectionString.IndexOf("]"); if (index == -1) throw new ApplicationException("DAL Create; Can't find closing square bracket in connection string. Usage [dbtype] Key1=Value1;Key2=Value2;"); databaseType = connectionString.Substring(1, index - 1).Trim().ToLower(); connectionString = connectionString.Substring(index + 1).Trim(); } string[] assType; switch (databaseType) { case "sql": assType = new string[] { "Spludlow", "Spludlow.Data.DALSql" }; break; case "odbc": assType = new string[] { "Spludlow", "Spludlow.Data.DALODBC" }; break; case "oledb": assType = new string[] { "Spludlow", "Spludlow.Data.DALOleDb" }; break; case "mysql": assType = new string[] { "Spludlow.Data.MySQL", "Spludlow.Data.DALMySQL" }; break; case "sqlite": assType = new string[] { "Spludlow.Data.SQLite", "Spludlow.Data.DALSQLite" }; break; case "oracle": assType = new string[] { "Spludlow.Data.Oracle", "Spludlow.Data.DALOracle" }; break; case "postgresql": assType = new string[] { "Spludlow.Data.PostgreSQL", "Spludlow.Data.DALPostgreSQL" }; break; case "db2": assType = new string[] { "Spludlow.Data.DB2", "Spludlow.Data.DALDB2" }; break; case "dbase": assType = new string[] { "Spludlow.Data.dBase", "Spludlow.Data.DALdBase" }; break; default: throw new ApplicationException("DAL Create; Unknown databse type: " + databaseType); } return (IDAL)Spludlow.Reflections.MakeInstance(assType[0], assType[1], new object[] { connectionString }); } public static DbParameter AddParameter(DbCommand command, string name, object data) { DbParameter parameter = command.CreateParameter(); parameter.ParameterName = name; parameter.Value = data; command.Parameters.Add(parameter); return parameter; } } }