Giter Club home page Giter Club logo

Comments (13)

MKuckert avatar MKuckert commented on September 13, 2024 2

Should be pretty simple to leverage the ExpandoObject

from sqlite-net.

rgl avatar rgl commented on September 13, 2024 1

Why did you close this issue @tofutim ?

@praeclarum Can you consider this? Maybe using dynamics?

from sqlite-net.

tofutim avatar tofutim commented on September 13, 2024

I added the following to my version of SQLite. If anyone has any comments on improvements or gotchas, they are much appreciated. To SQLiteCommand:

    public List<Dictionary<string, object>> ExecuteQuery()
    {
        return ExecuteDeferredQuery().ToList();
    }

   public IEnumerable<Dictionary<string, object>> ExecuteDeferredQuery()
    {
        if (_conn.Trace)
        {
            Debug.WriteLine("Executing Query: " + this);
        }

        var stmt = Prepare();
        try
        {
            var cols = new string[SQLite3.ColumnCount (stmt)];

            for (int i = 0; i < cols.Length; i++) {
                var name = SQLite3.ColumnName16 (stmt, i);
                cols [i] = name;
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = new Dictionary<string, object>();
                for (int i = 0; i < cols.Length; i++)
                {
                    var colType = SQLite3.ColumnType(stmt, i);

                    Type targetType;
                    switch (colType)
                    {
                        case SQLite3.ColType.Text:
                            targetType = typeof(string);
                            break;
                        case SQLite3.ColType.Integer:
                            targetType = typeof(int);
                            break;
                        case SQLite3.ColType.Float:
                            targetType = typeof(double);
                            break;
                        default:
                            targetType = typeof(object);
                            break;
                    }

                    var val = ReadCol(stmt, i, colType, targetType);
                    obj.Add(cols[i], val);
                }
                OnInstanceCreated(obj);
                yield return obj;
            }
        }
        finally
        {
            SQLite3.Finalize(stmt);
        }
    }

from sqlite-net.

marcucio avatar marcucio commented on September 13, 2024

Sorry if this seems ignorant but I'm new to c#... how would you call this function, currently I am using:

var results = db.Query<Dictionary<string, object>>(query, query_params);

And it only calls this function not yours:

    public List<T> ExecuteQuery<T> () where T : new()
    {
        return ExecuteDeferredQuery<T>(_conn.GetMapping(typeof(T))).ToList();
    }

from sqlite-net.

marcucio avatar marcucio commented on September 13, 2024

Just an update, to work around this I made this function to call your functions:

    public List<Dictionary<string, object>> Query2(string query, params object[] args)
    {
        var cmd = CreateCommand(query, args);
        return cmd.ExecuteQuery();
    }

and call it with

var results = db.Query2(query, query_params);

maybe there is a better way to do this?

from sqlite-net.

jameshowe avatar jameshowe commented on September 13, 2024

This was exactly what I was looking for! Created a new issue #255 before I came across this issue. If you have been using this code without issue for while maybe you could use your post as a pull request to get it into master?

from sqlite-net.

gartmeier avatar gartmeier commented on September 13, 2024

Hi @tofutim. Thanks you for the great code

from sqlite-net.

dazinator avatar dazinator commented on September 13, 2024

Is there anyone that can get this into a new NuGet release?

from sqlite-net.

praeclarum avatar praeclarum commented on September 13, 2024

Yes this does seem like a reasonable feature. I think I would prefer dynamic over dictionary though.

from sqlite-net.

 avatar commented on September 13, 2024

@tofutim, I love you, man!

Putting the code in a partial SQLiteCommand implementation safely extends the class without the risk of loosing the changes after the sqlite.net module update/reinstall.

from sqlite-net.

bill2004158 avatar bill2004158 commented on September 13, 2024

@praeclarum

how about this? if no problem, can you add to master branch?

public partial class SQLiteConnection
{
	public List<ExpandoObject> Query(string query, params object[] args)
	{
		var cmd = CreateCommand (query, args);
		return cmd.ExecuteQuery();
	}
}

public partial class SQLiteCommand
{
	public List<ExpandoObject> ExecuteQuery() {
		return ExecuteDeferredQuery().ToList();
	}

	private IEnumerable<ExpandoObject> ExecuteDeferredQuery () {
		if (_conn.Trace) {
			_conn.Tracer?.Invoke ("Executing Query: " + this);
		}

		var stmt = Prepare ();
		try {
			var cols = new string[SQLite3.ColumnCount (stmt)];

			for (var i = 0; i < cols.Length; i++) {
				var name = SQLite3.ColumnName16 (stmt, i);
				cols[i] = name;
			}

			while (SQLite3.Step (stmt) == SQLite3.Result.Row) {
				var obj = new ExpandoObject() as IDictionary<string, object>;
				for (var i = 0; i < cols.Length; i++) {
					var colType = SQLite3.ColumnType (stmt, i);

					var val = ReadCol (stmt, i, colType, MapToCSharpType(colType));
					obj[cols[i]] = val;
				}
				OnInstanceCreated (obj);
				yield return (ExpandoObject) obj;
			}
		}
		finally {
			SQLite3.Finalize (stmt);
		}
	}

	private Type MapToCSharpType (SQLite3.ColType colType)
	{
		Type targetType;
		switch (colType) {
			case SQLite3.ColType.Text:
				targetType = typeof (string);
				break;
			case SQLite3.ColType.Integer:
				targetType = typeof (int);
				break;
			case SQLite3.ColType.Float:
				targetType = typeof (double);
				break;
			case SQLite3.ColType.Blob:
				targetType = typeof (byte[]);
				break;
			default:
				targetType = typeof (object);
				break;
		}

		return targetType;
	}
}

from sqlite-net.

lapoPaolacci avatar lapoPaolacci commented on September 13, 2024

with this method if I do complex query is very slow and device crash

from sqlite-net.

rogerskk avatar rogerskk commented on September 13, 2024

I have a select query that I need to use a dynamic type with. I am trying
var result= _database.Query<Dictionary<string,object>>(query);
This returns the correct number in the list, but the keys and values are blank. Is there a different way I should be running this to get the dynamic results?

from sqlite-net.

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.