Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping SQL to Abstract Class #318

Open
icu222much opened this issue Feb 9, 2019 · 4 comments
Open

Mapping SQL to Abstract Class #318

icu222much opened this issue Feb 9, 2019 · 4 comments

Comments

@icu222much
Copy link

My project is using the hexagonal architecture and I am wondering if I can use sql2o custom converters (https://github.com/aaberg/sql2o/wiki/Implementing-and-using-custom-converters) to determine which Adapter sql2o should be mapped to.

Would it be possible use sql2o custom converters (https://github.com/aaberg/sql2o/wiki/Implementing-and-using-custom-converters) to map to an abstracted class based on the result from the database? So for example, I have the abstract class Device, and two classes that implements Device (ElectricDevice, SolarDevice). Device has the property 'type' specifying if it is electric or solar. Would it be possible to write a sql2o custom converter that can read the response from the fetch, and then determine whether to create an ElectricDevice object or SolarDevice object based off the database fetch?

I have tried following the instructions on the custom converter page (https://github.com/aaberg/sql2o/wiki/Implementing-and-using-custom-converters) but the custom converter code I wrote never got executed. Is this custom converter used for converting values and cannot be used for determine which class sql2o maps the sql response into?

public Device fetch(int id) {
	final String query = "SELECT * FROM device WHERE id = :idParam";

	try (Connection connection = sql2o.open()) {
		List<Device> devices = connection.createQuery(query)
				.addParameter("idParam", id.toString())
				.executeAndFetch(Device.class); // Can we use a converter here to specify ElectricDevice.class or SoloarDevice.class?
		if (!devices.isEmpty()) {
			return devices.get(0);
		}
		return null;
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public abstract class Device {
	private int id;
	private string type;
	private string name;
}

public class ElectricDevice extends Device { ... }

public class SolarDevice extends Device { ... }

@arnaudroger
Copy link

@icu222much simpleflatmapper supports discriminator and support sql2o, unfortunately right now it's not possible to directly customised the SfmResultSetHandlerFactoryBuilder to add discriminator definition. just added a ticket to allow for that. arnaudroger/SimpleFlatMapper#608

@icu222much
Copy link
Author

@arnaudroger Great, thank you.

@icu222much
Copy link
Author

@arnaudroger thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants