Union data type in database design - sql

I would need to store the following value V in a database. An instance of V is linked with a certain record in a certain table. The problem with V is that it has a resemblance to a union type and can indicate three things:
V has an integer value, meaning that the value should be used for the record in question.
V is absent, i.e. NULL, meaning that a global setting takes precedence for the record in question.
V has a meaning of "ANY", meaning that no value should be used for the record in question.
1 and 2 are easy (make it a NULLable integer column), but how to deal with 3? Now I don't feel comfortable using a special numeric value for indicating the ANY state, because e.g. -1 and 0 are totally valid values for case 1.
What has come to mind so far is
Putting this union type into a separate table that has two columns, one for the numeric value and one for the ANY condition (boolean), and a nillable foreign key reference to it.
Storing it as a VARCHAR column and using some special character (e.g. "*") for ANY state.
Is there any "industry standard" way of doing this? :)
For reference, this union type looks something like this in XSD representation:
<complexType name="V">
<choice>
<element name="anyValue" type="xs:string" fixed="" />
<element name="numericValue" type="xs:int" />
</choice>
</complexType>
<complexType name="E">
<sequence>
<element ... />
<element ... />
<element name="configValue" type="V" minOccurs="0" />
</sequence>
</complexType>

I would solve it as either.
Nullable foregn key. And in the foregn key table there would be a row that indicates that it has the "any state".. for example has all columns as null.
or addin a int/boolean column to inditake an overiding state.
Please don't use varchar for linking to other tables...
I have seen many solutions to this problem and all of them have serious issues. This is never going to be pretty. And try to tailor your solution for the code that is going to consume this mess. Make it as simple for that as you can. In those situations readability is king.
But best advice is never to get into those situations but that I know is not always wihtin your power

Related

Case insensitive search in Examine Index (Lucene)

Is it possible to do a case insensitive search using Examine Index and Lucene without altering the data stored?
I'm saving articles with Id, title, the text and a date.
I don't want to index my data as lowercase since I want to read my data from the index and display it as it is. So I can skip the step going to the DB to get data.
Saving the same data twice, once as it is and once as lower case, dosn't feel like the right way of doing it.
Any suggestions of how to aproach this?
ExamineIndex.config
<IndexSet SetName="MySearchIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/MySearch/" >
<IndexUserFields>
<add Name="Id" />
<add Name="Title" />
<add Name="Text" />
<add Name="Date" />
</IndexUserFields>
ExamineSettings.config
<add name="MySearchIndexer" type="Examine.LuceneEngine.Providers.SimpleDataIndexer, Examine"
dataService="X.Service.MyIndexerService, X"
indexTypes="CustomData"
runAsync="false"
enableDefaultEventHandler="true"
analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>
<add name="MySearchSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net" enableLeadingWildcard="true" />
In lucene analyzers does not alter your data. They determine how data is indexed only. So you can index your data as you want (don't lowercase your data in your code), and retrieve values as they are.
As a side note in lucene you can have fields with different attributes (indexed/not indexed, stored/not stored). So you can add same field twice: one for retrieving only (stored & not indexed) and one for searching (indexed as lowercase but not stored). Check if examine supports these types of fields.

IgniteRDD SQL Table Names

When using the .sql method on IgniteRDDs, I need to have the "table" name to do the WHERE clause. I don't know how to retrieve it, and the example provides the name Integer for an [Int, Int] type cache. I have tried recreating it with [String, Int] caches etc. but haven't been able to figure out how they got that Integer name.
Does it have to do with Spark naming conventions, or is it a part of Ignite?
It seems a simple issue, but I just can't find the RDD's table name.
Their example can be viewed here: https://github.com/apache/ignite/blob/master/examples/src/main/spark/org/apache/ignite/examples/spark/SharedRDDExample.java
Config file: https://github.com/apache/ignite/blob/master/examples/config/spark/example-shared-rdd.xml
In configuration you can find:
<property name="indexedTypes">
<list>
<value>java.lang.Integer</value>
<value>java.lang.Integer</value>
</list>
</property>
this is a list key(odd) and value(even) types which would be indexed. more details you can find here: https://apacheignite-sql.readme.io/docs/schema-and-indexes#section-registering-indexed-types
The table has a name of value type, in this case, it's "Integer".

ColdFusion ORM Relationship

Before ORM, if I wanted to display the combined output of a normalized table, I’d just do a quick CFQUERY, join the tables on the fields I want and display the output. I just can’t get my head wrapped around it using ORM.
For example with these two tables:
customers
(id,
name,
customerType)
customerTypes
(id,
Name)
How would you create a single entity you can load to display the following when the customerType field in customers links to an id in customerTypes?
customers.id, customers.name, customerTypes.name
All of the ORM relationship examples I’ve walked through for some reason can’t make me understand how to do it. It seems so simple it’s killing me. Any help shedding some light on this would be appreciated!
Or alternatively
<cfproperty name="type" type="string" column="Name" table="customerTypes" joincolumn="id">
see Join mapping in a CFC
So in your Customers CFC you will need something like this:
<cfproperty name="customerType" type="CustomerTypes" fieldtype="many-to-one" cfc="CustomerTypes" fkcolumn="id" lazy="true" />
Then you should be able to dump an instance of a Customers object and see that it has a customerType property and hence you can write something like this:
<cfset cust = entityLoad("Customers", 1) />
<cfset type = cust.getCustomerType().getName() />
Hope that helps!

NHibernate count of items in xml mapping

Is there a way in NHibernate to map the result of a count query to a property on a class? I'd like to do this in the XML mapping.
I know I could formulate this via code (either with a construct that actually queries the count, or by cheating, doing the full query, and counting the resulting items), but it would be nice if I could write some short SQL or HQL and jam that into my XML mapping, somehow.
A concrete example. My DB has these tables -
Entry
Id
BodySummary
Comment
Id
EntryId
Body
I want to get a summary of entries. For each of the entries, I want to get the comment count (and body summary).
FYI: I've omitted irrelevant parts of my DB, like authors, entry title/body, timestamps, etc. This of course should have no bearing on the part of the query I am asking about.
I don't think you can do it with HQL. But here's how to do it with SQL:
<class name="Entry" .... >
<id>
//ID Strategy
</id>
<property name="BodySummary" />
...
<property name="CommentCount" formula="(SELECT COUNT(*) FROM Comment c WHERE c.EntryId = Id)" type="Int32" />
</class>
Important to note:
Wrap the sql in parenthesis - it will error if it is not wrapped
This is not HQL - you have to use the database column/table names not your mapped classes/properties
Provide a return type so NHibernate knows how to map it back to the property
You will probably want to make this a readonly field but this is the basics of how you would map it.
The resultant SQL would be something like this:
SELECT this_.Id as Id11_0_,
this_.BodySummary as BodySummary10_11_0_,
(SELECT COUNT(* )
FROM Comment c
WHERE c.EntryId = this_.Id) as formula0_0_
FROM Entry this_

Storing a double.MinValue with Nhibernate to sqlite

I am using nhibernate for my OR persistence and I store a list of doubles into a table using the following mapping (where the list is embedded in another class).
<list name="Values" access="field" table="Values_double" >
<key column="variable_id"/>
<index column="no_data_values_list_index"/>
<element column="value" type="System.Double"/>
</list>
This works fine except when I try to store double.MinValue or double.MaxValue. I get an error
when reloading from my DB saying:
System.OverflowException: Value was either too large or too small for a Decimal.
Which seems to be related to NHibernate storing the doubles as 'NUMERIC' values in my sqlite dBase. The conversion back seems to go broke. Any suggestions are very welcome.
Greetings,
Martijn
In the end I solved it by introducing another custom IUserType for this simple problem :(
I have no experience with SQLite, but you can override NHibernate's choice of SQL datatype by using the sql-type attribute in the mapping. This might help if NUMERIC is not appropriate for the given circumstance.
For example:
<property name="Foo" type="String">
<column name="foo" length="64" not-null="true" sql-type="text"/>
</property>