How do I specify a Postgresql schema in ScalaQuery? - scalaquery

I tried, for instance:
object WebCache extends Table[(...)]("myschema.mytable") {
...
}
But that doesn't work.

Slick now has this functionality:
object WebCache extends Table[(...)](Some("myschema"), "mytable") {
...
}
Should work.

Got an answer from the scalaquery mailing list. This is a limitation of ScalaQuery.
This is not supported at the moment but it should be very easy to add.
I've created https://github.com/szeiger/scala-query/issues/19 for this
issue.

Related

Initialize JsonB configuration once

I am customizing Jsonb using a ContextResolver<Jsonb> as in the example below.
It works correctly, but I have seen that the method gets invoked on every call which seems to me a waste of performance. ¿Isn't there a way to initialize Jsonb only once?
#Override
public Jsonb getContext(Class type) {
final JsonbConfig config = new JsonbConfig()
.withDateFormat(dateFormat, Locale.getDefault())
.withSerializers(
new UserSerializer(),
new PaperSerializer()
);
return JsonbBuilder.create(config);
}
PS: I am aware I can setup serializers using #JsonbTypeSerializer. I am not looking for alternative ways to do the same configuration.
You need to store the instance of Jsonb in your context resolver yourself. Probably with a volatile and a double checked locking to be on the safe side.
I'm not entirely sure if it's a bug or a feature (i.e. if it's the desired behavior that you have to do it yourself and that it's delegated to the ContextResolver for each call).
See the implementation here: https://github.com/resteasy/Resteasy/blob/master/providers/json-binding/src/main/java/org/jboss/resteasy/plugins/providers/jsonb/AbstractJsonBindingProvider.java#L27 .
I'll check with the RESTEasy developers.
There is now a new way to customize JSON-B with Quarkus, it is documented here: https://quarkus.io/guides/rest-json-guide#configuring-json-support
So the correct answer is now to implements a JsonbConfigCustomizer.
The following should works for your use case:
import io.quarkus.jsonb.JsonbConfigCustomizer;
import javax.inject.Singleton;
import javax.json.bind.JsonbConfig;
import javax.json.bind.serializer.JsonbSerializer;
#Singleton
public class MyJsonbConfigCustomizer implements JsonbConfigCustomizer {
public void customize(JsonbConfig config) {
config.withDateFormat(dateFormat, Locale.getDefault())
.withSerializers(
new UserSerializer(),
new PaperSerializer()
);
}
}

TYPO3 extbase: get some from FrontendUserGroupRepository

In a class file I can get all records from another repository that is not mine
$allUsergroups = $this->feGroupRepository->findAll();
How to make custom function to acomplish something like this on such a repository in the most correct way?
// magic default function that takes a uid list (or array) as argument
$someUsergroups = $this->feGroupRepository->findSomeByUidList('2,4,6,8');
Or can I extent an existing repository with my own custom functions, in this case based on $query->in(list)?
You can create your own method in your extensionRepository.php class
you can use :
in($propertyName, $operand)
or
contains($propertyName, $operand)
Contrarily, the methods in() and contains() accept multi-value data types as arguments (e.g. Array, ObjectStorage).
take a look how some other extension are doing stuff. (like the tx_news extension)
or read some docs here :
https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html
Yes, you can extend another class in TYPO3 without any need to change any other code. It´s called Dependency Injection in ExtBase context.
First, create a new repository class your_ext/Classes/Domain/Repository/FrontendUserRepository.php and add below content to it:
<?php
namespace Tillebeck\YourExt\Domain\Repository;
class FrontendUserRepository extends \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository {
/**
* #param array $uidList
* #return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByUidList(Array $uidList)
{
$query = $this->createQuery();
//$query->getQuerySettings()->setRespectStoragePage(false);
$query->matching(
$query->in('uid', $uidList)
);
return $query->execute();
}
/**
* #return string
*/
protected function getRepositoryClassName()
{
return get_parent_class($this);
}
}
Here we have implemented your method findByUidList with the required argument $uidList which needs to be an array.
Because repositories resolve their model names by their own class name, we need to change the method getRepositoryClassName to return the parent class name, in this case TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository.
But this alone won't work. We need to tell ExtBase that every time we inject or initialize a TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository, either by PHPDocBlock annotation #inject or by the objectManager->get, then we really want to initialize our new repository. This is done in TypoScript.
config.tx_extbase.objects {
TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository {
className = Tillebeck\YourExt\Domain\Repository\FrontendUserRepository
}
}
You can also restrict your change to your own extension alone by replacing config.tx_extbase with plugin.tx_yourext.
Last step: clear ALL cache, and possibly delete all files in typo3temp directory.
Now in your controller (or other class) you can run below code.
$uidList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', '2,4,6,8', true);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(
$this->frontendUserRepository->findByUidList($uidList)
);
I have tested above solution in TYPO3 7.6 and it works. Dependency Injection has existed since version 6.1.
This is by my definition the must correct way, as you asked, to implement this feature in your own TYPO3 extension.

Yii passing parameters to widget

Sorry if I am using wrong keyword. Might be I did not get sufficient information due to wrong keyword.
we create widget in Yii by this way:
class streamList extends CWidget {
//do some stuff
}
Also, we use this widget anywhere as
$this->widget("application.components.widget.streamList");
How can we write the widget in such a way that it accepts parameter(s) as
$this->widget("application.components.widget.streamList",array('title'=>'Posts');
I googled but did not solve. Please help, thank in advance.
Edit log:
Also, how can we define default parameter value to 'titile'? I tried public $title = Shk::getTitle(), but it did not work.
Use
class StreamList extends CWidget {
//do some stuff
public $title;
}
Any attributes can be initialized with default values and overwritten by
$this->widget("application.components.widget.StreamList",array('title'=>'Posts',.....)
EDIT
You can't initialize class attributes with functions. An explanation is given here. An option is to check whether $title is set and if not set it to Shk::getTitle() in the init() method like
public function init(){
....
if(!isset($this->title) || !$this->title)
$this->title=Shk::getTitle();
....
}
P.S for consistency it's better to Capitalize your class names.

Is there anyway to get the class name that an instance is injected into (using ninject)?

I'm injecting my dependencies into my classes fine, but I'm wondering if it's possible to get the class name I'm injecting into?
For example:
Bind<ISomething>.ToMethod(c => new Something([GIVE INJECTING *TO* CLASS NAME]));
So, if I had:
public class Blah{
public Blah(ISomething something) { /**/ }
}
When injecting Ninject would in effect call:
new Blah(new Something("Blah"));
Can this be done?
Yes, it can be done. You use the IContext you're given in the ToMethod method to get the name of the type you're being injected into like this:
Bind<ISomething>().ToMethod(c => new Something(GetParentTypeName(c)));
Which uses this little helper method (which could also be turned into a nice extension method):
private string GetParentTypeName(IContext context)
{
return context.Request.ParentRequest.ParentRequest.Target.Member.DeclaringType.Name;
}
It has probably changed in later versions of Ninject. As for version v3.2.0 the accepted solution didn't work for me.
The following does though:
Bind<ISomething>().ToMethod((ctx)
=> new Something(ctx.Request.Target?.Member?.DeclaringType?.Name ?? ""));

fluent nhibernate polymorphism. how to check for type of class

I have an Icon which has a Content (one to one) relationship.
public class Icon
{
public virtual Content Content {get; set;}
}
By default, it is lazy loaded which is what I want.
However, at some point in the code, I need to check what kind of Content is, Content being polymorphic, something like
if(icon.Content is TextContent)
{
...
}
Icon is part of another association and it is automatically obtained by the NHibernate, I do not get it manually.
What is the recommended way of checking for the actual type in this situation?
I can have a specific property like ContentType which will be an enum in order to identify the actual content type, but I am looking to know if there's a different way.
If you want to do that kind of check, you have to remove the proxy from the property.
There is a few ways to do it:
If you have access to the session call:
session.PersistenceContext.Unproxy(icon.Content);
Implement a virtual method (in a base class if possible) that forces the removal of the proxy by returning the instance with the proper type.
public virtual U As<U>() where U : YourType {
return this as U;
}
Disable the lazy initialization of the property.
This is very similar to another recent question.
To add to csanchez's list, a fourth method is to add a Self property to the Content base class that returns the un-proxied type:
public virtual void Self
{
get { return this; }
}
And a fifth method is to use 'lazy="no-proxy"` in the mapping as described on Ayende's blog.
Thanks for the suggestions but meanwhile I found an interesting solution, better I think.
Using the Visitor pattern, I can define an IconContent visitor and pass an Action to be executed to it.
For example, suppose there is a TextContent and an ImageContent, it will be something like this:
IconContentVisitor.Func(()=> { Console.WriteLine("this is TextContent"; }, ()=> { Console.WriteLine("this is ImageContent"));
Idea came from here: http://mookid.dk/oncode/archives/991