SimpleData casting objects with ALL_CAPS and underscores - oracle

I'm not sure if anyone else has run into this, but I am having a heck of a problem when trying to cast returns from an Oracle database that has column names in ALL_CAPS with underscores. I am trying to figure out a code way to get it done, but it looks like the cast calls aren't homogenizing. Here is a quick example:
MY_TABLE { COLUMN, COLUMN_ONE, COLUMN_TWO } = {{"a", "b", "c"}}
When mapping to
public class MyClass
{
public string Column { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
}
Will only map Column, and ignore mapping Column1 and Column2.
Thanks in advance for any input and advice!

This was my bad. The issue was a hidden conversion error (there was trouble converting from the decimal? that the databse was sending to the int? the class expected), and a string to GUID conversion error.

Related

SQLite-net-pcl - Always returning ID as 0 (Xamarin)

I recently moved across from SQLite.NET to SQLite-net-pcl due to the Android 7.0 SQlite issue.
In the process I have updated all my libraries and all is working well with regards to insert/drop etc.
The only issue I have is that every time I retrieve an item from the DB it always has an ID of 0. This is causing problems with updating items. Has anyone had this issue before?
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int ID { get; set; }
public string objectId { get; set; }
public DateTime createdAt { get; set; }
public DateTime updatedAt { get; set; }
public string Title { get; set; }
Thanks in advance.
Please try using this, this worked for me
using SQLite.Net.Attributes;
[PrimaryKey, AutoIncrement]
public int? Id { get; set; }
Had almost the same situation.
with all rows returning id of 0
class ToDo : Java.Lang.Object
{
[PrimaryKey, AutoIncrement]
public int ID { get; }
except I simply had forgotten to type set; in the property.
one thing that might help someone with this sorta problem is using the
GetMapping() method to get some info on the mapping used when CreateTable() is used to create the table.
as example:
Toast.MakeText(this, db.GetMapping<ToDo>().TableName.ToString(), ToastLength.Long).Show();
Toast.MakeText(this, db.GetMapping<ToDo>().HasAutoIncPK.ToString(), ToastLength.Long).Show();
using this I found out that AutoIncrement (HasAutoIncPK = false) wasn't being set on my table.
See if you created the table with the methods CreateComand(query) and ExecuteNonQuery(). If this is the case, create your table with the CreateTable<Type>() method. The primary key and autoincrement attributes are initialized at the time of creating the table through said method
I have been struggling with the same issue here.
I was manually creating the tables to guarantee a smoother update process moving forwards rather than using the CreateTable methods available.
The fix that I eventually stumbled upon was that I was using the wrong data type for my PRIMARY KEY column.
Wrong definition
[RecordIndexId] int PRIMARY KEY NOT NULL
Correct definition
[RecordIndexId] integer PRIMARY KEY NOT NULL
To add a little context there is a big different between the int and integer data types. Explained in this SO answer
My problem was that I had an internal set for my ID property setter. This had to be public to work correctly.
I'd add this as a comment but alas... I don't have enough rep.

Linq-to-Sqlite : Unable to map a TEXT type sqlite column against a System.Double type of model property?

I have a Sqlite database table named Movie whose columns basically store data simply in TEXT or INTEGER type (of course, Sqlite basically stores everything as just string the columns only have an affinity towards the type, if i am not wrong).
I am using Linq-to-Sqlite ORM to query my table data against a model named Movie. Below is the DDL for the table and code for the class.
CREATE TABLE Movie
(
Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
Title Text,
Rating TEXT,
IsSubtitle INTEGER
)
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public double Rating { get; set; }
public bool IsSubtitle { get; set; }
}
Now when I try to fetch the movie records from the database using the ORM, it throws an exception :
System.InvalidOperationException was caught
HResult=-2146233079
Message=The 'Rating' property on 'Movie' could not be set to a 'System.String' value. You must set this property to a non-null value of type 'System.Double'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at LinqToSqliteDemoApp.Program.Main(String[] args) in e:\Projects\TestApplications\LinqToSqliteDemoApp\LinqToSqliteDemoApp\Program.cs:line 25
InnerException:
Obviously, it cannot cast the TEXT type of data from the Rating column into the double data type of Movie class.
So I would like to know, is there any workaround to tell the ORM to implicitly map or convert the Rating column data to double data type while retrieving from the database ?
You need to read the docs on sqlite data types http://www.sqlite.org/datatype3.html, so use double in your create table statement

Modelling objects with changing attribute

I am trying to create a simple program in Ruby to track the price movements of stocks, but I'm not entirely sure of how it should be designed.
Basically, I'm thinking of a class Stock, with all the attributes such as name, desc, etc. However, I'm not sure of how the price attribute would work. Because for each stock, I also want to track the history of prices and plot them on a graph. So, my question is, should I create another class, Prices and associate it with Stock? or is there a better way?
I'm a newbie at OOD and would love some explanation, helpful links or other advice. Thank you in advance.
Some of this will depend on your choice of DB: OO, document, or relational. If you're using the typical relational DB, then you would have a table for prices that represents the one-to-many relationship between prices and stock.
It seems like you should have a separate class for price, because I'm assuming you'll want to track price with time. I know this isn't Ruby, but your Stock class could look something like this:
public class Stock
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Price> PriceHistory { get; set; }
}
And then Price:
public class Price
{
public int StockId { get; set; }
public DateTime PriceDate { get; set; }
public decimal Price { get; set; }
}
Note: You'll need to ignore the fact that the IDs are public, and therefore able to be changed by outside classes. However, this code is presented this way for simplicity.
Hope that helps.

Why does Linq need a setter for a 'read-only' object property?

I'm using a Linq DataContext.ExecuteQuery("some sql statement") to populate a list of objects
var incomes = db.ExecuteQuery<IncomeAggregate>(sqlIncomeStatement(TimeUnit));
The IncomeAggregate is an object I made to hold the result of the records of this query.
One of the properties of this object is YQM:
public int Year { get; set; }
public int Quarter { get; set; }
public int Month { get; set; }
public string YQM
{
get { return string.Format("Y{0}-Q{1}-M{2}", Year, Quarter, Month); }
}
... more properties
Everything compiles OK, but when it executes the Linq I get the following error:
Cannot assign value to member 'YQM'. It does not define a setter.
But clearly, I don't want to 'set' it.
Y, Q and M are provided by the query to the database. YQM is NOT provided by the query.
Do I need to change the definition of my object somehow?
(I've just started using Linq and I'm still getting up to speed, so it could be very simple)
Well, I finally wound up just making the setter private
public string YQM {
get
{
return string.Format("Y{0}-Q{1}-M{2}", Year, Quarter, Month);
}
private set { ;}
}
Seems to work.
Linq is assuming that the properties of this object are values to load from the database, and clearly it can't set the YQM property because it has no setter. Try making YQM a method instead:
public string YQM()
{
return string.Format("Y{0}-Q{1}-M{2}", Year, Quarter, Month);
}

Populate DTO from several queries

I have a DTO with 40+ properties. But in order to populate all properties I need to execute 4 separate queries. My first query is in charged of getting basic information. For each row returned I run 3 more queries based on the id's given from the main query (N+1 problem). I can set use eager loading but then I'm loading thousands of objects that I don't need.
Should I split my DTO and create a separate DTO for each query I run then link then tie them all together into a central DTO by id?
I was envisioning a final DTO like this.
public class FooDto
{
public string Foo { get; set; }
public string Bar { get; set; }
public FirstDto FirstQueryResults { get; set; }
public SecondDto SecondQueryResults { get; set; }
public ThirdDto ThirdQueryResults { get; set; }
}
Is there a better way of solving this? I'm using Oracle and NHibernate doesn't support multi criterias. Note that I am joining most of my data. The problem comes when I need to query data with a complete new set of criteria.
How about creating a VIEW that joins the data to give the 40 properties all in one go, and basing your DTO on that - whatever a DTO may be ;-)

Resources