GithubHelp home page GithubHelp logo

Database.Exists is not optimal about npoco HOT 4 CLOSED

zii-dmg avatar zii-dmg commented on August 20, 2024
Database.Exists is not optimal

from npoco.

Comments (4)

zii-dmg avatar zii-dmg commented on August 20, 2024

Attempt to use GetExistsSql + custom WHERE overload + expression WHERE overload. Tested only with non-composite primary key on MySql.

public bool ExistsById<T>(object primaryKey) // renamed old Exists(object primaryKey) method
{
    var index = 0;
    var pd = PocoData.ForType(typeof(T), PocoDataFactory);
    var tableName = DatabaseType.EscapeTableName(pd.TableInfo.TableName);
    var pkValues = GetPrimaryKeyValues(pd.TableInfo.PrimaryKey, primaryKey);
    var pkSql = BuildPrimaryKeySql(pkValues, ref index);
    var sql = string.Format(_dbType.GetExistsSql(), tableName, pkSql);
    var args = pkValues.Select(x => x.Value).ToArray();
    return ExecuteScalar<int>(sql, args) != 0;
}

public bool Exists<T>(Sql sql)
{
    return Exists<T>(sql.SQL, sql.Arguments);
}

public bool Exists<T>(string sql, params object[] args)
{
    var pd = PocoData.ForType(typeof(T), PocoDataFactory);
    var tableName = DatabaseType.EscapeTableName(pd.TableInfo.TableName);

    if (sql.StartsWith("WHERE", StringComparison.InvariantCultureIgnoreCase))
    {
        sql = sql.Substring(5);
    }

    var fullSql = string.Format(_dbType.GetExistsSql(), tableName, sql);
    return ExecuteScalar<int>(fullSql, args) != 0;
}

public bool Exists<T>(Expression<Func<T, bool>> expression)
{
    var ev = _dbType.ExpressionVisitor<T>(this);
    var sql = ev.Where(expression).Context.ToWhereStatement();
    var args = ev.Context.Params.ToArray();
    return Exists<T>(sql, args);
}

from npoco.

mabakay avatar mabakay commented on August 20, 2024

Is it and when if is planed to commit this code to branch?

from npoco.

schotime avatar schotime commented on August 20, 2024

The Exists has been changed to use the Count(*) sql, and the expression based exists is now available via the Query().Any() method.
I'm still thinking about the middle two. Might not be as necessary if you have the linq way.
Now available on Nuget 2.4.60+

from npoco.

zii-dmg avatar zii-dmg commented on August 20, 2024

As code is now using GetExistsSql I think I can close this.

from npoco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.