Skip to content

Instantly share code, notes, and snippets.

View nitindhar7's full-sized avatar

Nitin Dhar nitindhar7

View GitHub Profile
@BindingAnnotation(BindPost.PostBinderFactory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface BindPost {
class PostBinderFactory implements BinderFactory {
public Binder build(Annotation annotation) {
return new Binder<BindPost, Post>() {
public void bind(SQLStatement q, BindPost bind, Post post) {
Array array = q.getContext().getConnection().createArrayOf("varchar", post.getTags());
q.bind("title", post.getTitle());
// Mapping a string array to `varchar[]` in Postgres
String[] tags = ...
Array array = q.getContext().getConnection().createArrayOf("varchar", tags);
/**
* name - column name
* value - data to be bind to column
* sqlType - SQL data type to map to
*/
bindBySqlType(String name, Object value, int sqlType)
@SqlUpdate("INSERT INTO posts(title, tags) VALUES (:title, :tags)")
int createPost(@Bind("title") String title, @Bind("tags") List<String> tags);
#!/usr/bin/env ruby
CODE_PATH = "/path/to/repos"
RUN_PATH = Dir.pwd
IGNORED_DIRS = [".", "..", "temp", "dont-search-in-here"]
Dir.entries(CODE_PATH).select do |f|
begin
if !IGNORED_DIRS.include? f
Dir.chdir("#{CODE_PATH}/#{f}")
mongo> db.sequences.find().pretty()
{
"key": "orders",
"counter": 3
},
{
"key": "favorites",
"counter": 15
},
...
/*
* processOrder(...); // orderNumber => 1
* processOrder(...); // orderNumber => 2
* ...
*/
public Order processOrder(...) {
long orderNumber = getNext("orders");
return createOrder(orderNumber, ...);
}
/**
* Assuming an entity class called 'Sequence' has been created to
* represent the sequence collection
**/
public long getNext(String collection) {
Sequence seq = datastore.findAndModify(
datastore.find(Sequence.class, "key = ", collection), // query
datastore.createUpdateOperations(Sequence.class).inc("counter") // update
);
{
"key": "orders",
"counter": 0
}
val registerForm = Form(
mapping(
"email" -> nonEmptyText.verifying(emailCheckConstraint),
"password" -> nonEmptyText.verifying(passwordCheckConstraint),
"cities" -> optional(list(text)),
"other_city" -> optional(text)
)(RegisterForm.apply)(RegisterForm.unapply).verifying(citiesCheckConstraint)
)