Now I'm using Slick with Spray. I have to say Slick works much nicer alone, non-disturbingly with Spray than with Play (which is really troublesome).
However, I still can't solve a huge problem: database construction.
If there a way for me to maybe pass a list of TableQuery to a function, and it will match variables I passed in with tables in the database, and only create ones that are not created?
That would be really neat.
Assume I have two tables:
val articles = TableQuery[ArticleTable]
val users = TableQuery[UserTable]
I'm creating a function that may look like this:
def createDatabase(list: List[TableQuery[*]]) {
//.... (something like: (Article.articles.ddl ++ User.users.ddl).create)
}
Something like someTableQuery.baseTableRow.tableName should give you the table name. MTable.apply allows you to query for tables. Github search the slick code for examples of MTable.
Related
I'm able to generate query for multi inserts or update thanks to pg-promise helpers but I was wondering if I could follow the advice of the author and put all queries outside of my javascript code (See here https://github.com/vitaly-t/pg-promise/wiki/SQL-Files and here : https://github.com/vitaly-t/pg-promise-demo).
When I use the insert helpers, the return query looks like :
INSERT INTO "education"("candidate_id","title","content","degree","school_name","start_date","still_in","end_date","picture_url") VALUES('6','My degree','Business bachelor','Bachelor +','USC','2018-05-15T02:00:00.000+02:00'::date,false,null::date,null),('6','Another degree','Engineering','Master degree','City University','2018-05-15T02:00:00.000+02:00'::date,false,null::date,null)
The idea is that I don't know how many inserts I want to do at the same time, so it has to be dynamic.
The following code doesn't work as I'm passing an array of object instead of an object :
db.none(`INSERT INTO "education"("candidate_id","title","content","degree","school_name","start_date","still_in","end_date","picture_url")
VALUES($<candidate_id>, $<title>, $<content>, $<degree>, $<school_name>, $<start_date>, $<still_in>, $<end_date>, $<picture_url>)`, data)
This code spreads the object but is still not correct to make a proper query :
db.none(`INSERT INTO "education"("candidate_id","title","content","degree","school_name","start_date","still_in","end_date","picture_url")
VALUES($1:list)`,
[data])
Any idea ? Is it at least possible or in the case where I don't know how many records I want to insert in advance I have to call pgp.helpers everytime ?
You confuse static and dynamic SQL. SQL files are there for SQL queries that are mainly static, i.e. you still can inject dynamically a lot, but when most of the query is dynamic, there is no longer any point putting it into an SQL file.
And the helpers namespace is there for dynamic queries only. So you are asking about two separate things, to join things that do not need to be joined.
I find myself doing a lot of queries to fetch just the first couple of items of a big set, e.g. to show the three most recent news articles or blog posts on the homepage of a website.
As long as this query only involves predefined or custom Parts, I can do something like this:
public IEnumerable<ContentItem> GetTopArticles(int amount)
{
var cultureRecord = _cultureManager.GetCultureByName(_orchardServices.WorkContext.CurrentCulture);
var articles = _orchardServices.ContentManager.Query().ForType("Article")
.Where<LocalizationPartRecord>(lpr => lpr.CultureId == cultureRecord.Id)
.OrderBy<CommonPartRecord>(cpr => cpr.PublishedUtc)
.Slice(0, amount);
return articles;
}
I'm assuming this will more or less be the same as a SELECT TOP [amount] ... in SQL and will have good performance on a large number of records.
However, sometimes I use Migrations or Import to create Content Types from an external source and want to conditionally check a field from the generic Part. In this case I don't have a Part or PartRecord class that I can pass as a parameter to the ContentQuery methods and if I want to do a conditional check on any of the fields I currently do something like this:
public IEnumerable<ContentItem> GetTopArticles(int amount)
{
var articles = _orchardServices.ContentManager.Query().ForType("Article")
.OrderBy<CommonPartRecord>(cpr => cpr.PublishedUtc)
.List()
.Where(a => a.Content.Article.IsFeatured.Value == true)
.Take(amount);
return articles;
}
This is really wasteful and causes large overhead on big sets but I really, REALLY, do not want to delve into the database to figure out Orchard's inner workings and construct long and complex HQL queries every time I want to do something like this.
Is there any way to rewrite the second query with IContentQuery methods without incurring a large performance hit?
I'm working on something similar (being able to query model data with a dynamic name). Sadly, I haven't found anything that makes it easy.
The method I've found that works is to do plain SQL queries against the database. Check out this module for syntax on that if you do later find yourself willing to delve into the database.
Is it possible to apply a function like LookUpRows or Lookup to an array created with BuildRowSetFromString?
I have this:
SET #rowSet = BuildRowSetFromString(#ItemsString2, '|')
I'd like to know if there's a function on which I can do:
SET #var = LookupRows(#rowSet, ITEM_ID, ... )
I am trying already using a FOR loop. I want to know if there's a function that can do this.
No. I wish.
Best bet would be to use arrays in Server-Side JavaScript or possibly GTL.
If you want to over-engineer it, you can use XML and XPATH to do some array functions in AMPScript. I've written up a use-case with examples here on my personal blog.
Also, there is a lot more SFMC dicussion going on over in http://salesforce.stackexchange.com.
This is very very very frustrating. I have been trying to pick up Slick for a while, and obstacles just keep coming. The concept of Slick is really awesome, but it is very difficult to learn, and unlike Scala, it doesn't have "beginner", "intermediate", and "advanced" style where people in all stages can use it easily.
I'm using Play-Slick (Slick 2.0.0) https://github.com/freekh/play-slick, following its Multi-DB cake example: https://github.com/freekh/play-slick/tree/master/samples/play-slick-cake-sample/app
For some reason, first, ddl does not belong to TableQuery, unlike the claim in the document: "The TableQuery‘s ddl method creates DDL". This shows through the scaladoc: http://slick.typesafe.com/doc/2.0.0/api/#scala.slick.lifted.TableQuery There is no ddl method there.
Second, my slick.lifted.Query can't generate delete method. It works fine with list, but not with delete.
val S3Files = TableQuery[S3Files]
S3Files.where(_.url === url).delete
This wouldn't work...then I tried:
val query = (for(s <- S3Files if s.url === url) yield s)
query.list //this works
query.delete //ehh?? can't find the method
val query2 = (for(s <- S3Files if s.url === url))
query2.delete //still won't work
Well...since Slick uses a very complicated (at least to newbies) implicit type conversion system, I don't really know what went wrong.
I tried it by simply adding
Cats.ddl.create
Cats.filter(_.name===cat.name).delete
to play-slick-cake-sample/app/controllers/Application.scala. Works fine for me.
Looks like you are using the wrong imports. Look at https://github.com/freekh/play-slick/blob/master/samples/play-slick-sample/app/controllers/Application.scala and mimic the imports.
slick 0.8.1 and slick 2.1.0 and I had the same Issue.
The reason why delete is not available on the Query is cause the play-slick Query does not contain a equivalent method of the delete method from slick Query.
I solved this Problem by changing to the original slick Driver
//import play.api.db.slick.Config.driver.simple._ //play-slick extensional Driver
import slick.driver.PostgresDriver.simple._ //original slick Driver
I've defined a map function in CouchDB that produces data that looks like this:-
var data =
{"total_rows":10,"offset":0,"rows":[
{"id":"338b79f07dfe8b3877b3aa41a5bb8a58","key":"2000-06-23T23:59:00+00:00","value":{"country":"United States"}},
{"id":"338b79f07dfe8b3877b3aa41a5bb983e","key":"2000-06-23T23:59:00+00:00","value":{"country":"Norway"}},
{"id":"338b79f07dfe8b3877b3aa41a5ddfefe","key":"2000-06-23T23:59:00+00:00","value":{"country":"Hungary"}},
{"id":"338b79f07dfe8b3877b3aa41a5fe29d7","key":"2000-06-23T23:59:00+00:00","value":{"country":"United States"}},
{"id":"b6ed02fb38d6506d7371c419751e8a14","key":"2000-06-23T23:59:00+00:00","value":{"country":"Germany"}},
{"id":"b6ed02fb38d6506d7371c419753e20b6","key":"2000-06-23T23:59:00+00:00","value":{"country":"Hungary"}},
{"id":"b6ed02fb38d6506d7371c419755f34ad","key":"2000-06-23T23:59:00+00:00","value":{"country":"United States"}},
{"id":"b6ed02fb38d6506d7371c419755f3e17","key":"2000-06-23T23:59:00+00:00","value":{"country":"Germany"}},
{"id":"338b79f07dfe8b3877b3aa41a506082f","key":"2003-01-08T19:34:00+00:00","value":{"country":"United Kingdom"}},
{"id":"9366afb036bf8b63c9f45379bbe29509","key":"2003-01-08T19:34:00+00:00","value":{"":"United Kingdom"}}
]
}
I'm now trying to create reduce function that produces data that looks like this:-
{"rows":[
{"key":"United States","value":2},
{"key":"Norway","value":1},
{"key":"Hungary","value":2},
{"key":"Germany","value":2}
{"key":"United Kingdom","value":1}
]}
I need to the ability to use the "StartKey" and "EndKey" parameters in couch to define the date ranges. This is proving trickier than I expected. Can somebody show me what my reduce function should look like to handle this?
Regards,
Carlskii
I'm afraid there is a common anti-pattern of using map reduce.
Please read Example 3, “Don’t use this, it’s an example broken on purpose”