How to change the result set the find() method of a specific table returns - cakephp

Currently I have a table named DocumentContents. The contents used to be saved with blobs, but the amount of data has become too much, to the point that backing up the table is difficult and any corruption could lose us way too much. So, in the DocumentContentsTable file, I now split the contents from the table entry and save it to the disk in a location that I can trace with the table entry's id.
However, I can't find a function to edit the entity before it gets retrieved with the find() function. Currently, a retrieved entity would just contain:
Entity{
['id'] => 0
['created'] => blabla
['modified'] => blabla
Now, I'd like to automatically include the file back into the retrieved entity when loading from the table. Like how I used the beforeSave and afterSave functions automatically split the file off when something is saved to the table. Is there a function I'm missing for this?

Related

How can I get table tuple and transform it into an array in C for postgres?

I'm following Postgres documentation https://www.postgresql.org/docs/8.2/xfunc-c.html for writing C function and creating the extension (for hierarchial clustering) and I'm confused.
So I can get a tuple by using HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
How can I get attribute values in this tuple? We have GET_ARGUMENT_BY_NUM, can I get a value for each column and put it into an array? (For some reason i want to get data from table and for example, clusterize it).
There is an example of using specific table for a function (emp table). How can I use random table for my function (I couldn't find the example)?
Is c_overpaid(emp, limit) (in documentation) called one time for emp table, or is it called as much as the rows in the table?
for hierarchical-clustering: can I get table data from postgres, write it into a temp file, read that file, put it into array, clusterize it and put the result into database? (like create or alter table and do a partitioning? like this: hub - is whole table, part_1 is one cluster, part_2 is the second one etc)
You should read the documentation for the current version.
Yes.
As the example shows, with GetAttributeByName, but there is also a GetAttributeByNum function. I assume you are talking about a C array and not a PostgreSQL array. You can stuff all the values into an array, sure, if they have the same data type.
Then you would have to use the special type record. For a code sample, look at the functions record_to_json and composite_to_json in src/backend/utils/adt/json.c.
It is called for each row found, since it appears in the SELECT list.
That's a bit vague, but sure. I don't see why you'd want to extract that from a table though. Why not write your own table access method, since it looks like you want to define a new way of storing tables.
But be warned, that would be decidedly non-trivial, and you'd better first get your feet wet with more mundane stuff.

How does SQLITE DB saves data of multiple tables in a single file?

I am working on a project to create a simplified version of SQLite Database. I got stuck when trying to figure out how does it manages to store data of multiple tables with different schema, in a single file. I suppose it should be using some indexes to map the data of different tables. Can someone shed more light on how its actually done? Thanks.
Edit: I suppose there is already an explanation in the docs, but looking for some easier way to understand it better and faster.
The schema is the list of all entities (tables, views etc) (the database as a whole) rather than a database existing of many schemas on a per entity basis.
Data itself is stored in pages each page being owned by an entity. It is these blocks that are saved.
The default page size is 4k. You will notice that the file size will always be a mutliple of 4K. You could also, with experimentation create a database with some tables, note it's size, then add some data, and if the added data does not require another page, see that the size of the file is the same. This demonstrating how it's all about pages rather than a linear/contiguos stream of data.
It, the schema, is saved in a table called sqlite_master. This table has columns :-
type (the type e.g. table etc),
name (the name given to the entity),
tbl_name (the tale to which the entity applies )
root page (the map to the first page)
sql (the SQL used to generate the entity, if any)
note that another schema, sqlite_temp_master, may also exist if there are temporary tables.
For example :-
Using SELECT * FROM sqlite_master; could result in something like :-
2.6. Storage Of The SQL Database Schema

Python+peewee: retrieve a field after Model.save() (and trigger execution in Sqlite database)

In my application I work with Sqlite. In one of the tables inside database I've implemented a trigger (basically, after an insert event on the table TAB, it has to update a column named codecolumn which depends on the ID PK field)
In my code I create and object from a PeeweeModel previously setted
objfromModel = Model(params....)
After the execution of line:
objfromModel.save()
We hoped to get appart from the _id field generated -in fact objfromModel.id is retrieved from DB-, but also the codecolumn new field generated by the trigger execution on insert event. But objfromModel.codecolumn is None
Question: is there a trick to make on Peewee in order to recover this new field generated in database by trigger.
Unfortunately SQLite does not support the concept of INSERT ... RETURNING. What you could do is a couple of things:
A. After creation simply re-fetch the codecolumn. e.g. self.codecolumn = MyModel.select(MyModel.codecolumn).where(MyModel.id == self.id).scalar(convert=True) (the use of "scalar" says return just one value, the "convert=True" says convert the underyling database type to a Python type. This is really only necessary if the database type is a date or datetime
B. Create a post-insert trigger that calls a user-defined function. Register a handler for the user-defined function on your database instance, and have your callback receive the new codecolumn value and set it as a database attribute in the callback. Hopefully this makes sense?
C. Move the codecolumn trigger out of SQL and into Python, making it easier to know ahead-of-time what its value will be. This depends obviously on what that column contains.
Hope these ideas help.

Web2Py - Create table from user input

I'm trying to create a table in a database in web2py. I'm new to this and trying to get a hold of the MVC structure and how to call in between.
What I have done is in /modles/db.py I created a DB:
TestDB = DAL("sqlite://storage.sqlite")
Then in my /controllers/default.py I have:
def index():
form = FORM(INPUT(_name='name', requires=IS_NOT_EMPTY()),
INPUT(_type='submit'))
if form.process().accepted:
TestDB().define_table(form.vars.name, Field('testField', unique=True))
return dict(form=form)
return dict(form=form)
But this isn't working. Could somebody help me with understanding how to achieve this?
Thank you.
First, to define the table, it would be TestDB.define_table(...), not TestDB().define_table(...).
Second, table definitions don't persist across requests (of course, the database tables themselves persist, but the DAL table definitions do not). So, if you define a table inside the index() function, that is the only place it will exist and be accessible in your app. If you want to access the table elsewhere in your app, you'll need to store the metadata about the table definition (in this case, just the table name) somewhere, perhaps in the database or a file. You would then retrieve that information on each request (and probably cache it to speed things up) and use it to create the table definition.
Another option would be to generate the table definition code and append it to a model file so it will automatically be run on each request. This is roughly how the new application wizard in the "admin" app works when generating a new application.
Finally, you could leave the table definition in the index() function as you have it, and then when you create the database connection on each request, you can use auto_import:
TestDB = DAL("sqlite://storage.sqlite", auto_import=True)
That will automatically create the table definitions for all tables in TestDB based on the metadata stored in the application's /databases/*.table files. Note, the metadata only includes database-specific metadata, such as table and field names and field types -- it does not include web2py-specific attributes, such as validators, default values, compute functions, etc. So, this option is of limited usefulness.
Of course, all of this has security implications. If you let users define tables and fields, a particular submission could mistakenly or maliciously alter existing database tables. So, you'll have to do some careful checking before processing user submissions.

How to load whole table into a model?

I have a database table and I want to load it to model? Not by specified condition but whole table. How to do it?
models in ATK4 are used to represent data and to abstract the underlying technicalities but not directly hold them.
it is used by MVCGrid, MVCForm and CRUD to know how data is to be presented / laid out to the user's interface.
though models may not hold data, they are used to retrieve it through dsql.
example:
$m = $this->add('Model_UserAccess');
$u = $m->dsql()
->field('usernm')
->field('acclvl')
//->do_getOne(); // return only 1 record
->do_getAll(); // return all records
the use of DSQL or Dynamic SQL is important
to retrieve data from tables and save it back.
I have a very simple script that creates the necessary .php files to copy to /lib/Model.
I did it because was migrating an Access application with tables of 20-30 attributes and was very tedious to create it by hand.
The script it's very very basic, but it's very useful for me.
Here is the link
https://github.com/ajmmartinez/atk4_create_mod
Try
$data = $mymodel->getRows();
This will get all the data from the model and store into the array. Your question is difficult to understand, so I'm not sure how to reply.

Resources