https://github.com/tylersuehr7/sqlite-class-parser
A lightweight object-to-SQL statement parser.
https://github.com/tylersuehr7/sqlite-class-parser
java java-libraries parser sql sql-parser
Last synced: 28 days ago
JSON representation
A lightweight object-to-SQL statement parser.
- Host: GitHub
- URL: https://github.com/tylersuehr7/sqlite-class-parser
- Owner: tylersuehr7
- License: mit
- Created: 2016-09-22T19:58:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-10-26T23:41:54.000Z (about 9 years ago)
- Last Synced: 2025-01-18T04:32:06.632Z (about 1 year ago)
- Topics: java, java-libraries, parser, sql, sql-parser
- Language: Java
- Homepage:
- Size: 372 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SQLite Class Parser
### Summary
No more need to write your own SQLite queries and statements! You can automatically
create an SQLite query or statement simply by making a Java class. This parser will
read the fields in a Java class and create SQLite queries or statements based off of
it.
You can use annotations to denote constraints. *i.e.* FOREIGN KEY, NOT NULL, UNIQUE.
An annotation called IGNORE is included if you want a field that shouldn't be in a table.
There are also various options for the parser too; *i.e.* you can pluralize table names
and capitalize column names.
##### Full usage of SQLiteParser
```java
// Initialization
SQLiteParser parser = SQLiteParser.getInstance();
// Options
parser.setTablePluralize(true);
parser.setTableLowercase(true);
parser.setFilter(true);
parser.setColFirstUppercase(false);
// Parsing
String sqlTable = parser.parseTable(AnyKindOfObject.class);
```
### Example Code Simple
#### Create Java Class
```java
public class User {
private int id;
private String firstName;
private String lastName;
private String username;
public User() {}
//...
}
```
#### Using the SQLiteParser
```java
public class Main {
public static void main(String[] args) {
// Get instance of SQLiteParser
final SQLiteParser parser = SQLiteParser.getInstance();
// Get table creation statement for User class
String userTable = parser.parseTable(User.class);
// This will print out the following SQLite statement:
// CREATE TABLE [users] ([id] INTEGER PRIMARY KEY, [firstName] TEXT, [lastName] TEXT, [username] TEXT);
System.out.println(userTable);
}
}
```
### Example Code Advanced (Foreign Keys & Annotations)
#### Create Java Classes
```java
public class Address {
private int id;
private String streetName;
public Address() {}
//...
}
public class User {
private int id;
private String firstName;
FOREIGNKEY(fkDataType = int.class)
private Address address;
IGNORE
private String lastname;
public User() {}
//...
}
```
#### Using the SQLiteParser
```java
public class Main {
public static void main(String[] args) {
// Get instance of SQLiteParser
final SQLiteParser parser = SQLiteParser.getInstance();
// Create SQLite table creation statement for Address model
String addressTable = parser.parseTable(Address.class);
// Create SQLite table creation statement for User model
String userTable = parser.parseTable(User.class);
// This will print out the following SQLite statements:
// CREATE TABLE [addresses] ([id] INTEGER PRIMARY KEY, [streetName] TEXT);
// CREATE TABLE [users] ([id] INTEGER PRIMARY KEY, [firstName] TEXT, [addressId] INTEGER, FOREIGN KEY ([addressId]) REFERENCES [addresses]([id]));
System.out.println(addressTable);
Syste.out.println(userTable);
}
}
```
### Overview of SQLiteParser Design
