5 ms·
"I've never used a tool that has a simple method for me to extend the ORM bits halfway through the query building process with some custom SQL" Well, I am not
by Spyro7 15y ago
"I've never used a tool that has a simple method for me to extend the ORM bits halfway through the query building process with some custom SQL"
Well, I am not sure if this is exactly what you're looking for, but Django's ORM allows you to write custom SQL and then collect the resulting rows as objects. You can also circumvent the object layer entirely and deal directly with the SQL. Take a look at this page:
https://docs.djangoproject.com/en/dev/topics/db/sql/ https://docs.djangoproject.com/en/dev/topics/db/sql/
I have found this functionality to be incredibly valuable for quite a few of the projects that I have worked on.
- dools 15y agoYeah most ORMs have a "custom SQL" option - what I mean is this (example in some non-existant language): function something(db_obj) print db_obj.getSql() #shows SQL built so far db_obj.having('SOMETHING = SOMETHING ELSE') db_obj.fromClause(this.bizarreCaseStatement() db_obj.order(this.weirdoOrderClauseThatReferencesCaseStatement()); return db_obj When I start working with db_obj, it might come from some other part of the application for which the "basic ORM" was just fine. Now I want to add some weirdo custom stuff in but I can't - I have to go back to the part of the application that produced db_obj in the first place and refactor that to use custom SQL. It's one or the other.
- mnutt 15y agoCheck out ARel. (it's in ActiveRecord 3, part of Rails) It uses relational algebra and is pretty good about letting you mix and match: enabled = User.where(:disabled => false) enabled.group("date(created_at)").having("created_at > ?", 1.month.ago).order(weird_order_clause).limit(5)
- dools 15y agoRight, yeah that's pretty nice!
- eropple 15y agoThe CodeIgniter ActiveRecord library follows the same pattern. Unfortunately, it's pretty kludgey and requires you to use CodeIgniter.
- Xurinos 15y agoDo you have an example that shows how to use ARel to do relational algebra?
- mnutt 15y agoI believe it just uses it under the hood, but I'm not familiar enough with relational algebra (or ARel) to know for sure. Here's a blog post from the author: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/ http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-...
- smunz 15y agoI think activerecord in rails 3 comestibles Odette close - except the modification of from.
- jcromartie 15y agoClojureQL (also based on relational algebra like the other comment) lets you compose bits and pieces of queries a lot like that.
- mcao 15y agoThat sounds like something I wrote. https://github.com/mikecao/sparrow https://github.com/mikecao/sparrow It's a simple class that builds and executes SQL, but you can customize query at any time.
- troels 15y agoThere's php-activerecord[1] or if you want something a bit different, do take a look at notorm[2]. I'd also take this opportunity to mention my own creation, pdoext[3]. [1] https://github.com/kla/php-activerecord https://github.com/kla/php-activerecord [2] https://github.com/vrana/notorm https://github.com/vrana/notorm [3] https://github.com/troelskn/pdoext https://github.com/troelskn/pdoext
- EGreg 15y agoYeah phponpie.com has that :) $u = new User(); $u->id = 69; $u->retrieve(true)->having( )->orderBy( )->resume(); something like that :)
- ehutch79 15y agodjango Q and F functions? they let you build funky where clauses. see: https://docs.djangoproject.com/en/1.3/topics/db/queries/#complex-lookups-with-q-objects https://docs.djangoproject.com/en/1.3/topics/db/queries/#com... https://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model https://docs.djangoproject.com/en/dev/topics/db/queries/#fil...