I have an aliased table
Author a = AUTHOR.as("a");
and I'm trying to access the underlying table AUTHOR from a but cant see how to.
I've tried a.alias.wrapped but that doesn't work for me
The underlying table is not exposed via the Aliased table and so you can't get it.
Instead if trying to get check if the Aliased table is a certain type (which was the original reason I wanted the underlying table) you can do
if (a is Author) {
// Do Something
}
Related
I have created the following user defined types (UDT) in cassandra :
CREATE TYPE keyspace.location (
latitude text,
longitude text,
accuracy text,
address text
);
CREATE TYPE keyspace.person (
name text,
contact_no text,
alternate_contact text
);
and I want to use these to create another UDT
CREATE TYPE keyspace.pinpoint(
user <person>,
location <location>
);
You can nest UDTs by simply specifying your UDT as the type within another UDT in this manner:
CREATE TYPE keyspace.pinpoint (
user person,
location location
);
You don't enclose them in <> brackets because those are used for collections.
As a side note, I personally wouldn't nest UDTs unless you have no other option. UDTs are not as flexible as native columns. Inserting or updating data in a nested UDT can get very complicated and hard to maintain.
Whenever possible, try to use generic table definitions. For example instead of defining the type pinpoint, try to use a table with native column types and clustered rows. Cheers!
You need to declare these nested UDTs as frozen<UDTName>, like:
CREATE TYPE keyspace.pinpoint(
user frozen<person>,
location frozen<location>
);
But this means that you won't be able to update their individual fields - you'll able only update the whole field, with complete UDT instance, for example, complete user or location.
Looking at the following lines of code:
query = "DROP TABLE IF EXISTS my_table"
cur.execute(query)
conn.commit()
# print(table_name)
I'm running the query against multiple tables with various query and I want to return the name of the table and the action executed each time. Is there a way to get some kind of meta data from cur.execute or conn.commit on the action running?
In the example above I'd like to print the table name (my_table) and the action (DROP TABLE). however I want this to be dynamic. If I'm creating a table I want to the name of the table newly created and the action (CREATE TABLE).
Thanks.
Quick and Dirty
tables = ['table_1', 'table_2', 'table_3']
action = 'DROP TABLE'
for table in tables:
cur.execute(f'{str(action)} IF EXISTS {str(table)}')
print(f'ACTION: {action}')
print(f'TABLE: {table}')
conn.commit()
HOWEVER, please do not ever do something like this in anything other than a tiny app that will never leave your computer, and especially not with anything that will accept input from a user.
Bad things will happen.
Dynamically interfacing with databases using OOP is a solved problem, and its not worth reinventing the wheel. Have you considered using an ORM like SQLAlchemy?
I am trying to add an associated table to a query that is joined through another table. For example below, I would like to include facility (though I know it is not valid syntax).
Actual belongs to Encounter and Encounter belongs to Facility. (The Actual table has an encounter_id field, and the Encounters table has a facility_id field.)
Sudo Code:
respond_with(Actual.where(:encounter_id => params[:encounter_id]),
:include => [:encounter, :encounter.facility])
You can do something like:
respond_with(Actual.where(encounter_id: params[:encounter_id]).includes(encounter: :facility))
My question is pretty simple. I am working on Kentico 9 with its SQL Server database which contains several tables which had been added directly from the SQL Management Studio by an external contractor. The fact is that those tables are being used to store custom content which will be displayed for a site, but, in the code they don't have the code for making queries. I mean, they don't have Info and Provider classes.
https://docs.kentico.com/display/K82/Retrieving+database+data+using+ObjectQuery+API
According with this, all tables into the Kentico database can be accessed by invoking methods on these classes, but I don't have it this time.
Something like this, it will not work if I use my table name:
var user = UserInfoProvider.GetUserInfo("administrator");
var items = CustomTableItemProvider.GetItems("MyTable")
.TopN(10)
.WhereEquals("ItemCreatedBy", user.UserID)
.OrderBy("ItemCreatedWhen");
My question is:
can I query any table by its name?
One last thing:
I cannot declared those table as "custom table" because it seems to be a bug in the CMS.
Or you can pull data using your own SQL query:
var ds = ConnectionHelper.ExecuteQuery("select ....", null, QueryTypeEnum.SQLQuery);
Nevertheless I would recommend to create a custom class inside a custom module (much more robust than custom tables) instead and use the generated Info and InfoProvider classes to get and manipulate data.
I think an object has to be registered within the system (created through Kentico UI or API) in order to be pulled from DB with object query.
So I'd choose one of the following options:
Use Entity Framework or something similar to work with that data
Create appropriate custom tables or even custom module and push data there. Not sure why you can't create a custom table... What is an error you're getting?
If you need to present data on the UI only (without processing on the back end) - use just custom queries
Hope this helps.
If you are accessing in code then you could do it the good old fashioned way. If you want to pull data from the database to display on the website you could also do so by creating a custom query and using a transformation to display the fields, then use a repeater on the page to display the transformed data. Alternatively you can use a SQL datasource with a basic repeater, but you still have to create a transformation to display the data. Both methods allow you to access the data in the tables from within the CMS UI, no need to touch any code behind.
If your objective is to read data from these database tables to transform on webpage e.g. using CMS Repeater webpart, you can simply create custom query(s) in Kentico itself and load data using it. You can find the detail here on how to create custom custom queries and load data using it.
On the other hand you can also write your custom classes and define the custom methods where you can pull data using your own SQL query like this:
var ds = ConnectionHelper.ExecuteQuery("select ....", null, QueryTypeEnum.SQLQuery);
Lastly I don't think there should be any issue to create custom table instead of those direct DB tables, only thing we have to ensure code name of custom table should be unique means don't try to use exact same name because it'll cause exception due to same table name already exist in DB. You can please share exception you getting while creating custom table so that I can help you out further.
I'm trying to provide a service for user validation of table structures, one component of which is column data type, like uuid, text, and bigint in the `CREATE TABLE' statement below.
USE my_keyspace;
CREATE TABLE users (
id uuid,
name text,
age bigint);
If I do
USE system;
SELECT validator FROM schema_columns
WHERE keyspace_name='my_keyspace' AND columnfamily_name='users';
I get
org.apache.cassandra.db.marshal.UUIDType
org.apache.cassandra.db.marshal.UTF8Type
org.apache.cassandra.db.marshal.LongType
Which seems informative, but on closer inspection, multiple distinct datatypes can map to the same validator value. Is there a way I can pull the data type info as entered in the `CREATE TABLE' statement, or at least find some distinction between the types?
Also, I'm curious as to why the validator data has the 'org.apache.cassandra...' prepended to it, and couldn't find an explanation, so if anybody knows why that is, I'd be very interested to know.
Which seems informative, but on closer inspection, multiple distinct datatypes can map to the same validator value.
If this is the case, as for example with varchar and text, I believe that the data types map on one another and are interchangeable. Anyone else correct me if I am wrong.
Is there a way I can pull the data type info as entered in the `CREATE TABLE' statement, or at least find some distinction between the types?
The only way I know would be:
DESC TABLE users;
Also, I'm curious as to why the validator data has the 'org.apache.cassandra...' prepended to it, and couldn't find an explanation, so if anybody knows why that is, I'd be very interested to know.
Cassandra is implemented in Java and this is the full path to the Class that implements the data type.
More info:
http://docs.datastax.com/en/cql/3.0/cql/cql_reference/cql_data_types_c.html
https://github.com/apache/cassandra/tree/trunk/src/java/org/apache/cassandra/db/marshal
Use following query:
select column_name,type from system_schema.columns where keyspace_name ='my_keyspace' AND table_name='users';