Skip to content

Commit

Permalink
implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MSBilgin committed Mar 4, 2019
1 parent 861f6ba commit 8fe005d
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions app/src/main/java/com/msbilgin/sqlkolayexample/AppDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.msbilgin.sqlkolay.SQLkolay;

public class AppDB extends SQLkolay {
private static final String databaseName = "database";
private static final int version = 14;
private static final String databaseName = "app.db";
private static final int version = 16;

public static TableUser user = new TableUser();
public TableBooks books = new TableBooks();

public AppDB(Context context) {
super(context, databaseName, version);
registerTables(books);
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/msbilgin/sqlkolayexample/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.msbilgin.sqlkolayexample;

import java.util.Date;

public class Book {
public int id;
public String name;
public String author;
public String publishDate;
public double price;

public Book() {
}

public Book(String name, String author, String publishDate, double price) {
this.name = name;
this.author = author;
this.publishDate = publishDate;
this.price = price;
}
}
10 changes: 7 additions & 3 deletions app/src/main/java/com/msbilgin/sqlkolayexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//add a book
AppDB appDB = new AppDB(getApplicationContext());
User user1 = new User(111,"Mehmet Selim", "Bilgin", 29,100000);
appDB.user.add(user1);
Log.d("TEST", String.valueOf(appDB.user.get(111).calc()));
Book book = new Book("Seyahatname", "Evliya Çelebi", "01.01.1600", 2000);
appDB.books.add(book);

//query
Log.d("TOTAL BOOKS", String.valueOf(appDB.books.getByAuthor("Evliya").size()));

}
}
77 changes: 77 additions & 0 deletions app/src/main/java/com/msbilgin/sqlkolayexample/TableBooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.msbilgin.sqlkolayexample;

import android.content.ContentValues;
import android.database.Cursor;

import com.msbilgin.sqlkolay.Column;
import com.msbilgin.sqlkolay.Table;

import java.util.ArrayList;
import java.util.List;

public class TableBooks extends Table {
private static final String tableName = "BOOKS";

@Column(type = Column.Type.INTEGER_AUTOINC, primary = true)
private Column id;

private Column name;
private Column author;

@Column(type = Column.Type.NUMBER)
private Column date;

@Column(type = Column.Type.REAL)
private Column price;

public TableBooks() {
super(tableName);
}

public void add(Book book) {
ContentValues values = new ContentValues();
values.put("name", book.name);
values.put("author", book.author);
values.put("date", book.publishDate);
values.put("price", book.price);
insert(null, values);
}

public Book getByID(int id) {
Book book = new Book();

String sql = String.format("select * from %s where id=%s", tableName, id);
Cursor cursor = getDB().rawQuery(sql, null);
if (cursor.moveToFirst()) {
book.id = cursor.getInt(cursor.getColumnIndex("id"));
book.name = cursor.getString(cursor.getColumnIndex("name"));
book.author = cursor.getString(cursor.getColumnIndex("author"));
book.price = cursor.getDouble(cursor.getColumnIndex("price"));
book.publishDate = cursor.getString(cursor.getColumnIndex("date"));
}
return book;
}

public List<Book> getByAuthor(String author) {
List<Book> books = new ArrayList<>();
String sql = "select * from " + tableName + " where author like '%" + author + "%'";

Cursor cursor = getDB().rawQuery(sql, null);
if (cursor.moveToFirst()) {
do {
Book book = new Book();
book.id = cursor.getInt(cursor.getColumnIndex("id"));
book.name = cursor.getString(cursor.getColumnIndex("name"));
book.author = cursor.getString(cursor.getColumnIndex("author"));
book.price = cursor.getDouble(cursor.getColumnIndex("price"));
book.publishDate = cursor.getString(cursor.getColumnIndex("date"));
books.add(book);
} while (cursor.moveToNext());
}
return books;
}

public void remove(int id) {
delete("id=" + id, null);
}
}
57 changes: 0 additions & 57 deletions app/src/main/java/com/msbilgin/sqlkolayexample/TableUser.java

This file was deleted.

27 changes: 0 additions & 27 deletions app/src/main/java/com/msbilgin/sqlkolayexample/User.java

This file was deleted.

14 changes: 4 additions & 10 deletions sqlkolay/src/main/java/com/msbilgin/sqlkolay/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
enum Type {TEXT, NUM, INT, INT_AUTOINC, REAL, BLOB}

enum PrimaryKey {True, False}

enum Unique {True, False}

enum NotNull {True, False}
enum Type {TEXT, INTEGER, REAL, NUMBER, BLOB, INTEGER_AUTOINC}

Type type() default Type.TEXT;

PrimaryKey primary() default PrimaryKey.False;
boolean primary() default false;

Unique unique() default Unique.False;
boolean unique() default false;

NotNull notNull() default NotNull.True;
boolean notNull() default false;
}

70 changes: 40 additions & 30 deletions sqlkolay/src/main/java/com/msbilgin/sqlkolay/SQLkolay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,68 @@
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public abstract class SQLkolay {
private final int version;
private final String databaseName;
private final Context context;

private boolean isRegistered = false;

public SQLkolay(Context context, String databaseName, int version) {
List<Table> tables = getTables();
Sqlite sqlite = new Sqlite(context, databaseName, version, tables);
SQLiteDatabase sqLiteDatabase = sqlite.getWritableDatabase();
for (Table table : tables) {
this.context = context;
this.databaseName = databaseName;
this.version = version;
}

protected void registerTables(Table... tables) {
if (isRegistered) {
Log.w("SQLkolay", "registerTables can be called only one time");
return;
}

boolean calledConstructer = false;
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for (StackTraceElement traceElement : stackTraceElements) {
calledConstructer = traceElement.getMethodName().equals("<init>") ? true : calledConstructer;
}

if (!calledConstructer) {
Log.w("SQLkolay", "registerTables can be called only in constructer");
return;
}

SqliteDB sqliteDB = new SqliteDB(context, databaseName, version, tables);
SQLiteDatabase sqLiteDatabase = sqliteDB.getWritableDatabase();

for (int i = 0; i < tables.length; i++) {
try {
Class tableClass = table.getClass().getSuperclass();
Class tableClass = tables[i].getClass().getSuperclass();
Method method = tableClass.getDeclaredMethod("setDB", SQLiteDatabase.class);
method.setAccessible(true);
method.invoke(table,sqLiteDatabase);
method.invoke(tables[i], sqLiteDatabase);
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* {@link Table} tipindeki nesnelerin listesini getirir.
*
* @return Table listesi
*/
private List<Table> getTables() {
List<Table> tables = new ArrayList<>();
for (Field field : this.getClass().getDeclaredFields()) {
if (Table.class.isAssignableFrom(field.getType())) {
try {
tables.add((Table) field.get(this));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

return tables;
isRegistered = true;
}


/**
* SQLite sınıfı
* SQLite class
*/
private class Sqlite extends SQLiteOpenHelper {
private List<Table> tables;
private class SqliteDB extends SQLiteOpenHelper {
private Table[] tables;

Sqlite(Context context, String name, int version, List<Table> tables) {
SqliteDB(Context context, String name, int version, Table[] tables) {
super(context, name, null, version);
this.tables = tables;
}
Expand All @@ -70,7 +81,6 @@ public void onCreate(SQLiteDatabase db) {
} catch (Exception e) {
e.printStackTrace();
}

}
}

Expand Down
Loading

0 comments on commit 8fe005d

Please sign in to comment.