Coder Social home page Coder Social logo

sql2poco's Introduction

Sql2poco

Parses a sql script to generate POCOs for input parameters and for scalar, single or multiple resultsets. Triggered on the save event of a script file. See the Sql2Poco output pane for help.

##Usage

  • Create a sql file and add it to your project
  • Sql2Poco uses the last DSN connection in your config file (a la PetaPoco)
  • add the following lines to your sql file:
--Sql2Poco-Result Names()
--DECLARE Variables here
--Sql2Poco Begin Set Test Params
--SET Test Variables here
--Sql2Poco End Set Test Params
--Write SQL here and return at least 1 result set
  • Sql2Poco will use the sql file name appended with "Result" for the first result set or you can enter result names in the parens
  • Sql2Poco will generate ResultClasses, Sql Properties and TestParmam classes which you can use in your sql implementation of choice, for example here's some test code for PetaPoco:
            var db = new Database("DefaultConnection");
            using (new Sql2PocoWrapperDb(db))
            {
                var result = db.Fetch<CalendarResult>(CalendarScript.Sql, CalendarScript.TestParamValues);
                Assert.True(result.Count > 0);
            }

Which uses the Sql2PocoWrapperDb which enforces that EnableNamedParams and EnableAutoSelect are false:

    public class Sql2PocoWrapperDb : IDisposable
    {
        readonly bool _enableNamedParams;
        readonly bool _enableAutoSelect;
        readonly IDatabase _db;
        public Sql2PocoWrapperDb(IDatabase db)
        {
            _enableNamedParams = db.EnableNamedParams;
            _enableAutoSelect = db.EnableAutoSelect;
            _db = db;
            db.EnableNamedParams = false;
            db.EnableAutoSelect = false;
        }

        public void Dispose()
        {
            _db.EnableNamedParams = _enableNamedParams;
            _db.EnableAutoSelect = _enableAutoSelect;
        }
    }

Changes from the QueryFirst extension

  • Better support for DI injection of connection
  • vsix testability
  • Multi results
  • Params object for input model binding (partial)

sql2poco's People

Contributors

charlieknoll avatar

Watchers

James Cloos avatar  avatar

sql2poco's Issues

Petapoco multiple result set support

https://stackoverflow.com/questions/19576370/petapoco-multiple-result-set-support

Put the following in an "ExecSql" function:

                var result = new UserDetailResult();

                using (new Sql2PocoWrapperDb(_db))
                {
                    var paramVals = new UserDetailParams {UserName = userName}.GetParamValues();
                    using (var multi = _db.QueryMultiple(UserDetailScript.Sql,paramVals))
                    {
                        result.UserDetailRecordResults = multi.Read<UserDetailRecord>().ToList();
                        result.UserRoleRecordResults = multi.Read<UserRoleRecord>().ToList();
                    }
                }

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.