Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ packages/
.vscode/
.vs/
*.exe
/src/.idea
/src/SimpleQuery.sln.DotSettings.user
2 changes: 1 addition & 1 deletion src/SimpleQuery.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</configSections>
<connectionStrings>
<add name="hana" connectionString="Server=192.168.1.200:30015;UID=SYSTEM;Password=Qt7k7dp=y;Current Schema=SYSTEM" />
<add name="sqlserver" connectionString="server=172.16.26.54\B2; database=master; user id=sa; password=sap@123" />
<add name="sqlserver" connectionString="server=localhost\B1; database=DBTeste; user id=sa; password=sap@123" />
<add name="mysql" connectionString="User ID=root;Password=sap@123;Host=localhost;Port=3306;Database=sys;Protocol=TCP;Compress=false;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;" />
<add name="postgres" connectionString="Server = localhost;Port=5432;Database=postgres;User Id=postgres;Password=sap@123; " />
<add name="maxdb" connectionString="DATA SOURCE=localhost;INITIAL CATALOG=MAXDB;PASSWORD=sap@123;USER ID=DBADMIN;"/>
Expand Down
56 changes: 55 additions & 1 deletion src/SimpleQuery.Tests/UnitTestDialectHana.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,24 @@ public void TestInsertOperationHana()
[TestMethod]
public void TestSelectOperationHana()
{
IScriptBuilder builder = new ScriptHanaBuilder();
var hanaConnection = System.Data.Common.DbProviderFactories.GetFactory("Sap.Data.Hana").CreateConnection();
hanaConnection.ConnectionString = ConfigurationManager.ConnectionStrings["hana"].ConnectionString;
hanaConnection.Open();

try
{
builder.Execute("drop table \"Cliente\"", hanaConnection);
}
catch (Exception)
{

}

var trans = hanaConnection.BeginTransaction();
using (var conn = hanaConnection)
{
IScriptBuilder builder = new ScriptHanaBuilder();


var cliente = new Cliente() { Id = 1, Nome = "Moisés", Ativo = true };
var cliente2 = new Cliente() { Id = 2, Nome = "José", Ativo = true };
Expand All @@ -200,6 +211,49 @@ public void TestSelectOperationHana()
}

[TestMethod]
public void TestSelectOperationHanaWithQuery()
{
IScriptBuilder builder = new ScriptHanaBuilder();
var hanaConnection = System.Data.Common.DbProviderFactories.GetFactory("Sap.Data.Hana").CreateConnection();
hanaConnection.ConnectionString = ConfigurationManager.ConnectionStrings["hana"].ConnectionString;
hanaConnection.Open();

try
{
builder.Execute("drop table \"Cliente\"", hanaConnection);
}
catch (Exception)
{

}

var trans = hanaConnection.BeginTransaction();
using (var conn = hanaConnection)
{


var cliente = new Cliente() { Id = 1, Nome = "Moisés", Ativo = true };
var cliente2 = new Cliente() { Id = 2, Nome = "José", Ativo = true };

var createTableScript = builder.GetCreateTableCommand<Cliente>();
var insertScript1 = builder.GetInsertCommand<Cliente>(cliente);
var insertScript2 = builder.GetInsertCommand<Cliente>(cliente2);
builder.Execute(createTableScript, conn);
builder.Execute(insertScript1, conn);
builder.Execute(insertScript2, conn);

var clientes = conn.Select<Cliente>("Select * From \"Cliente\" Order By \"Id\"");
Assert.AreEqual(2, clientes.Count());
Assert.AreEqual("Moisés", clientes.ToList()[0].Nome);
Assert.AreEqual("José", clientes.ToList()[1].Nome);

trans.Rollback();
builder.Execute("drop table \"Cliente\"", hanaConnection);
}
}


[TestMethod]
public void TestHanaCreateTableScript()
{
IScriptBuilder builder = new ScriptHanaBuilder();
Expand Down
42 changes: 42 additions & 0 deletions src/SimpleQuery.Tests/UnitTestDialectSQLServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,48 @@ public void TestSelectOperationSqlServer()
}
}
}



[TestMethod]
public void TestSelectOperationHanaWithQuery()
{
IScriptBuilder builder = new ScriptSqlServerBuilder();
var sqlConnection = new SqlConnection(connstring);
sqlConnection.Open();

try
{
builder.Execute("drop table \"Cliente\"", sqlConnection);
}
catch (Exception)
{

}

using (var conn = sqlConnection)
{


var cliente = new Cliente() { Id = 1, Nome = "Mois�s", Ativo = true };
var cliente2 = new Cliente() { Id = 2, Nome = "Jos�", Ativo = true };

var createTableScript = builder.GetCreateTableCommand<Cliente>();
var insertScript1 = builder.GetInsertCommand<Cliente>(cliente);
var insertScript2 = builder.GetInsertCommand<Cliente>(cliente2);
builder.Execute(createTableScript, conn);
builder.Execute(insertScript1, conn);
builder.Execute(insertScript2, conn);

var clientes = conn.Select<Cliente>("Select * From \"Cliente\" Order By \"Id\"");
Assert.AreEqual(2, clientes.Count());
Assert.AreEqual("Mois�s", clientes.ToList()[0].Nome);
Assert.AreEqual("Jos�", clientes.ToList()[1].Nome);

builder.Execute("drop table \"Cliente\"", sqlConnection);
}
}

public string connstring => ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;


Expand Down
9 changes: 9 additions & 0 deletions src/SimpleQuery.Tests/UnitTestExtentionsSQLServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public void SQLServerUpdateModel()
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString);
var scriptBuilder = conn.GetScriptBuild();

try
{
scriptBuilder.Execute("drop table \"Cliente\"", conn);
}
catch (Exception)
{

}

var cliente = new Cliente() { Nome = "Miranda" };

var createTableScript = scriptBuilder.GetCreateTableCommand<Cliente>();
Expand Down
5 changes: 5 additions & 0 deletions src/SimpleQuery/Data/Dialects/ScriptAnsiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ private string GetSequenceName(string entityName, string columnName)
return $"sequence_{entityName}_{columnName}".ToLower();
}

public string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new()
{
throw new NotImplementedException();
}

public string GetDeleteCommand<T>(T obj, object key) where T : class, new()
{
var allProperties = obj.GetType().GetProperties();
Expand Down
7 changes: 7 additions & 0 deletions src/SimpleQuery/Data/Dialects/ScriptCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,12 @@ internal static bool IsPrimitive(Type t)
typeof(DateTime?)
}.Contains(t);
}

protected string GetCountqueryComand<T>(T obj) where T : class, new()
{
var entityName = GetEntityName<T>();

return $"select * from \"{entityName}\" ";
}
}
}
14 changes: 13 additions & 1 deletion src/SimpleQuery/Data/Dialects/ScriptHanaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Execute(string commandText, IDbConnection dbConnection, IDbTransacti
var wasClosed = dbConnection.State == ConnectionState.Closed;
if (wasClosed) dbConnection.Open();
var rowsCount = command.ExecuteNonQuery();
var dataTable = new DataTable();

Console.WriteLine($"{rowsCount} affected rows");
if (wasClosed) dbConnection.Close();
}
Expand Down Expand Up @@ -272,6 +272,18 @@ public object GetLastId<T>(T model, IDbConnection dbConnection, IDbTransaction t
var where = GetWhereCommand<T>(expression);
return select + " " + where;
}

public string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new()
{
var select = GetCountqueryComand<T>(obj);

if (expression is null)
{
return select;
}

return select + " " + GetWhereCommand<T>(expression);;
}

public Tuple<string, IEnumerable<DbSimpleParameter>> GetInsertCommandParameters<T>(T obj, bool includeKey = false) where T : class, new()
{
Expand Down
7 changes: 6 additions & 1 deletion src/SimpleQuery/Data/Dialects/ScriptMySqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Execute(string commandText, IDbConnection dbConnection, IDbTransacti
var wasClosed = dbConnection.State == ConnectionState.Closed;
if (wasClosed) dbConnection.Open();
var rowsCount = command.ExecuteNonQuery();
var dataTable = new DataTable();

Console.WriteLine($"{rowsCount} affected rows");
if (wasClosed) dbConnection.Close();
}
Expand Down Expand Up @@ -116,6 +116,11 @@ private object GetTypeMySql(PropertyInfo item)
}
}

public string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new()
{
throw new NotImplementedException();
}

public string GetDeleteCommand<T>(T obj, object key) where T : class, new()
{
var allProperties = obj.GetType().GetProperties();
Expand Down
7 changes: 6 additions & 1 deletion src/SimpleQuery/Data/Dialects/ScriptPostGresBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Execute(string commandText, IDbConnection dbConnection, IDbTransacti
var wasClosed = dbConnection.State == ConnectionState.Closed;
if (wasClosed) dbConnection.Open();
var rowsCount = command.ExecuteNonQuery();
var dataTable = new DataTable();

Console.WriteLine($"{rowsCount} affected rows");
if (wasClosed) dbConnection.Close();
}
Expand Down Expand Up @@ -91,6 +91,11 @@ private string GetSequenceName(string entityName, string columnName)
return $"sequence_{entityName}_{columnName}".ToLower();
}

public string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new()
{
throw new NotImplementedException();
}

public string GetDeleteCommand<T>(T obj, object key) where T : class, new()
{
var allProperties = obj.GetType().GetProperties();
Expand Down
21 changes: 19 additions & 2 deletions src/SimpleQuery/Data/Dialects/ScriptSqlServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ public IDataReader ExecuteReader(string commandText, IDbConnection dbConnection,
public void Execute(string commandText, IDbConnection dbConnection, IDbTransaction dbTransaction = null)
{
var command = dbConnection.CreateCommand();

if (dbTransaction != null)
command.Transaction = dbTransaction;

command.CommandText = commandText;

var wasClosed = dbConnection.State == ConnectionState.Closed;
if (wasClosed) dbConnection.Open();

if (wasClosed)
dbConnection.Open();

var rowsCount = command.ExecuteNonQuery();
var dataTable = new DataTable();

Console.WriteLine($"{rowsCount} affected rows");
if (wasClosed) dbConnection.Close();
}
Expand Down Expand Up @@ -158,6 +163,18 @@ public object GetLastId<T>(T model, IDbConnection dbConnection, IDbTransaction t
return sql;
}

public string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new()
{
var select = GetCountqueryComand<T>(obj);

if (expression is null)
{
return select;
}

return select + " " + GetWhereCommand<T>(expression);;
}

public string GetUpdateCommand<T>(T obj) where T : class, new()
{
var allProperties = ScriptCommon.GetValidProperty<T>();
Expand Down
5 changes: 5 additions & 0 deletions src/SimpleQuery/Data/Dialects/ScriptSqliteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ private string GetTypeSqlite(PropertyInfo item)
}
}

public string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new()
{
throw new NotImplementedException();
}

public string GetDeleteCommand<T>(T obj, object key) where T : class, new()
{
var allProperties = obj.GetType().GetProperties();
Expand Down
9 changes: 9 additions & 0 deletions src/SimpleQuery/Domain/Data/Dialects/IScriptBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public interface IScriptBuilder
/// <param name="obj">Instance model</param>
/// <returns></returns>
string GetSelectCommand<T>(T obj) where T : class, new();


/// <summary>
/// Return table name based in Table Attribute or Class name
Expand All @@ -55,6 +56,14 @@ public interface IScriptBuilder
/// <param name="obj">Instance model</param>
/// <returns></returns>
string GetSelectCommand<T>(T obj, Expression<Func<T, bool>> expression) where T : class, new();

/// <summary>
/// Return count from instance model
/// </summary>
/// <typeparam name="T">Class type</typeparam>
/// <param name="obj">Instance model</param>
/// <returns></returns>
string GetCountCommand<T>(T obj, Expression<Func<T, bool>> expression = null) where T : class, new();

/// <summary>
/// Return delete command from instance model
Expand Down
Loading