Skip to content

Commit 86cae33

Browse files
committed
Updated readme for 1.0
1 parent 2fc64f3 commit 86cae33

1 file changed

Lines changed: 123 additions & 58 deletions

File tree

README.md

Lines changed: 123 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,140 @@ of classes might follow as I see a need for them.
88

99
The database structure is basically the same as the one I present in my [tutorial](https://github.com/spacecowboy/AndroidTutorialContentProvider).
1010

11+
Please see _sample.py_ for an example of how to generate all the database files and the
12+
sample project in _/sample/_ where they are used. The sample is a modified version of the
13+
tutorial project mentioned above so check that out for a preview of the app itself.
14+
1115
An example generation
1216
can be seen below where quite a simple table is created and
1317
then a java OrmClass is generated.
1418

1519
```python
16-
from db_table import Table, Column, ForeignKey, Unique
20+
from db_table import Table, Column, ForeignKey, Unique
21+
from dbitem import DBItem
1722

18-
t = Table('Album').cols(Column('_id').integer.primary_key,\
19-
Column('albumname').text.not_null.default("''"), \
20-
Column('artistname').text.not_null)\
21-
.constraints(ForeignKey('artistname').references('artist', 'name')\
22-
.on_delete_cascade,\
23-
Unique('albumname').on_conflict_replace)
23+
t = Table('Album').cols(Column('_id').integer.primary_key,\
24+
Column('albumname').text.not_null.default("''"), \
25+
Column('artistname').text.not_null)\
26+
.constraints(ForeignKey('artistname').references('artist', 'name')\
27+
.on_delete_cascade,\
28+
Unique('albumname').on_conflict_replace)
2429

25-
get_orm_class(t)
30+
print(DBItem(t))
2631
```
2732

2833
Result:
2934

3035
```java
31-
package com.example.appname.database;
32-
33-
import android.content.ContentValues;
34-
import android.database.Cursor;
35-
36-
/**
37-
* Represents Album in the database.
38-
*
39-
*/
40-
public class AlbumItem {
41-
public static final String TABLE_NAME = "Album";
42-
// Column names
43-
public static final String COL__ID = "_id";
44-
public static final String COL_ALBUMNAME = "albumname";
45-
public static final String COL_ARTISTNAME = "artistname";
46-
47-
// For database projection so order is consistent
48-
public static final String[] FIELDS = { COL__ID, COL_ALBUMNAME, COL_ARTISTNAME };
49-
50-
public long _id = -1;
51-
public String albumname = "";
52-
public String artistname;
53-
54-
public AlbumItem() {
55-
}
56-
57-
public AlbumItem(final Cursor cursor) {
58-
// Projection expected to match FIELDS array
59-
this._id = cursor.getLong(0);
60-
this.albumname = cursor.getString(1);
61-
this.artistname = cursor.getString(2);
62-
}
63-
64-
public ContentValues getContent() {
65-
ContentValues values = new ContentValues();
66-
values.put(COL__ID, _id);
67-
values.put(COL_ALBUMNAME, albumname);
68-
values.put(COL_ARTISTNAME, artistname);
69-
70-
return values;
71-
}
72-
73-
public static final String CREATE_TABLE =
74-
"CREATE TABLE Album
75-
(_id INTEGER PRIMARY KEY,
76-
albumname TEXT NOT NULL DEFAULT '',
77-
artistname TEXT NOT NULL
78-
79-
FOREIGN KEY (artistname) REFERENCES artist(name) ON DELETE CASCADE,
80-
UNIQUE(albumname) ON CONFLICT REPLACE)";
36+
package com.example.appname.database;
37+
38+
import android.content.ContentValues;
39+
import android.content.UriMatcher;
40+
import android.database.Cursor;
41+
import android.net.Uri;
42+
43+
/**
44+
* Represents Album in the database.
45+
*
46+
*/
47+
public class AlbumItem extends DBItem {
48+
public static final String TABLE_NAME = "Album";
49+
50+
public static Uri URI() {
51+
return Uri.withAppendedPath(
52+
Uri.parse(ItemProvider.SCHEME
53+
+ ItemProvider.AUTHORITY), TABLE_NAME);
54+
}
55+
56+
// Column names
57+
public static final String COL__ID = "_id";
58+
public static final String COL__ID = "_id";
59+
public static final String COL_ALBUMNAME = "albumname";
60+
public static final String COL_ARTISTNAME = "artistname";
61+
62+
// For database projection so order is consistent
63+
public static final String[] FIELDS = { COL__ID, COL__ID, COL_ALBUMNAME, COL_ARTISTNAME };
64+
65+
public long _id = -1;
66+
public long _id = -1;
67+
public String albumname = "";
68+
public String artistname;
69+
70+
public static final int BASEURICODE = 3993119;
71+
public static final int BASEITEMCODE = 1102568;
72+
73+
public static void addMatcherUris(UriMatcher sURIMatcher) {
74+
sURIMatcher.addURI(ItemProvider.AUTHORITY, TABLE_NAME, BASEURICODE);
75+
sURIMatcher.addURI(ItemProvider.AUTHORITY, TABLE_NAME + "/#", BASEITEMCODE);
76+
}
77+
78+
public static final String TYPE_DIR = "vnd.android.cursor.dir/vnd.example." + TABLE_NAME;
79+
public static final String TYPE_ITEM = "vnd.android.cursor.item/vnd.example." + TABLE_NAME;
80+
81+
public AlbumItem() {
82+
super();
83+
}
84+
85+
public AlbumItem(final Cursor cursor) {
86+
super();
87+
// Projection expected to match FIELDS array
88+
this._id = cursor.getLong(0);
89+
this._id = cursor.getLong(1);
90+
this.albumname = cursor.getString(2);
91+
this.artistname = cursor.getString(3);
92+
}
93+
94+
public ContentValues getContent() {
95+
ContentValues values = new ContentValues();
96+
values.put(COL_ALBUMNAME, albumname);
97+
values.put(COL_ARTISTNAME, artistname);
98+
99+
return values;
100+
}
101+
102+
public String getTableName() {
103+
return TABLE_NAME;
81104
}
105+
106+
public String[] getFields() {
107+
return FIELDS;
108+
}
109+
110+
public long getId() {
111+
return _id;
112+
}
113+
114+
public void setId(final long id) {
115+
_id = id;
116+
}
117+
118+
public static final String CREATE_TABLE =
119+
"CREATE TABLE Album"
120+
+" (_id INTEGER PRIMARY KEY,"
121+
+" _id INTEGER PRIMARY KEY,"
122+
+" albumname TEXT NOT NULL DEFAULT '',"
123+
+" artistname TEXT NOT NULL"
124+
+""
125+
+" FOREIGN KEY (artistname) REFERENCES artist(name) ON DELETE CASCADE,"
126+
+" UNIQUE(albumname) ON CONFLICT REPLACE)";
127+
}
128+
```
129+
130+
The program can generate the java files directly, as follows:
131+
```python
132+
"""Generate a sample project"""
133+
134+
from AndroidCodeGenerator.generator import Generator
135+
from AndroidCodeGenerator.db_table import Table, Column, ForeignKey, Unique
136+
137+
persons = Table('Person').cols(Column('firstname').text.not_null.default("''"),\
138+
Column('lastname').text.not_null.default("''"),\
139+
Column('bio').text.not_null.default("''"))
140+
141+
g = Generator(path='./sample/src/com/example/appname/database/')
142+
143+
g.add_tables(persons)
144+
145+
g.write()
146+
82147
```

0 commit comments

Comments
 (0)