Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixEditor committed Dec 10, 2023
0 parents commit 0258f0b
Show file tree
Hide file tree
Showing 42 changed files with 3,553 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/
.idea/
*.iml
docs/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

## Version 1.0.0

Initial release.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Proto4j

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Proto4j-Redis

A type-safe framework to turn a java interface into a database worker. For more information please visit [the documentation](https://proto4j.github.io/proto4j-redis/).

This repository contains the source code for the `Redis` module from `Proto4j`. It is considered to be a development repository where changes can be made and features can be requested. `SQLFactory` implementation modules are provided in the `factories` directory. Currently, only SQLite and MySQL are provided.

# Example usage

This framework is built to be user-friendly when working with generated service workers. The following example illustrates a simple user service:

````java
@SQL(SQLiteFactory.FACTORY)
public interface UserDao {
@SQL.Select("select * from {table};")
public List<User> fetchUsers(@Param("table") String tableName);
}

public static void main(String[]args) {
SQLiteConfiguration config = new SQLiteConfiguration("mydb");
UserDao userDao = Redis.create(UserDao.class, config);
// fetch all user objects
for (User user : userDao.fetchUsers("user_table1")) {
// ...
}
}
````

## Download

Download the [latest JAR file](https://github.com/Proto4j/proto4j-redis/releases) from the releases tab and the `SQLFactory` implementation JAR file what you intend to use. This framework requires a minimum of Java 8+ for developing and running.

## License

MIT License

Copyright (c) 2022 Proto4j

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* MIT License
*
* Copyright (c) 2022 Proto4j
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.proto4j.redis.mysql; //@date 03.09.2022

import org.proto4j.redis.sql.SQLConfiguration;
import org.proto4j.redis.sql.SQLPrincipal;

import java.util.Objects;
import java.util.Properties;

public class MySQLConfiguration extends SQLConfiguration {

private final String host;
private final int port;
private final String database;

public MySQLConfiguration(String host, int port, String database) {
this(null, host, port, database);
}

public MySQLConfiguration(SQLPrincipal principal,
String host, int port, String database) {
this(null, principal, host, port, database);
}

public MySQLConfiguration(Properties properties, SQLPrincipal principal,
String host, int port, String database) {
super(MySQLFactory.FACTORY, properties, principal);
this.host = Objects.requireNonNull(host);
this.port = port;
this.database = Objects.requireNonNull(database);
}


@Override
public String getQualifiedName() {
String path = "//" + host + ':' + port + '/' + database;
return String.join(":", "jdbc", getDriverType(), path);
}

public String getHost() {
return host;
}

public int getPort() {
return port;
}

public String getDatabase() {
return database;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2022 Proto4j
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.proto4j.redis.mysql; //@date 03.09.2022

import org.proto4j.redis.sql.SQLContext;
import org.proto4j.redis.sql.SQLSource;

public class MySQLContext implements SQLContext {

private final SQLSource source;

private Class<?> parent;
private Object reference;

public MySQLContext(SQLSource source) {this.source = source;}

@Override
public SQLSource getSource() {
return source;
}

@Override
public Class<?> getAPIServiceType() {
return MySQLService.class;
}

@Override
public Object getReference() {
return reference;
}

@Override
public void setReference(Object reference) {
this.reference = reference;
}

@Override
public Class<?> getReferenceParent() {
return parent;
}

@Override
public void setReferenceParent(Class<?> cls) {
this.parent = cls;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* MIT License
*
* Copyright (c) 2022 Proto4j
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.proto4j.redis.mysql; //@date 03.09.2022

import org.proto4j.redis.FactoryManager;
import org.proto4j.redis.sql.SQLConfiguration;
import org.proto4j.redis.sql.SQLFactory;
import org.proto4j.redis.sql.SQLService;
import org.proto4j.redis.sql.SQLSource;

import java.io.IOException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.Properties;

public class MySQLFactory implements SQLFactory {

public static final String FACTORY = "mysql";

static {
try {
// This factory should work only if the MySQL-Driver
// is registered. Otherwise, this service can not be called
// because the corresponding driver is missing.
DriverManager.getDriver("jdbc:mysql:temp");

FactoryManager.registerFactory(new MySQLFactory());
} catch (SQLException e) {
System.err.println("WARNING: no suitable driver for jdbc:mysql");
}
}

public MySQLFactory() {
}

@Override
public SQLSource engineGetSource(SQLConfiguration conf) throws SQLDataException, SQLWarning {
return new MySQLSource(conf);
}

@Override
public SQLService engineGetService(SQLSource source) throws SQLException {
return new MySQLService(source);
}

@Override
public String engineDriverType() {
return FACTORY;
}

@Override
public int getMajorVersion() {
String[] vInfo = getVersion().split("\\.");
return vInfo.length > 1 ? Integer.parseInt(vInfo[1]) : 1;
}

@Override
public int getMinorVersion() {
String[] vInfo = getVersion().split("\\.");
return vInfo.length > 2 ? Integer.parseInt(vInfo[2]) : 0;
}

public static String getVersion() {
URL versionFile = MySQLFactory.class.getResource("/redis-sqlite.properties");
if (versionFile == null) {
throw new UnsupportedClassVersionError("Could not examine version");
}

String version = "unknown";
try {
Properties data = new Properties();
data.load(versionFile.openStream());
version = data.getProperty("version", version);
version = version.trim();
} catch (IOException e) {
System.err.println(e.getMessage());
}
return version;
}
}
Loading

0 comments on commit 0258f0b

Please sign in to comment.